Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[bin/crypt] fuse hooks

  • Loading branch information...
commit d6f6977816eb76dc0722e070e0b6fd7c5416f49f 1 parent f5c6aa3
@masak authored
Showing with 36 additions and 0 deletions.
  1. +36 −0 bin/crypt
View
36 bin/crypt
@@ -506,6 +506,7 @@ class Adventure::Engine {
has %!remove_from_hooks;
has %!take_hooks;
has $!game_finished;
+ has %!tick_hooks;
method connect(@rooms, $direction) {
die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -572,6 +573,16 @@ class Adventure::Engine {
return False;
}
+ method !tick() {
+ my @events;
+ for %!tick_hooks.kv -> $name, %props {
+ if --%props<ticks> == 0 {
+ @events.push(%props<hook>());
+ }
+ }
+ return @events;
+ }
+
method walk($direction) {
die X::Adventure::GameOver.new()
if $!game_finished;
@@ -607,6 +618,7 @@ class Adventure::Engine {
));
}
}
+ @events.push(self!tick);
self!apply_and_return: @events;
}
@@ -950,6 +962,10 @@ class Adventure::Engine {
%!take_hooks{$thing} = &hook;
}
+ method light_fuse($n, $name, &hook) {
+ %!tick_hooks{$name} = { :ticks($n), :&hook };
+ }
+
my class Save {
has @.events;
}
@@ -2681,6 +2697,26 @@ multi MAIN('test') {
'cannot do things once the game has finished';
}
+ {
+ my $engine = Adventure::Engine.new();
+
+ my @rooms = <kitchen veranda>;
+ $engine.connect(@rooms, my $direction = 'south');
+ $engine.place_player('kitchen');
+ $engine.light_fuse(3, 'end_game', { $engine.finish });
+ $engine.walk('south');
+ $engine.walk('north');
+ is $engine.walk('south'),
+ [
+ Adventure::PlayerWalked.new(
+ :to<veranda>,
+ ),
+ Adventure::GameFinished.new(
+ ),
+ ],
+ 'counting down to a hook auto-activating';
+ }
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.