m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-08-24 20:58:20 -0700
committerMarcin Chrzanowski <marcin.j.chrzanowski@gmail.com>2019-08-24 20:58:20 -0700
commit6f43ae23837f8336804b9b2e20a49196f13b82ad (patch)
tree7d2b8bbbe40476bd13f6c9d2b4412d19fc8c59c2
parent7d086ae2edcc6edabe65e6aa9265e756504a64ad (diff)
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)
-rw-r--r--hex_engine.rb94
-rw-r--r--rooms.rb158
2 files changed, 92 insertions, 160 deletions
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
diff --git a/rooms.rb b/rooms.rb
index ff71507..2e0bc01 100644
--- a/rooms.rb
+++ b/rooms.rb
@@ -5,142 +5,23 @@ module Rooms
end
def resolve command
- @command = command
- if @engine.state == 'invalid'
- @engine.state = 'play'
- end
-
- if @engine.state == 'play'
- _do_resolve
- end
- end
-
- def check_death
- if @engine.health <= 0
- @engine.state = 'dead'
- elsif @engine.endurance <= 0
- @engine.state = 'dead'
- end
- end
-
- def _do_resolve
- do_resolve
- check_death
- if !instant_resolve
- puts "Adding message: #{resolve_message}"
- @engine.add_message resolve_message
- end
- end
-
- # TODO: limit weapon/armor levels
- def do_resolve
- case @command
- when 'c'
- when 'w'
- if @engine.xp >= 50
- @engine.xp -= 50
- @engine.magic_weapon += 1
- @engine.add_message "You've upgraded your magic weapon!"
- else
- @engine.add_message 'Not enough XP to upgrade...'
- end
- when 'a'
- if @engine.xp >= 50
- @engine.xp -= 50
- @engine.magic_armor += 1
- @engine.add_message "You've upgraded your magic armor!"
- else
- @engine.add_message 'Not enough XP to upgrade...'
- end
- else
- do_invalid
- end
- end
-
- def do_invalid
- @engine.state = 'invalid'
- @engine.add_message "Invalid command: #{@command}"
- enter
- end
-
- def resolve_message
- if @engine.state == 'dead'
- if @engine.health <= 0
- death_message
- elsif @engine.endurance <= 0
- exhaust_message
- end
- else
- success_message
- end
- end
-
- def enter_message
- ''
- end
-
- def death_message
- 'You die'
- end
-
- def exhaust_message
- 'You fall over exhausted'
- end
-
- def success_message
- ''
- end
-
- def options
- [
- 'continue',
- ]
- end
-
- def enter
- @engine.add_message enter_message
- if instant_resolve
- do_instant_resolve
- @engine.add_message resolve_message
- end
-
- options.each do |option|
- @engine.add_message ">[#{option[0]}]#{option[1..]}"
- end
- end
- end
-
- class Store < Room
- def instant_resolve
- false
- end
-
- def enter_message
- 'You have enough XP to upgrade your equipment!'
- end
-
- def options
- [
- 'weapon upgrade',
- 'armor upgrade'
- ]
- end
-
- def do_resolve
case @command
when 'a'
@engine.xp -= 50
@engine.magic_armor += 1
- @engine.add_message "You've upgraded your magic armor!"
+ @message = "You've upgraded your magic armor!"
when 'w'
@engine.xp -= 50
@engine.magic_weapon += 1
- @engine.add_message "You've upgraded your magic weapon!"
+ @message = "You've upgraded your magic weapon!"
else
do_invalid
end
end
+ def to_s
+ 'store'
+ end
end
class Empty < Room
@@ -181,6 +62,10 @@ module Rooms
'scavenge for food (+3 H, -1 E)'
]
end
+
+ def to_s
+ 'empty'
+ end
end
class Trap < Room
@@ -218,6 +103,10 @@ module Rooms
"disable trap (-#{@engine.level} E)"
]
end
+
+ def to_s
+ 'trap'
+ end
end
class Monster < Room
@@ -254,6 +143,10 @@ module Rooms
"fight (-#{@engine.level} H, -#{@engine.level} E, #{@engine.level} X)"
]
end
+
+ def to_s
+ 'monster'
+ end
end
class Treasure < Room
@@ -285,6 +178,10 @@ module Rooms
"You found some gold! (+#{@engine.level} X)"
end
end
+
+ def to_s
+ 'treasure'
+ end
end
class Stairs < Room
@@ -293,7 +190,6 @@ module Rooms
end
def do_instant_resolve
- p 'increasing level'
@engine.level += 1
end
@@ -304,6 +200,10 @@ module Rooms
def success_message
'You journey downwards.'
end
+
+ def to_s
+ 'stairs'
+ end
end
class Boss < Room
@@ -322,6 +222,10 @@ module Rooms
def success_message
'You defeat the fiend!'
end
+
+ def to_s
+ 'boss'
+ end
end
class Exit < Room
@@ -336,5 +240,9 @@ module Rooms
def success_message
'You won! Congratulations!'
end
+
+ def to_s
+ 'exit'
+ end
end
end