Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[bin/crypt] introduced in/out aliases

  • Loading branch information...
commit 1e9e0cfc887b7975119ed0df1223b3e05d38892e 1 parent aedec5f
@masak authored
Showing with 54 additions and 1 deletion.
  1. +54 −1 bin/crypt
View
55 bin/crypt
@@ -241,6 +241,12 @@ class Adventure::TwoRoomsConnected does Event {
has $.direction;
}
+class Adventure::DirectionAliased does Event {
+ has $.room;
+ has $.direction;
+ has $.alias;
+}
+
class X::Adventure is Exception {
}
@@ -275,6 +281,7 @@ class Adventure::Engine {
has $!player_location;
has %!exits;
+ has %!exit_aliases;
method connect(@rooms, $direction) {
die X::Adventure::NoSuchDirection.new(:action('connect rooms'), :$direction)
@@ -289,7 +296,13 @@ class Adventure::Engine {
die X::Adventure::PlayerIsNowhere.new()
unless defined $!player_location;
- my $to = %!exits{$!player_location}{$direction};
+ my $actual_direction =
+ %!exit_aliases.exists($!player_location)
+ && %!exit_aliases{$!player_location}.exists($direction)
+ ?? %!exit_aliases{$!player_location}{$direction}
+ !! $direction;
+
+ my $to = %!exits{$!player_location}{$actual_direction};
die X::Adventure::NoExitThere.new(:$direction)
unless defined $to;
@@ -305,6 +318,14 @@ class Adventure::Engine {
@events;
}
+ method alias_direction($room, $alias, $direction) {
+ my @events = Adventure::DirectionAliased.new(
+ :$room, :$alias, :$direction
+ );
+ self!apply($_) for @events;
+ @events;
+ }
+
# RAKUDO: private multimethods NYI
method !apply(Event $_) {
when Adventure::TwoRoomsConnected {
@@ -318,6 +339,9 @@ class Adventure::Engine {
when Adventure::PlayerWasPlaced {
$!player_location = .in;
}
+ when Adventure::DirectionAliased {
+ %!exit_aliases{.room}{.alias} = .direction;
+ }
}
}
@@ -636,6 +660,35 @@ multi MAIN('test') {
'going up to the second floor';
}
+ {
+ my $engine = Adventure::Engine.new();
+
+ my @rooms = <outside inside>;
+ is $engine.connect(@rooms, my $direction = 'southwest'),
+ Adventure::TwoRoomsConnected.new(
+ :@rooms,
+ :$direction,
+ ),
+ 'connecting outside and inside';
+ is $engine.alias_direction('outside', 'in', 'southwest'),
+ Adventure::DirectionAliased.new(
+ :room<outside>,
+ :direction<southwest>,
+ :alias<in>,
+ ),
+ 'aliasing "southwest" as "in"';
+ is $engine.place_player('outside'),
+ Adventure::PlayerWasPlaced.new(
+ :in<outside>,
+ ),
+ 'placing the player';
+ is $engine.walk('in'),
+ Adventure::PlayerWalked.new(
+ :to<inside>,
+ ),
+ 'going inside now means going southwest';
+ }
+
done;
}
Please sign in to comment.
Something went wrong with that request. Please try again.