Game-Entities
view release on metacpan or search on metacpan
valid, or a falsy value otherwise. An entity is valid if it has been created
and not yet deleted.
## created
$count = $ECS->created;
Returns the number of entities that have been created. Calling [clear](#clear)
resets this number.
## alive
$count = $ECS->alive;
Returns the number of created entities that are still valid. That is:
entities that have been created and not yet deleted.
## clear
$ECS = $ECS->clear;
Resets the internal storage of the registry. Calling this method leaves no
trace from the previous state.
lib/Game/Entities.pm view on Meta::CPAN
sub new ( $class, %args ) {
my $self = bless { %args{prefix} }, $class;
$self->clear;
}
sub created ($self) { scalar @{ $self->{entities} } - 1 }
# Get the number of created entities that are still valid; that is, that have
# not been deleted.
sub alive ($self) {
my $size = @{ $self->{entities} } - 1;
my $current = $self->{available};
until ( $current->$is_null ) {
$size--;
$current = $self->{entities}[ $current->$entity ];
}
return $size;
}
lib/Game/Entities.pod view on Meta::CPAN
valid, or a falsy value otherwise. An entity is valid if it has been created
and not yet deleted.
=head2 created
$count = $ECS->created;
Returns the number of entities that have been created. Calling L<clear|/clear>
resets this number.
=head2 alive
$count = $ECS->alive;
Returns the number of created entities that are still valid. That is:
entities that have been created and not yet deleted.
=head2 clear
$ECS = $ECS->clear;
Resets the internal storage of the registry. Calling this method leaves no
trace from the previous state.
t/entities.t view on Meta::CPAN
$R->delete($a);
ok !$R->valid($a), 'A is not valid';
ok !$R->get( $a, 'Other' ), 'A does not have Other';
ok !$R->get( $a, 'Named' ), 'A does not have Named';
};
subtest 'Recycling GUIDs' => sub {
my $R = Game::Entities->new;
is $R->alive, 0, 'Right number of alive entities when none created';
# Create 10 entities; will use the first 10 entity IDs ( 0 .. 9 )
my @e = map $R->create, 1 .. 10;
is $R->alive, 10, 'Right number of alive entities when all alive';
ok $R->valid(10), 'Entity is valid';
ok !$R->valid(11), 'Entity is not valid';
# Delete the entities we've just generated
# Will mark their IDs as ready to be recycled
$R->delete($_) for @e;
is $R->created, 10, 'Created counts all created entities';
is $R->alive, 0, 'Right number of alive entities when all dead';
ok !$R->valid(10), 'Entity is not valid';
# Create 20 entities
# They should re-use the first 10 IDs and use the next 10 ( 0 .. 19 )
@e = map $R->create( Other->new ), 0 .. 19;
is [ sort { $a <=> $b } map $_ & 0xFFFFF, @e ],
[ 1 .. 20 ],
'Recycled and generated the right IDs';
ok $R->valid($e[8]), 'Entity is valid';
is $R->alive, 20, 'Recorded the right number of alive entities after recycling';
@e = $R->view('Other')->entities;
is @e, 20, 'Only alive entities match view';
$R->clear;
is $R->alive, 0, 'Clear invalidates all entities';
is $R->created, 0, 'No records remain';
};
subtest 'View' => sub {
my $R = Game::Entities->new;
my $named = $R->create;
my $aging = $R->create;
my $both = $R->create;
my $dead = $R->create;
t/entities.t view on Meta::CPAN
exception { $R->create( $component ) }
qr/Component must be a reference/,
"$message on create";
my $guid = $R->create;
exception { $R->add( $guid => $component ) }
qr/Component must be a reference/,
"$message on add";
is $R->alive, 1, 'Did not create entities';
$R->clear;
}
};
subtest 'Components instead of names' => sub {
my $guid = $R->create;
for (
[ undef => undef ],
[ ref => {} ],
) {
t/namespace.t view on Meta::CPAN
is [ sort keys %Game::Entities:: ], [qw(
BEGIN
GUID::
Set::
VERSION
View::
__ANON__
_dump_entities
a
add
alive
b
check
clear
create
created
delete
get
import
new
sort
( run in 2.302 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )