Exemple #1
0
    private void Start()
    {
        spriteRenderer = GetComponent <SpriteRenderer>();
        controller     = GetComponent <Controller>();
        currentAnim    = Idle;
        ResetAnim();
        Color color = new Color(PlayerPrefs.GetFloat("R"), PlayerPrefs.GetFloat("G"), PlayerPrefs.GetFloat("B"));

        spriteRenderer.color = color;
    }
Exemple #2
0
        // Add curve to animation clip.
        public void AddCurve(string clipName, string path, string type, string propertyName, AnimationCurve curve)
        {
            // Find this clip.
            AnimClip clip = FindClip(clipName);

            if (clip == null)
            {
                return;
            }

            // New one curve.
            Curve c = new Curve();

            c.path         = path;
            c.type         = type;
            c.propertyName = propertyName;
            c.curve        = curve;

            // Save key frames.
            Keyframe[] keys = c.curve.keys;

            if (keys.Length > 0)
            {
                c.maxVal = -Mathf.Infinity;
                c.minVal = Mathf.Infinity;

                // Find the maximum and minimum value.
                foreach (Keyframe f in keys)
                {
                    if (f.value > c.maxVal)
                    {
                        c.maxVal = f.value;
                    }

                    if (f.value < c.minVal)
                    {
                        c.minVal = f.value;
                    }
                }
            }
            else
            {
                c.maxVal = 0;
                c.minVal = 0;
            }

            // Add to list.
            clip.curves.Add(c);
        }
Exemple #3
0
        // Add curve event.
        public void AddEvent(string clipName, AnimationEvent evn)
        {
            AnimClip clip = FindClip(clipName);

            if (clip == null)
            {
                return;
            }

            Event e = new Event();

            e.FromAnimEvent(evn);

            clip.events.Add(e);
        }
Exemple #4
0
        // Get animation dstAnimClip length.
        public bool GetClipLength(string clipName, out float length)
        {
            length = 0;

            // Find animation dstAnimClip data.
            AnimClip anmClp = FindClip(clipName);

            if (anmClp == null)
            {
                return(false);
            }

            length = anmClp.length;

            return(true);
        }
Exemple #5
0
        // Add animation clip.
        public void AddClip(string clipName, WrapMode wrapMode, float frameRate, float length)
        {
            if (FindClip(clipName) != null)
            {
                return;
            }

            // New animation clip and add to list.
            AnimClip clip = new AnimClip();

            clip.name      = clipName;
            clip.wrapMode  = wrapMode;
            clip.frameRate = frameRate;
            clip.length    = length;

            animClips.Add(clip);
        }
    public void Play(string clipName)
    {
        AnimClip clip = null;

        if (m_clipDic.TryGetValue(clipName, out clip))
        {
            m_curClip  = clip;
            m_curIndex = 0;
            m_timer    = 0;

            m_sprite.sprite = m_curClip.m_frames[0];

            m_playing = true;
        }
        else
        {
            Debug.Log("Can not found clip. " + clipName);
        }
    }
Exemple #7
0
        private static void ProcessAnimClip(int animClipIx, AnimClip animClip, Context ctx)
        {
            ctx.AnimClip = new Tuple <int, AnimClip>(animClipIx, animClip);

            var keyframes = animClip.Keyframes;

            if (keyframes == null)
            {
                return;
            }

            ctx.RunKeyFramesAction(ctx);

            for (var ix = 0; ix < keyframes.Value.ProceduralFaceKeyFrameLength; ix++)
            {
                var proceduralFace = keyframes.Value.ProceduralFaceKeyFrame(ix);
                if (proceduralFace != null)
                {
                    ProcessProceduralFace(ix, proceduralFace.Value, ctx);
                }
            }
        }
Exemple #8
0
    public void ChangeController()
    {
        if (currentState != controller.currentState)
        {
            currentState = controller.currentState;
            switch (currentState)
            {
            case Controller.State.Idle:
                currentAnim = Idle;
                break;

            case Controller.State.Walking:
                currentAnim = Walking;
                break;

            case Controller.State.Ducking:
                currentAnim = Ducking;
                break;

            case Controller.State.Jumping:
                currentAnim = Jumping;
                break;

            case Controller.State.Falling:
                currentAnim = Falling;
                break;

            case Controller.State.Door:
                currentAnim = Door;
                break;

            case Controller.State.Cut:
                currentAnim = Cutter;
                break;
            }
            ResetAnim();
        }
    }
Exemple #9
0
        // Get curve value range.
        public bool GetClipCrvValRange(string clipName, string crvProperty, out float valRange)
        {
            valRange = 0;

            // Find animation dstAnimClip data.
            AnimClip anmClp = FindClip(clipName);

            if (anmClp == null)
            {
                return(false);
            }

            // Find curve.
            foreach (Curve c in anmClp.curves)
            {
                if (crvProperty == c.propertyName)
                {
                    valRange = c.maxVal - c.minVal;
                    return(true);
                }
            }

            return(false);
        }
