Class: Monster

Inherits:
Creature show all
Defined in:
lib/terminal_hero/classes/monster.rb

Overview

Represents an enemy that the player can fight

Instance Attribute Summary collapse

Attributes inherited from Creature

#avatar, #coords, #current_hp, #level, #max_hp, #name, #stats

Instance Method Summary collapse

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: "Monster", coords: nil, stats: GameData::DEFAULT_STATS, health_lost: 0, level_base: 1, level: nil, avatar: "%".colorize(:red), event: :combat) ⇒ Monster

Returns a new instance of Monster.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/terminal_hero/classes/monster.rb', line 9

def initialize(
  name: "Monster",
  coords: nil,
  stats: GameData::DEFAULT_STATS,
  health_lost: 0,
  level_base: 1,
  level: nil,
  avatar: "%".colorize(:red),
  event: :combat
)
  level = select_level(level_base) if level.nil?
  stats = allocate_stats(stats, level)
  super(name, coords, stats, health_lost, level, avatar)
  @event = event
end

Instance Attribute Details

#eventObject (readonly)

Returns the value of attribute event.



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

def event
  @event
end

Instance Method Details

#allocate_stats(starting_stats, level) ⇒ Object

Calculate stat points based on monster level, and randomly allocate them among stats



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/terminal_hero/classes/monster.rb', line 33

def allocate_stats(starting_stats, level)
  # Monster starts with 5 less stat points, so Player is slightly stronger
  stat_points = (level - 1) * GameData::STAT_POINTS_PER_LEVEL
  stats = Utils.depth_two_clone(starting_stats)
  # Allocate a random number of available points to each stat, in random order
  keys = stats.keys.shuffle
  keys.each do |key|
    point_spend = rand(0..stat_points)
    stats[key][:value] += point_spend
    stat_points -= point_spend
  end
  # Allocate remaining stat points to the last stat
  stats[keys[-1]][:value] += stat_points
  return stats
end

#calc_xp(level: @level, exponent: GameData::LEVELING_EXPONENT, constant: level) ⇒ Object

Calculate the amount of XP a monster is worth, based on its level and an exponent and range



51
52
53
# File 'lib/terminal_hero/classes/monster.rb', line 51

def calc_xp(level: @level, exponent: GameData::LEVELING_EXPONENT, constant: level)
  return constant + (level**exponent).round
end

#choose_move(player_coords) ⇒ Object

Decide whether and where to move. There is a 75% chance the monster will move at all, and if it does, it will move towards the palayer if the player is within 6 tiles of the moster.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/terminal_hero/classes/monster.rb', line 57

def choose_move(player_coords)
  return nil unless rand < 0.75

  x_difference = @coords[:x] - player_coords[:x]
  y_difference = @coords[:y] - player_coords[:y]
  directions = { x: [:left, :right], y: [:up, :down] }
  if x_difference.abs + y_difference.abs <= 6
    axis = x_difference.abs > y_difference.abs ? :x : :y
    direction = @coords[axis] > player_coords[axis] ? directions[axis][0] : directions[axis][1]
  else
    direction = [:left, :right, :up, :down][rand(0..3)]
  end
  return direction
end

#exportObject

Export all values required for initialization to a hash, to be stored in a JSON save file



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/terminal_hero/classes/monster.rb', line 73

def export
  return {
    name: @name,
    coords: @coords,
    level: @level,
    stats: @stats,
    health_lost: (@max_hp - @current_hp),
    avatar: @avatar,
    event: @event
  }
end

#select_level(level_base) ⇒ Object

Set level, based on base level and maximum deviation from that base



26
27
28
29
30
# File 'lib/terminal_hero/classes/monster.rb', line 26

def select_level(level_base)
  min = [level_base - GameData::MONSTER_LEVEL_VARIANCE, 1].max
  max = [level_base + GameData::MONSTER_LEVEL_VARIANCE, 1].max
  return rand(min..max)
end