Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] added up/down directions

Also as part of this put the 'clearing' initialization in the right class.
  • Loading branch information...
commit aedec5ff877878172219b58b43422f5eadd36c2b 1 parent 42e6642
Carl Mäsak authored July 06, 2012

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

  1. 48  bin/crypt
48  bin/crypt
@@ -232,6 +232,10 @@ class Adventure::PlayerWalked does Event {
232 232
     has $.to;
233 233
 }
234 234
 
  235
+class Adventure::PlayerWasPlaced does Event {
  236
+    has $.in;
  237
+}
  238
+
235 239
 class Adventure::TwoRoomsConnected does Event {
236 240
     has @.rooms;
237 241
     has $.direction;
@@ -256,13 +260,20 @@ class X::Adventure::NoExitThere is X::Adventure {
256 260
     }
257 261
 }
258 262
 
  263
+class X::Adventure::PlayerIsNowhere is X::Adventure {
  264
+    method message {
  265
+        "Cannot move because the player isn't anywhere"
  266
+    }
  267
+}
  268
+
259 269
 class Adventure::Engine {
260 270
     my @possible_directions = <
261 271
         north south east west
262 272
         northeast northwest southeast southwest
  273
+        up down
263 274
     >;
264 275
 
265  
-    has $!player_location = 'clearing';
  276
+    has $!player_location;
266 277
     has %!exits;
267 278
 
268 279
     method connect(@rooms, $direction) {
@@ -275,6 +286,9 @@ class Adventure::Engine {
275 286
     }
276 287
 
277 288
     method walk($direction) {
  289
+        die X::Adventure::PlayerIsNowhere.new()
  290
+            unless defined $!player_location;
  291
+
278 292
         my $to = %!exits{$!player_location}{$direction};
279 293
 
280 294
         die X::Adventure::NoExitThere.new(:$direction)
@@ -285,6 +299,12 @@ class Adventure::Engine {
285 299
         @events;
286 300
     }
287 301
 
  302
+    method place_player($in) {
  303
+        my @events = Adventure::PlayerWasPlaced.new(:$in);
  304
+        self!apply($_) for @events;
  305
+        @events;
  306
+    }
  307
+
288 308
     # RAKUDO: private multimethods NYI
289 309
     method !apply(Event $_) {
290 310
         when Adventure::TwoRoomsConnected {
@@ -295,6 +315,9 @@ class Adventure::Engine {
295 315
         when Adventure::PlayerWalked {
296 316
             $!player_location = .to;
297 317
         }
  318
+        when Adventure::PlayerWasPlaced {
  319
+            $!player_location = .in;
  320
+        }
298 321
     }
299 322
 }
300 323
 
@@ -314,6 +337,7 @@ class Crypt::Game {
314 337
 
315 338
         given $!engine {
316 339
             .connect(<clearing hill>, 'east');
  340
+            .place_player('clearing');
317 341
         }
318 342
     }
319 343
 
@@ -590,6 +614,28 @@ multi MAIN('test') {
590 614
             'the player actually moves to the next room';
591 615
     }
592 616
 
  617
+    {
  618
+        my $engine = Adventure::Engine.new();
  619
+
  620
+        my @rooms = <first_floor second_floor>;
  621
+        is $engine.connect(@rooms, my $direction = 'up'),
  622
+            Adventure::TwoRoomsConnected.new(
  623
+                :@rooms,
  624
+                :$direction,
  625
+            ),
  626
+            'connecting two rooms vertically';
  627
+        is $engine.place_player('first_floor'),
  628
+            Adventure::PlayerWasPlaced.new(
  629
+                :in<first_floor>,
  630
+            ),
  631
+            'placing the player';
  632
+        is $engine.walk('up'),
  633
+            Adventure::PlayerWalked.new(
  634
+                :to<second_floor>,
  635
+            ),
  636
+            'going up to the second floor';
  637
+    }
  638
+
593 639
     done;
594 640
 }
595 641
 

0 notes on commit aedec5f

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