Exemple #10
0
        // Add curve to one game object.
        public bool AddCrvClipToObj(GameObject obj, string clipName, Dictionary <string, float> valRange, float timeScale)
        {
            // Find animation dstAnimClip data.
            AnimClip srcAnimClip = FindClip(clipName);

            if (srcAnimClip == null)
            {
                return(false);
            }

            // Get animation from the target object.
            Animation dstAnim = obj.GetComponent <Animation>();

            if (dstAnim == null)
            {
                dstAnim = obj.AddComponent <Animation>();
            }

            AnimCurve dstAnimCurve = obj.GetComponent <AnimCurve>();

            if (dstAnimCurve == null)
            {
                dstAnimCurve = obj.AddComponent <AnimCurve>();
            }

            // Create one animation dstAnimClip for this dstAnimClip data.
            AnimationClip dstAnimClip = new AnimationClip();

            dstAnimClip.name = srcAnimClip.name;

            // Add curve.
            foreach (Curve srcCurve in srcAnimClip.curves)
            {
                Keyframe[] keys = srcCurve.curve.keys;

                float startTime = -1;
                float endTime   = -1;
                float startVal  = srcCurve.minVal;
                float endVal    = srcCurve.maxVal;

                // Get start/end value
                foreach (Event e in srcAnimClip.events)
                {
                    if (e.functionName == "TranslateStart")
                    {
                        foreach (Keyframe key in keys)
                        {
                            if (Mathf.Approximately(key.time, e.time))
                            {
                                startTime = key.time;
                                startVal  = key.value;
                                break;
                            }
                        }
                    }
                    else if (e.functionName == "TranslateEnd")
                    {
                        foreach (Keyframe key in keys)
                        {
                            if (Mathf.Approximately(key.time, e.time))
                            {
                                endTime = key.time;
                                endVal  = key.value;
                                break;
                            }
                        }
                    }
                }

                float curRange = endVal - startVal;

                if (curRange != 0)
                {
                    float range = curRange;

                    foreach (KeyValuePair <string, float> kvp in valRange)
                    {
                        if (kvp.Key == srcCurve.propertyName)
                        {
                            range = kvp.Value;
                            break;
                        }
                    }

                    // Scale value.
                    if (range != curRange)
                    {
                        float valueScale     = range / curRange;
                        float lastValueDelta = 0;
                        float lastTimeDelta  = 0;

                        for (int i = 0; i < keys.Length; i++)
                        {
                            if (startTime >= 0 && keys[i].time <= startTime)
                            {
                                continue;
                            }

                            if (endTime >= 0 && keys[i].time > endTime)
                            {
                                keys[i].value += lastValueDelta;
                                keys[i].time  += lastTimeDelta;
                            }
                            else
                            {
                                float _value = keys[i].value;
                                keys[i].value *= valueScale;
                                lastValueDelta = keys[i].value - _value;

                                float _time = keys[i].time;
                                keys[i].time  = startTime + (keys[i].time - startTime) / timeScale;
                                lastTimeDelta = keys[i].time - _time;
                            }
                        }
                    }
                }
                else
                {
                    continue;
                    //Debug.LogWarning(string.Format("Can not scale curve {0} in {1}", srcCurve.propertyName, clipName));
                }

                dstAnimClip.SetCurve(srcCurve.path, System.Type.GetType(srcCurve.type), srcCurve.propertyName, new AnimationCurve(keys));
            }

            // Add events.
            foreach (Event e in srcAnimClip.events)
            {
                if (e.functionName == "TranslateStart" || e.functionName == "TranslateEnd")
                {
                    continue;
                }

                AnimationEvent animationEvent = e.ToAnimEvent();

                animationEvent.time = e.time / timeScale;

                dstAnimClip.AddEvent(animationEvent);
            }

            // Add this dstAnimClip.
            if (dstAnim[dstAnimClip.name] != null)
            {
                dstAnim.RemoveClip(dstAnimClip.name);
            }

            dstAnim.AddClip(dstAnimClip, dstAnimClip.name);

            return(true);
        }
    /*
     * private MotionFrame GetBakedMotionFrame(string motionName, float normalizedTime, MotionClipType clipType)
     * {
     *  for (int i = 0; i < MotionClips.Count; i++) {
     *      var clip = MotionClips[i];
     *
     *      if (clipType != null) {
     *          if (clipType == clip.ClipType) {
     *              return clip.MotionFrames[0];
     *          }
     *      } else if (clip.Name.Equals(motionName)) {
     *          int frameBasedOnTime = Mathf.FloorToInt(clip.MotionFrames.Length * normalizedTime);
     *
     *          return clip.MotionFrames[frameBasedOnTime];
     *      }
     *  }
     *
     *  return null;
     * }
     */

    public void ExtractMotionClips(AnimClip animationClip)
    {
        var motionClip = new MotionClipData();

        motionClip.Name = animationClip.name;
        //motionClip.MotionClipLengthInMilliseconds = animationClip.ClipLengthInMilliseconds; //no value for animationClip.ClipLengthInMilliseconds
        motionClip.ClipType     = animationClip.ClipType;
        motionClip.MotionFrames = new MotionFrame[animationClip.Frames.Count - 10];

        // The very first frame
        var firstMotionFrame        = new MotionFrame();
        var firstFrame              = animationClip.Frames[0];
        var stubAnimationjointPoint = new AnimationJointPoint {
            Position = Vector3.zero
        };

        firstMotionFrame.Joints = (from jp in firstFrame.JointPoints
                                   select MakeMotionJoint(jp, stubAnimationjointPoint)).ToArray();
        foreach (var jt in firstMotionFrame.Joints)
        {
            jt.BaseRotation = jt.Rotation;
        }

        var rootMotionJoint = firstMotionFrame.Joints.First(x => x.Name.Equals(RootName));

        firstMotionFrame.AngularVelocity = Vector3.Angle(Vector3.forward, rootMotionJoint.Velocity) / 180f;
        firstMotionFrame.Velocity        = rootMotionJoint.Velocity.sqrMagnitude;
        firstMotionFrame.Direction       = rootMotionJoint.Velocity.normalized;
        firstMotionFrame.Time            = firstFrame.Time;
        GetClipTrajectoryData(firstMotionFrame);

        //motionClip.MotionFrames[0] = firstMotionFrame;

        // All the other ones
        for (int i = 10; i < animationClip.Frames.Count; i++)
        {
            var frame       = animationClip.Frames[i];
            var lastFrame   = animationClip.Frames[i - 1];
            var motionFrame = new MotionFrame();

            motionFrame.Time = frame.Time;

            var joints = (from jp in frame.JointPoints
                          from jp2 in lastFrame.JointPoints
                          where jp.Name.Equals(jp2.Name)
                          select MakeMotionJoint(jp, jp2)).ToArray();

            foreach (var jt in joints)
            {
                var firstJt = firstMotionFrame.Joints.First(x => x.Name.Equals(jt.Name));
                jt.BaseRotation = firstJt.Rotation;
            }

            motionFrame.Joints = joints;

            var root = joints.First(x => x.Name.Equals(RootName));
            motionFrame.AngularVelocity = Vector3.Angle(Vector3.forward, root.Velocity) / 180f;
            motionFrame.Velocity        = root.Velocity.sqrMagnitude;
            motionFrame.Direction       = root.Velocity.normalized;
            GetClipTrajectoryData(motionFrame);

            motionClip.MotionFrames[i - 10] = motionFrame;
        }

        motionClip.MotionClipLengthInMilliseconds = animationClip.Frames.Last().Time;

        MotionClips.Add(motionClip);
    }
    private void Animate(AnimClip clip)
    {
        bool isDystopia = gameManager.GetState() == GameManager.State.DYSTOPIA;
        bool isUtopia   = gameManager.GetState() == GameManager.State.UTOPIA;

        string name = "";

        switch (clip)
        {
        case AnimClip.idle:
            if (isDystopia)
            {
                name = "dysto_idle";
            }
            else if (isUtopia)
            {
                name = "uto_idle";
            }
            break;

        case AnimClip.running:
            if (isDystopia)
            {
                name = "dysto_running";
            }
            else if (isUtopia)
            {
                name = "uto_running";
            }
            break;

        case AnimClip.jumping:
            if (isDystopia)
            {
                name = "dysto_jumping";
            }
            else if (isUtopia)
            {
                name = "uto_jumping";
            }
            break;

        case AnimClip.walled:
            if (isDystopia)
            {
                name = "dysto_wall";
            }
            else if (isUtopia)
            {
                name = "uto_wall";
            }
            break;
        }

        if (clip == currentAnimation)
        {
            // Start at same frame
            AnimatorStateInfo stateInfo  = GetComponent <Animator>().GetCurrentAnimatorStateInfo(0);
            float             completion = stateInfo.normalizedTime;
            float             duration   = stateInfo.length;
            float             frameTime  = (completion % duration);
            GetComponent <Animator>().Play(name, 0, frameTime);
        }
        else
        {
            // Simply play from start
            GetComponent <Animator>().Play(name);
        }

        currentAnimation = clip;
    }