Skip to content

Commit

Permalink
samples: core: freeze children tree
Browse files Browse the repository at this point in the history
Part-of: #215
  • Loading branch information
dbartolini committed Dec 20, 2024
1 parent daa2faa commit 1ffd779
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions samples/core/editors/unit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,39 @@ require "core/lua/class"

UnitUtils = UnitUtils or {}

function UnitUtils.freeze(world, unit_id)
local pw = World.physics_world(world)
local actor = PhysicsWorld.actor_instance(pw, unit_id)
if (actor ~= nil) then
PhysicsWorld.actor_set_kinematic(pw, actor, true)
function UnitUtils.collect_children(scene_graph, unit_id, children)
local transform = SceneGraph.instance(scene_graph, unit_id)
if transform == nil then return end

local cur_child = SceneGraph.first_child(scene_graph, transform)
while cur_child ~= nil do
local child_id = SceneGraph.owner(scene_graph, cur_child)
UnitUtils.collect_children(scene_graph, child_id, children)
table.insert(children, child_id)
cur_child = SceneGraph.next_sibling(scene_graph, cur_child)
end

table.insert(children, unit_id)
end

function UnitUtils.destroy_tree(world, unit_id)
local function collect_children(scene_graph, unit_id, children)
local transform = SceneGraph.instance(scene_graph, unit_id)
if transform == nil then return end

local cur_child = SceneGraph.first_child(scene_graph, transform)
while cur_child ~= nil do
local child_id = SceneGraph.owner(scene_graph, cur_child)
collect_children(scene_graph, child_id, children)
table.insert(children, child_id)
cur_child = SceneGraph.next_sibling(scene_graph, cur_child)
end
function UnitUtils.freeze(world, unit_id)
local physics_world = World.physics_world(world)
local scene_graph = World.scene_graph(world)
local children = {}
UnitUtils.collect_children(scene_graph, unit_id, children)

table.insert(children, unit_id)
for _, child_id in ipairs(children) do
local actor = PhysicsWorld.actor_instance(physics_world, child_id)
if actor ~= nil then
PhysicsWorld.actor_set_kinematic(physics_world, actor, true)
end
end
end

function UnitUtils.destroy_tree(world, unit_id)
local scene_graph = World.scene_graph(world)
local children = {}
collect_children(scene_graph, unit_id, children)
UnitUtils.collect_children(scene_graph, unit_id, children)

for _, child_id in ipairs(children) do
World.destroy_unit(world, child_id)
Expand Down

0 comments on commit 1ffd779

Please sign in to comment.