Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] error if the thing isn't there

  • Loading branch information...
commit fcd3e8d0927d447907867137777cc0a6072ab464 1 parent 015b292
Carl Mäsak authored July 09, 2012

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

  1. 39  bin/crypt
39  bin/crypt
@@ -256,6 +256,11 @@ class Adventure::PlayerExamined does Event {
256 256
     has $.thing;
257 257
 }
258 258
 
  259
+class Adventure::ThingPlaced does Event {
  260
+    has $.thing;
  261
+    has $.room;
  262
+}
  263
+
259 264
 class X::Adventure is Exception {
260 265
 }
261 266
 
@@ -281,6 +286,14 @@ class X::Adventure::PlayerIsNowhere is X::Adventure {
281 286
     }
282 287
 }
283 288
 
  289
+class X::Adventure::NoSuchThingHere is X::Adventure {
  290
+    has $.thing;
  291
+
  292
+    method message {
  293
+        "You see no $.thing here"
  294
+    }
  295
+}
  296
+
284 297
 class Adventure::Engine {
285 298
     my @possible_directions = <
286 299
         north south east west
@@ -294,6 +307,7 @@ class Adventure::Engine {
294 307
     has %!exit_aliases;
295 308
     has %!seen_room;
296 309
     has %!try_exit_hooks;
  310
+    has %!thing_rooms;
297 311
 
298 312
     method connect(@rooms, $direction) {
299 313
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -364,9 +378,17 @@ class Adventure::Engine {
364 378
     }
365 379
 
366 380
     method place_thing($thing, $room) {
  381
+        my @events = Adventure::ThingPlaced.new(
  382
+            :$thing, :$room
  383
+        );
  384
+        self!apply($_) for @events;
  385
+        @events;
367 386
     }
368 387
 
369 388
     method examine($thing) {
  389
+        die X::Adventure::NoSuchThingHere.new(:$thing)
  390
+            unless (%!thing_rooms{$thing} // '') eq $!player_location;
  391
+
370 392
         my @events = Adventure::PlayerExamined.new(
371 393
             :$thing
372 394
         );
@@ -425,6 +447,9 @@ class Adventure::Engine {
425 447
         when Adventure::DirectionAliased {
426 448
             %!exit_aliases{.room}{.alias} = .direction;
427 449
         }
  450
+        when Adventure::ThingPlaced {
  451
+            %!thing_rooms{.thing} = .room;
  452
+        }
428 453
     }
429 454
 }
430 455
 
@@ -868,6 +893,20 @@ multi MAIN('test') {
868 893
             'examining an object (+)';
869 894
     }
870 895
 
  896
+    {
  897
+        my $engine = Adventure::Engine.new();
  898
+
  899
+        $engine.place_player('street');
  900
+        throws_exception
  901
+            { $engine.examine('ball') },
  902
+            X::Adventure::NoSuchThingHere,
  903
+            'examining an object (-) no such object here',
  904
+            {
  905
+                is .thing, 'ball', '.thing attribute';
  906
+                is .message, "You see no ball here", '.message attribute';
  907
+            };
  908
+    }
  909
+
871 910
     done;
872 911
 }
873 912
 

0 notes on commit fcd3e8d

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