Class: Gemwarrior::Evaluator
- Inherits:
-
Object
- Object
- Gemwarrior::Evaluator
- Defined in:
- lib/gemwarrior/evaluator.rb
Constant Summary collapse
- PROGRAM_NAME =
CONSTANTS MESSAGES
'Gem Warrior'- QUIT_MESSAGE =
'Thanks for playing the game. Until next time...'.colorize(:yellow)
- RESUME_MESSAGE =
'Back to adventuring!'.colorize(:green)
- SEPARATOR =
'=========================================================================='- GO_PARAMS =
'Options: north, east, south, west'- CHANGE_PARAMS =
'Options: name'- LIST_PARAMS =
'Options: monsters, items, locations'- DEBUG_PARAMS =
'Options: vars, map, stat'- DEBUG_STAT_PARAMS =
'Options: atk_lo, atk_hi, strength, dexterity'- ERROR_COMMAND_INVALID =
ERRORS
'That is not something the game yet understands.'- ERROR_GO_PARAM_MISSING =
'Just wander aimlessly? A direction would be nice.'- ERROR_GO_PARAM_INVALID =
'The place in that direction is far, far, FAR too dangerous. You should try a different way.'- ERROR_ATTACK_PARAM_MISSING =
'You cannot just "attack". You gotta choose something to attack.'- ERROR_ATTACK_PARAM_INVALID =
'That monster does not exist here or can\'t be attacked.'- ERROR_TAKE_PARAM_MISSING =
'You cannot just "take". You gotta choose something to take.'- ERROR_DROP_PARAM_MISSING =
'You cannot just "drop". You gotta choose something to drop.'- ERROR_EQUIP_PARAM_MISSING =
'You cannot just "equip". You gotta choose something to equip.'- ERROR_UNEQUIP_PARAM_MISSING =
'You cannot just "unequip". You gotta choose something to unequip.'- ERROR_CHANGE_PARAM_MISSING =
'You cannot just "change". You gotta choose something to change.'- ERROR_CHANGE_PARAM_INVALID =
'You cannot change that...yet.'- ERROR_LIST_PARAM_MISSING =
'You cannot just "list". You gotta choose something to list.'- ERROR_LIST_PARAM_INVALID =
'You cannot list that...yet.'- ERROR_DEBUG_PARAM_MISSING =
'You cannot just "debug". You gotta choose a debug command.'- ERROR_DEBUG_PARAM_INVALID =
'You cannot debug that...yet.'- ERROR_DEBUG_STAT_PARAM_MISSING =
'You cannot just "change stats". You gotta choose a stat to change.'- ERROR_DEBUG_STAT_PARAM_INVALID =
'You cannot change that stat...yet.'
Instance Attribute Summary collapse
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#commands ⇒ Object
Returns the value of attribute commands.
-
#descriptions ⇒ Object
Returns the value of attribute descriptions.
-
#devaliases ⇒ Object
Returns the value of attribute devaliases.
-
#devcmds ⇒ Object
Returns the value of attribute devcmds.
-
#world ⇒ Object
Returns the value of attribute world.
Instance Method Summary collapse
- #evaluate(input) ⇒ Object
-
#initialize(world) ⇒ Evaluator
constructor
A new instance of Evaluator.
Constructor Details
#initialize(world) ⇒ Evaluator
Returns a new instance of Evaluator.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/gemwarrior/evaluator.rb', line 43 def initialize(world) self.world = world self.devcmds = %w(debug) self.devaliases = %w(db) self.commands = %w(character inventory list rest look take drop equip unequip go attack change help quit quit!) self.aliases = %w(c i ls r l t d e ue g a ch h q qq) self.descriptions = [ 'Display character information', 'Look in your inventory', 'List all the objects of a type that exist in the world', 'Take a load off and regain HP', 'Look around your current location', 'Take item', 'Drop item', 'Equip item', 'Unequip item', 'Go in a direction', 'Attack a monster', 'Change something', 'This help menu', 'Quit w/ confirmation (also exit/x)', 'Quit w/o confirmation (also exit!/xx)' ] end |
Instance Attribute Details
#aliases ⇒ Object
Returns the value of attribute aliases.
41 42 43 |
# File 'lib/gemwarrior/evaluator.rb', line 41 def aliases @aliases end |
#commands ⇒ Object
Returns the value of attribute commands.
41 42 43 |
# File 'lib/gemwarrior/evaluator.rb', line 41 def commands @commands end |
#descriptions ⇒ Object
Returns the value of attribute descriptions.
41 42 43 |
# File 'lib/gemwarrior/evaluator.rb', line 41 def descriptions @descriptions end |
#devaliases ⇒ Object
Returns the value of attribute devaliases.
41 42 43 |
# File 'lib/gemwarrior/evaluator.rb', line 41 def devaliases @devaliases end |
#devcmds ⇒ Object
Returns the value of attribute devcmds.
41 42 43 |
# File 'lib/gemwarrior/evaluator.rb', line 41 def devcmds @devcmds end |
#world ⇒ Object
Returns the value of attribute world.
41 42 43 |
# File 'lib/gemwarrior/evaluator.rb', line 41 def world @world end |
Instance Method Details
#evaluate(input) ⇒ Object
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/gemwarrior/evaluator.rb', line 68 def evaluate(input) case input # Ctrl-D or empty command when nil, "" return # real command else tokens = input.split unless input_valid?(input) return ERROR_COMMAND_INVALID end end command = tokens.first.downcase param1 = tokens[1] param2 = tokens[2] param3 = tokens[3] case command # dev commands when 'debug', 'db' if param1.nil? puts ERROR_DEBUG_PARAM_MISSING DEBUG_PARAMS else case param1 when 'vars', 'v' world.print_all_vars when 'godmode', 'iddqd', 'god', 'g' world.player.god_mode = !world.player.god_mode when 'beastmode', 'beast', 'b' world.player.beast_mode = !world.player.beast_mode when 'map', 'm' world.print_map when 'stat' if param2.nil? puts ERROR_DEBUG_STAT_PARAM_MISSING DEBUG_STAT_PARAMS else case param2 when 'atk_lo' unless param3.nil? param3 = param3.to_i if param3.is_a? Numeric if param3 >= 0 world.player.atk_lo = param3 end end end when 'atk_hi' unless param3.nil? param3 = param3.to_i if param3.is_a? Numeric if param3 >= 0 world.player.atk_hi = param3 end end end when 'strength', 'str', 'st' unless param3.nil? param3 = param3.to_i if param3.is_a? Numeric if param3 >= 0 world.player.atk_lo = param3 world.player.atk_hi = param3 end end end when 'dexterity', 'dex', 'd' unless param3.nil? param3 = param3.to_i if param3.is_a? Numeric if param3 >= 0 world.player.dexterity = param3 end end end else ERROR_DEBUG_STAT_PARAM_INVALID end end else ERROR_DEBUG_PARAM_INVALID end end # normal commands when 'character', 'c' world.player.check_self when 'inventory', 'i' if param1 world.player.inventory.describe_item(param1) else world.player.list_inventory end when 'list', 'ls' if param1.nil? puts ERROR_LIST_PARAM_MISSING LIST_PARAMS else world.list(param1) end when 'rest', 'r' world.player.rest when 'look', 'l' if param1 world.describe_entity(world.location_by_coords(world.player.cur_coords), param1) else world.describe(world.location_by_coords(world.player.cur_coords)) end when 'take', 't' if param1.nil? ERROR_TAKE_PARAM_MISSING else world.player.inventory.add_item(world.location_by_coords(world.player.cur_coords), param1) end when 'drop', 'd' if param1.nil? ERROR_DROP_PARAM_MISSING else world.player.inventory.remove_item(param1) end when 'equip', 'e' if param1.nil? ERROR_EQUIP_PARAM_MISSING else world.player.inventory.equip_item(param1) end when 'unequip', 'ue' if param1.nil? ERROR_UNEQUIP_PARAM_MISSING else world.player.inventory.unequip_item(param1) end when 'go', 'g' if param1.nil? puts ERROR_GO_PARAM_MISSING GO_PARAMS else direction = param1 if world.can_move?(direction) world.player.go(world.locations, param1) world.location_by_coords(world.player.cur_coords).checked_for_monsters = false world.describe(world.location_by_coords(world.player.cur_coords)) else ERROR_GO_PARAM_INVALID end end when 'attack', 'a' if param1.nil? ERROR_ATTACK_PARAM_MISSING else monster_name = param1 if world.has_monster_to_attack?(monster_name) monster = world.location_by_coords(world.player.cur_coords).monster_by_name(monster_name) world.player.attack(world, monster) else ERROR_ATTACK_PARAM_INVALID end end when 'change', 'ch' if param1.nil? puts ERROR_CHANGE_PARAM_MISSING CHANGE_PARAMS else case param1 when 'name' world.player.modify_name else ERROR_CHANGE_PARAM_INVALID end end when 'help', 'h' list_commands when 'quit', 'exit', 'q', 'x' puts "You sure you want to quit? (y/n): " response = gets.chomp.downcase if (response.eql?("y") || response.eql?("yes")) puts QUIT_MESSAGE exit(0) else puts RESUME_MESSAGE end when 'quit!', 'exit!', 'qq', 'xx' puts QUIT_MESSAGE exit(0) else return end end |