Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Entity Tree for 2D isometric games

by holyghost (Acolyte)
on May 09, 2017 at 06:42 UTC ( #1189857=CUFP: print w/replies, xml ) Need Help??

Hello, I've made a z-order speedy Game Entity Tree for use in 2D isometric view games. The thing is you can use an adapter on the tree and use references to alleviate the algorithm. You make lists of entities which get drawn when descending in the balanced tree, a key value is the depth of e.g. the house entities on a background. If you want a more OOP tree, you can use leaf and node tags, consed to data and check these before returning data from e.g. a leaf.
### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. use EntityTreeAdapter; package EntityTree; sub new { my ($class) = @_; my $self = { %nodes => {}, @data = (), @_ }; bless ($self, $class); } sub adapter { my ($class) = @_; return EntityTreeAdapter($self); } sub insert { my ($self, $key, $d) = shift; foreach my $k (keys $self->{nodes}) { if ($k == $key) { push ($self->{data}, $d); return $self->{data}; } if ($k > $key and $k < $key) { $self->nodes = {}; $self->{nodes}{$key} .= EntityTree->new->(data + => $d, nodes => $self->collect_nodes($self->nodes)); return $self->{data}; } else { @keys = keys $self->nodes; while (@keys) { $self->insert(pop(@keys), $d); } } } } ### Normally there are only 2 keys per node sub collect_nodes { my ($self, $collection) = shift; my @keys = keys $collection; my @values = values $collection; foreach my $k (@keys) { $collection .= ($k => pop(@values)); } return $collection; } ### depth-frist search sub search { my ($self, $key) = shift; for my $k (keys $self->{nodes}) { if ($self->{nodes}{$k} == $key) { return push ($self->{nodes}{$k}->search($key), + $self->{nodes}{$key}); } else { return $self->{nodes}{$k}->search($key); } } return (); } 1;
### Copyright (C) The Holy Ghost 2017 ###This program is released under the GPL 3.0 and artistic license 2.0 +. package EntityTreeAdapter; sub new { my ($class) = @_; my $self = { $tree = shift, @_ }; bless ($self, $class); } sub insert { return $self->{tree}->insert($key, $d); } sub search { return $self->{tree}->search($key); } 1;
Holly

Replies are listed 'Best First'.
Re: Entity Tree for 2D isometric games
by vrk (Chaplain) on May 09, 2017 at 20:33 UTC

    I doubt this is working code, for a couple of reasons:

    • insert, collect_nodes and search subroutines have incorrect parameter assigments (all but first parameter are uninitialised)
    • search is nondeterministic (hash keys have random iteration order since Perl 5.18 and unpredictable order in earlier versions)
    • search will probably die if you ever get to the "return push(...)" branch
Re: Entity Tree for 2D isometric games
by Anonymous Monk on May 09, 2017 at 17:22 UTC
    Got any unit tests?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://1189857]
Approved by beech
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-08-19 19:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Who is your favorite scientist and why?



























    Results (312 votes). Check out past polls.

    Notices?