Exemplo n.º 1
0
        public override void move(FrameEvent evt, Environment env)
        {
            Stone stoneTarget = null;
            if (state == "free")
            {
                stoneTarget = findTarget(env);
            }
            if (!mWalking && mAnimationState.HasEnded)
            {
                mAnimationState.Enabled = false;
                mAnimationState = ent.GetAnimationState("Walk");
                mAnimationState.Loop = true;
                mAnimationState.Enabled = true;
                mWalking = true;
            }
            #region has a stone target
            if (stoneTarget != null)
            {
                //mWalking = false;

                mDestination = mWalkList.First.Value;
                mDirection = mDestination - Node.Position;
                mDistance = mDirection.Normalise();
                float move = (MWalkSpeed*walkSpeedFactor) * evt.timeSinceLastFrame;
                mDistance -= move;

                if (mDistance <= 0.2f)
                {
                    mAnimationState = ent.GetAnimationState("Backflip");
                    mAnimationState.Enabled = true;
                    mAnimationState.Loop = false;

                    mWalking = false;
                    stoneTarget.Node.Parent.RemoveChild(stoneTarget.Node);
                    node.AddChild(stoneTarget.Node);
                    stoneTarget.Node.Position = new Vector3(0, 200, 0);
                    //must set stone to unavailable
                    mWalkList.RemoveFirst();
                    mWalkList.AddFirst(castle);
                    this.state = "stone";
                    env.setCarriedStone(carriedStoneName);
                }
                if (env.outOfGround(Node.Position))
                {

                    //set our node to the destination we've just reached & reset direction to 0
                    Node.Position = lastPosition;
                    mDirection = Vector3.ZERO;
                    mWalking = false;

                }
                else
                {
                    lastPosition = Node.Position;
                    //Rotation code goes here
                    Vector3 src = Node.Orientation * forward;
                    if ((1.0f + src.DotProduct(mDirection)) < 0.0001f)
                    {
                        Node.Yaw(180.0f);
                    }
                    else
                    {
                        Quaternion quat = src.GetRotationTo(mDirection);
                        Node.Rotate(quat);
                    }
                    //movement code goes here
                    Node.Translate(mDirection * move);
                }
                //Update the Animation State.
                mAnimationState.AddTime(evt.timeSinceLastFrame * (MWalkSpeed*walkSpeedFactor) / 20);
            }
            #endregion
            #region carry a stone
            else if (state == "stone")
            {
                mDestination = mWalkList.First.Value;
                mDirection = mDestination - Node.Position;
                mDistance = mDirection.Normalise();
                float move = MWalkSpeed * evt.timeSinceLastFrame;
                mDistance -= move;

                if (mDistance <= 0.2f)
                {
                    mWalkList.RemoveFirst();
                    mAnimationState = ent.GetAnimationState("Backflip");
                    mAnimationState.Enabled = true;
                    mAnimationState.Loop = false;
                    mWalking = false;
                    try
                    {
                        Node temp = node.GetChild("stoneNode"+carriedStoneName);
                        //node.RemoveChild(0);
                        node.RemoveAllChildren();
                        node.Parent.AddChild(temp);
                        temp.Position = node.Position;
                        env.setUncarriedStone(carriedStoneName);
                    }
                    catch
                    {

                    }
                    this.state = "free";
                }
                if (env.outOfGround(Node.Position))
                {
                    //set our node to the destination we've just reached & reset direction to 0
                    Node.Position = lastPosition;
                    mDirection = Vector3.ZERO;
                    mWalking = false;

                }
                else
                {
                    lastPosition = Node.Position;
                    //Rotation code goes here
                    Vector3 src = Node.Orientation * forward;
                    if ((1.0f + src.DotProduct(mDirection)) < 0.0001f)
                    {
                        Node.Yaw(180.0f);
                    }
                    else
                    {
                        Quaternion quat = src.GetRotationTo(mDirection);
                        Node.Rotate(quat);
                    }
                    //movement code goes here
                    Node.Translate(mDirection * move);
                }
                //Update the Animation State.
                mAnimationState.AddTime(evt.timeSinceLastFrame * MWalkSpeed / 20);
            }
            #endregion
            #region no stone targeted
            else if (stoneTarget == null)
            {
                if (mWalkList.Count == 0)
                {
                    float x = (float)rnd.NextDouble() + (float)rnd.Next(1000) - (float)rnd.Next(1000);
                    float z = (float)rnd.NextDouble() + (float)rnd.Next(1000) - (float)rnd.Next(1000);
                    mWalkList.AddFirst(new Vector3(x, 0, z));
                }

                mDestination = mWalkList.First.Value;
                mDirection = mDestination - Node.Position;
                mDistance = mDirection.Normalise();
                float move = MWalkSpeed * evt.timeSinceLastFrame;
                mDistance -= move;
                mWalking = true;

                if (mDistance <= 0.2f)
                {
                    mWalkList.RemoveFirst();
                }
                if (env.outOfGround(Node.Position))
                {

                    //set our node to the destination we've just reached & reset direction to 0
                    Node.Position = lastPosition;
                    mDirection = Vector3.ZERO;
                    mWalking = false;

                }
                else
                {
                    lastPosition = Node.Position;
                    //Rotation code goes here
                    Vector3 src = Node.Orientation * forward;
                    if ((1.0f + src.DotProduct(mDirection)) < 0.0001f)
                    {
                        Node.Yaw(180.0f);
                    }
                    else
                    {
                        Quaternion quat = src.GetRotationTo(mDirection);
                        Node.Rotate(quat);
                    }
                    //movement code goes here
                    Node.Translate(mDirection * move);
                }
                //Update the Animation State.
                mAnimationState.AddTime(evt.timeSinceLastFrame * MWalkSpeed / 20);
            }
            #endregion
        }