コード例 #1
0
    int FindNearestShip(int playerIndex, ref WarsContent warsContent)
    {
        Dictionary <int, float> dict = new Dictionary <int, float>();
        int  i  = 1;
        Ship me = warsContent.getPlayersShip(playerIndex);
        Ship other;

        while ((other = warsContent.getPlayersShip(i)) != null)
        {
            if (i != playerIndex)
            {
                dict[i] = (me.Pos - other.Pos).Length();
            }
            i++;
        }
        if (dict.Count == 0)
        {
            return(0);
        }
        KeyValuePair <int, float> nearest = new KeyValuePair <int, float>(0, 0);

        foreach (KeyValuePair <int, float> kvp in dict)
        {
            if (nearest.Key == 0 || kvp.Value < nearest.Value)
            {
                nearest = kvp;
            }
        }
        return(nearest.Key);
    }
コード例 #2
0
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed ||
                Keyboard.GetState().IsKeyDown(Keys.Q))
            {
                this.Exit();
            }

            if (Keyboard.GetState().IsKeyDown(Keys.M))
            {
                warsContent.AddExplosion(1);
                warsContent.PlayExplosionSound();
            }

            /*
             * Keys[] keys = Keyboard.GetState().GetPressedKeys();
             * foreach(Keys key in keys)
             * {
             *  Console.WriteLine("Key pressed: {0}", key);
             * }
             */

            // The time since Update was called last.
            float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

            addShipCounter -= elapsed;
            if (Keyboard.GetState().IsKeyDown(Keys.Y) && addShipCounter <= 0)
            {
                addShipCounter = 0.3f;
                warsContent.AddShip(random.Next(4));
            }

            counter -= elapsed;
            if (counter <= 0)
            {
                counter = 3f;
                //Console.WriteLine("elapsed game time: {0}, elapsed real time: {1}", elapsed, (float)gameTime.ElapsedRealTime.TotalSeconds);
                if (warsContent.ships.Count == 1)
                {
                    warsContent.AddShip(random.Next(4));

/*
 *                  for (int i = 0; i < 50; i++)
 *                  {
 *                      warsContent.AddShip(random.Next(4));
 *                  }
 */
                }

                //warsContent.AddShip();
                //Console.WriteLine("Angle: {0}", MathHelper.ToDegrees(((Ship)warsContent.ships[0]).RotationAngle));
            }


            // Joystick
#if HAVE_LEGACY_DIRECTX
            try
            {
                // poll the joystick
                joystickDevice.Poll();
                // update the joystick state field
                joystickState = joystickDevice.CurrentJoystickState;
            }
            catch (Exception err)
            {
                // we probably lost connection to the joystick
                // was it unplugged or locked by another application?
                Console.WriteLine(err.Message);
            }
