Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] cannot put things in non-containers

  • Loading branch information...
commit 0161050d5f4c52c58d7d4f23b07b50dbdb1181eb 1 parent a9fe2f7
Carl Mäsak authored July 11, 2012

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

  1. 41  bin/crypt
41  bin/crypt
@@ -270,6 +270,10 @@ class Adventure::PlayerPut does Event {
270 270
     has $.in;
271 271
 }
272 272
 
  273
+class Adventure::ThingMadeAContainer does Event {
  274
+    has $.thing;
  275
+}
  276
+
273 277
 class X::Adventure is Exception {
274 278
 }
275 279
 
@@ -319,6 +323,14 @@ class X::Adventure::ThingAlreadyOpen is X::Adventure {
319 323
     }
320 324
 }
321 325
 
  326
+class X::Adventure::CannotPutInNonContainer is X::Adventure {
  327
+    has $.in;
  328
+
  329
+    method message {
  330
+        "You cannot put things in the $.in"
  331
+    }
  332
+}
  333
+
322 334
 class Adventure::Engine {
323 335
     my @possible_directions = <
324 336
         north south east west
@@ -335,6 +347,7 @@ class Adventure::Engine {
335 347
     has %!thing_rooms;
336 348
     has %!openable_things;
337 349
     has %!open_things;
  350
+    has %!containers;
338 351
 
339 352
     method connect(@rooms, $direction) {
340 353
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -440,9 +453,15 @@ class Adventure::Engine {
440 453
     }
441 454
 
442 455
     method make_thing_a_container($thing) {
  456
+        my @events = Adventure::ThingMadeAContainer.new(:$thing);
  457
+        self!apply($_) for @events;
  458
+        @events;
443 459
     }
444 460
 
445 461
     method put_thing_in($thing, $in) {
  462
+        die X::Adventure::CannotPutInNonContainer.new(:$in)
  463
+            unless %!containers{$in};
  464
+
446 465
         Adventure::PlayerPut.new(:$thing, :$in);
447 466
     }
448 467
 
@@ -503,6 +522,9 @@ class Adventure::Engine {
503 522
         when Adventure::PlayerOpened {
504 523
             %!open_things{.thing} = True;
505 524
         }
  525
+        when Adventure::ThingMadeAContainer {
  526
+            %!containers{.thing} = True;
  527
+        }
506 528
     }
507 529
 }
508 530
 
@@ -1020,6 +1042,25 @@ multi MAIN('test') {
1020 1042
             'putting a thing inside another (+)';
1021 1043
     }
1022 1044
 
  1045
+    {
  1046
+        my $engine = Adventure::Engine.new();
  1047
+
  1048
+        $engine.place_thing('brick', 'street');
  1049
+        $engine.place_thing('doll', 'street');
  1050
+        # don't make brick a container
  1051
+        $engine.place_player('street');
  1052
+        throws_exception
  1053
+            { $engine.put_thing_in('doll', 'brick') },
  1054
+            X::Adventure::CannotPutInNonContainer,
  1055
+            'putting a thing inside another (-) it is not a container',
  1056
+            {
  1057
+                is .in, 'brick', '.in attribute';
  1058
+                is .message,
  1059
+                    "You cannot put things in the brick",
  1060
+                    '.message attribute';
  1061
+            };
  1062
+    }
  1063
+
1023 1064
     done;
1024 1065
 }
1025 1066
 

0 notes on commit 0161050

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