Beispiel #1
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
            {
            }
        }