#endif

            // TODO: Add your update logic here
            foreach (Cat cat in warsContent.cats)
            {
                cat.RedShotSpacingCounter -= elapsed;
            }
            bool playerAbove1throttle = false;
            for (int player = 1; player <= warsContent.ships.Count; player++)
            {
                //Ship ship = (Ship)warsContent.ships[player];
                Ship ship = warsContent.getPlayersShip(player);

                Commands com = warsControl.GetPlayerCommands(player, ref warsContent
#if HAVE_LEGACY_DIRECTX
                                                             , ref joystickState
#endif
                                                             );
                if (com.left)
                {
                    ship.RotationAngle += 4 * elapsed;
                    //Console.WriteLine("RotationAngle: {0}", MathHelper.ToDegrees(ship.RotationAngle));
                }
                if (com.right)
                {
                    ship.RotationAngle -= 4 * elapsed;
                    //Console.WriteLine("RotationAngle: {0}", MathHelper.ToDegrees(ship.RotationAngle));
                }
                if (com.throttle)
                {
                    // Sound
                    if (player == 1)
                    {
                        warsContent.PlayEngineSound(player);
                    }
                    ship.Throttle = elapsed * ship.ThrottlePower; // NOTE NOTE NOTE TODO: Make dependent on time (elapsed)
                    //Console.WriteLine("Ship speed: {0}", ship.Speed.ToString());
                    warsContent.AddFume(player);
                    if (player > 1)
                    {
                        playerAbove1throttle = true;
                    }
                }
                else
                {
                    if (player == 1)
                    {
                        warsContent.PauseEngineSound(player);
                    }
                    ship.Throttle = 0;
                }

                ship.RedShotSpacingCounter -= elapsed;
                if (com.fire == 1)
                {
                    if (ship.RedShotSpacingCounter <= 0)
                    {
                        ship.RedShotSpacingCounter = ship.RedShotSpacing;
                        //Console.WriteLine("Shot!");
                        warsContent.AddShot(player);
                    }
                    if (player == 1)
                    {
                        foreach (Cat cat in warsContent.cats)
                        {
                            if (cat.RedShotSpacingCounter <= 0)
                            {
                                cat.RedShotSpacingCounter = cat.RedShotSpacing;
                                warsContent.AddCatShot(player, cat);
                            }
                        }
                    }
                }
            }

            if (playerAbove1throttle)
            {
                warsContent.PlayEngineSound(2);
            }
            else
            {
                warsContent.PauseEngineSound(2);
            }

            //            if(Keyboard.GetState().IsKeyDown(Keys.S)) {
            //                ship.Throttle -= 4 * elapsed;
            //            }

            foreach (Cat cat in warsContent.cats)
            {
                cat.RotationAngle    += 4 * elapsed;
                cat.PosRotationAngle -= 4 * elapsed;
                cat.Pos = warsContent.getPlayersShip(cat.Owner).Pos;
                //Vector2 goal = warsContent.getPlayersShip(cat.Owner).Pos + cat.PosRotationVector * cat.ShipDistance;
                cat.Pos += cat.PosRotationVector * cat.ShipDistance;
                //cat.Speed = goal;
                //cat.move();
            }

            foreach (Ship ship in warsContent.ships)
            {
                ship.updateSpeed();
            }
            for (int playerMinus1 = 0; playerMinus1 < warsContent.ships.Count; playerMinus1++)
            {
                if (playerMinus1 != 0)
                {
                    warsContent.getPlayersShip(playerMinus1 + 1).move();
                    warsContent.getPlayersShip(playerMinus1 + 1).Pos -= warsContent.getPlayersShip(1).Speed;
                }
            }

            foreach (WarsObject obj in warsContent.planetObjects)
            {
                obj.Speed = -warsContent.getPlayersShip(1).Speed;
                obj.move();
            }

            foreach (WarsObject obj in warsContent.bgObjects)
            {
                //obj.Speed = new Vector2(-ship.Speed.X, ship.Speed.Y);
                //obj.Speed = -((Ship)warsContent.ships[1]).Speed;
                obj.Speed = -warsContent.getPlayersShip(1).Speed;

                obj.move();
                Vector2 newPos = obj.Pos;
                newPos.X = newPos.X % Constants.RESOLUTION_WIDTH;
                newPos.Y = newPos.Y % Constants.RESOLUTION_HEIGHT;
                if (newPos.X < 0)
                {
                    newPos.X += Constants.RESOLUTION_WIDTH;
                }
                if (newPos.Y < 0)
                {
                    newPos.Y += Constants.RESOLUTION_HEIGHT;
                }
                obj.Pos = newPos;
            }

            foreach (WarsObject obj in warsContent.fumeObjects)
            {
                obj.move();
                //obj.Pos += new Vector2(-ship.Speed.X / Constants.FUME_SPEED_AFFECT_FACTOR, ship.Speed.Y / Constants.FUME_SPEED_AFFECT_FACTOR);
                //obj.Pos += new Vector2(-ship.Speed.X, ship.Speed.Y);
                //obj.Pos -= ((Ship)warsContent.ships[1]).Speed;
                obj.Pos -= warsContent.getPlayersShip(1).Speed;
            }
            for (int i = 0; i < warsContent.fumeObjects.Count; i++)
            {
                WarsObject obj = (WarsObject)warsContent.fumeObjects[i];
                //newPos.X = newPos.X % Constants.RESOLUTION_WIDTH;
                //newPos.Y = newPos.Y % Constants.RESOLUTION_HEIGHT;
                if (obj.updateTTL(elapsed))
                {
                    warsContent.fumeObjects.RemoveAt(i);
                }
                else
                {
                    if (obj.Pos.X < 0)
                    {
                        warsContent.fumeObjects.RemoveAt(i);
                    }
                    else if (obj.Pos.Y < 0)
                    {
                        warsContent.fumeObjects.RemoveAt(i);
                    }
                }
            }


            // Shots
            foreach (WarsObject obj in warsContent.shotObjects)
            {
                obj.move();
                //obj.Pos += new Vector2(-ship.Speed.X, ship.Speed.Y);
                //obj.Pos -= ((Ship)warsContent.ships[1]).Speed;
                obj.Pos -= warsContent.getPlayersShip(1).Speed;
            }
            for (int i = 0; i < warsContent.shotObjects.Count; i++)
            {
                WarsObject obj = (WarsObject)warsContent.shotObjects[i];
                if (obj.updateTTL(elapsed))
                {
                    warsContent.shotObjects.RemoveAt(i);
                }
            }
            // Collision detection (player 1 shoots others)
            for (int playerIndexMinus1 = 1; playerIndexMinus1 < warsContent.ships.Count; playerIndexMinus1++)
            {
                Ship           ship       = warsContent.getPlayersShip(playerIndexMinus1 + 1);
                BoundingSphere shipSphere = new BoundingSphere(new Vector3(ship.Pos, 0), (float)ship.Texture.Width / 2f);
                for (int i = 0; i < warsContent.shotObjects.Count; i++)
                {
                    WarsObject     obj        = (WarsObject)warsContent.shotObjects[i];
                    BoundingSphere shotSphere = new BoundingSphere(new Vector3(obj.Pos, 0), (float)warsContent.redShot.Height / 2f);
                    if (shotSphere.Intersects(shipSphere))
                    {
                        if (obj.Owner != playerIndexMinus1 + 1)
                        {
                            warsContent.AddExplosion(playerIndexMinus1 + 1);
                            warsContent.PlayExplosionSound();
                            warsContent.shotObjects.RemoveAt(i);
                            warsContent.ships.RemoveAt(playerIndexMinus1);
                            whiteFlash = true;
                            break;
                        }
                    }
                }
            }
            // Collision detection (player 1 gets shot)
            Ship           ship1       = warsContent.getPlayersShip(1);
            BoundingSphere ship1Sphere = new BoundingSphere(new Vector3(ship1.Pos, 0), (float)ship1.Texture.Width / 2f);
            for (int i = 0; i < warsContent.shotObjects.Count; i++)
            {
                WarsObject     obj        = (WarsObject)warsContent.shotObjects[i];
                BoundingSphere shotSphere = new BoundingSphere(new Vector3(obj.Pos, 0), (float)warsContent.redShot.Height / 2f);
                if (shotSphere.Intersects(ship1Sphere))
                {
                    if (obj.Owner != 1)
                    {
                        //                        warsContent.AddExplosion(1);
                        //                        warsContent.PlayExplosionSound();
                        warsContent.shotObjects.RemoveAt(i);
                        //                        warsContent.ships.RemoveAt(playerIndexMinus1);
                        warsContent.PlayImHit();
                        break;
                    }
                }
            }


            base.Update(gameTime);
        }
