Skip to content

Instantly share code, notes, and snippets.

@wildwildtrees
Created March 18, 2020 18:46
Show Gist options
  • Save wildwildtrees/7772b19414458de1442a4e595c867147 to your computer and use it in GitHub Desktop.
Save wildwildtrees/7772b19414458de1442a4e595c867147 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) ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment