From 6f43ae23837f8336804b9b2e20a49196f13b82ad Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sat, 24 Aug 2019 20:58:20 -0700 Subject: Modify state * Welcome message handled through room * Store last room, not last roll (fixes bug where Store would be entered even if a different room appeared beforehand) --- hex_engine.rb | 94 +++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 59 insertions(+), 35 deletions(-) (limited to 'hex_engine.rb') diff --git a/hex_engine.rb b/hex_engine.rb index f01adfc..cd44d56 100644 --- a/hex_engine.rb +++ b/hex_engine.rb @@ -8,7 +8,7 @@ class Hex < Engine end def hash_to_state hash - @state = hash['state'] || 'init' + @state = hash['state'] || 'play' @health = (hash['health'] || 6).to_i @endurance = (hash['endurance'] || 6).to_i @xp = (hash['xp'] || 49).to_i @@ -16,19 +16,37 @@ class Hex < Engine @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 + @room = str_to_room(hash['room'] || 'welcome').new self @messages = [] end + def str_to_room name + case name + when 'welcome' + Rooms::Welcome + when 'store' + Rooms::Store + when 'empty' + Rooms::Empty + when 'trap' + Rooms::Trap + when 'monster' + Rooms::Monster + when 'treasure' + Rooms::Treasure + when 'stairs' + Rooms::Stairs + when 'boss' + Rooms::Boss + when 'exit' + Rooms::Exit + end + 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 'play', 'invalid' + resolve_last_room command when 'dead' add_message <<~MSG.chomp You are dead. @@ -40,7 +58,6 @@ class Hex < Engine Congratulations and thanks for playing! MSG else - resolve_last_room command end if @state == 'play' @@ -49,41 +66,48 @@ class Hex < Engine end def resolve_last_room command - room = get_room - room.resolve command + @room.resolve command end def enter_next_room roll_room - room = get_room - room.enter + @room = get_room + @room.enter + end + + def get_room_from_roll + (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).new self end def get_room - (if @xp >= 50 - Rooms::Store + if @xp >= 50 + Rooms::Store.new self 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 + get_room_from_roll + end end def roll_room - @last_roll = roll_die + if @next_roll != nil + @last_roll = @next_roll + else + @last_roll = roll_die + end end def roll_die @@ -126,7 +150,7 @@ class Hex < Engine hash['keys'] = @keys hash['magic_weapon'] = @magic_weapon hash['magic_armor'] = @magic_armor - hash['last_roll'] = @last_roll + hash['room'] = @room.to_s hash end -- cgit v1.2.3