Skip to content

Commit

Permalink
[bin/crypt] added up/down directions
Browse files Browse the repository at this point in the history
Also as part of this put the 'clearing' initialization in the right class.
  • Loading branch information
Carl Masak committed Jul 5, 2012
1 parent 42e6642 commit aedec5f
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion bin/crypt
Expand Up @@ -232,6 +232,10 @@ class Adventure::PlayerWalked does Event {
has $.to;
}

class Adventure::PlayerWasPlaced does Event {
has $.in;
}

class Adventure::TwoRoomsConnected does Event {
has @.rooms;
has $.direction;
Expand All @@ -256,13 +260,20 @@ class X::Adventure::NoExitThere is X::Adventure {
}
}

class X::Adventure::PlayerIsNowhere is X::Adventure {
method message {
"Cannot move because the player isn't anywhere"
}
}

class Adventure::Engine {
my @possible_directions = <
north south east west
northeast northwest southeast southwest
up down
>;

has $!player_location = 'clearing';
has $!player_location;
has %!exits;

method connect(@rooms, $direction) {
Expand All @@ -275,6 +286,9 @@ class Adventure::Engine {
}

method walk($direction) {
die X::Adventure::PlayerIsNowhere.new()
unless defined $!player_location;

my $to = %!exits{$!player_location}{$direction};

die X::Adventure::NoExitThere.new(:$direction)
Expand All @@ -285,6 +299,12 @@ class Adventure::Engine {
@events;
}

method place_player($in) {
my @events = Adventure::PlayerWasPlaced.new(:$in);
self!apply($_) for @events;
@events;
}

# RAKUDO: private multimethods NYI
method !apply(Event $_) {
when Adventure::TwoRoomsConnected {
Expand All @@ -295,6 +315,9 @@ class Adventure::Engine {
when Adventure::PlayerWalked {
$!player_location = .to;
}
when Adventure::PlayerWasPlaced {
$!player_location = .in;
}
}
}

Expand All @@ -314,6 +337,7 @@ class Crypt::Game {

given $!engine {
.connect(<clearing hill>, 'east');
.place_player('clearing');
}
}

Expand Down Expand Up @@ -590,6 +614,28 @@ multi MAIN('test') {
'the player actually moves to the next room';
}

{
my $engine = Adventure::Engine.new();

my @rooms = <first_floor second_floor>;
is $engine.connect(@rooms, my $direction = 'up'),
Adventure::TwoRoomsConnected.new(
:@rooms,
:$direction,
),
'connecting two rooms vertically';
is $engine.place_player('first_floor'),
Adventure::PlayerWasPlaced.new(
:in<first_floor>,
),
'placing the player';
is $engine.walk('up'),
Adventure::PlayerWalked.new(
:to<second_floor>,
),
'going up to the second floor';
}

done;
}

Expand Down

0 comments on commit aedec5f

Please sign in to comment.