ksCurves

Summary

Use ksCurves to animate the position of a server entity.

Description

ksCurves allow developers to use Unity AnimationCurves on Reactor server scripts. Simply add a ksCurve field to your server script and expose it to the editor with a [ksEditable] tag. In the Unity inspector this field will be displayed as a AnimationCurve property. To use the curve, call its Evaluate(float time) method whenever you need a curve value. The following script shows how you may use the ksCurves to animate a server entity.

using KS.Reactor;
using KS.Reactor.Server;

/**
 * Animate the position of an entity relative to its rotation.
 * This should be used on kinematic entities.
 * If another script moves the entity, then the new position will
 * be used as the new root of the animation.
 */
public class sePositionAnimation : ksServerEntityScript
{
    [ksEditable] private ksCurve m_x;
    [ksEditable] private ksCurve m_y;
    [ksEditable] private ksCurve m_z;
    private ksVector3 m_rootPosition;
    private ksVector3 m_lastPosition;

    public override void Initialize()
    {
        m_rootPosition = Transform.Position;
        m_lastPosition = Transform.Position;
        Room.OnUpdate[0] += Update;
    }

    public override void Detached()
    {
        Room.OnUpdate[0] -= Update;
    }

    public void Update()
    {
        if (m_lastPosition != Transform.Position)
        {
            m_rootPosition = Transform.Position;
        }

        ksVector3 offset = new ksVector3(
            (m_x != null) ? m_x.Evaluate((float)Time.Time) : 0.0f,
            (m_y != null) ? m_y.Evaluate((float)Time.Time) : 0.0f,
            (m_z != null) ? m_z.Evaluate((float)Time.Time) : 0.0f
        );

        offset *= Transform.Rotation;
        m_lastPosition = m_rootPosition + offset;
        Transform.MoveTo(m_lastPosition);
    }
}