Skip to content

Commit

Permalink
[bin/crypt] allow game to remark things
Browse files Browse the repository at this point in the history
Also add an .on_put hook, and a message in the descriptions file.
  • Loading branch information
Carl Masak committed Jul 19, 2012
1 parent 0093720 commit 867e41d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
79 changes: 76 additions & 3 deletions bin/crypt
Expand Up @@ -325,6 +325,10 @@ class Adventure::ContentsRevealed does Event {
has @.contents;
}

class Adventure::GameRemarked does Event {
has $.remark;
}

class X::Adventure is Exception {
}

Expand Down Expand Up @@ -456,6 +460,7 @@ class Adventure::Engine {
has %!carryable_things;
has %!implicit_things;
has %!open_hooks;
has %!put_hooks;

method connect(@rooms, $direction) {
die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
Expand Down Expand Up @@ -648,9 +653,12 @@ class Adventure::Engine {
if %!openable_things{$in} && !%!open_things{$in} {
@events.push(Adventure::PlayerOpened.new(:thing($in)));
}

@events.push(Adventure::PlayerPutIn.new(:$thing, :$in));
@events;
if %!put_hooks{$in} -> &hook {
@events.push(&hook($thing));
}

self!apply_and_return: @events;
}

method make_thing_a_platform($thing) {
Expand All @@ -674,7 +682,11 @@ class Adventure::Engine {
die X::Adventure::YoDawg.new(:relation<on>, :thing($on))
if $thing eq $on;

Adventure::PlayerPutOn.new(:$thing, :$on);
my @events = Adventure::PlayerPutOn.new(:$thing, :$on);
if %!put_hooks{$on} -> &hook {
@events.push(&hook($thing));
}
self!apply_and_return: @events;
}

method make_thing_readable($thing) {
Expand Down Expand Up @@ -737,6 +749,11 @@ class Adventure::Engine {
self!apply_and_return: @events;
}

method remark($remark) {
my @events = Adventure::GameRemarked.new(:$remark);
self!apply_and_return: @events;
}

method make_thing_implicit($thing) {
my @events = Adventure::ThingMadeImplicit.new(:$thing);
self!apply_and_return: @events;
Expand All @@ -754,6 +771,10 @@ class Adventure::Engine {
%!open_hooks{$thing} = &hook;
}

method on_put($thing, &hook) {
%!put_hooks{$thing} = &hook;
}

my class Save {
has @.events;
}
Expand Down Expand Up @@ -870,6 +891,14 @@ class Crypt::Game {
.place_thing('rope', 'contents:car');
.make_thing_carryable('rope');
.make_thing_openable('car');
.make_thing_a_container('car');
.on_put(
'car',
-> $_ {
when 'leaves' { $!engine.remark('car-full-of-leaves') }
();
}
);

# Things on hill
.place_thing('grass', 'hill');
Expand Down Expand Up @@ -912,6 +941,14 @@ class Crypt::Game {
return $!engine.take($thing);
}

method put_thing_in($thing, $in) {
return $!engine.put_thing_in($thing, $in);
}

method put_thing_on($thing, $on) {
return $!engine.put_thing_on($thing, $on);
}

method save {
$!engine.save;
}
Expand Down Expand Up @@ -1023,6 +1060,11 @@ multi MAIN() {
proceed;
}

when /^ 'put' \h+ (\w+) \h+ ('in'|'on') \h+ (\w+) $/ {
$command = "put_thing_$1 $0 $2";
proceed;
}

my $verb = $command.words[0];
my @args = $command.words[1..*];
when %commands.exists($verb) {
Expand Down Expand Up @@ -1063,6 +1105,15 @@ multi MAIN() {
when Adventure::PlayerTook {
say "You take the {.thing}.";
}
when Adventure::PlayerOpened {
say "You open the {.thing}.";
}
when Adventure::PlayerPutIn {
say "You put the {.thing} in the {.in}.";
}
when Adventure::GameRemarked {
say %descriptions{"remark:{.remark}"};
}
}
CATCH {
when X::Adventure { say .message, '.' }
Expand Down Expand Up @@ -1938,6 +1989,28 @@ multi MAIN('test') {
'taking the leaves';
}

{
my $game = Crypt::Game.new();

$game.walk('east');
$game.take('leaves');
$game.walk('west');
is $game.put_thing_in('leaves', 'car'),
[
Adventure::PlayerOpened.new(
:thing<car>,
),
Adventure::PlayerPutIn.new(
:thing<leaves>,
:in<car>,
),
Adventure::GameRemarked.new(
:remark<car-full-of-leaves>,
),
],
'putting the leaves in the car';
}

done;
}

Expand Down
3 changes: 3 additions & 0 deletions game-data/descriptions
Expand Up @@ -52,3 +52,6 @@ all their leaves -- red, yellow, brown ones -- to the ground.

== leaves
They look like the kind of leaves that would love a good rustle.

== remark:car-full-of-leaves
Great. Now your car is full of leaves.

0 comments on commit 867e41d

Please sign in to comment.