Class: StudioGame::Player

Inherits:
Object
  • Object
show all
Includes:
Playable
Defined in:
lib/player_class.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

overwrites the default method when Player.new is created! Default health is now 100.



13
14
15
16
17
# File 'lib/player_class.rb', line 13

def initialize(name, health=100) #overwrites the default method when Player.new is created! Default health is now 100.
    @name = name.capitalize
    @health = health
    @found_treasures = Hash.new(0)
end

Instance Attribute Details

#healthObject

Returns the value of attribute health.



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

def health
  @health
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/player_class.rb', line 7

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object

this overrides default for whenever the compare <=> operator is used on player class object.



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

def <=>(other) #this overrides default for whenever the compare <=> operator is used on player class object.
  other.score <=> score
end

#each_found_treasureObject



45
46
47
48
49
# File 'lib/player_class.rb', line 45

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

#found_treasure(treasure) ⇒ Object



19
20
21
22
23
# File 'lib/player_class.rb', line 19

def found_treasure(treasure)
    @found_treasures[treasure.name] += treasure.points
    puts "#{@name} found a #{treasure.name} worth #{treasure.points} points."
    puts "#{@name}'s treasures: #{@found_treasures}"
end

#pointsObject

POINTS *********************



25
26
27
# File 'lib/player_class.rb', line 25

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

#scoreObject

SCORE *********************



29
30
31
# File 'lib/player_class.rb', line 29

def score
  points + @health
end

#to_sObject

to_s overwrites/defines the default method for PUTS.



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

def to_s #to_s overwrites/defines the default method for PUTS.
  "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}."   
end