Class: StudioGame::Player

Inherits:
Object
  • Object
show all
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

BerserkPlayer, ClumsyPlayer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Playable

#blam, #strong?, #w00t

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

#healthObject

Returns the value of attribute health.



13
14
15
# File 'lib/studio_game/player.rb', line 13

def health
  @health
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Class Method Details

.from_csv(string) ⇒ Object



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

def self.from_csv(string)
  name, health = string.split(',')
  Player.new(name, Integer(health))
end

Instance Method Details

#<=>(other) ⇒ Object



47
48
49
# File 'lib/studio_game/player.rb', line 47

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

#each_found_treasureObject



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

#pointsObject



64
65
66
# File 'lib/studio_game/player.rb', line 64

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

#scoreObject



31
32
33
# File 'lib/studio_game/player.rb', line 31

def score
  @health + points
end

#to_sObject

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