luxeengine / alpha

alpha - deprecated 2015~2016. unrelated to the new engine! view the new engine here - https://luxeengine.com/
MIT License
565 stars 74 forks source link

Relative depths for child Visuals #284

Open Aleph0Null opened 9 years ago

Aleph0Null commented 9 years ago

Current depth values are absolute. If you have a composite Sprite made up of many child Sprites and you change the depth of the parent, the depth of the children won't change together with it, which is inconvenient.

Visual could have a relative_depth field. If relative_depth is non-null and a parent is set, the child's depth will be set to parent.depth + relative_depth. The child's depth should also change when the parent's depth changes.

This behaviour can be mimicked by overriding update() to update the child's depth every frame, but it seems more efficient and organized to have in-built support for it.

(If this is ok, I can make a pull request.)

le-doux commented 8 years ago

Just my two cents, but I also think something like this would be useful.

anissen commented 8 years ago

Good idea! However, it might be confusing to have both depth and relative_depth - what is the behavior if both are set? I usually do something like the following for handling relative depths:

class SomeEntity extends luxe.Sprite {
    var icon :luxe.Sprite;
    var text :luxe.Text;

    public function new(options :SpriteOptions) {
        super(options);

        icon = new Sprite({
            // ...
            parent: this,
            batcher: options.batcher,
            scene: options.scene,
            depth: options.depth + 0.01
        });

        text = new luxe.Text({
            // ...
            parent: this,
            batcher: options.batcher,
            scene: options.scene,
            depth: options.depth + 0.02
        });
    }
}
ghost commented 8 years ago

I suggest to make function in Luxe class:

new Sprite({
   name: "sprite1",
   depth: 1, //depth 3
});
new Sprite({
   name: "sprite2",
   depth: 1.1, //depth 3.1
});
Luxe.something.relative_depth("sprite", 2);

So, every sprite with name "sprite" will have depth 2 + original depth