Ejemplo n.º 1
0
        public override void Update(GameTime gameTime)
        {
            if (pathIndex < path.nodes.Count - 1)
            {
                targetPos = path.GetNextPointOnPath(Pos, ref pathIndex);
                Vector2 dist = targetPos - Pos;
                if (dist != Vector2.Zero)
                {
                    velocity += Vector2.Normalize(dist) * GetSpeed();

                    float targetOrientation = (float)Math.Atan2(dist.Y, dist.X);
                    float angleDist         = Calculate.AngleDistance(orientation + orientationVelocity * 10f, targetOrientation);

                    float maxSteer = GetSpeed() * 0.1f;

                    if (Math.Abs(angleDist) > maxSteer)
                    {
                        angleDist = Math.Sign(angleDist) * maxSteer;
                    }

                    orientationVelocity += (angleDist - orientationVelocity) * 0.1f;// (angleDist - orientationVelocity) * 0.5f;////Math.Sign(angleDist) * Math.Max(0.1f, angleDist;
                }
            }
            base.Update(gameTime);
        }
Ejemplo n.º 2
0
        public void UpdateEnd(int _resX, int _resY)
        {
            resX = _resX;
            resY = _resY;

            if (zoomControl)
            {
                float cZoom = 1f;

                if (Input.mbWheel > 0)
                {
                    if (!minZoom.HasValue || targetZoom > minZoom)
                    {
                        if ((!zoomIncrementWithShift || !Input.leftShift.down || targetZoom < 2) && (!zoomUseWholeNumbersOverOne || targetZoom < 2))
                        {
                            cZoom = 1 / zoomStep;
                        }
                        else
                        {
                            cZoom = (targetZoom - 1) / targetZoom;
                        }
                    }
                }
                else if (Input.mbWheel < 0)
                {
                    if (!maxZoom.HasValue || targetZoom < maxZoom)
                    {
                        if ((!zoomIncrementWithShift || !Input.leftShift.down || targetZoom < 1) && (!zoomUseWholeNumbersOverOne || targetZoom < 1))
                        {
                            cZoom = zoomStep;
                        }
                        else
                        {
                            cZoom = (targetZoom + 1) / targetZoom;
                        }
                    }
                }

                if (cZoom != 1)
                {
                    if (zoomControlToMouse)
                    {
                        Vector2 mouseDist = mousePos - pos;
                        mouseDist /= cZoom;
                        pos        = targetPos = mousePos - mouseDist;
                    }

                    targetZoom *= cZoom;
                }
            }


            if (middleMouseMoveControl && Input.mbMiddle.down)
            {
                Vector2 camMove = mousePos - mousePosPast;

                targetPos -= camMove;
            }

            if (resetWithDoubleMiddleMouse)
            {
                if (Input.mbMiddle.pressedDouble)
                {
                    ResetTransform();
                }
            }

            if (arrowMoveControl && !Input.leftControl.down)
            {
                if (Input.left.down)
                {
                    targetPos.X -= arrowMoveSpeed / zoom;
                }
                if (Input.right.down)
                {
                    targetPos.X += arrowMoveSpeed / zoom;
                }
                if (Input.up.down)
                {
                    targetPos.Y -= arrowMoveSpeed / zoom;
                }
                if (Input.down.down)
                {
                    targetPos.Y += arrowMoveSpeed / zoom;
                }
            }

            if (boundsMouseMoveControl && (boundsMoveFunc == null || boundsMoveFunc()))
            {
                if (Input.mbPos.X <= 0)
                {
                    targetPos.X -= arrowMoveSpeed / zoom;
                }
                if (Input.mbPos.X >= resX - 1)
                {
                    targetPos.X += arrowMoveSpeed / zoom;
                }
                if (Input.mbPos.Y <= 0)
                {
                    targetPos.Y -= arrowMoveSpeed / zoom;
                }
                if (Input.mbPos.Y >= resY - 1)
                {
                    targetPos.Y += arrowMoveSpeed / zoom;
                }
            }


            #region pos

            Vector2 move = targetPos - pos;
            pos += move * moveSpeed;

            #endregion

            float dist;

            #region rotation

            if (targetRotation < 0)
            {
                targetRotation = MathHelper.TwoPi - (-targetRotation % MathHelper.TwoPi);
            }

            dist = targetRotation - rotation;
            if (Math.Abs(dist) > Math.PI)
            {
                dist = -Math.Sign(dist) * ((float)MathHelper.TwoPi - Math.Abs(dist));
            }

            if (partRotation)
            {
                if (rotationSpeed < 1)
                {
                    rotation += dist * rotationSpeed;

                    if (rotation < 0)
                    {
                        rotation = MathHelper.TwoPi - (-rotation % MathHelper.TwoPi);
                    }
                    else if (rotation > MathHelper.TwoPi)
                    {
                        rotation = (rotation % MathHelper.TwoPi);
                    }
                }
                else
                {
                    rotation = targetRotation;
                }
            }
            else
            {
                if (dist != 0)
                {
                    rotation += rotationSpeed * Math.Sign(dist);

                    if (rotation < 0)
                    {
                        rotation = MathHelper.TwoPi - (-rotation % MathHelper.TwoPi);
                    }
                    else if (rotation > MathHelper.TwoPi)
                    {
                        rotation = (rotation % MathHelper.TwoPi);
                    }

                    if (Math.Sign(Calculate.AngleDistance(rotation, targetRotation)) != Math.Sign(dist))
                    {
                        rotation = targetRotation;
                    }
                }
            }

            #endregion

            #region zoom

            if (minZoom.HasValue && targetZoom < minZoom.Value)
            {
                targetZoom = minZoom.Value;
            }
            if (maxZoom.HasValue && targetZoom > maxZoom.Value)
            {
                targetZoom = maxZoom.Value;
            }

            if (zoomSpeed < 1)
            {
                float dz = (targetZoom / zoom);

                if (dz > 1 || !zoomSpeedOut.HasValue)
                {
                    dz = (float)Math.Pow(dz, zoomSpeed);
                }
                else
                {
                    dz = (float)Math.Pow(dz, zoomSpeedOut.Value);
                }
                zoom *= dz;

                //dist = targetZoom - zoom;
                //zoom += dist * zoomSpeed;
            }
            else
            {
                zoom = targetZoom;
            }

            #endregion

            if (roomBoundary != null)
            {
                SetCameraInBoundary();
            }

            UpdateMatrix();
        }
