147 lines
4.5 KiB
Plaintext
147 lines
4.5 KiB
Plaintext
#! Contains relationships: parents and children, which a world keeps of its
|
|
#! own, as a scene's transforms are within their parents', and despawns
|
|
#! together.
|
|
|
|
import std::containers;
|
|
module malunal::ecs;
|
|
|
|
#! The entity an entity is a child of, which `world::set_parent` gives it: it's
|
|
#! despawned with its parent, and its parent's `children` lists it.
|
|
pub struct child_of {
|
|
val of: entity;
|
|
}
|
|
|
|
#! Its parent.
|
|
pub val child_of::parent: entity {
|
|
get => this.of;
|
|
}
|
|
|
|
#! The children of an entity, in the order they were given it, which the world
|
|
#! keeps as its children are given and taken.
|
|
pub struct children : owning {
|
|
var list: vector<entity>;
|
|
}
|
|
|
|
pub def children::close(&mut this): void {
|
|
this.list.close();
|
|
}
|
|
|
|
#! How many it has.
|
|
pub val children::length: u64 {
|
|
get => this.list.length;
|
|
}
|
|
|
|
#! Them, in order.
|
|
pub val children::span: span<entity> {
|
|
get => this.list.span;
|
|
}
|
|
|
|
#! One of them, by its place in the order.
|
|
pub def children::at(&this, index: u64): entity {
|
|
return this.list[index];
|
|
}
|
|
|
|
#! Makes an entity a child of another: it's despawned with it, and its parent's
|
|
#! `children` lists it, last. It leaves the parent it had. Where a pass goes
|
|
#! over the world, it's made as the pass ends, as what moves an entity is.
|
|
#! @param this The world.
|
|
#! @param child The entity.
|
|
#! @param parent Its parent, which mustn't be the child or within it.
|
|
pub def world::set_parent(&mut this, child: entity, parent: entity): void {
|
|
if (!this.alive(child) && !this.awaited(child)) || (!this.alive(parent) && !this.awaited(parent)) {
|
|
fault("`set_parent` was given an entity that isn't alive.");
|
|
}
|
|
if this.deferring > 0 {
|
|
this.lock();
|
|
this.pending.list.append(command{ what = op::parent, who = child, at = parent.bits });
|
|
this.unlock();
|
|
return;
|
|
}
|
|
this.set_parent_now(child, parent);
|
|
}
|
|
|
|
#! Takes an entity from its parent, so it's of the world itself, as `set_parent`
|
|
#! makes a child, once a pass ends.
|
|
#! @returns Whether it had one.
|
|
pub def world::remove_parent(&mut this, child: entity): bool {
|
|
return false if !this.has_id(child, this.child_of_id);
|
|
if this.deferring > 0 {
|
|
this.defer(op::unparent, child, null);
|
|
} else {
|
|
this.remove_now(child, this.child_of_id);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
#! The parent of an entity, where it has one.
|
|
pub def world::parent_of(&this, child: entity): maybe<entity> {
|
|
val at := this.value_of(child, this.child_of_id);
|
|
return nothing{} if at == null;
|
|
return through(at as *child_of).of;
|
|
}
|
|
|
|
#! The children of an entity, in order, none where it has none. They last until
|
|
#! it's next given or loses one.
|
|
pub def world::children_of(&this, parent: entity): span<entity> {
|
|
val at := this.value_of(parent, this.children_id);
|
|
return span::<entity>::blank() if at == null;
|
|
return through(at as *children).list.span;
|
|
}
|
|
|
|
# Makes an entity a child of another at once.
|
|
def world::set_parent_now(&mut this, child: entity, parent: entity): void {
|
|
return if !this.alive(child) || !this.alive(parent);
|
|
var above := parent;
|
|
while above != entity::none {
|
|
if above == child {
|
|
fault($"`set_parent` would make {child.show()} a child of itself.");
|
|
}
|
|
above = this.parent_of(above).unwrap_or(entity::none);
|
|
}
|
|
if this.parent_of(child) is some(had) && had == parent {
|
|
return;
|
|
}
|
|
this.deferring++;
|
|
this.leave_parent(child);
|
|
var link := child_of{ of = parent };
|
|
this.set_bytes(child, info_of::<child_of>(), &mut link as *mut u8);
|
|
val list := this.value_of(parent, this.children_id);
|
|
if list != null {
|
|
through(list as *mut children).list.append(child);
|
|
} else {
|
|
var made := children{ list = vector::<entity>::new() };
|
|
made.list.append(child);
|
|
this.set_bytes(parent, info_of::<children>(), &mut made as *mut u8);
|
|
emptied(&mut made);
|
|
}
|
|
this.deferring--;
|
|
this.settle();
|
|
}
|
|
|
|
# Takes an entity from its parent's `children`, where it has a parent, leaving
|
|
# its `child_of` for the caller.
|
|
def world::leave_parent(&mut this, child: entity): void {
|
|
val above := this.parent_of(child) else { return; };
|
|
val list := this.value_of(above, this.children_id);
|
|
if list != null {
|
|
through(list as *mut children).list.remove(&child);
|
|
}
|
|
}
|
|
|
|
# Each entity within one, its children before theirs, appended.
|
|
def world::within(&this, e: entity, into: &mut vector<entity>): void {
|
|
var at := into.length;
|
|
val kids := this.children_of(e);
|
|
for i in 0..kids.length {
|
|
into.append(kids[i]);
|
|
}
|
|
while at < into.length {
|
|
val each := into[at];
|
|
at++;
|
|
val more := this.children_of(each);
|
|
for i in 0..more.length {
|
|
into.append(more[i]);
|
|
}
|
|
}
|
|
}
|