– Unsorted contemplations of how HeapsIO “should” be used. –
(This is an answer to Rahil in “Entity systems 101”)
Yeah, actually one should maintain one’s own data structures for that. (It may just be a list, an array, some reference var
s etc. that’s okay )
About findAll()
, it’s in the nature of doing what it does. It searches through the entire tree of the object’s (here h2d.Scene
) children and children’s children (so recursively) etc. to apply a condition/function on each of them. Then it creates (and under the hood allocates RAM) a fresh new Array of data and then you (I, in that case) call that findAll()
every(!!!) time calling the update function (as I don’t even use dt
– delta time – that’s really on every frame possible !!!)… wow! see? – I wanted to rely on the HeapsIO API and avoid maintaining my own Arrays for the sake of keeping the tutorial simple. So, good point. I should mention that maybe in the class description or sort of right away…
Yes, this whole thing confused me too when I started with Heaps. It’s really that h2d.Scene
extends h2d.Layers
, which extends h2d.Object
. It’s all there already. And when you have a game with no layers or just attach an object to another (“node” tree like in Godot, see below) you can just use the new Object(parent)
approach. Otherwise, yes, it’s s2d.add( o, layer_index )
.
Trees (adding one object to another) allow to move the root – an enemy goblin for instance – with all of its children: the goblin’s health bar, that is drawn on top of him, the text on the health bar itselft saying “life”. The little icon of a heart attached next to it. etc. etc. and you can also rotate etc. them all at once.
Layers allow to structure different kinds of objects. For instance a platformer has a layer for UI_layer=4
, platformsLayer=3
, playerAndEnemiesLayer=2
, backgroundSceneryLayer=1
and veryDistantBackgroundSceneryLayer=0
(like mountains, really far away). These var
s are just Int
s, that represent the priority in which order objects in that layer are to be drawn. But you decide for yourself what you need.
Then, for a top-down game, you can use e.g. .Ysort()
on a layer so what’s in front is drawn in the end (on top of what’s behind). Draw earth and lawn, then players and houses, then clouds, then UI, because it’s above everything else.