Exemplo n.º 1
0
        public override void Update()
        {
            if (target == null)
            {
                if (head != null)
                {
                    target = head;
                }
                else
                {
                    target = Scene.GetEntity <Player>();
                }
            }

            if (head == null)
            {
                Vector2 toTarget     = GetTargetPos() - new Vector2(X, Y);
                var     newAngle     = Util.RAD_TO_DEG * (float)Math.Atan2(-toTarget.Y, toTarget.X);
                var     angleDiff    = ((((newAngle - sprite.Angle - angleOffset) % 360) + 540) % 360) - 180;
                var     rotateAmount = Util.Clamp(angleDiff, -dirStepAmount, dirStepAmount);
                velocity     = Util.Rotate(velocity, rotateAmount);
                sprite.Angle = (float)Math.Atan2(-velocity.Y, velocity.X) * Util.RAD_TO_DEG - angleOffset;
            }
            else
            {
                velocity     = GetTargetPos() - new Vector2(X, Y);
                sprite.Angle = (float)Math.Atan2(-velocity.Y, velocity.X) * Util.RAD_TO_DEG - angleOffset;
            }

            velocity.Normalize();
            velocity *= speed;
            //velocity = Vector2.Zero;

            base.Update();

            if (health <= 0)
            {
                return;
            }

            if (head == null)
            {
            }
            else if (personalSpace.Overlap(X, Y, head.personalSpace))
            {
                Vector2 direction = GetTargetPos() - new Vector2(X, Y);
                direction.Normalize();
                direction *= spaceAmount;
                X          = GetTargetPos().X - direction.X;
                Y          = GetTargetPos().Y - direction.Y;
            }
        }