Class: StudioGame::Player

Inherits:
Object
  • Object
show all
Includes:
Playable
Defined in:
lib/studio_game/player.rb

Direct Known Subclasses

BerserkPlayer, ClumsyPlayer

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Playable

#blam, #strong?, #w00t

Constructor Details

#initialize(name, health = 100) ⇒ Player

Returns a new instance of Player.



10
11
12
13
14
# File 'lib/studio_game/player.rb', line 10

def initialize( name, health=100 )
  @name = name.capitalize
  @health = health
  @treasures = Hash.new(0)
end

Instance Attribute Details

#healthObject

Returns the value of attribute health.



8
9
10
# File 'lib/studio_game/player.rb', line 8

def health
  @health
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'lib/studio_game/player.rb', line 8

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



45
46
47
# File 'lib/studio_game/player.rb', line 45

def <=>(other)
  other.score <=> score
end

#each_found_treasureObject



26
27
28
29
30
31
# File 'lib/studio_game/player.rb', line 26

def each_found_treasure
  @treasures.each do |name, points|
    t = Treasure.new(name, points)
    yield t
  end
end

#found_treasure(new_treasure) ⇒ Object



20
21
22
23
24
# File 'lib/studio_game/player.rb', line 20

def found_treasure( new_treasure)
  @treasures[new_treasure.name] += new_treasure.points
  puts "\t#{@name} found a #{new_treasure.name}"
  #puts "\tTreasure chest includes: #{@treasures}"
end

#pointsObject



16
17
18
# File 'lib/studio_game/player.rb', line 16

def points
  @treasures.values.reduce(0, :+) 
end

#say_helloObject



41
42
43
# File 'lib/studio_game/player.rb', line 41

def say_hello
  puts "Hello, I'm #{@name}, and I have #{@health} health units and a score of #{score}."
end

#scoreObject



33
34
35
# File 'lib/studio_game/player.rb', line 33

def score
  return @health + points 
end

#to_sObject



37
38
39
# File 'lib/studio_game/player.rb', line 37

def to_s
  "#{@name.ljust(31, '.')} #{@health.to_s.rjust(4)} #{points.to_s.rjust(5)} #{score.to_s.rjust(5)}"
end