Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] introduced in/out aliases

  • Loading branch information...
commit 1e9e0cfc887b7975119ed0df1223b3e05d38892e 1 parent aedec5f
Carl Mäsak authored July 06, 2012

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

  1. 55  bin/crypt
55  bin/crypt
@@ -241,6 +241,12 @@ class Adventure::TwoRoomsConnected does Event {
241 241
     has $.direction;
242 242
 }
243 243
 
  244
+class Adventure::DirectionAliased does Event {
  245
+    has $.room;
  246
+    has $.direction;
  247
+    has $.alias;
  248
+}
  249
+
244 250
 class X::Adventure is Exception {
245 251
 }
246 252
 
@@ -275,6 +281,7 @@ class Adventure::Engine {
275 281
 
276 282
     has $!player_location;
277 283
     has %!exits;
  284
+    has %!exit_aliases;
278 285
 
279 286
     method connect(@rooms, $direction) {
280 287
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -289,7 +296,13 @@ class Adventure::Engine {
289 296
         die X::Adventure::PlayerIsNowhere.new()
290 297
             unless defined $!player_location;
291 298
 
292  
-        my $to = %!exits{$!player_location}{$direction};
  299
+        my $actual_direction =
  300
+            %!exit_aliases.exists($!player_location)
  301
+            && %!exit_aliases{$!player_location}.exists($direction)
  302
+                ?? %!exit_aliases{$!player_location}{$direction}
  303
+                !! $direction;
  304
+
  305
+        my $to = %!exits{$!player_location}{$actual_direction};
293 306
 
294 307
         die X::Adventure::NoExitThere.new(:$direction)
295 308
             unless defined $to;
@@ -305,6 +318,14 @@ class Adventure::Engine {
305 318
         @events;
306 319
     }
307 320
 
  321
+    method alias_direction($room, $alias, $direction) {
  322
+        my @events = Adventure::DirectionAliased.new(
  323
+            :$room, :$alias, :$direction
  324
+        );
  325
+        self!apply($_) for @events;
  326
+        @events;
  327
+    }
  328
+
308 329
     # RAKUDO: private multimethods NYI
309 330
     method !apply(Event $_) {
310 331
         when Adventure::TwoRoomsConnected {
@@ -318,6 +339,9 @@ class Adventure::Engine {
318 339
         when Adventure::PlayerWasPlaced {
319 340
             $!player_location = .in;
320 341
         }
  342
+        when Adventure::DirectionAliased {
  343
+            %!exit_aliases{.room}{.alias} = .direction;
  344
+        }
321 345
     }
322 346
 }
323 347
 
@@ -636,6 +660,35 @@ multi MAIN('test') {
636 660
             'going up to the second floor';
637 661
     }
638 662
 
  663
+    {
  664
+        my $engine = Adventure::Engine.new();
  665
+
  666
+        my @rooms = <outside inside>;
  667
+        is $engine.connect(@rooms, my $direction = 'southwest'),
  668
+            Adventure::TwoRoomsConnected.new(
  669
+                :@rooms,
  670
+                :$direction,
  671
+            ),
  672
+            'connecting outside and inside';
  673
+        is $engine.alias_direction('outside', 'in', 'southwest'),
  674
+            Adventure::DirectionAliased.new(
  675
+                :room<outside>,
  676
+                :direction<southwest>,
  677
+                :alias<in>,
  678
+            ),
  679
+            'aliasing "southwest" as "in"';
  680
+        is $engine.place_player('outside'),
  681
+            Adventure::PlayerWasPlaced.new(
  682
+                :in<outside>,
  683
+            ),
  684
+            'placing the player';
  685
+        is $engine.walk('in'),
  686
+            Adventure::PlayerWalked.new(
  687
+                :to<inside>,
  688
+            ),
  689
+            'going inside now means going southwest';
  690
+    }
  691
+
639 692
     done;
640 693
 }
641 694
 

0 notes on commit 1e9e0cf

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