Implement game states.
catseye
12 years ago
51 | 51 | |
52 | 52 | this.subway = false; |
53 | 53 | |
54 | this.done = false; | |
55 | 54 | this.skip = false; |
56 | 55 | this.moved = false; |
56 | ||
57 | this.gameState = 'stateTitleScreen'; | |
57 | 58 | |
58 | 59 | var loc_adj = [ |
59 | 60 | 'Rocky', |
132 | 133 | this.arrows = 1; |
133 | 134 | |
134 | 135 | tty.write("SUPER WUMPUS LAND\n\n"); |
135 | this.show(); | |
136 | this.ask(); | |
136 | this.pause('statePrompt'); | |
137 | }; | |
138 | ||
139 | this.pause = function(nextState) { | |
140 | this.tty.write("\n[Press ENTER to continue.] "); | |
141 | this.gameState = nextState; | |
137 | 142 | }; |
138 | 143 | |
139 | 144 | this.show = function() { |
178 | 183 | print("" + room.tokens + " subway tokens lie on the ground here.\n"); |
179 | 184 | } |
180 | 185 | |
181 | if (room.guano > 0) | |
182 | { | |
183 | print("\nThere is a "); | |
184 | if (room.guano === 1) { print("small"); } | |
185 | else if (room.guano === 2) { print("sizable"); } | |
186 | else if (room.guano === 3) { print("large"); } | |
187 | else if (room.guano === 4) { print("huge"); } | |
188 | else { print("gigantic"); } | |
189 | print(" pile of Wumpus dung here.\n"); | |
186 | if (room.guano > 0) { | |
187 | print("\nThere is a "); | |
188 | if (room.guano === 1) { print("small"); } | |
189 | else if (room.guano === 2) { print("sizable"); } | |
190 | else if (room.guano === 3) { print("large"); } | |
191 | else if (room.guano === 4) { print("huge"); } | |
192 | else { print("gigantic"); } | |
193 | print(" pile of Wumpus dung here.\n"); | |
190 | 194 | } |
191 | 195 | |
192 | 196 | print("\n"); |
204 | 208 | if (this.codliver > 0) { |
205 | 209 | print(" ...and immediately BARFED YOU BACK OUT!!!!\n"); |
206 | 210 | } else { |
207 | this.pause(); | |
208 | this.done = true; | |
209 | return; | |
211 | this.pause('stateGameOver'); | |
212 | return false; | |
210 | 213 | } |
211 | 214 | } |
212 | 215 | } else { |
231 | 234 | print("* Super Bats in this location stay well away from your awful stench!\n"); |
232 | 235 | } else { |
233 | 236 | print("* Zap! Super Bat Snatch! Elsewheresville for you!\n"); |
234 | this.pause(); | |
235 | 237 | this.roomNo = d(1,100); |
236 | 238 | this.skip = 1; |
237 | return; | |
239 | this.pause('statePrompt'); | |
240 | return false; | |
238 | 241 | } |
239 | 242 | } |
240 | 243 | var batsNearby = false; |
254 | 257 | print("* You deftly stick to the edge of the bottomless pit!\n"); |
255 | 258 | } else { |
256 | 259 | print("* Yiiiieeee!!! Fell in a pit!\n"); |
257 | this.pause(); | |
258 | this.done = true; | |
259 | return; | |
260 | this.pause('stateGameOver'); | |
261 | return false; | |
260 | 262 | } |
261 | 263 | } |
262 | 264 | var aDraft = false; |
284 | 286 | } |
285 | 287 | } |
286 | 288 | print("\n"); |
289 | return true; | |
287 | 290 | }; |
288 | 291 | |
289 | 292 | this.ask = function() { |
341 | 344 | print(" [I]nventory and Score\n\n"); |
342 | 345 | print(" [Q]uit\n\n"); |
343 | 346 | print("What would you like to do next, " + this.name + "? "); |
344 | }; | |
345 | ||
346 | this.pause = function() { | |
347 | this.tty.write("\n[Press ENTER to continue.] "); | |
348 | this.paused = true; | |
349 | }; | |
350 | ||
351 | this.handleInput = function(input) { | |
352 | if (this.paused) { | |
353 | this.paused = false; | |
354 | this.show(); | |
347 | ||
348 | this.gameState = 'stateProcessCommand'; | |
349 | }; | |
350 | ||
351 | this.moveWumpi = function() { | |
352 | var self = this; | |
353 | var print = function(str) { | |
354 | self.tty.write(str); | |
355 | }; | |
356 | ||
357 | for (var i = 0; i < this.wumpi.length; i++) { | |
358 | var wumpus = this.wumpi[i]; | |
359 | if (wumpus.room === 0) { | |
360 | if (d(1, 5) === 1) { | |
361 | // restart wumpus | |
362 | wumpus.room = d(1, 100); | |
363 | while (wumpus.room === this.roomNo) { | |
364 | wumpus.room = d(1, 100); | |
365 | } | |
366 | wumpus.asleep = false; | |
367 | } | |
368 | continue; | |
369 | } | |
370 | if (wumpus.asleep) { | |
371 | if (d(1, 4) === 1) { | |
372 | wumpus.asleep = false; | |
373 | if (d(1, 5) !== 1) { | |
374 | this.rooms[wumpus.room].guano++; | |
375 | } | |
376 | } | |
377 | } else { | |
378 | if (d(1, 3) === 1) { | |
379 | var dest = this.rooms[wumpus.room].exits[d(1,3)-1]; | |
380 | if (dest !== this.roomNo || !this.moved) { | |
381 | wumpus.room = dest; | |
382 | if (dest === this.roomNo) { | |
383 | print("From around a corner, a hungry-looking Wumpus appears!!\n"); | |
384 | this.pause(); | |
385 | } | |
386 | } | |
387 | } | |
388 | if (d(1,8) === 1) { wumpus.asleep = true; } | |
389 | if (d(1,8) === 1) { this.rooms[wumpus.room].guano++; } | |
390 | } | |
391 | } | |
392 | }; | |
393 | ||
394 | /* -*-*-*- GAME STATES -*-*-*- */ | |
395 | ||
396 | this.statePrompt = function(input) { | |
397 | if (this.show()) { | |
355 | 398 | this.ask(); |
356 | return; | |
357 | } | |
358 | ||
399 | } | |
400 | }; | |
401 | ||
402 | this.stateGameOver = function(input) { | |
403 | this.tty.write("Game over, " + this.name + ".\n"); | |
404 | }; | |
405 | ||
406 | this.stateProcessCommand = function(input) { | |
359 | 407 | var self = this; |
360 | 408 | var print = function(str) { |
361 | 409 | self.tty.write(str); |
364 | 412 | |
365 | 413 | input = input.toUpperCase(); |
366 | 414 | if (input === 'Q') { |
367 | this.done = true; | |
415 | this.pause('stateGameOver'); | |
368 | 416 | return; |
369 | 417 | } else if (input === 'I') { |
370 | 418 | //alert('ya'); |
371 | 419 | print("Your score is 7."); |
372 | this.pause(); | |
420 | this.pause('statePrompt'); | |
373 | 421 | return; |
374 | 422 | } else if (input === 'A' && this.cans > 0) { |
375 | 423 | this.cans--; |
410 | 458 | if (this.codliver > 0) this.codliver = 1; |
411 | 459 | if (this.batbgon > 0) this.batbgon = 1; |
412 | 460 | } |
413 | this.pause(); | |
461 | this.pause('statePrompt'); | |
462 | return; | |
414 | 463 | } else if (input === 'D' && room.guano > 0) { |
415 | 464 | this.ustink += d(3,3); |
416 | 465 | room.guano--; |
418 | 467 | if (d(1,6) == 1) { room.cans++; } |
419 | 468 | if (d(1,12) == 1) { room.tokens++; } |
420 | 469 | print("\nEw. You now stink so bad that you can't smell anything but yourself.\n"); |
421 | this.pause(); | |
470 | this.pause('statePrompt'); | |
422 | 471 | return; |
423 | 472 | } else if (input === 'P') { |
424 | 473 | this.arrows += room.arrows; room.arrows = 0; |
438 | 487 | |
439 | 488 | this.moveWumpi(); |
440 | 489 | |
441 | this.show(); | |
442 | this.ask(); | |
443 | }; | |
444 | ||
445 | this.moveWumpi = function() { | |
446 | var self = this; | |
447 | var print = function(str) { | |
448 | self.tty.write(str); | |
449 | }; | |
450 | ||
451 | for (var i = 0; i < this.wumpi.length; i++) { | |
452 | var wumpus = this.wumpi[i]; | |
453 | if (wumpus.room === 0) { | |
454 | if (d(1, 5) === 1) { | |
455 | // restart wumpus | |
456 | wumpus.room = d(1, 100); | |
457 | while (wumpus.room === this.roomNo) { | |
458 | wumpus.room = d(1, 100); | |
459 | } | |
460 | wumpus.asleep = false; | |
461 | } | |
462 | continue; | |
463 | } | |
464 | if (wumpus.asleep) { | |
465 | if (d(1, 4) === 1) { | |
466 | wumpus.asleep = false; | |
467 | if (d(1, 5) !== 1) { | |
468 | this.rooms[wumpus.room].guano++; | |
469 | } | |
470 | } | |
471 | } else { | |
472 | if (d(1, 3) === 1) { | |
473 | var dest = this.rooms[wumpus.room].exits[d(1,3)-1]; | |
474 | if (dest !== this.roomNo || !this.moved) { | |
475 | wumpus.room = dest; | |
476 | if (dest === this.roomNo) { | |
477 | print("From around a corner, a hungry-looking Wumpus appears!!\n"); | |
478 | this.pause(); | |
479 | } | |
480 | } | |
481 | } | |
482 | if (d(1,8) === 1) { wumpus.asleep = true; } | |
483 | if (d(1,8) === 1) { this.rooms[wumpus.room].guano++; } | |
484 | } | |
485 | } | |
490 | if (this.show()) { | |
491 | this.ask(); | |
492 | } | |
493 | }; | |
494 | ||
495 | this.handleInput = function(input) { | |
496 | this[this.gameState](input); | |
486 | 497 | }; |
487 | 498 | }; |