Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] implement saving/restoring

  • Loading branch information...
commit 02ca38f7d6a2cf7cf42a777922283455be157796 1 parent 334a4d7
Carl Mäsak authored

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

  1. 43  bin/crypt
43  bin/crypt
@@ -283,6 +283,7 @@ class Adventure::Engine {
283 283
         up down
284 284
     >;
285 285
 
  286
+    has @!events;
286 287
     has $!player_location;
287 288
     has %!exits;
288 289
     has %!exit_aliases;
@@ -343,6 +344,20 @@ class Adventure::Engine {
343 344
         @events;
344 345
     }
345 346
 
  347
+    my class Save {
  348
+        has @.events;
  349
+    }
  350
+
  351
+    method save {
  352
+        return Save.new(:@!events);
  353
+    }
  354
+
  355
+    method restore(Save $save) {
  356
+        my $new-engine = Adventure::Engine.new();
  357
+        $new-engine!apply($_) for $save.events.list;
  358
+        return $new-engine;
  359
+    }
  360
+
346 361
     sub opposite($direction) {
347 362
         my %opposites =
348 363
             'north'     => 'south',
@@ -360,6 +375,7 @@ class Adventure::Engine {
360 375
 
361 376
     # RAKUDO: private multimethods NYI
362 377
     method !apply(Event $_) {
  378
+        push @!events, $_;
363 379
         when Adventure::TwoRoomsConnected {
364 380
             my ($room1, $room2) = .rooms.list;
365 381
             my $direction = .direction;
@@ -382,7 +398,7 @@ class X::Crypt is Exception {
382 398
 }
383 399
 
384 400
 class Crypt::Game {
385  
-    has $!engine handles <walk>;
  401
+    has $!engine handles <walk save>;
386 402
 
387 403
     submethod BUILD() {
388 404
         $!engine = Adventure::Engine.new();
@@ -402,6 +418,11 @@ class Crypt::Game {
402 418
     method look {
403 419
         return $!engine.look;
404 420
     }
  421
+
  422
+    method restore($save) {
  423
+        $!engine .= restore($save);
  424
+        return;
  425
+    }
405 426
 }
406 427
 
407 428
 multi MAIN() {
@@ -415,6 +436,7 @@ multi MAIN() {
415 436
     say "";
416 437
 
417 438
     my $game = Crypt::Game.new();
  439
+    my $save;
418 440
     my %descriptions;
419 441
     for slurp("game-data/descriptions").trim.split(/\n\n/) {
420 442
         /^^ '== ' (\N+) \n (.*)/
@@ -460,6 +482,25 @@ multi MAIN() {
460 482
                 say "[walk] north/south/east/west";
461 483
             }
462 484
 
  485
+            when 'save' {
  486
+                $save = $game.save;
  487
+            }
  488
+
  489
+            when 'restore' {
  490
+                when !defined $save {
  491
+                    say "No game has been saved yet.";
  492
+                }
  493
+                $game.restore($save);
  494
+
  495
+                my @events = $game.look;
  496
+                for @events {
  497
+                    when Adventure::PlayerLooked {
  498
+                        say ucfirst .description_of;
  499
+                        say %descriptions{.description_of};
  500
+                    }
  501
+                }
  502
+            }
  503
+
463 504
             when any @possible_directions {
464 505
                 $command = "walk $command";
465 506
                 proceed;

0 notes on commit 02ca38f

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