Skip to content

Instantly share code, notes, and snippets.

@AlexDaniel
Forked from wildwildtrees/ootut2.pm6
Last active March 18, 2020 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexDaniel/c97b428fa88bd9998a4b1d7af7df1fb5 to your computer and use it in GitHub Desktop.
Save AlexDaniel/c97b428fa88bd9998a4b1d7af7df1fb5 to your computer and use it in GitHub Desktop.
bug free, but bugs out when follow is called with "Cannot invoke this object (REPR: Null; VMNull)" if I change self.id to self!id in method follow in role Follow line 52
our %world = {} ;
role Identifier {
has $.id is rw ;
}
role Container {
has @.contents is rw ;
method add($item) {
@!contents.push($item) ;
}
method remove($item) {
@!contents = @!contents.grep(!(* ~~ $item)) ;
}
}
role HasLocation {
has $.location is rw ;
}
class Room does Identifier does Container {
}
role Movable does HasLocation {
method move($target) {
my $source = self.location ;
my $source-object = %world{$source} ;
my $target-object = %world{$target} ;
if ($source-object ~~ Container && $target-object ~~ Container) {
my $id = self.id ;
$source-object.remove($id) ;
self.location = $target ;
$target-object.add($id) ;
return True ;
} else {
return False ;
}
}
}
class Item does Identifier does Movable {
}
role Follow {
has @.followers is rw ;
has @.following is rw ;
method follow($id) {
my $target-object = %world{$id} ;
if ($target-object ~~ Follow) && !($id ~~ self!id) {
if !($id ~~ any @!following) { @!following.push($id) ; }
if !($id ~~ any $target-object.followers) { $target-object.followers.push($id) ; }
return True ;
} else {
return False ;
}
}
method unfollow($id) {
}
}
class Person is Item does Follow {
}
sub populate($object) {
%world{$object.id} = $object ;
}
our $room1 = Room.new(id => 1, contents => [10,100]) ;
our $room2 = Room.new(id => 2, contents => [200]) ;
our $item10 = Item.new(id => 10, location => 1) ;
our $person100 = Person.new(id => 100, location => 1) ;
our $person200 = Person.new(id => 200, location => 2) ;
populate($room1) ;
populate($room2) ;
populate($item10) ;
populate($person100) ;
populate($person200) ;
run <perl6 -Isandbox/ -Mootut2 -e>, ‘%world{100}.follow(200)’
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment