Esempio n. 1
0
    public void AddCatShot(int playerIndex, Cat cat)
    {
        float posFactor = 30f;

        Ship ship = getPlayersShip(playerIndex);

        cat.TotShotsFired += 1;
        cat.Counter       -= 1;

        Vector2 shotPos   = cat.Pos + ship.RotationVector * posFactor;
        Vector2 shotSpeed = ship.RotationVector * Constants.REDSHOT_SPEED;

        shotSpeed += ship.Speed;
        WarsObject newShotObj;

        //if (playerIndex == 1)
        //{
        //    newShotObj = new WarsObject(ref blueShot, shotPos, shotSpeed, ship.RotationAngle);
        //    PlayBlueShotSound();
        //}
        //else
        {
            newShotObj = new WarsObject(ref redShot, shotPos, shotSpeed, ship.RotationAngle);
            PlayShotSound();
        }
        newShotObj.TTL = Constants.REDSHOT_TTL;
        newShotObj.DrawCompensationRotationAngle = MathHelper.PiOver2;
        newShotObj.Owner = playerIndex;
        shotObjects.Add(newShotObj);
        latestShotSpeedVector = shotSpeed;
    }
Esempio n. 2
0
    public void AddExplosion(int playerIndex)
    {
        //Ship ship = (Ship)ships[playerIndex];
        Ship ship = getPlayersShip(playerIndex);

        for (int i = 0; i < Constants.EXPLOSION_DEBRIS_COUNT; i++)
        {
            Vector2 expPos      = ship.Pos + getRandomVector(ship1.Width);
            Vector2 debrisSpeed = ship.Speed;
            float   angle       = MathHelper.ToRadians((float)random.Next(360));
            debrisSpeed += GetUnitVectorFromAngle(angle) * (Constants.EXPLOSION_DEBRIS_SPEED - Constants.EXPLOSION_DEBRIS_SPEED_VARIANCE + random.Next((int)(2000f * Constants.EXPLOSION_DEBRIS_SPEED_VARIANCE)) / 1000f);
            WarsObject newDebris = new WarsObject(ref fume, expPos, debrisSpeed, ship.RotationAngle);
            newDebris.TTL = Constants.EXPLOSION_DEBRIS_TTL - Constants.EXPLOSION_DEBRIS_TTL_VARIANCE + random.Next((int)(2000f * Constants.EXPLOSION_DEBRIS_TTL_VARIANCE)) / 1000f;
            fumeObjects.Add(newDebris);
        }
    }
Esempio n. 3
0
    public void AddFume(int playerIndex)
    {
        float posFactor   = 20f;
        float speedFactor = Constants.FUME_SPEED_FACTOR;

        //Ship ship = (Ship)ships[playerIndex];
        Ship ship = getPlayersShip(playerIndex);

        for (int i = 0; i < Constants.FUME_COUNT; i++)
        {
            Vector2 fumePos   = ship.Pos - ship.RotationVector * posFactor;
            Vector2 fumeSpeed = -ship.RotationVector * speedFactor;
            fumeSpeed += ship.Speed;
            fumePos   += getRandomVector(3);
            fumeSpeed += getRandomVector(2); // randomize the direction (velocity) somewhat
            WarsObject newFumeObj = new WarsObject(ref fume, fumePos, fumeSpeed);
            newFumeObj.TTL = Constants.FUME_TTL;
            fumeObjects.Add(newFumeObj);
        }
    }
Esempio n. 4
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);
        }