Class: StudioGame::Player

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

Direct Known Subclasses

Berserk, Klutz

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Playable

#blam, #strong?, #w00t, #weak?

Constructor Details

#initialize(name, health = 100) ⇒ Player

Returns a new instance of Player.



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

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

Instance Attribute Details

#healthObject

Returns the value of attribute health.



6
7
8
# File 'lib/game/player.rb', line 6

def health
  @health
end

#nameObject

Returns the value of attribute name.



6
7
8
# File 'lib/game/player.rb', line 6

def name
  @name
end

Class Method Details

.from_csv(row) ⇒ Object



24
25
26
27
28
# File 'lib/game/player.rb', line 24

def self.from_csv(row)
  name = row[0]
  health = row[1]    
  new(name, Integer(health))
end

Instance Method Details

#<=>(a_player) ⇒ Object



42
43
44
# File 'lib/game/player.rb', line 42

def <=>(a_player)
  a_player.score <=> self.score
end

#found_item(item) ⇒ Object



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

def found_item(item)
  @riches[item.name] += item.value
end

#found_treasure(item) ⇒ Object



20
21
22
# File 'lib/game/player.rb', line 20

def found_treasure(item)
  found_item(item)
end

#item_valuesObject



34
35
36
# File 'lib/game/player.rb', line 34

def item_values
  @riches.values.reduce(0, :+)
end

#itemsObject



46
47
48
49
50
51
# File 'lib/game/player.rb', line 46

def items
  @riches.each do |name, value|
    treasure = Treasure.new(name, value)
    yield treasure
  end
end

#scoreObject



38
39
40
# File 'lib/game/player.rb', line 38

def score
  @health + item_values
end

#to_sObject



30
31
32
# File 'lib/game/player.rb', line 30

def to_s
  "My name is #{@name} with health of #{@health} and a score of #{score}"    
end