Beispiel #1
0
        private void Move()
        {
            //Trailing smoke particles
            if (tiLifetimeCounter.ElapsedSoFar > 0.5 - GM.r.FloatBetween(0.1f, 0.3f))
            {
                Vector2 offset = new Vector2(20 * RotationHelper.MyDirection(this, 180).X, 20 * RotationHelper.MyDirection(this, 180).Y);
                new SmokeParticle(Position2D + offset, Vector3.Zero, Vector2.Zero, 0.2f);
                GM.eventM.AddTimer(tiSmokeCounter = new Event(0.5f, "Smoke Counter"));
            }

            if (target.Dead)
            {
                RotationHelper.VelocityInCurrentDirection(this, speed, 0);
            }
            else
            {
                //turnDir = -1, anticlockwise; 0, none; 1, clockwise
                int turnDir = (int)RotationHelper.AngularDirectionTo(this, target.Position, 0, false);

                //Direction to add velocity to, additional angle is 90 * turnDir so -90 for anticlockwise, 90 for clockwise, straight ahead for none
                Vector3 velDir = RotationHelper.MyDirection(this, 90 * turnDir);

                Velocity  = RotationHelper.MyDirection(this, 0) * speed;
                Velocity += velDir * (turnAmount / 5);
                RotationHelper.FaceVelocity(this, DirectionAccuracy.free, false, 0);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Code to run each tick
        /// </summary>
        private void Tick()
        {
            //DEBUG
            //GM.textM.Draw(FontBank.arcadePixel,
            //    "Hull Front  " + hitBoxHullFront.Health + "~Hull Back   " + hitBoxHullBack.Health + "~Hull Left   " + hitBoxHullLeft.Health + "~Hull Right  " + hitBoxHullRight.Health +
            //    "~Sail Front  " + hitBoxSailFront.Health + "~Sail Middle " + hitBoxSailMiddle.Health + "~Sail Back   " + hitBoxSailBack.Health, GM.screenSize.Width - 100, 100, TextAtt.TopRight);
            //GM.textM.Draw(FontBank.arcadePixel, "Crew: " + CrewNum, GM.screenSize.Width - 150, 50, TextAtt.TopRight);
            //GM.textM.Draw(FontBank.arcadePixel, "State: " + state, GM.screenSize.Width - 150, 75, TextAtt.TopRight);

            if (GM.eventM.Elapsed(stateTick))
            {
                state = StateMachine();
            }

            if (state == 1 || state == 3) //Attacking or boarding
            {
                sailAmount = 2;
                Point movePoint = Point.Zero;

                //Direction vectors for front and right of player
                Vector2 playerFront       = RotationHelper.Direction2DFromAngle(player.RotationAngle, 0);
                Vector2 playerRight       = RotationHelper.Direction2DFromAngle(player.RotationAngle, 90);
                Vector2 directionToPlayer = Vector2.Normalize(Position2D - player.Position2D);

                ////1 if front of ships opposite, -1 otherwise.
                //float angleBetweenFront = RotationHelper.BearingTo(RotationHelper.Direction2DFromAngle(RotationAngle, 0), playerFront, DirectionAccuracy.free, 0) - RotationAngle;
                //int frontOpposite = -1;
                //if (angleBetweenFront > 135 || angleBetweenFront < -135)
                //{
                //    frontOpposite = 1;
                //}

                float angleFromPlayer = RotationHelper.BearingTo(Position2D, player.Position2D, DirectionAccuracy.free, 0) - RotationAngle;
                if (angleFromPlayer < -180)
                {
                    angleFromPlayer += 360;
                }

                //1 if side of ships opposite, -1 otherwise.
                int   playerOnRight    = -1;
                float angleBetweenSide = RotationHelper.BearingTo(Vector2.Normalize(Position2D - RotationHelper.Direction2DFromAngle(RotationAngle, 0)), playerRight, DirectionAccuracy.free, 0) - RotationAngle;
                if (angleFromPlayer > 0)
                {
                    playerOnRight = 1;
                }

                //See if player is aligned with the midpoint of the AI's side.
                sideFaceSprite.Position2D    = Position2D;
                sideFaceSprite.RotationAngle = RotationAngle + (90 * playerOnRight);
                float alignment = RotationHelper.AngularDirectionTo(sideFaceSprite, player.Position, 0, false);

                bool readyToFire = false;
                if (alignmentLastTick != alignment /* && ((angleFromPlayer < -80 && angleFromPlayer > -100) || angleFromPlayer > 80 && angleFromPlayer < 100)*/)
                {
                    readyToFire = true;
                }
                alignmentLastTick = alignment;

                if ((state == 1 && Vector2.DistanceSquared(Position2D, player.Position2D) > 100000) || (state == 3 && Vector2.DistanceSquared(Position2D, player.Position2D) > 125000)) //Out of range
                {
                    movePoint = PointHelper.PointFromVector2(player.Position2D + (playerRight * 100 * playerOnRight));
                }
                else //Within range
                {
                    sailAmount = 1;
                    //movePoint = PointHelper.PointFromVector2(player.Position2D + (playerRight * 100) + (playerFront * 1000 * frontOpposite));
                    movePoint = PointHelper.PointFromVector2(Position2D + 100 * RotationHelper.Direction2DFromAngle(RotationAngle, 90 * alignment * playerOnRight));

                    if (readyToFire || isBoarded || isBoarding)
                    {
                        if (angleFromPlayer > 45 && angleFromPlayer < 135)
                        {
                            Fire(false, shotTypeLeft);
                        }
                        else if (angleFromPlayer < -45 && angleFromPlayer > -135)
                        {
                            Fire(true, shotTypeRight);
                        }
                    }
                }
                //DEBUG
                GM.textM.Draw(FontBank.arcadePixel, "Alignment" + angleFromPlayer + "~Rotation" + RotationAngle, GM.screenSize.Width - 150, 10, TextAtt.TopRight);

                MoveToPoint(movePoint);
            }
            if (state == 2) //Retreating
            {
                MoveToPoint(PointHelper.PointFromVector2(Position2D + (Vector2.Normalize(Position2D - player.Position2D) * 200)));
            }
            if (state == 3)//Boarding
            {
            }
        }
Beispiel #3
0
        /// <summary>
        /// Accelerates the ship towards point and keeps the ship from sliding sideways
        /// </summary>
        /// <param name="point">Point to move towards</param>
        internal void MoveToPoint(Point point)
        {
            Vector2 movePos = PointHelper.Vector2FromPoint(point);

            //Stop moving once close enough to movePos, or when boarding is in progress
            if ((Vector2.DistanceSquared(Position2D, movePos) + 5000 > Height * Height) && !isBoarded && !isBoarding)
            {
                if (isPlayer)
                {
                    moveLocSprite.Visible = true;
                }
                moveLocSprite.Position2D = movePos;

                //DEBUG
                //if (!isPlayer)
                //{
                //    moveLocSprite.Visible = true;
                //    moveLocSprite.Wash = Color.Red;
                //}


                if (sailAmount == 0)
                {
                    if (smoothRotationVelocity > 0)
                    {
                        smoothRotationVelocity -= 0.1f;
                        RotationVelocity        = smoothRotationVelocity;
                    }
                    if (smoothRotationVelocity < 0)
                    {
                        smoothRotationVelocity += 0.1f;
                        RotationVelocity        = smoothRotationVelocity;
                    }
                }
                else
                {
                    //Calculations for turning
                    int dirMul = (int)RotationHelper.AngularDirectionTo(this, new Vector3(movePos, 0), 0, false);
                    smoothRotationVelocity += 0.05f * dirMul;
                    if ((dirMul > 0 && smoothRotationVelocity > 5 * dirMul) || (dirMul < 0 && smoothRotationVelocity < 5 * dirMul))
                    {
                        smoothRotationVelocity = 5 * dirMul;
                    }
                    RotationVelocity = smoothRotationVelocity;

                    Vector3 currentVel = Velocity;
                    currentVel.Normalize();
                    currentVel = Position + currentVel;
                    float velOffsetAngle = RotationHelper.AngularDirectionTo(this, currentVel, 0, false);

                    //Calculations for wind speed multiplier
                    float velFromWindAngle = (RotationAngle - GameSetup.WeatherController.WindDir) % 360;
                    if (velFromWindAngle < 0) //Absolute value
                    {
                        velFromWindAngle = -velFromWindAngle;
                    }
                    if (velFromWindAngle > 180) //Keep between 0 and 180
                    {
                        velFromWindAngle = 360 - velFromWindAngle;
                    }//Create multiplier that's <1
                    velFromWindAngle = (1 / (velFromWindAngle + 100) * 50);

                    //Keep from sliding to the side
                    if (velOffsetAngle > 0)
                    {
                        Velocity += RotationHelper.MyDirection(this, -90) * 0.5f;
                    }
                    else
                    {
                        Velocity += RotationHelper.MyDirection(this, 90) * 0.5f;
                    }

                    //Add velocity
                    if (sailAmount == 1)
                    {
                        Velocity += RotationHelper.MyDirection(this, 0) * 0.1f * velFromWindAngle * sailDamageSpeedMul;
                    }
                    if (sailAmount == 2)
                    {
                        Velocity += RotationHelper.MyDirection(this, 0) * 0.2f * velFromWindAngle * sailDamageSpeedMul;//Sort this out so it doesnt divide by 0
                    }
                }
            }
            else if (isBoarding || isBoarded)
            {
                Ship target = GameSetup.Opponent;
                if (!isPlayer)
                {
                    target = GameSetup.Player;
                }

                float angleBetween = RotationAngle - target.RotationAngle;
                bool  opposite     = true;
                if (angleBetween > -90 && angleBetween < 90)
                {
                    opposite = false;
                }

                //Angle ships parallel to eachother.
                if (opposite &&
                    Vector2.DistanceSquared(hitBoxHullFront.Position2D, target.hitBoxHullBack.Position2D)
                    > Vector2.DistanceSquared(hitBoxHullBack.Position2D, target.hitBoxHullFront.Position2D))
                {
                    RotationVelocity = 2;
                }
                else if (!opposite &&
                         Vector2.DistanceSquared(hitBoxHullFront.Position2D, target.hitBoxHullFront.Position2D)
                         > Vector2.DistanceSquared(hitBoxHullBack.Position2D, target.hitBoxHullBack.Position2D))
                {
                    RotationVelocity = 2;
                }
                else
                {
                    RotationVelocity = -2;
                }

                //Move ships towards eachother
                if (Vector2.DistanceSquared(this.Position2D, target.Position2D) > 10000)
                {
                    Vector2 velDir = Vector2.Normalize(Position2D - target.Position2D);
                    Velocity2D -= 0.01f * velDir;
                }
                else
                {
                    GameSetup.BoardingInProgress = true;
                    Velocity2D = Vector2.Zero;
                }

                //DEBUG
                GM.textM.Draw(FontBank.arcadePixel, Convert.ToString(opposite), 200, 200);
            }
            else
            {
                if (!isPlayer)
                {
                    moveTargetReached = true;
                }
                else
                {
                    GameSetup.Player.MovePath.Dequeue();
                    GameSetup.Player.ArrowQueue.Dequeue();
                }
                moveLocSprite.Visible = false;
                RotationVelocity      = 0;
            }
        }