Finally if you don’t use a single “class” for a field in the octree itself, I think that adds runtime overhead. You would need a Box<dyn Trait>
for the octree to put different types of structs in the same field. As I understand it that would add an extra byte to each node’s memory footprint (so Rust could enforce type rules during runtime).
If you put the BlockState directly behind a pointer then you don’t have to worry about all that, it’s more efficient since all blocks with the same state share the same instance of BlockState.
Octree Blueprints v3, now supporting block state (I edited away a v2)
## pub BlockState struct
fields:
* color
* shape
* weight
* hardness
* etc
methods:
* pub new returns a new &BlockState, probably from a static hashmapped pool to
prevent exact duplicates
* pub get_color which borrows self and returns a color
* pub get_shape which borrows self and returns a shape
* pub get_weight which borrows self and returns weight
* pub get_hardness which borrows self and returns hardness
* getters for anything else you think all blocks should have
## Node enum (private)
variants:
### Interior
fields:
* color
* Node children[8]
### Leaf
fields:
* &BlockState block_state
methods:
* get_child that takes a number between 0 and 7 and returns the corresponding
child node from children[] if it exists, or children[0] if there is only one
child node, or an error if there are no children in the list. Fails if called
on a leaf.
* pub get_color which returns a color from either self or by calling the getter
on block_state
* get_block_state which returns block_state. Fails if called
on an interior node
* set_child which takes a Node and a number between 0 and 7. If there is a
corresponding child node in children[] then compare it with the new node and
replace it if there is a difference. Fails if called on a leaf.
* set_color which sets the color field but fails if called on a leaf.
* update which calculates a new average color from the child nodes and
compares it to the existing color, if different then set the color field and
return true, otherwise return false. Fails if called on a leaf.
## pub Octree struct
fields:
* max_depth
* something that will allocate immutable nodes on the heap
methods:
* pub new takes max_depth and fill block as input and outputs a new Octree
* pub get_block_state which takes x y z coordinates and returns the block_state at
that leaf node (return type &BlockState)
* pub get_color which takes x y z coordinates and a depth level and
returns get_color from the node covering those coordinates at that depth level
(used for rendering)
* pub set_block_state that borrows mutable self and takes x y z coordinates and
a &BlockState, replaces the leaf node at those coords, and calls update on
parent nodes going up the tree until it returns false