Code: Select all
<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" CameraPosition="0 0 40" FileVersion="2">
<OnLoaded>
<ZLibrary Comment="Tween Functions">
<Source>
<![CDATA[//
// Inspired by https://www.lexaloffle.com/bbs/?pid=52681&tid=31274
const float back = 1.70158;
float tween_easing(float t, string type)
{
float s = back;
switch (type)
{
case "linear":
return t;
break;
case "quad_out":
return -1*t*(t-2);
break;
case "quad_in":
return t*t;
break;
case "quad_in_out":
t = t*2;
if (t<1) return 0.5*t*t;
return -0.5*((t-1)*(t-3)-1);
break;
case "back_in":
return t*t*((s+1)*t-s);
break;
case "back_out":
t-=1;
return t*t*((s+1)*t+s)+1;
break;
case "back_in_out":
s = back;
t *= 2;
if (t<1)
{
s *= 1.525;
return 0.5*(t*t*((s+1)*t-s));
}
t -= 2;
s *= 1.525;
return 0.5*(t*t*((s+1)*t+s)+2);
break;
}
return t; // return linear if error in type string
}
void tween_init(model t, model m, string ease, float start, float end, float duration, float delay)
{
t.TweenModel = m; // Attached model to the tween
t.TweenEase = ease; // See list of easings in Tween Definitions
t.TweenStart = start; // Start value
t.TweenDiff = end - start; // End value
t.TweenTime = duration; // Duration of the tween (in seconds)
t.TweenDelay = delay; // Delay before starting the tween (in seconds)
t.TweenProgress = 0;
}]]>
</Source>
</ZLibrary>
<ZExpression Comment="Tween Definitions">
<Expression>
<![CDATA[//
TweenNames[0] = "linear";
TweenNames[1] = "quad_in";
TweenNames[2] = "quad_out";
TweenNames[3] = "quad_in_out";
TweenNames[4] = "back_in";
TweenNames[5] = "back_out";
TweenNames[6] = "back_in_out";]]>
</Expression>
</ZExpression>
<ZExpression>
<Expression>
<![CDATA[App.CameraPosition.Z = 40;
model m, t;
for (int i=0; i<TweenNames.SizeDim1; i++)
{
m = createModel(Ball);
m.Position.X = -12 + i * 4;
m.Position.Y = -10;
t = createModel(Tween);
tween_init(t, m, TweenNames[i], m.Position.Y, 10, 1, 1);
}]]>
</Expression>
</ZExpression>
</OnLoaded>
<Content>
<Mesh Name="SphereMesh">
<Producers>
<MeshSphere/>
</Producers>
</Mesh>
<Model Name="Ball">
<OnRender>
<RenderMesh Mesh="SphereMesh"/>
</OnRender>
</Model>
<Model Name="Tween">
<Definitions>
<Variable Name="TweenEase" Type="2"/>
<Variable Name="TweenDelay"/>
<Variable Name="TweenTime"/>
<Variable Name="TweenStart"/>
<Variable Name="TweenProgress"/>
<Variable Name="TweenDiff"/>
<Variable Name="TweenModel" Type="3"/>
</Definitions>
<OnUpdate>
<ZExpression>
<Expression>
<![CDATA[//
float i;
if (CurrentModel.TweenDelay > 0)
{
CurrentModel.TweenDelay -= App.DeltaTime;
i = CurrentModel.TweenStart;
}
else
{
CurrentModel.TweenProgress += App.DeltaTime / CurrentModel.TweenTime;
i = CurrentModel.TweenStart + CurrentModel.tweenDiff * tween_easing(clamp(CurrentModel.TweenProgress, 0, 1), CurrentModel.TweenEase);
}
// See how can I pass some parameters to move the model on another axis, or rotate it or return some floats to the TweenModel
CurrentModel.TweenModel.Position.Y = i;
if (CurrentModel.TweenProgress >= 1)
{
// Do something on tween end. That's the part where I would like to pass a function
model t = createModel(Tween);
tween_init(t, CurrentModel.TweenModel, CurrentModel.TweenEase, CurrentModel.TweenModel.Position.Y, CurrentModel.TweenModel.Position.Y *= -1, 1, 1);
@RemoveModel();
}]]>
</Expression>
</ZExpression>
</OnUpdate>
</Model>
<Array Name="TweenNames" Type="2" SizeDim1="7"/>
</Content>
</ZApplication>
And my second concern is: is it possible to pass a function so it can be called when the tween ends? You'll see that in Tween (Model) OnUpdate.
Thanks !