Class: Adventure::Player
- Inherits:
-
Object
- Object
- Adventure::Player
- Defined in:
- lib/adventure/player.rb
Overview
Represents the game player.
Instance Attribute Summary collapse
-
#classes ⇒ Object
readonly
The player's classes.
-
#gender ⇒ Object
readonly
The player's gender.
-
#inventory ⇒ Object
readonly
The player's Inventory.
-
#level ⇒ Object
readonly
The player's total level.
-
#name ⇒ Object
readonly
The player's name.
-
#purse ⇒ Object
readonly
The player's Purse.
-
#species ⇒ Object
readonly
The player's species.
Instance Method Summary collapse
-
#initialize(name, classes, species, gender, **opts) ⇒ Player
constructor
Create a new instance of Player.
-
#level_up ⇒ Integer
Bump the player's level by
1. -
#new_class(new_class) ⇒ Array<String>
Add a new class to the player and levels them up.
-
#to_i ⇒ Integer
Return an Integer as the player's total level.
-
#to_s ⇒ String
Return a String with the player's name, gender, species, and total level.
Constructor Details
#initialize(name, classes, species, gender, **opts) ⇒ Player
Create a new instance of Player.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/adventure/player.rb', line 30 def initialize(name, classes, species, gender, **opts) # Mandatory parameters. @name = name.strip @classes = classes.to_a.map(&:strip).map(&:capitalize) @species = species.strip.capitalize @gender = gender.strip.capitalize # Optional parameters. @level = opts.key?(:level) ? opts[:level].to_i : 1 @purse = opts.key?(:purse) ? opts[:purse] : nil @purse = Purse.new unless @purse.is_a? Purse @inventory = opts.key?(:inventory) ? opts[:inventory] : nil @inventory = Inventory.new unless @inventory.is_a? Inventory end |
Instance Attribute Details
#classes ⇒ Object (readonly)
The player's classes.
11 12 13 |
# File 'lib/adventure/player.rb', line 11 def classes @classes end |
#gender ⇒ Object (readonly)
The player's gender.
15 16 17 |
# File 'lib/adventure/player.rb', line 15 def gender @gender end |
#inventory ⇒ Object (readonly)
The player's Inventory.
19 20 21 |
# File 'lib/adventure/player.rb', line 19 def inventory @inventory end |
#level ⇒ Object (readonly)
The player's total level.
9 10 11 |
# File 'lib/adventure/player.rb', line 9 def level @level end |
#name ⇒ Object (readonly)
The player's name.
7 8 9 |
# File 'lib/adventure/player.rb', line 7 def name @name end |
#purse ⇒ Object (readonly)
The player's Adventure::Purse.
17 18 19 |
# File 'lib/adventure/player.rb', line 17 def purse @purse end |
#species ⇒ Object (readonly)
The player's species.
13 14 15 |
# File 'lib/adventure/player.rb', line 13 def species @species end |
Instance Method Details
#level_up ⇒ Integer
Bump the player's level by 1.
61 62 63 64 |
# File 'lib/adventure/player.rb', line 61 def level_up @level += 1 @level end |
#new_class(new_class) ⇒ Array<String>
Add a new class to the player and levels them up.
69 70 71 72 73 |
# File 'lib/adventure/player.rb', line 69 def new_class(new_class) @classes.push new_class.strip.capitalize level_up @classes end |
#to_i ⇒ Integer
Return an Integer as the player's total level.
54 55 56 |
# File 'lib/adventure/player.rb', line 54 def to_i @level end |
#to_s ⇒ String
Return a String with the player's name, gender, species, and total level.
47 48 49 |
# File 'lib/adventure/player.rb', line 47 def to_s "#{@name}, #{@gender.downcase} #{@species.downcase}, level #{@level}" end |