Class: Player
Overview
Represents the player’s character
Instance Attribute Summary collapse
-
#current_xp ⇒ Object
readonly
Returns the value of attribute current_xp.
-
#stats ⇒ Object
Returns the value of attribute stats.
Attributes inherited from Creature
#avatar, #coords, #current_hp, #level, #max_hp, #name
Instance Method Summary collapse
-
#allocate_stats(stats) ⇒ Object
Update the player’s stats to reflect a given statblock, and restore hp to full.
-
#calc_xp_to_level(current_lvl: @level, constant: GameData::LEVELING_CONSTANT, exponent: GameData::LEVELING_EXPONENT) ⇒ Object
Given a level, calculate the XP required to level up.
-
#export ⇒ Object
Export all values required for initialization to a hash, to be stored in a JSON save file.
-
#gain_xp(xp_gained) ⇒ Object
Gain a given amount of XP, and return the amount gained.
-
#initialize(name: "Player", coords: GameData::DEFAULT_COORDS, level: 1, stats: GameData::DEFAULT_STATS, health_lost: 0, current_xp: 0, avatar: "@".colorize(:blue)) ⇒ Player
constructor
A new instance of Player.
-
#level_up ⇒ Object
Levels up the player based on current XP and returns the number of levels gained.
-
#leveled_up? ⇒ Boolean
Returns whether the player’s current xp is sufficient to level up.
-
#lose_xp(level: @level, exponent: GameData::LEVELING_EXPONENT, constant: level, modifier: GameData::XP_LOSS_MULTIPLIER) ⇒ Object
Lose an amount of XP based on player level (but not reducing current XP below 0), and return the amount lost.
-
#post_combat(outcome, enemy) ⇒ Object
Apply any healing and XP gain or loss after the end of a combat encounter, based on the outcome of the combat and the enemy fought.
-
#xp_progress ⇒ Object
Return a string showing Player’s progress to next level.
Methods inherited from Creature
#calc_damage_dealt, #calc_damage_range, #calc_destination, #calc_max_hp, #dead?, #flee, #heal_hp, #health_color, #receive_damage
Constructor Details
#initialize(name: "Player", coords: GameData::DEFAULT_COORDS, level: 1, stats: GameData::DEFAULT_STATS, health_lost: 0, current_xp: 0, avatar: "@".colorize(:blue)) ⇒ Player
Returns a new instance of Player.
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/terminal_hero/classes/player.rb', line 9 def initialize( name: "Player", coords: GameData::DEFAULT_COORDS, level: 1, stats: GameData::DEFAULT_STATS, health_lost: 0, current_xp: 0, avatar: "@".colorize(:blue) ) super(name, coords, stats, health_lost, level, avatar) @current_xp = current_xp end |
Instance Attribute Details
#current_xp ⇒ Object (readonly)
Returns the value of attribute current_xp.
7 8 9 |
# File 'lib/terminal_hero/classes/player.rb', line 7 def current_xp @current_xp end |
#stats ⇒ Object
Returns the value of attribute stats.
6 7 8 |
# File 'lib/terminal_hero/classes/player.rb', line 6 def stats @stats end |
Instance Method Details
#allocate_stats(stats) ⇒ Object
Update the player’s stats to reflect a given statblock, and restore hp to full
80 81 82 83 84 |
# File 'lib/terminal_hero/classes/player.rb', line 80 def allocate_stats(stats) @stats = stats @max_hp = calc_max_hp @current_hp = @max_hp end |
#calc_xp_to_level(current_lvl: @level, constant: GameData::LEVELING_CONSTANT, exponent: GameData::LEVELING_EXPONENT) ⇒ Object
Given a level, calculate the XP required to level up
23 24 25 |
# File 'lib/terminal_hero/classes/player.rb', line 23 def calc_xp_to_level(current_lvl: @level, constant: GameData::LEVELING_CONSTANT, exponent: GameData::LEVELING_EXPONENT) return (constant * (current_lvl**exponent)).round end |
#export ⇒ Object
Export all values required for initialization to a hash, to be stored in a JSON save file
87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/terminal_hero/classes/player.rb', line 87 def export return { name: @name, coords: @coords, level: @level, stats: @stats, health_lost: (@max_hp - @current_hp), current_xp: @current_xp, avatar: @avatar } end |
#gain_xp(xp_gained) ⇒ Object
Gain a given amount of XP, and return the amount gained
42 43 44 45 |
# File 'lib/terminal_hero/classes/player.rb', line 42 def gain_xp(xp_gained) @current_xp += xp_gained return xp_gained end |
#level_up ⇒ Object
Levels up the player based on current XP and returns the number of levels gained
66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/terminal_hero/classes/player.rb', line 66 def level_up return 0 unless leveled_up? levels_gained = 0 while @current_xp >= calc_xp_to_level @current_xp -= calc_xp_to_level @level += 1 levels_gained += 1 end return levels_gained end |
#leveled_up? ⇒ Boolean
Returns whether the player’s current xp is sufficient to level up
61 62 63 |
# File 'lib/terminal_hero/classes/player.rb', line 61 def leveled_up? return @current_xp >= calc_xp_to_level end |
#lose_xp(level: @level, exponent: GameData::LEVELING_EXPONENT, constant: level, modifier: GameData::XP_LOSS_MULTIPLIER) ⇒ Object
Lose an amount of XP based on player level (but not reducing current XP below 0), and return the amount lost
49 50 51 52 53 |
# File 'lib/terminal_hero/classes/player.rb', line 49 def lose_xp(level: @level, exponent: GameData::LEVELING_EXPONENT, constant: level, modifier: GameData::XP_LOSS_MULTIPLIER) xp_lost = (constant + (level**exponent) * modifier).round @current_xp = [@current_xp - xp_lost, 0].max return xp_lost end |
#post_combat(outcome, enemy) ⇒ Object
Apply any healing and XP gain or loss after the end of a combat encounter,
based on the outcome of the combat and the enemy fought. Return xp gained or lost (if any) for display to the user.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/terminal_hero/classes/player.rb', line 29 def post_combat(outcome, enemy) case outcome when :victory return gain_xp(enemy.calc_xp) when :defeat heal_hp(@max_hp) return lose_xp else return nil end end |
#xp_progress ⇒ Object
Return a string showing Player’s progress to next level
56 57 58 |
# File 'lib/terminal_hero/classes/player.rb', line 56 def xp_progress return "#{@current_xp}/#{calc_xp_to_level}" end |