Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] unhiding things through a callback

  • Loading branch information...
commit 51ce8171d8dc4a80a4436599bbdcf9587be167df 1 parent 20e37ab
Carl Mäsak authored

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

  1. 39  bin/crypt
39  bin/crypt
@@ -295,6 +295,10 @@ class Adventure::ThingHidden does Event {
295 295
     has $.thing;
296 296
 }
297 297
 
  298
+class Adventure::ThingUnhidden does Event {
  299
+    has $.thing;
  300
+}
  301
+
298 302
 class X::Adventure is Exception {
299 303
 }
300 304
 
@@ -397,6 +401,7 @@ class Adventure::Engine {
397 401
     has %!platforms;
398 402
     has %!readable_things;
399 403
     has %!hidden_things;
  404
+    has %!examine_hooks;
400 405
 
401 406
     method connect(@rooms, $direction) {
402 407
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -490,6 +495,10 @@ class Adventure::Engine {
490 495
         my @events = Adventure::PlayerExamined.new(
491 496
             :$thing
492 497
         );
  498
+        if %!examine_hooks{$thing} -> &hook {
  499
+            @events.push(&hook());
  500
+        }
  501
+
493 502
         self!apply($_) for @events;
494 503
         @events;
495 504
     }
@@ -586,10 +595,20 @@ class Adventure::Engine {
586 595
         @events;
587 596
     }
588 597
 
  598
+    method unhide_thing($thing) {
  599
+        my @events = Adventure::ThingUnhidden.new(:$thing);
  600
+        self!apply($_) for @events;
  601
+        @events;
  602
+    }
  603
+
589 604
     method on_try_exit($room, $direction, &hook) {
590 605
         %!try_exit_hooks{$room}{$direction} = &hook;
591 606
     }
592 607
 
  608
+    method on_examine($thing, &hook) {
  609
+        %!examine_hooks{$thing} = &hook;
  610
+    }
  611
+
593 612
     my class Save {
594 613
         has @.events;
595 614
     }
@@ -655,6 +674,9 @@ class Adventure::Engine {
655 674
         when Adventure::ThingHidden {
656 675
             %!hidden_things{.thing} = True;
657 676
         }
  677
+        when Adventure::ThingUnhidden {
  678
+            %!hidden_things{.thing} = False;
  679
+        }
658 680
     }
659 681
 }
660 682
 
@@ -1352,6 +1374,23 @@ multi MAIN('test') {
1352 1374
             'opening a thing (-) cannot because it is in another room';
1353 1375
     }
1354 1376
 
  1377
+    {
  1378
+        my $engine = Adventure::Engine.new();
  1379
+
  1380
+        $engine.place_thing('door', 'hill');
  1381
+        $engine.place_thing('grass', 'hill');
  1382
+        $engine.make_thing_openable('door');
  1383
+        $engine.hide_thing('door');
  1384
+        $engine.on_examine('grass', { $engine.unhide_thing('door') });
  1385
+        $engine.place_player('hill');
  1386
+        $engine.examine('grass');
  1387
+        is $engine.open('door'),
  1388
+            Adventure::PlayerOpened.new(
  1389
+                :thing<door>,
  1390
+            ),
  1391
+            'opening a thing (+) unhidden by a callback';
  1392
+    }
  1393
+
1355 1394
     done;
1356 1395
 }
1357 1396
 

0 notes on commit 51ce817

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