Class: Creature
- Inherits:
-
Object
- Object
- Creature
- Defined in:
- lib/terminal_hero/classes/creature.rb
Overview
Represents a creature that can participate in combat (ie. Player and Monsters)
Instance Attribute Summary collapse
-
#avatar ⇒ Object
readonly
Returns the value of attribute avatar.
-
#coords ⇒ Object
Returns the value of attribute coords.
-
#current_hp ⇒ Object
readonly
Returns the value of attribute current_hp.
-
#level ⇒ Object
readonly
Returns the value of attribute level.
-
#max_hp ⇒ Object
readonly
Returns the value of attribute max_hp.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#stats ⇒ Object
readonly
Returns the value of attribute stats.
Instance Method Summary collapse
-
#calc_damage_dealt(min: calc_damage_range[:min], max: calc_damage_range[:max]) ⇒ Object
Determine damage within a range based on a random (or given) roll.
-
#calc_damage_range(attack: stats[:atk][:value]) ⇒ Object
Calculate damage range based on a given attack stat value, returning min, max: max.
-
#calc_destination(direction) ⇒ Object
Given a direction to move, return the destination coords.
-
#calc_max_hp ⇒ Object
Calculate max HP based on stats (constitution).
-
#dead? ⇒ Boolean
Returns true if the creature is dead (hp at or below zero).
-
#flee(enemy) ⇒ Object
Attempt to flee from an enemy in combat.
-
#heal_hp(healing) ⇒ Object
Increase hp by healing received, not exceeding max hp.
-
#health_color ⇒ Object
Returns the color to use in displaying the creature’s health based on its percentage of max hp.
-
#initialize(name, coords, stats, health_lost, level, avatar) ⇒ Creature
constructor
A new instance of Creature.
-
#receive_damage(base_damage, defence: @stats[:dfc][:value]) ⇒ Object
Reduce hp by damage taken, after applying defence stat, but not below 0.
Constructor Details
#initialize(name, coords, stats, health_lost, level, avatar) ⇒ Creature
Returns a new instance of Creature.
10 11 12 13 14 15 16 17 18 |
# File 'lib/terminal_hero/classes/creature.rb', line 10 def initialize(name, coords, stats, health_lost, level, avatar) @name = name @level = level @stats = Utils.depth_two_clone(stats) @max_hp = calc_max_hp @current_hp = @max_hp - health_lost @coords = coords @avatar = avatar end |
Instance Attribute Details
#avatar ⇒ Object (readonly)
Returns the value of attribute avatar.
8 9 10 |
# File 'lib/terminal_hero/classes/creature.rb', line 8 def avatar @avatar end |
#coords ⇒ Object
Returns the value of attribute coords.
7 8 9 |
# File 'lib/terminal_hero/classes/creature.rb', line 7 def coords @coords end |
#current_hp ⇒ Object (readonly)
Returns the value of attribute current_hp.
8 9 10 |
# File 'lib/terminal_hero/classes/creature.rb', line 8 def current_hp @current_hp end |
#level ⇒ Object (readonly)
Returns the value of attribute level.
8 9 10 |
# File 'lib/terminal_hero/classes/creature.rb', line 8 def level @level end |
#max_hp ⇒ Object (readonly)
Returns the value of attribute max_hp.
8 9 10 |
# File 'lib/terminal_hero/classes/creature.rb', line 8 def max_hp @max_hp end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/terminal_hero/classes/creature.rb', line 8 def name @name end |
#stats ⇒ Object (readonly)
Returns the value of attribute stats.
8 9 10 |
# File 'lib/terminal_hero/classes/creature.rb', line 8 def stats @stats end |
Instance Method Details
#calc_damage_dealt(min: calc_damage_range[:min], max: calc_damage_range[:max]) ⇒ Object
Determine damage within a range based on a random (or given) roll
42 43 44 |
# File 'lib/terminal_hero/classes/creature.rb', line 42 def calc_damage_dealt(min: calc_damage_range[:min], max: calc_damage_range[:max]) return rand(min..max) end |
#calc_damage_range(attack: stats[:atk][:value]) ⇒ Object
Calculate damage range based on a given attack stat value, returning min, max: max
37 38 39 |
# File 'lib/terminal_hero/classes/creature.rb', line 37 def calc_damage_range(attack: stats[:atk][:value]) return { min: attack, max: (attack * 1.5).round } end |
#calc_destination(direction) ⇒ Object
Given a direction to move, return the destination coords
21 22 23 24 25 26 27 28 |
# File 'lib/terminal_hero/classes/creature.rb', line 21 def calc_destination(direction) return nil if direction.nil? return { x: @coords[:x] + GameData::MOVE_KEYS[direction][:x], y: @coords[:y] + GameData::MOVE_KEYS[direction][:y] } end |
#calc_max_hp ⇒ Object
Calculate max HP based on stats (constitution)
31 32 33 |
# File 'lib/terminal_hero/classes/creature.rb', line 31 def calc_max_hp return @stats[:con][:value] * GameData::CON_TO_HP end |
#dead? ⇒ Boolean
Returns true if the creature is dead (hp at or below zero)
68 69 70 |
# File 'lib/terminal_hero/classes/creature.rb', line 68 def dead? return @current_hp <= 0 end |
#flee(enemy) ⇒ Object
Attempt to flee from an enemy in combat. Chance of success varies with level difference
61 62 63 64 65 |
# File 'lib/terminal_hero/classes/creature.rb', line 61 def flee(enemy) level_difference = @level - enemy.level target = Utils.collar(0.05, 0.5 - (level_difference / 10.0), 0.95) return rand >= target end |
#heal_hp(healing) ⇒ Object
Increase hp by healing received, not exceeding max hp
55 56 57 58 |
# File 'lib/terminal_hero/classes/creature.rb', line 55 def heal_hp(healing) @current_hp = [@current_hp + healing, @max_hp].min return healing end |
#health_color ⇒ Object
Returns the color to use in displaying the creature’s health based on its percentage of max hp
73 74 75 76 77 78 79 |
# File 'lib/terminal_hero/classes/creature.rb', line 73 def health_color health_percent = @current_hp.to_f / @max_hp * 100 return :green if health_percent > 60 return :light_yellow if health_percent > 25 return :red end |
#receive_damage(base_damage, defence: @stats[:dfc][:value]) ⇒ Object
Reduce hp by damage taken, after applying defence stat, but not below 0
47 48 49 50 51 52 |
# File 'lib/terminal_hero/classes/creature.rb', line 47 def receive_damage(base_damage, defence: @stats[:dfc][:value]) reduction = (defence.to_f / 2).round damage = [base_damage - reduction, 1].max @current_hp = [@current_hp - damage, 0].max return damage end |