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

In reply to Entity Tree for 2D isometric games by holyghost

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.