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