Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[bin/crypt] unhiding things through a callback

  • Loading branch information...
commit 51ce8171d8dc4a80a4436599bbdcf9587be167df 1 parent 20e37ab
@masak authored
Showing with 39 additions and 0 deletions.
  1. +39 −0 bin/crypt
View
39 bin/crypt
@@ -295,6 +295,10 @@ class Adventure::ThingHidden does Event {
has $.thing;
}
+class Adventure::ThingUnhidden does Event {
+ has $.thing;
+}
+
class X::Adventure is Exception {
}
@@ -397,6 +401,7 @@ class Adventure::Engine {
has %!platforms;
has %!readable_things;
has %!hidden_things;
+ has %!examine_hooks;
method connect(@rooms, $direction) {
die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -490,6 +495,10 @@ class Adventure::Engine {
my @events = Adventure::PlayerExamined.new(
:$thing
);
+ if %!examine_hooks{$thing} -> &hook {
+ @events.push(&hook());
+ }
+
self!apply($_) for @events;
@events;
}
@@ -586,10 +595,20 @@ class Adventure::Engine {
@events;
}
+ method unhide_thing($thing) {
+ my @events = Adventure::ThingUnhidden.new(:$thing);
+ self!apply($_) for @events;
+ @events;
+ }
+
method on_try_exit($room, $direction, &hook) {
%!try_exit_hooks{$room}{$direction} = &hook;
}
+ method on_examine($thing, &hook) {
+ %!examine_hooks{$thing} = &hook;
+ }
+
my class Save {
has @.events;
}
@@ -655,6 +674,9 @@ class Adventure::Engine {
when Adventure::ThingHidden {
%!hidden_things{.thing} = True;
}
+ when Adventure::ThingUnhidden {
+ %!hidden_things{.thing} = False;
+ }
}
}
@@ -1352,6 +1374,23 @@ multi MAIN('test') {
'opening a thing (-) cannot because it is in another room';
}
+ {
+ my $engine = Adventure::Engine.new();
+
+ $engine.place_thing('door', 'hill');
+ $engine.place_thing('grass', 'hill');
+ $engine.make_thing_openable('door');
+ $engine.hide_thing('door');
+ $engine.on_examine('grass', { $engine.unhide_thing('door') });
+ $engine.place_player('hill');
+ $engine.examine('grass');
+ is $engine.open('door'),
+ Adventure::PlayerOpened.new(
+ :thing<door>,
+ ),
+ 'opening a thing (+) unhidden by a callback';
+ }
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.