Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[bin/crypt] get flashlight to work

  • Loading branch information...
commit bc2bb141e7ea50a526db00736814d6d39c6f0d4e 1 parent e90ce36
Carl Mäsak authored July 21, 2012

Showing 1 changed file with 69 additions and 4 deletions. Show diff stats Hide diff stats

  1. 73  bin/crypt
73  bin/crypt
@@ -336,6 +336,18 @@ class Adventure::RoomMadeDark does Event {
336 336
     has $.room;
337 337
 }
338 338
 
  339
+class Adventure::PlayerUsed does Event {
  340
+    has $.thing;
  341
+}
  342
+
  343
+class Adventure::ThingMadeALightSource does Event {
  344
+    has $.thing;
  345
+}
  346
+
  347
+class Adventure::LightSourceSwitchedOn does Event {
  348
+    has $.thing;
  349
+}
  350
+
339 351
 class X::Adventure is Exception {
340 352
 }
341 353
 
@@ -469,6 +481,8 @@ class Adventure::Engine {
469 481
     has %!open_hooks;
470 482
     has %!put_hooks;
471 483
     has %!dark_rooms;
  484
+    has %!light_sources;
  485
+    has %!things_shining;
472 486
 
473 487
     method connect(@rooms, $direction) {
474 488
         die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -518,6 +532,14 @@ class Adventure::Engine {
518 532
         || self!is_thing_in($thing, 'player inventory');
519 533
     }
520 534
 
  535
+    method !shining_thing_here() {
  536
+        for %!things_shining.kv -> $thing, $shining {
  537
+            next unless $shining;
  538
+            return True if self!thing_in_room_or_inventory($thing);
  539
+        }
  540
+        return False;
  541
+    }
  542
+
521 543
     method walk($direction) {
522 544
         die X::Adventure::PlayerNowhere.new()
523 545
             unless defined $!player_location;
@@ -536,7 +558,8 @@ class Adventure::Engine {
536 558
 
537 559
         my @events = Adventure::PlayerWalked.new(:$to);
538 560
         unless %!seen_room{$to}++ {
539  
-            my $pitch_black = %!dark_rooms{$!player_location};
  561
+            my $pitch_black = %!dark_rooms{$!player_location}
  562
+                && !self!shining_thing_here();
540 563
 
541 564
             if $pitch_black {
542 565
                 @events.push(Adventure::PlayerLookedAtDarkness.new());
@@ -556,7 +579,8 @@ class Adventure::Engine {
556 579
         die X::Adventure::PlayerNowhere.new()
557 580
             unless defined $!player_location;
558 581
 
559  
-        my $pitch_black = %!dark_rooms{$!player_location};
  582
+        my $pitch_black = %!dark_rooms{$!player_location}
  583
+            && !self!shining_thing_here();
560 584
 
561 585
         my @events = $pitch_black
562 586
             ?? Adventure::PlayerLookedAtDarkness.new()
@@ -790,6 +814,25 @@ class Adventure::Engine {
790 814
         self!apply_and_return: @events;
791 815
     }
792 816
 
  817
+    method use($thing) {
  818
+        die X::Adventure::PlayerNowhere.new()
  819
+            unless defined $!player_location;
  820
+
  821
+        die X::Adventure::NoSuchThingHere.new(:$thing)
  822
+            unless self!thing_in_room_or_inventory($thing);
  823
+
  824
+        my @events = Adventure::PlayerUsed.new(:$thing);
  825
+        if %!light_sources{$thing} {
  826
+            @events.push(Adventure::LightSourceSwitchedOn.new(:$thing));
  827
+        }
  828
+        self!apply_and_return: @events;
  829
+    }
  830
+
  831
+    method make_thing_a_light_source($thing) {
  832
+        my @events = Adventure::ThingMadeALightSource.new(:$thing);
  833
+        self!apply_and_return: @events;
  834
+    }
  835
+
793 836
     method on_try_exit($room, $direction, &hook) {
794 837
         %!try_exit_hooks{$room}{$direction} = &hook;
795 838
     }
@@ -893,6 +936,12 @@ class Adventure::Engine {
893 936
         when Adventure::RoomMadeDark {
894 937
             %!dark_rooms{.room} = True;
895 938
         }
  939
+        when Adventure::ThingMadeALightSource {
  940
+            %!light_sources{.thing} = True;
  941
+        }
  942
+        when Adventure::LightSourceSwitchedOn {
  943
+            %!things_shining{.thing} = True;
  944
+        }
896 945
     }
897 946
 }
898 947
 
@@ -910,7 +959,6 @@ class Crypt::Game {
910 959
             .connect(<chamber hall>, 'south');
911 960
             .alias_direction('chamber', 'in', 'south');
912 961
             .alias_direction('hall', 'out', 'north');
913  
-            .connect(<hall cave>, 'down');
914 962
             .connect(<cave crypt>, 'northwest');
915 963
             .on_try_exit('cave', 'northwest', {
916 964
                 say "You try to walk past the fire, but it's too hot!";
@@ -924,6 +972,7 @@ class Crypt::Game {
924 972
             .place_thing('car', 'clearing');
925 973
             .place_thing('flashlight', 'contents:car');
926 974
             .make_thing_carryable('flashlight');
  975
+            .make_thing_a_light_source('flashlight');
927 976
             .place_thing('rope', 'contents:car');
928 977
             .make_thing_carryable('rope');
929 978
             .make_thing_openable('car');
@@ -1003,6 +1052,10 @@ class Crypt::Game {
1003 1052
         return $!engine.read($thing);
1004 1053
     }
1005 1054
 
  1055
+    method use($thing) {
  1056
+        return $!engine.use($thing);
  1057
+    }
  1058
+
1006 1059
     method save {
1007 1060
         $!engine.save;
1008 1061
     }
@@ -2127,7 +2180,19 @@ multi MAIN('test') {
2127 2180
         is $game.look(),
2128 2181
             Adventure::PlayerLookedAtDarkness.new(
2129 2182
             ),
2130  
-            'looking without the flashlight';
  2183
+            'looking without the flashlight switched on';
  2184
+    }
  2185
+
  2186
+    {
  2187
+        my $game = game_from_hall();
  2188
+
  2189
+        $game.use('flashlight');
  2190
+        is $game.look(),
  2191
+            Adventure::PlayerLooked.new(
  2192
+                :room<hall>,
  2193
+                :exits<north>,
  2194
+            ),
  2195
+            'looking with the flashlight switched on';
2131 2196
     }
2132 2197
 
2133 2198
     done;

0 notes on commit bc2bb14

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