m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/hex_engine.rb
blob: f01adfc579757f431d495c956c3d3ab41cf55e5f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
require './engine'
require './rooms'

class Hex < Engine
  attr_accessor :state, :health, :endurance, :xp, :level, :keys, :magic_weapon, :magic_armor
  def initialize hash
    super
  end

  def hash_to_state hash
    @state = hash['state'] || 'init'
    @health = (hash['health'] || 6).to_i
    @endurance = (hash['endurance'] || 6).to_i
    @xp = (hash['xp'] || 49).to_i
    @level =( hash['level'] || 1).to_i
    @keys = (hash['keys'] || 0).to_i
    @magic_weapon = (hash['magic_weapon'] || 0).to_i
    @magic_armor = (hash['magic_armor'] || 0).to_i
    @last_roll = (hash['last_roll'] || 0).to_i
    @messages = []
  end

  def step command
    case @state
    when 'init'
      add_message <<~MSG.chomp
        A curse has infested an ancient keep near your town.
        The evil magic has filled the keep with monsters.
        Can you save your home from this Hex?
      MSG
      @state = 'play'
    when 'dead'
      add_message <<~MSG.chomp
        You are dead.
        Thanks for playing! Better luck next time.
      MSG
    when 'win'
      add_message <<~MSG.chomp
        You have slain the monsters and emerge from the keep victorious!
        Congratulations and thanks for playing!
      MSG
    else
      resolve_last_room command
    end

    if @state == 'play'
      enter_next_room
    end
  end

  def resolve_last_room command
    room = get_room
    room.resolve command
  end

  def enter_next_room
    roll_room
    room = get_room
    room.enter
  end

  def get_room
    (if @xp >= 50
      Rooms::Store
    else
      case @last_roll + @keys
      when 1, 3
        Rooms::Empty
      when 2
        Rooms::Trap
      when 4, 5
        Rooms::Monster
      when 6
        Rooms::Treasure
      when 7
        Rooms::Stairs
      when 8
        Rooms::Boss
      when 9
        Rooms::Exit
      end
    end).new self
  end

  def roll_room
    @last_roll = roll_die
  end

  def roll_die
    1 + rand(6)
  end

  def add_message message
    @messages.push(message)
  end

  def update_with_bonus current, update, bonus
    if update < 0
      current + [update + bonus, 0].min
    else
      current + update
    end
  end

  def update_health health
    @health = update_with_bonus @health, health, @magic_armor
  end

  def update_endurance endurance
    @endurance = update_with_bonus @endurance, endurance, @magic_weapon
  end

  def update_hex health, endurance, xp
    update_health health
    update_endurance endurance
    @xp += xp
  end

  def state_to_hash
    hash = {}
    hash['state'] = @state
    hash['health'] = @health
    hash['endurance'] = @endurance
    hash['xp'] = @xp
    hash['level'] = @level
    hash['keys'] = @keys
    hash['magic_weapon'] = @magic_weapon
    hash['magic_armor'] = @magic_armor
    hash['last_roll'] = @last_roll
    hash
  end

  def message
    <<~MSG
      H: #{@health} E: #{@endurance} X: #{@xp} Keys: #{@keys}
      Weapon: #{@magic_weapon} Armor: #{@magic_armor} Level: #{@level}
      #{@messages.join "\n"}
    MSG
  end
end