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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.