Ejemplo n.º 3
0
        protected override EnvBumper.Action GetAction()
        {
            //Pos
            //Velocity
            //enemies[0].Pos
            //enemies[0].Velocity

            Vector2 dist = enemies[0].Pos - Pos;


            float myAngle = (float)Math.Atan2(dist.Y, dist.X);

            float distToCenter = Pos.Length();

            float targetToCenter = EnvBumper.RADIUS * 0.5f;

            myAngle  = (float)Math.Atan2(-Pos.Y, -Pos.X);
            myAngle += MathHelper.PiOver2 * 0.25f;

            float angleToCenter = (float)Math.Atan2(-Pos.Y, -Pos.X);
            float angleOrtho    = angleToCenter + MathHelper.PiOver2;


            float lerp = Velocity.Length() * 6f;

            lerp    = Math.Min(1, lerp);
            lerp    = 1 - lerp;
            myAngle = angleToCenter - MathHelper.PiOver2 * lerp;


            Vector2 predictedPos = Predict(Pos, Velocity);

            //float a = Velocity.X;
            //float b = Velocity.Y;
            //float x = Pos.X;
            //float y = Pos.Y;
            //float r = EnvBumper.RADIUS;
            //float t = (float)Math.Sqrt(a * a * (r * r - y * y) + 2 * a * b * x * y + b * b * (r * r - x * x) + a * x + b * y) / (a * a + b * b);
            //Console.WriteLine(t);

            //if (frames > 60 * 5)
            {
                if (goAway <= 0)
                {
                    Vector2 target = Predict(enemies[0].Pos, enemies[0].Velocity);// enemies[0].Pos + enemies[0].Velocity * Math.Min(200, enemies[0].Velocity.Length() * 80f);

                    dist = target - predictedPos;

                    myAngle = (float)Math.Atan2(dist.Y, dist.X);

                    float angleFromCenterToEnemy;

                    Vector2 myGoal   = enemies[0].Pos * 0.5f;
                    Vector2 goalDist = myGoal - predictedPos;
                    angleFromCenterToEnemy = (float)Math.Atan2(goalDist.Y, goalDist.X);
                    float angleDist = Calculate.AngleDistance(myAngle, angleFromCenterToEnemy);
                    myAngle += angleDist * 0.25f;
                    float vChange = Vector2.Distance(lastVelocity, Velocity);
                    Console.WriteLine(vChange);
                    if (vChange > 0.1f && Vector2.Dot(Velocity, dist) < 0)
                    {
                        //goAway = 30;
                    }
                }
                else
                {
                    // myAngle = (float)Math.Atan2(-dist.Y, -dist.X);
                    goAway--;
                }
            }


            if (distToCenter > EnvBumper.RADIUS * 0.6f)
            {
                myAngle = angleToCenter;
            }
            else if (Velocity.Length() > 0.2f)
            {
                myAngle = (float)Math.Atan2(-Velocity.Y, -Velocity.X);
            }

            frames++;

            lastVelocity = Velocity;
            return(new EnvBumper.Action()
            {
                angle = myAngle,
                accelerate = true
            });
        }