summaryrefslogtreecommitdiff
path: root/utils/bodyLifecycleManager.lua
blob: 219537c5b23f9b532f558f28d38b8c481adca519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
local bodyLifeCycleManager = {}
local physics = require "logic.physics"

function bodyLifeCycleManager.new()
    local self = setmetatable({
        toCreate = {},
        toDestroy = {},
        toSetPosition = {}
    }, {__index = bodyLifeCycleManager})
    return self
end

function bodyLifeCycleManager:create(object, shape, bodyType, sensor)
    table.insert(
        self.toCreate,
        {object = object, shape = shape, bodyType = bodyType, sensor = sensor == nil and false or sensor}
    )
end

function bodyLifeCycleManager:destroy(object)
    table.insert(
        self.toDestroy,
        object
    )
end

function bodyLifeCycleManager:setPosition(object, axis, val)
    table.insert(
        self.toSetPosition,
        {object = object, axis = axis, val = val}
    )
end

function bodyLifeCycleManager:update(dt)
    if self.toCreate and #self.toCreate > 0 then
        for _, data in ipairs(self.toCreate) do
            physics.bodifyObject(G.world, data.object, data.shape, data.bodyType)
            if data.sensor then
                data.object.fixture:setSensor(true)
            end
        end
        self.toCreate = {}
    end

    if self.toDestroy and #self.toDestroy > 0 then
        for _, body in ipairs(self.toDestroy) do
            if not body:isDestroyed() then
                body:destroy()
            end
        end
        self.toDestroy = {}
    end

    if self.toSetPosition and #self.toSetPosition > 0 then
        for _, data in ipairs(self.toSetPosition) do
            if data.axis == "x" then
                data.object.physicsBody:setX(data.val)
            elseif data.axis == "y" then
                data.object.physicsBody:setY(data.val)
            end
        end
        self.toSetPosition = {}
    end
end

return bodyLifeCycleManager