Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] can't open already open things

  • Loading branch information...
commit 7132e8509553e6e4ed78302af2efa429b0e488eb 1 parent e0ac8d7
Carl Mäsak authored July 10, 2012

Showing 1 changed file with 35 additions and 1 deletion. Show diff stats Hide diff stats

  1. 36  bin/crypt
36  bin/crypt
@@ -306,6 +306,14 @@ class X::Adventure::ThingNotOpenable is X::Adventure {
306 306
     }
307 307
 }
308 308
 
  309
+class X::Adventure::ThingAlreadyOpen is X::Adventure {
  310
+    has $.thing;
  311
+
  312
+    method message {
  313
+        "The $.thing is open"
  314
+    }
  315
+}
  316
+
309 317
 class Adventure::Engine {
310 318
     my @possible_directions = <
311 319
         north south east west
@@ -321,6 +329,7 @@ class Adventure::Engine {
321 329
     has %!try_exit_hooks;
322 330
     has %!thing_rooms;
323 331
     has %!openable_things;
  332
+    has %!open_things;
324 333
 
325 334
     method connect(@rooms, $direction) {
326 335
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -417,7 +426,12 @@ class Adventure::Engine {
417 426
         die X::Adventure::ThingNotOpenable.new(:$thing)
418 427
             unless %!openable_things{$thing};
419 428
 
420  
-        Adventure::PlayerOpened.new(:$thing);
  429
+        die X::Adventure::ThingAlreadyOpen.new(:$thing)
  430
+            if %!open_things{$thing};
  431
+
  432
+        my @events = Adventure::PlayerOpened.new(:$thing);
  433
+        self!apply($_) for @events;
  434
+        @events;
421 435
     }
422 436
 
423 437
     method on_try_exit($room, $direction, &hook) {
@@ -474,6 +488,9 @@ class Adventure::Engine {
474 488
         when Adventure::ThingPlaced {
475 489
             %!thing_rooms{.thing} = .room;
476 490
         }
  491
+        when Adventure::PlayerOpened {
  492
+            %!open_things{.thing} = True;
  493
+        }
477 494
     }
478 495
 }
479 496
 
@@ -959,6 +976,23 @@ multi MAIN('test') {
959 976
             };
960 977
     }
961 978
 
  979
+    {
  980
+        my $engine = Adventure::Engine.new();
  981
+
  982
+        $engine.place_thing('car', 'street');
  983
+        $engine.make_thing_openable('car');
  984
+        $engine.place_player('street');
  985
+        $engine.open('car');
  986
+        throws_exception
  987
+            { $engine.open('car') },
  988
+            X::Adventure::ThingAlreadyOpen,
  989
+            'opening an object (-) it is already open',
  990
+            {
  991
+                is .thing, 'car', '.thing attribute';
  992
+                is .message, "The car is open", '.message attribute';
  993
+            };
  994
+    }
  995
+
962 996
     done;
963 997
 }
964 998
 

0 notes on commit 7132e85

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