Class: Creature

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal_hero/classes/creature.rb

Overview

Represents a creature that can participate in combat (ie. Player and Monsters)

Direct Known Subclasses

Monster, Player

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#avatarObject (readonly)

Returns the value of attribute avatar.



8
9
10
# File 'lib/terminal_hero/classes/creature.rb', line 8

def avatar
  @avatar
end

#coordsObject

Returns the value of attribute coords.



7
8
9
# File 'lib/terminal_hero/classes/creature.rb', line 7

def coords
  @coords
end

#current_hpObject (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

#levelObject (readonly)

Returns the value of attribute level.



8
9
10
# File 'lib/terminal_hero/classes/creature.rb', line 8

def level
  @level
end

#max_hpObject (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

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/terminal_hero/classes/creature.rb', line 8

def name
  @name
end

#statsObject (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_hpObject

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)

Returns:

  • (Boolean)


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_colorObject

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