m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/hex_engine.rb
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-08-17 23:31:13 -0700
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-08-17 23:31:13 -0700
commite3afffc91d678301fcf7660db541cacc8b7a4e8d (patch)
treefd391ede8eb099d5b66672751ee01ae07f6deb98 /hex_engine.rb
Initial commit
Diffstat (limited to 'hex_engine.rb')
-rw-r--r--hex_engine.rb140
1 files changed, 140 insertions, 0 deletions
diff --git a/hex_engine.rb b/hex_engine.rb
new file mode 100644
index 0000000..f01adfc
--- /dev/null
+++ b/hex_engine.rb
@@ -0,0 +1,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