JD2022-TU1/data/EngineData/Helpers/tweening_helpers.ilu

333 lines
10 KiB
Text

tweening = {}
function tweening.setStartIndex(instructionSetIndex, startInstruction)
if (startInstruction <= 1) then
return
end
local tween = getComponent("TweenComponent_Template")
shiftTable( tween.instructionSets[instructionSetIndex].InstructionSet.instructions, startInstruction - 1 )
end
-- if a RenderSimpleAnimComponent is present on the actor, we HAVE to run before it (otherwise just append)
function tweening.insertComponent(tweenComponent)
tweening.checkUnpack(tweenComponent)
tweening.removeEmptyInstructions(tweenComponent)
for i=1, #params.Actor_Template.COMPONENTS do
if params.Actor_Template.COMPONENTS[i].NAME == "RenderSimpleAnimComponent_Template" then
table.insert( params.Actor_Template.COMPONENTS, i, tweenComponent )
return
end
end
table.insert( params.Actor_Template.COMPONENTS, tweenComponent )
appendTag({VAL = "tween_mob"})
appendTag({VAL = "logic"}) -- pour éviter de générer un warning sur un tweening.tpl
end
function tweening.checkUnpack(tbl)
local i = 1
for key,value in pairs(tbl) do
local element = value
-- print(element)
if type(element) == "table" then
if element.unpackMe == true then
for j = 1, #element do
table.insert(tbl, i + 1, element[#element - j + 1])
end
-- tbl[i] = nil
tbl[i] = {}
else
tweening.checkUnpack(element)
end
end
i = i + 1
end
end
function tweening.removeEmptyInstructions(tbl)
local sets = tbl.TweenComponent_Template.instructionSets
-- print(#sets)
for is = 1, #sets do
local instructions = sets[is].InstructionSet.instructions
-- print(#instructions)
local copy = {}
for ii = 1, #instructions do
local instruction = instructions[ii]
if not isTableEmpty(instruction) then
table.insert(copy, instruction)
end
end
sets[is].InstructionSet.instructions = copy
end
end
function tweening.addInstruction( setIndex, instructionTable )
local tweenComponent = getComponent("TweenComponent_Template")
local instructions = tweenComponent.instructionSets[setIndex].InstructionSet.instructions
appendTable(instructions, {instructionTable})
end
function tweening.addInstructionSet( instructionSet )
local tweenComponent = getComponent("TweenComponent_Template")
appendTable( tweenComponent.instructionSets, {instructionSet} )
end
function tweening.addInstructionSetSafe( instructionSetTable )
if params ~= nil and params.Actor_Template ~= nil then
local tweenComponent = getComponent("TweenComponent_Template")
if tweenComponent ~= nil then
appendTable( tweenComponent.instructionSets, {instructionSetTable} )
end
end
end
function tweening.setStartEvent( setIndex, startEvent )
local tweenComponent = getComponent("TweenComponent_Template")
local instructionSet = tweenComponent.instructionSets[setIndex].InstructionSet
instructionSet.startEvent = startEvent
end
function tweening.setStopEvent( setIndex, stopEvent )
local tweenComponent = getComponent("TweenComponent_Template")
local instructionSet = tweenComponent.instructionSets[setIndex].InstructionSet
instructionSet.stopEvent = stopEvent
end
function tweening.makeTriggable( setIndex )
setIndex = setIndex or 1
local startEvent =
{
NAME = "EventTrigger",
EventTrigger =
{
activated = TRUE,
},
}
tweening.setStartEvent( setIndex, startEvent )
end
function tweening.makeStoppable( setIndex )
setIndex = setIndex or 1
local stopEvent =
{
NAME = "EventTrigger",
EventTrigger =
{
activated = FALSE,
},
}
tweening.setStopEvent( setIndex, stopEvent )
end
function tweening.getCycleDir( reverse )
return (reverse == 1) and -1 or 1
end
function tweening.getSetByName( sets, name )
for i = 1, #sets do
if sets[i].InstructionSet.name:lower() == name:lower() then
return sets[i].InstructionSet
end
end
end
function getNorm(vec)
return getNormXYZ(vec.x, vec.y, vec.z)
end
function getNormXYZ(x, y, z)
z = z or 0
return math.sqrt(x * x + y * y + z * z)
end
function tweening.setInstructionSpeed( instructionTable, speed )
local instruction
local length
if instructionTable.NAME == "TweenLine_Template" then
instruction = instructionTable.TweenLine_Template
length = getNorm(instruction.movement)
elseif instructionTable.NAME == "TweenSine_Template" then
instruction = instructionTable.TweenSine_Template
length = getNorm(instruction.movement)
elseif instructionTable.NAME == "TweenCircle_Template" then
instruction = instructionTable.TweenCircle_Template
local radius = getNormXYZ(instruction.pivot.x, instruction.pivot.y)
length = math.abs(instruction.cycleCount * math.pi * 2 * radius)
else
return nil -- not a translation instruction
end
instruction.duration = length / speed
return instruction
end
function tweening.setInstructionSetSpeed( set, speed )
local instructions = set.InstructionSet.instructions
for i = 1, #instructions do
tweening.setInstructionSpeed( instructions[i], speed )
end
end
function tweening.combineSets( params )
local tween = getComponent("TweenComponent_Template")
if tween == nil then
table.insert( _tweenInstructionSetList, { VAL = params.name } )
return
end
local combinedSet =
{
InstructionSet =
{
name = params.name,
instructions =
{
},
},
}
table.insert(tween.instructionSets, combinedSet)
local combinedSetLoop =
{
InstructionSet =
{
name = params.name.."_Loop",
instructions =
{
},
},
}
local combinedInstructions = combinedSet.InstructionSet.instructions
local setCount = #params.sets
-- set defaults now (so all values are available later)
local defaultSpeed = params.speed or 5.0
for i = 1, setCount do
local param = params.sets[i]
param.count = param.count or 1
param.speed = param.speed or defaultSpeed
end
for i = 1, setCount do
local param = params.sets[i]
local iterationCount = param.count
if iterationCount == 0 and setCount ~= 1 then
-- copy to the looping set
combinedSet.InstructionSet.iterationCount = 1
combinedSet.InstructionSet.nextSet = combinedSetLoop.InstructionSet.name
combinedInstructions = combinedSetLoop.InstructionSet.instructions
table.insert(tween.instructionSets, combinedSetLoop)
iterationCount = 1
end
-- copy instructions to target set (combined or combinedLoop), changing the speed
local set = tweening.getSetByName(_bricks, param.name)
for j = 1, iterationCount do
for k = 1, #set.instructions do
local instructionTable = cloneTable(set.instructions[k])
local instruction = tweening.setInstructionSpeed(instructionTable, param.speed)
if instruction then
-- we have a translation, see if we need to smooth (we need a prev and a next to smooth)
if param.smooth then
instruction.startSpeed = (i == 1) and 0 or params.sets[i-1].speed
instruction.endSpeed = (i == setCount) and 0 or params.sets[i+1].speed
instruction.startDuration = instruction.duration / 2
instruction.endDuration = instruction.duration / 2
end
end
table.insert(combinedInstructions, instructionTable)
end
end
combinedSet.InstructionSet.angleOffset = set.angleOffset
combinedSetLoop.InstructionSet.angleOffset = set.angleOffset
end
end
function tweening.addInstruction_disableCollision(disable)
return
{
NAME = "TweenEvent_Template",
TweenEvent_Template =
{
duration = 0,
event =
{
NAME = "EventDisableCollision",
EventDisableCollision =
{
disable = disable,
},
},
triggerSelf = TRUE,
triggerChildren = FALSE,
triggerBoundChildren = TRUE,
},
}
end
function tweening.addInstruction_fadeOut(fadeDuration, amount)
amount = amount or 0.0
return
{
NAME = "TweenEvent_Template",
TweenEvent_Template =
{
duration = 0,
event =
{
NAME = "EventShow",
EventShow =
{
alpha = amount,
transitionTime = fadeDuration,
},
},
triggerSelf = FALSE,
triggerChildren = FALSE,
triggerBoundChildren = TRUE,
},
}
end
function tweening.addInstruction_fadeIn(fadeDuration, amount)
amount = amount or 1.0
return
{
NAME = "TweenEvent_Template",
TweenEvent_Template =
{
duration = 0,
event =
{
NAME = "EventShow",
EventShow =
{
alpha = amount,
transitionTime = fadeDuration,
},
},
triggerSelf = FALSE,
triggerChildren = FALSE,
triggerBoundChildren = TRUE,
},
}
end
includeReference("EngineData/Helpers/Tweening/Include/tweening_helpers_fx.ilu")
includeReference("EngineData/Helpers/Tweening/Include/tweening_helpers_ld.ilu")
includeReference("EngineData/Helpers/Tweening/Include/tweening_library.ilu")