コード例 #3
0
    public Commands GetPlayerCommands(int playerIndex, ref WarsContent warsContent, ref Microsoft.DirectX.DirectInput.JoystickState joystickState)
    {
        Commands commands = new Commands();

        /*
         * if (true)//joystickState.X != 0)
         * {
         *  Console.WriteLine("joystickState.X = {0}", joystickState.X);
         * }
         * if (true)//joystickState.Y != 0)
         * {
         *  Console.WriteLine("joystickState.Y = {0}", joystickState.Y);
         * }
         */

        byte[] buttons = joystickState.GetButtons();

        /*
         * for (int i = 0; i < buttons.Length; i++)
         * {
         *  if (buttons[i] >= 128)
         *  {
         *      Console.WriteLine("Button {0} pressed!", i);
         *  }
         * }
         */

        Ship me = warsContent.getPlayersShip(playerIndex);

        if (me.ShipType == Constants.SHIP_TYPE_MANUALLY_CONTROLLED)
        {
            if (Keyboard.GetState().IsKeyDown(Keys.A) || joystickState.X == 0)
            {
                commands.left = true;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.D) || joystickState.X == 65535)
            {
                commands.right = true;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.W) || buttons[4] >= 128 || buttons[5] >= 128 || buttons[6] >= 128 || buttons[7] >= 128)
            {
                commands.throttle = true;
            }
            if (Keyboard.GetState().IsKeyDown(Keys.Space) || Keyboard.GetState().IsKeyDown(Keys.P) || buttons[0] >= 128 || buttons[1] >= 128 || buttons[2] >= 128 || buttons[3] >= 128)
            {
                commands.fire = 1;
            }
        }
        else if (me.ShipType == Constants.SHIP_TYPE_COMPUTER_CONTROLLED)
        {
            //int left = random.Next(2);
            //Ship me = warsContent.getPlayersShip(playerIndex);
            Ship other = warsContent.getPlayersShip(me.FightingPlayer);
            if (me.Counter <= 0) // find new ship to fight
            {
                me.Counter = 3;
                int newPlayerToFight = FindNearestShip(playerIndex, ref warsContent);
                if (newPlayerToFight != 0 && (other = warsContent.getPlayersShip(newPlayerToFight)) != null)
                {
                    me.FightingPlayer = newPlayerToFight;
                    Console.WriteLine("New player to fight: {0}", newPlayerToFight);
                }
                //else
                //{
                //    other = warsContent.getPlayersShip(1);
                //}
            }
            if (other == null)
            {
                other = warsContent.getPlayersShip(1);
            }
            //Ship other = warsContent.getPlayersShip(me.FightingPlayer);
            //Vector2 toOtherVec;
            toOtherVec = new Vector2();
            toOtherVec = other.Pos - me.Pos;

            Vector2 totSpeedVec  = other.Speed - me.Speed;
            float   toOtherAngle = GetVectorAngle(toOtherVec);
            Console.WriteLine("toOtherAngle: {0}", MathHelper.ToDegrees(toOtherAngle).ToString());
            float fromOtherAngle = MathHelper.WrapAngle(toOtherAngle - MathHelper.Pi);
            float speedAngle     = GetVectorAngle(totSpeedVec);
            float diffAngle      = MathHelper.WrapAngle(speedAngle - fromOtherAngle);
            if (diffAngle != 0)
            {
                float speedLength = totSpeedVec.Length();
                if (speedLength != 0)
                {
                    double sinDiffAngle = Math.Sin(diffAngle);
                    if (sinDiffAngle != 0)
                    {
                        double val = (double)(speedLength / Constants.REDSHOT_SPEED) * sinDiffAngle;
                        compensationAngle = -(float)Math.Asin(val);
                    }
                }
            }
            toOtherAngle += compensationAngle;
            //Console.WriteLine("compensationAngle: {0}", MathHelper.ToDegrees(compensationAngle));
            //Console.WriteLine("compensationAngle: {0}", MathHelper.ToDegrees(compensationAngle));
            //Console.WriteLine("GetVectorAngle: {0}, {1}", MathHelper.ToDegrees(toOtherAngle), MathHelper.ToDegrees(me.RotationAngle));
            if (me.RotationAngle - toOtherAngle > Constants.SMALLEST_ANGLE_DIFF_ACCEPT)
            {
                if (me.RotationAngle - toOtherAngle < MathHelper.Pi) // Handle wrap-around ("normal" case == < Pi ==> right)
                {
                    commands.right = true;
                }
                else
                {
                    commands.left = true;
                }
            }
            else if (toOtherAngle - me.RotationAngle > Constants.SMALLEST_ANGLE_DIFF_ACCEPT)
            {
                if (toOtherAngle - me.RotationAngle < MathHelper.Pi) // Handle wrap-around ("normal" case == < Pi ==> left)
                {
                    commands.left = true;
                }
                else
                {
                    commands.right = true;
                }
            }

            if (toOtherVec.Length() > 400 && (me.Speed.Length() <= other.Speed.Length() || Math.Abs(GetVectorAngle(me.Speed) - GetVectorAngle(toOtherVec)) > MathHelper.PiOver4))
            {
                commands.throttle = true;
            }

            if (random.Next(10) == 5)
            {
                commands.fire = 1;
            }
        }
        return(commands);
    }