Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] ending the game

  • Loading branch information...
commit f5c6aa34e8e2973eefd157c2348bb6ba84f6ddb1 1 parent 999dfc8
Carl Mäsak authored July 26, 2012

Showing 1 changed file with 61 additions and 0 deletions. Show diff stats Hide diff stats

  1. 61  bin/crypt
61  bin/crypt
@@ -351,6 +351,9 @@ class Adventure::LightSourceSwitchedOn does Event {
351 351
     has $.thing;
352 352
 }
353 353
 
  354
+class Adventure::GameFinished does Event {
  355
+}
  356
+
354 357
 class X::Adventure is Exception {
355 358
 }
356 359
 
@@ -466,6 +469,12 @@ class X::Adventure::PitchBlack is X::Adventure {
466 469
     }
467 470
 }
468 471
 
  472
+class X::Adventure::GameOver is X::Adventure {
  473
+    method message {
  474
+        "The game has already ended"
  475
+    }
  476
+}
  477
+
469 478
 class Adventure::Engine {
470 479
     my @possible_directions = <
471 480
         north south east west
@@ -496,6 +505,7 @@ class Adventure::Engine {
496 505
     has %!things_shining;
497 506
     has %!remove_from_hooks;
498 507
     has %!take_hooks;
  508
+    has $!game_finished;
499 509
 
500 510
     method connect(@rooms, $direction) {
501 511
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -563,6 +573,9 @@ class Adventure::Engine {
563 573
     }
564 574
 
565 575
     method walk($direction) {
  576
+        die X::Adventure::GameOver.new()
  577
+            if $!game_finished;
  578
+
566 579
         die X::Adventure::PlayerNowhere.new()
567 580
             unless defined $!player_location;
568 581
 
@@ -598,6 +611,9 @@ class Adventure::Engine {
598 611
     }
599 612
 
600 613
     method look() {
  614
+        die X::Adventure::GameOver.new()
  615
+            if $!game_finished;
  616
+
601 617
         die X::Adventure::PlayerNowhere.new()
602 618
             unless defined $!player_location;
603 619
 
@@ -641,6 +657,9 @@ class Adventure::Engine {
641 657
     }
642 658
 
643 659
     method examine($thing) {
  660
+        die X::Adventure::GameOver.new()
  661
+            if $!game_finished;
  662
+
644 663
         die X::Adventure::PlayerNowhere.new()
645 664
             unless defined $!player_location;
646 665
 
@@ -671,6 +690,9 @@ class Adventure::Engine {
671 690
     }
672 691
 
673 692
     method open($thing) {
  693
+        die X::Adventure::GameOver.new()
  694
+            if $!game_finished;
  695
+
674 696
         die X::Adventure::PlayerNowhere.new()
675 697
             unless defined $!player_location;
676 698
 
@@ -707,6 +729,9 @@ class Adventure::Engine {
707 729
     }
708 730
 
709 731
     method put_thing_in($thing, $in) {
  732
+        die X::Adventure::GameOver.new()
  733
+            if $!game_finished;
  734
+
710 735
         die X::Adventure::PlayerNowhere.new()
711 736
             unless defined $!player_location;
712 737
 
@@ -744,6 +769,9 @@ class Adventure::Engine {
744 769
     }
745 770
 
746 771
     method put_thing_on($thing, $on) {
  772
+        die X::Adventure::GameOver.new()
  773
+            if $!game_finished;
  774
+
747 775
         die X::Adventure::PlayerNowhere.new()
748 776
             unless defined $!player_location;
749 777
 
@@ -772,6 +800,9 @@ class Adventure::Engine {
772 800
     }
773 801
 
774 802
     method read($thing) {
  803
+        die X::Adventure::GameOver.new()
  804
+            if $!game_finished;
  805
+
775 806
         die X::Adventure::PlayerNowhere.new()
776 807
             unless defined $!player_location;
777 808
 
@@ -800,6 +831,9 @@ class Adventure::Engine {
800 831
     }
801 832
 
802 833
     method take($thing) {
  834
+        die X::Adventure::GameOver.new()
  835
+            if $!game_finished;
  836
+
803 837
         die X::Adventure::PlayerNowhere.new()
804 838
             unless defined $!player_location;
805 839
 
@@ -834,6 +868,9 @@ class Adventure::Engine {
834 868
     }
835 869
 
836 870
     method drop($thing) {
  871
+        die X::Adventure::GameOver.new()
  872
+            if $!game_finished;
  873
+
837 874
         die X::Adventure::PlayerNowhere.new()
838 875
             unless defined $!player_location;
839 876
 
@@ -860,6 +897,9 @@ class Adventure::Engine {
860 897
     }
861 898
 
862 899
     method use($thing) {
  900
+        die X::Adventure::GameOver.new()
  901
+            if $!game_finished;
  902
+
863 903
         die X::Adventure::PlayerNowhere.new()
864 904
             unless defined $!player_location;
865 905
 
@@ -878,6 +918,14 @@ class Adventure::Engine {
878 918
         self!apply_and_return: @events;
879 919
     }
880 920
 
  921
+    method finish() {
  922
+        die X::Adventure::GameOver.new()
  923
+            if $!game_finished;
  924
+
  925
+        my @events = Adventure::GameFinished.new();
  926
+        self!apply_and_return: @events;
  927
+    }
  928
+
881 929
     method on_try_exit($room, $direction, &hook) {
882 930
         %!try_exit_hooks{$room}{$direction} = &hook;
883 931
     }
@@ -1004,6 +1052,9 @@ class Adventure::Engine {
1004 1052
         when Adventure::PlayerPutIn {
1005 1053
             %!thing_rooms{.thing} = "contents:{.in}";
1006 1054
         }
  1055
+        when Adventure::GameFinished {
  1056
+            $!game_finished = True;
  1057
+        }
1007 1058
     }
1008 1059
 }
1009 1060
 
@@ -2620,6 +2671,16 @@ multi MAIN('test') {
2620 2671
             'dropping water in the helmet';
2621 2672
     }
2622 2673
 
  2674
+    {
  2675
+        my $engine = Adventure::Engine.new();
  2676
+
  2677
+        $engine.finish();
  2678
+        throws_exception
  2679
+            { $engine.walk('west') },
  2680
+            X::Adventure::GameOver,
  2681
+            'cannot do things once the game has finished';
  2682
+    }
  2683
+
2623 2684
     done;
2624 2685
 }
2625 2686
 

0 notes on commit f5c6aa3

Please sign in to comment.
Something went wrong with that request. Please try again.