Class: StudioGame::Player
- Inherits:
-
Object
- Object
- StudioGame::Player
show all
- Includes:
- Playable
- Defined in:
- lib/studio_game/player.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Playable
#blam, #strong?, #w00t
Constructor Details
#initialize(name, health = 100) ⇒ Player
12
13
14
15
16
17
|
# File 'lib/studio_game/player.rb', line 12
def initialize(name, health=100)
@name = name.capitalize
@health = health
@found_treasures = Hash.new(0)
end
|
Instance Attribute Details
#found_treasures ⇒ Object
Class for player generation
10
11
12
|
# File 'lib/studio_game/player.rb', line 10
def found_treasures
@found_treasures
end
|
#health ⇒ Object
Class for player generation
10
11
12
|
# File 'lib/studio_game/player.rb', line 10
def health
@health
end
|
#name ⇒ Object
Class for player generation
10
11
12
|
# File 'lib/studio_game/player.rb', line 10
def name
@name
end
|
Class Method Details
.player_csv(string) ⇒ Object
37
38
39
40
|
# File 'lib/studio_game/player.rb', line 37
def self.player_csv(string)
name, health = string.split(",")
Player.new(name,Integer(health))
end
|
Instance Method Details
#<=>(other) ⇒ Object
24
25
26
|
# File 'lib/studio_game/player.rb', line 24
def <=>(other)
other.points <=> points
end
|
42
43
44
|
# File 'lib/studio_game/player.rb', line 42
def format_name()
"#{@name},#{points}"
end
|
#found_treasure(treasure) ⇒ Object
28
29
30
31
|
# File 'lib/studio_game/player.rb', line 28
def found_treasure(treasure)
@found_treasures[treasure.name] += treasure.points
puts "#{@name} found a #{treasure.name} worth #{treasure.points}"
end
|
#get_total_treasures ⇒ Object
46
47
48
49
50
51
|
# File 'lib/studio_game/player.rb', line 46
def get_total_treasures
@found_treasures.each do |name, points|
treasure = Treasure.new(name, points)
yield treasure
end
end
|
#points ⇒ Object
33
34
35
|
# File 'lib/studio_game/player.rb', line 33
def points
@found_treasures.values.reduce(0, :+)
end
|
#to_s ⇒ Object
19
20
21
22
|
# File 'lib/studio_game/player.rb', line 19
def to_s
"I'm #{@name} with a health of #{@health} and a score of #{points}."
end
|