Commit fa3ea5a6 authored by Delisa Mason's avatar Delisa Mason

refactor: Separate animation group into its own object, functions

parent ad003d7c
......@@ -18,6 +18,70 @@ local SceneDSL = function()
end
end
local function newGroup(items)
local group = {loaded=false, duration=0, elapsed=0, directives={}, renderers={}}
function group:update(dt)
if not self.loaded then
for _, directive in pairs(self.directives) do
for _, transform in ipairs(directive.transforms) do
if transform.init ~= nil then
transform:init(directive.context)
end
end
end
self.loaded = true
end
for asset_id, directive in pairs(group.directives) do
local asset = scene.assets[asset_id]
for _, transform in ipairs(directive.transforms) do
transform:apply(asset, directive.context, dt)
end
end
self.elapsed = self.elapsed + dt
end
function group:draw()
for _, renderer in ipairs(self.renderers) do
if renderer.asset_id ~= nil then
local asset = scene.assets[renderer.asset_id]
local context = self.directives[renderer.asset_id].context
renderer.draw(asset, context)
else
renderer.draw(nil, renderer.context)
end
end
end
for _, item in pairs(items) do
if item.asset_id ~= nil then
if group.directives[item.asset_id] == nil then
group.directives[item.asset_id] = {transforms={}, context={}}
end
if item.context ~= nil then
group.directives[item.asset_id].context = item.context
end
if item.transform ~= nil then
table.insert(group.directives[item.asset_id].transforms, item.transform)
if item.transform.duration ~= nil and item.transform.duration > group.duration then
group.duration = item.transform.duration
end
end
end
if item.draw ~= nil then
table.insert(group.renderers, {asset_id=item.asset_id, draw=item.draw, context=item.context})
end
if item.duration ~= nil then
if item.duration ~= nil and item.duration > group.duration then
group.duration = item.duration
end
end
if item.await_dialog == true then
group.await_dialog = true
end
end
return group
end
-- Cache resources for later use
function scene.load_resources(items)
for _, asset in ipairs(items) do
......@@ -169,35 +233,7 @@ local SceneDSL = function()
end
function scene.group(items)
local group = {loaded=false, duration=0, elapsed=0, directives={}, renderers={}}
for _, item in pairs(items) do
if item.asset_id ~= nil then
if group.directives[item.asset_id] == nil then
group.directives[item.asset_id] = {transforms={}, context={}}
end
if item.context ~= nil then
group.directives[item.asset_id].context = item.context
end
if item.transform ~= nil then
table.insert(group.directives[item.asset_id].transforms, item.transform)
if item.transform.duration ~= nil and item.transform.duration > group.duration then
group.duration = item.transform.duration
end
end
if item.draw ~= nil then
table.insert(group.renderers, {asset_id=item.asset_id, draw=item.draw})
end
end
if item.duration ~= nil then
if item.duration ~= nil and item.duration > group.duration then
group.duration = item.duration
end
end
if item.await_dialog == true then
group.await_dialog = true
end
end
table.insert(scene.groups, group)
table.insert(scene.groups, newGroup(items))
end
function scene.wait(duration)
......@@ -221,23 +257,7 @@ local SceneDSL = function()
function scene.update(dt)
local group = scene.groups[scene.groupindex]
if not group.loaded then
for _, directive in pairs(group.directives) do
for _, transform in ipairs(directive.transforms) do
if transform.init ~= nil then
transform:init(directive.context)
end
end
end
group.loaded = true
end
for asset_id, directive in pairs(group.directives) do
local asset = scene.assets[asset_id]
for _, transform in ipairs(directive.transforms) do
transform:apply(asset, directive.context, dt)
end
end
group.elapsed = group.elapsed + dt
group:update(dt)
if group.elapsed >= group.duration and group.await_dialog ~= true then
if scene.groupindex == #scene.groups then
scene.done = true
......@@ -248,12 +268,7 @@ local SceneDSL = function()
end
function scene.draw()
local group = scene.groups[scene.groupindex]
for _, renderer in ipairs(group.renderers) do
local asset = scene.assets[renderer.asset_id]
local context = group.directives[renderer.asset_id].context
renderer.draw(asset, context)
end
scene.groups[scene.groupindex]:draw()
end
function scene.END()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment