Пример #1
0
        public void SetLookDirection(Vec3 pos)
        {
            Vec2 diff = pos.ToVec2() - Position.ToVec2();

            if (diff == Vec2.Zero)
            {
                return;
            }

            Radian dir = MathFunctions.ATan16(diff.Y, diff.X);

            float halfAngle = dir * 0.5f;
            Quat  rot       = new Quat(new Vec3(0, 0, MathFunctions.Sin16(halfAngle)), MathFunctions.Cos16(halfAngle));

            Rotation = rot;
        }
Пример #2
0
        void TickMove()
        {
            //path find control
            {
                if (pathFindWaitTime != 0)
                {
                    pathFindWaitTime -= TickDelta;
                    if (pathFindWaitTime < 0)
                    {
                        pathFindWaitTime = 0;
                    }
                }

                if (pathFoundedToPosition != MovePosition.ToVec2() && pathFindWaitTime == 0)
                {
                    path.Clear();
                }

                if (path.Count == 0)
                {
                    if (pathFindWaitTime == 0)
                    {
                        if (DoPathFind())
                        {
                            pathFoundedToPosition = MovePosition.ToVec2();
                            pathFindWaitTime      = .5f;
                        }
                        else
                        {
                            pathFindWaitTime = 1.0f;
                        }
                    }
                }
            }

            if (path.Count == 0)
            {
                return;
            }

            //line movement to path[ 0 ]
            {
                Vec2 destPoint = path[0];

                Vec2 diff = destPoint - Position.ToVec2();

                if (diff == Vec2.Zero)
                {
                    path.RemoveAt(0);
                    return;
                }

                Radian dir = MathFunctions.ATan16(diff.Y, diff.X);

                float halfAngle = dir * 0.5f;
                Quat  rot       = new Quat(new Vec3(0, 0, MathFunctions.Sin16(halfAngle)),
                                           MathFunctions.Cos16(halfAngle));
                Rotation = rot;

                Vec2 dirVector = diff.GetNormalizeFast();
                Vec2 dirStep   = dirVector * (Type.MaxVelocity * TickDelta);

                Vec2 newPos = Position.ToVec2() + dirStep;

                if (Math.Abs(diff.X) <= Math.Abs(dirStep.X) && Math.Abs(diff.Y) <= Math.Abs(dirStep.Y))
                {
                    //unit at point
                    newPos = path[0];
                    path.RemoveAt(0);
                }

                GridPathFindSystem.Instance.RemoveObjectFromMotionMap(this);

                bool free;
                {
                    float radius     = Type.Radius;
                    Rect  targetRect = new Rect(newPos - new Vec2(radius, radius), newPos + new Vec2(radius, radius));

                    free = GridPathFindSystem.Instance.IsFreeInMapMotion(targetRect);
                }

                GridPathFindSystem.Instance.AddObjectToMotionMap(this);

                if (free)
                {
                    float newZ = GridPathFindSystem.Instance.GetMotionMapHeight(newPos) + Type.Height * .5f;
                    Position = new Vec3(newPos.X, newPos.Y, newZ);
                }
                else
                {
                    path.Clear();
                }
            }
        }