Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] fuse hooks

  • Loading branch information...
commit d6f6977816eb76dc0722e070e0b6fd7c5416f49f 1 parent f5c6aa3
Carl Mäsak authored July 26, 2012

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

  1. 36  bin/crypt
36  bin/crypt
@@ -506,6 +506,7 @@ class Adventure::Engine {
506 506
     has %!remove_from_hooks;
507 507
     has %!take_hooks;
508 508
     has $!game_finished;
  509
+    has %!tick_hooks;
509 510
 
510 511
     method connect(@rooms, $direction) {
511 512
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -572,6 +573,16 @@ class Adventure::Engine {
572 573
         return False;
573 574
     }
574 575
 
  576
+    method !tick() {
  577
+        my @events;
  578
+        for %!tick_hooks.kv -> $name, %props {
  579
+            if --%props<ticks> == 0 {
  580
+                @events.push(%props<hook>());
  581
+            }
  582
+        }
  583
+        return @events;
  584
+    }
  585
+
575 586
     method walk($direction) {
576 587
         die X::Adventure::GameOver.new()
577 588
             if $!game_finished;
@@ -607,6 +618,7 @@ class Adventure::Engine {
607 618
                 ));
608 619
             }
609 620
         }
  621
+        @events.push(self!tick);
610 622
         self!apply_and_return: @events;
611 623
     }
612 624
 
@@ -950,6 +962,10 @@ class Adventure::Engine {
950 962
         %!take_hooks{$thing} = &hook;
951 963
     }
952 964
 
  965
+    method light_fuse($n, $name, &hook) {
  966
+        %!tick_hooks{$name} = { :ticks($n), :&hook };
  967
+    }
  968
+
953 969
     my class Save {
954 970
         has @.events;
955 971
     }
@@ -2681,6 +2697,26 @@ multi MAIN('test') {
2681 2697
             'cannot do things once the game has finished';
2682 2698
     }
2683 2699
 
  2700
+    {
  2701
+        my $engine = Adventure::Engine.new();
  2702
+
  2703
+        my @rooms = <kitchen veranda>;
  2704
+        $engine.connect(@rooms, my $direction = 'south');
  2705
+        $engine.place_player('kitchen');
  2706
+        $engine.light_fuse(3, 'end_game', { $engine.finish });
  2707
+        $engine.walk('south');
  2708
+        $engine.walk('north');
  2709
+        is $engine.walk('south'),
  2710
+            [
  2711
+                Adventure::PlayerWalked.new(
  2712
+                    :to<veranda>,
  2713
+                ),
  2714
+                Adventure::GameFinished.new(
  2715
+                ),
  2716
+            ],
  2717
+            'counting down to a hook auto-activating';
  2718
+    }
  2719
+
2684 2720
     done;
2685 2721
 }
2686 2722
 

0 notes on commit d6f6977

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