Class: StudioGame::Player
- Inherits:
-
Object
- Object
- StudioGame::Player
- Includes:
- Playable
- Defined in:
- lib/studio_game/player.rb
Overview
player class that includes both health and the name of the player
Direct Known Subclasses
Instance Attribute Summary collapse
-
#health ⇒ Object
Returns the value of attribute health.
-
#name ⇒ Object
Returns the value of attribute name.
Class Method Summary collapse
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #each_found_treasure ⇒ Object
- #found_treasure(treasure) ⇒ Object
-
#initialize(name, health = 100) ⇒ Player
constructor
initialize doesn’t need to be called, it magically happens when a new class object is created, here you store the instance variables that you want to use later on in the class.
- #points ⇒ Object
- #score ⇒ Object
-
#to_s ⇒ Object
to_s returns a string whenever the class is called, the string will ALWAYS be returned without having to specifically ask for it.
Methods included from Playable
Constructor Details
#initialize(name, health = 100) ⇒ Player
initialize doesn’t need to be called, it magically happens when a new class object is created, here you store the instance variables that you want to use later on in the class
20 21 22 23 24 |
# File 'lib/studio_game/player.rb', line 20 def initialize(name, health = 100) @name = name.capitalize @health = health @found_treasures = Hash.new(0) end |
Instance Attribute Details
#health ⇒ Object
Returns the value of attribute health.
13 14 15 |
# File 'lib/studio_game/player.rb', line 13 def health @health end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/studio_game/player.rb', line 12 def name @name end |
Class Method Details
Instance Method Details
#<=>(other) ⇒ Object
47 48 49 |
# File 'lib/studio_game/player.rb', line 47 def <=>(other) other.score <=> score end |
#each_found_treasure ⇒ Object
57 58 59 60 61 62 |
# File 'lib/studio_game/player.rb', line 57 def each_found_treasure @found_treasures.each do |name, points| treasure = Treasure.new(name, points) yield treasure end end |
#found_treasure(treasure) ⇒ Object
51 52 53 54 55 |
# File 'lib/studio_game/player.rb', line 51 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 |
#points ⇒ Object
64 65 66 |
# File 'lib/studio_game/player.rb', line 64 def points @found_treasures.values.reduce(0, :+) end |
#score ⇒ Object
31 32 33 |
# File 'lib/studio_game/player.rb', line 31 def score @health + points end |
#to_s ⇒ Object
to_s returns a string whenever the class is called, the string will ALWAYS be returned without having to specifically ask for it
43 44 45 |
# File 'lib/studio_game/player.rb', line 43 def to_s "I'm #{@name} with health = #{@health}, points = #{points}, and score = #{score}." end |