Class: BattleRoyal::Player
- Inherits:
-
Object
- Object
- BattleRoyal::Player
show all
- Includes:
- Playable
- Defined in:
- lib/battle_royal/player.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Playable
#damage, #power_up, #strong?
Constructor Details
#initialize(name, health = 100) ⇒ Player
Returns a new instance of Player.
8
9
10
11
12
|
# File 'lib/battle_royal/player.rb', line 8
def initialize(name, health = 100)
@name = name.capitalize
@health = health
@found_weapons = Hash.new(0)
end
|
Instance Attribute Details
#health ⇒ Object
Returns the value of attribute health.
7
8
9
|
# File 'lib/battle_royal/player.rb', line 7
def health
@health
end
|
#name ⇒ Object
Returns the value of attribute name.
7
8
9
|
# File 'lib/battle_royal/player.rb', line 7
def name
@name
end
|
Class Method Details
.from_csv(string) ⇒ Object
18
19
20
|
# File 'lib/battle_royal/player.rb', line 18
def self.from_csv(string)
Player.new(string[0], string[1].to_i)
end
|
Instance Method Details
#attack(player, found_weapon) ⇒ Object
29
30
31
32
33
34
|
# File 'lib/battle_royal/player.rb', line 29
def attack(player, found_weapon)
@health = health - found_weapon
print " and attacked #{player.name} \n"
sleep(0.5)
puts "#{player.name}'s health is now: #{player.health} \n"
end
|
#each_found_weapon ⇒ Object
40
41
42
43
44
|
# File 'lib/battle_royal/player.rb', line 40
def each_found_weapon
@found_weapons.each do |name, points|
yield Weapon.new(name, points)
end
end
|
#found_weapon(weapon) ⇒ Object
22
23
24
25
26
27
|
# File 'lib/battle_royal/player.rb', line 22
def found_weapon(weapon)
@found_weapons[weapon.name] += weapon.points
print "\n#{@name} found a #{weapon.name} worth #{weapon.points} damage points"
@found_weapons[weapon.name]
end
|
#points ⇒ Object
36
37
38
|
# File 'lib/battle_royal/player.rb', line 36
def points
@found_weapons.values.inject(0, :+)
end
|
#score ⇒ Object
46
47
48
|
# File 'lib/battle_royal/player.rb', line 46
def score
points + @health
end
|
#to_s ⇒ Object
14
15
16
|
# File 'lib/battle_royal/player.rb', line 14
def to_s
"#{@name} health = #{@health}, points = #{points}, and score = #{score}"
end
|