Esempio n. 1
0
        /// <summary>
        /// Test render level background with landscape objects.
        /// </summary>
        public static void TestRenderLevelBackground()
        {
            Mission dummyMission = null;

            TestGame.Start("TestRenderLevelBackground",
                delegate
                {
                    dummyMission = new Mission();
                },
                delegate
                {
                    dummyMission.RenderLevelBackground(
                        BaseGame.TotalTimeMs / 33.0f);
                });
        }
Esempio n. 2
0
        /// <summary>
        /// Render unit, returns false if we are done with it.
        /// Has to be removed then. Else it updates just position and AI.
        /// </summary>
        /// <returns>True if done, false otherwise</returns>
        public bool Render(Mission mission)
        {
            #region Skip if out of visible range
            float distance = Mission.LookAtPosition.Y - position.Y;
            const float MaxUnitDistance = 60;

            // Remove unit if it is out of visible range!
            if (distance > MaxUnitDistance)
                return true;
            #endregion

            #region Render
            float itemSize = Mission.ItemModelSize;
            float itemRotation = 0;
            Vector3 itemPos = new Vector3(position, Mission.AllShipsZHeight);
            mission.AddModelToRender(
                mission.itemModels[(int)itemType],
                Matrix.CreateScale(itemSize) *
                Matrix.CreateRotationZ(itemRotation) *
                Matrix.CreateTranslation(itemPos));
            // Add glow effect
            EffectManager.AddEffect(itemPos + new Vector3(0, 0, 1.01f),
                EffectManager.EffectType.LightInstant,
                7.5f, 0, 0);
            EffectManager.AddEffect(itemPos + new Vector3(0, 0, 1.02f),
                EffectManager.EffectType.LightInstant,
                5.0f, 0, 0);
            #endregion

            #region Collect
            // Collect item and give to player if colliding!
            Vector2 distVec =
                new Vector2(Player.shipPos.X, Player.shipPos.Y) -
                new Vector2(position.X, position.Y);
            if (distVec.Length() < 5.0f)
            {
                if (itemType == ItemTypes.Health)
                {
                    // Refresh health
                    Sound.Play(Sound.Sounds.Health);
                    Player.health = 1.0f;
                } // if
                else
                {
                    Sound.Play(Sound.Sounds.NewWeapon);
                    if (itemType == ItemTypes.Mg)
                        Player.currentWeapon = Player.WeaponTypes.MG;
                    else if (itemType == ItemTypes.Plasma)
                        Player.currentWeapon = Player.WeaponTypes.Plasma;
                    else if (itemType == ItemTypes.Gattling)
                        Player.currentWeapon = Player.WeaponTypes.Gattling;
                    else if (itemType == ItemTypes.Rockets)
                        Player.currentWeapon = Player.WeaponTypes.Rockets;
                    else if (itemType == ItemTypes.Emp &&
                        Player.empBombs < 5)
                        Player.empBombs++;
                } // else
                Player.score += 500;

                return true;
            } // else
            #endregion

            // Don't remove unit from items list
            return false;
        }
Esempio n. 3
0
        /// <summary>
        /// Test hud
        /// </summary>
        public static void TestHud()
        {
            Mission dummyMission = null;

            TestGame.Start("TestHud",
                delegate
                {
                    dummyMission = new Mission();
                    dummyMission.hudTopTexture = new Texture("HudTop");
                    dummyMission.hudBottomTexture = new Texture("HudBottom");
                },
                delegate
                {
                    dummyMission.RenderLevelBackground(
                        BaseGame.TotalTimeMs / 33.0f);

                    dummyMission.RenderHud();
                });
        }
Esempio n. 4
0
        /// <summary>
        /// Test unit AI movement
        /// </summary>
        public static void TestUnitAI()
        {
            Unit testUnit = null;
            Mission dummyMission = null;

            TestGame.Start("TestUnitAI",
                delegate
                {
                    dummyMission = new Mission();
                    testUnit = new Unit(UnitTypes.Corvette, Vector2.Zero,
                        MovementPattern.StraightDown);
                    // Call dummyMission.RenderLandscape once to initialize everything
                    dummyMission.RenderLevelBackground(0);
                    // Remove the all enemy units (the start enemies)+ all neutral objects
                    dummyMission.numOfModelsToRender = 2;
                },
                delegate
                {
                    BaseGame.Device.Clear(
                        ClearOptions.DepthBuffer | ClearOptions.Target,
                        Color.Black, 1.0f, 0);
                    TextureFont.WriteText(2, 30,
                        "Press 1-0 to test all available movement patterns");
                    TextureFont.WriteText(2, 60,
                        "Press C, S, F, R, A to switch unit type");
                    TextureFont.WriteText(2, 90,
                        "Space restarts the current unit");
                    TextureFont.WriteText(2, 150,
                        "Unit: "+testUnit.unitType);
                    TextureFont.WriteText(2, 180,
                        "Movement AI: " + testUnit.movementPattern);
                    TextureFont.WriteText(2, 210,
                        "Position: " + testUnit.position);
                    TextureFont.WriteText(2, 240,
                        "Speed: " + testUnit.speed);
                    TextureFont.WriteText(2, 270,
                        "LifeTime: " + (int)(testUnit.lifeTimeMs / 10) / 100.0f);
                    ResetUnitDelegate ResetUnit = delegate(MovementPattern setPattern)
                        {
                            testUnit.movementPattern = setPattern;
                            testUnit.position = new Vector2(
                                RandomHelper.GetRandomFloat(-20, +20),
                                Mission.SegmentLength/2);
                            testUnit.hitpoints = testUnit.maxHitpoints;
                            testUnit.speed = 0;
                            testUnit.lifeTimeMs = 0;
                        };
                    if (Input.KeyboardKeyJustPressed(Keys.D1))
                        ResetUnit(MovementPattern.StraightDown);
                    if (Input.KeyboardKeyJustPressed(Keys.D2))
                        ResetUnit(MovementPattern.GetFasterAndMoveDown);
                    if (Input.KeyboardKeyJustPressed(Keys.D3))
                        ResetUnit(MovementPattern.SinWave1);
                    if (Input.KeyboardKeyJustPressed(Keys.D4))
                        ResetUnit(MovementPattern.SinWave2);
                    if (Input.KeyboardKeyJustPressed(Keys.D5))
                        ResetUnit(MovementPattern.SinWave3);
                    if (Input.KeyboardKeyJustPressed(Keys.D6))
                        ResetUnit(MovementPattern.CosWave1);
                    if (Input.KeyboardKeyJustPressed(Keys.D7))
                        ResetUnit(MovementPattern.CosWave2);
                    if (Input.KeyboardKeyJustPressed(Keys.D8))
                        ResetUnit(MovementPattern.CosWave3);
                    if (Input.KeyboardKeyJustPressed(Keys.D9))
                        ResetUnit(MovementPattern.SweepLeft);
                    if (Input.KeyboardKeyJustPressed(Keys.D0))
                        ResetUnit(MovementPattern.SweepRight);
                    if (Input.KeyboardKeyJustPressed(Keys.Space))
                        ResetUnit(testUnit.movementPattern);

                    if (Input.KeyboardKeyJustPressed(Keys.C))
                        testUnit.unitType = UnitTypes.Corvette;
                    if (Input.KeyboardKeyJustPressed(Keys.S))
                        testUnit.unitType = UnitTypes.SmallTransporter;
                    if (Input.KeyboardKeyJustPressed(Keys.F))
                        testUnit.unitType = UnitTypes.Firebird;
                    if (Input.KeyboardKeyJustPressed(Keys.R))
                        testUnit.unitType = UnitTypes.RocketFrigate;
                    if (Input.KeyboardKeyJustPressed(Keys.A))
                        testUnit.unitType = UnitTypes.Asteroid;

                    // Update and render unit
                    if (testUnit.Render(dummyMission))
                        // Restart unit if it was removed because it was too far down
                        ResetUnit(testUnit.movementPattern);

                    // Render all models the normal way
                    for (int num = 0; num < dummyMission.numOfModelsToRender; num++)
                        dummyMission.modelsToRender[num].model.Render(
                            dummyMission.modelsToRender[num].matrix);
                    BaseGame.MeshRenderManager.Render();
                    // Restore number of units as before.
                    // Our test unit will be added next frame again.
                    dummyMission.numOfModelsToRender = 2;

                    // Show all effects (unit smoke, etc.)
                    BaseGame.effectManager.HandleAllEffects();
                });
        }
Esempio n. 5
0
        /// <summary>
        /// Render unit, returns false if we are done with it.
        /// Has to be removed then. Else it updates just position and AI.
        /// </summary>
        /// <returns>True if done, false otherwise</returns>
        public bool Render(Mission mission)
        {
            #region Update movement with AI
            lifeTimeMs += BaseGame.ElapsedTimeThisFrameInMs;
            float moveSpeed = BaseGame.MoveFactorPerSecond;
            if (Player.GameOver)
                moveSpeed = 0;
            switch (movementPattern)
            {
                case MovementPattern.StraightDown:
                    position += new Vector2(0, -1) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.GetFasterAndMoveDown:
                    // Out of visible area? Then keep speed slow and wait.
                    if (position.Y - Mission.LookAtPosition.Y > 30)
                        lifeTimeMs = 300;
                    if (lifeTimeMs < 3000)
                        speed = lifeTimeMs / 3000;
                    position += new Vector2(0, -1) * speed * 1.5f * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.SinWave1:
                    position += new Vector2(
                        (float)Math.Sin(lifeTimeMs / 759.0f),
                        -1) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.SinWave2:
                    position += new Vector2(
                        -(float)Math.Sin(lifeTimeMs / 759.0f),
                        -1) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.SinWave3:
                    position += new Vector2(
                        (float)Math.Sin(lifeTimeMs / 359.0f),
                        -1) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.CosWave1:
                    position += new Vector2(
                        (float)Math.Cos(lifeTimeMs / 759.0f),
                        -1) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.CosWave2:
                    position += new Vector2(
                        -(float)Math.Cos(lifeTimeMs / 759.0f),
                        -1) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.CosWave3:
                    position += new Vector2(
                        (float)Math.Cos(lifeTimeMs / 1759.0f),
                        -1) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.SweepLeft:
                    position += new Vector2(0, -1) * maxSpeed * moveSpeed;
                    if (lifeTimeMs > 1750)
                        position += new Vector2(-0.7f, 0) * maxSpeed * moveSpeed;
                    break;
                case MovementPattern.SweepRight:
                    position += new Vector2(0, -1) * maxSpeed * moveSpeed;
                    if (lifeTimeMs > 1750)
                        position += new Vector2(+0.7f, 0) * maxSpeed * moveSpeed;
                    break;
            } // switch(movementPattern)

            // Keep in bounds
            if (position.X < -Player.MaxXPosition)
                position.X = -Player.MaxXPosition;
            if (position.X > Player.MaxXPosition)
                position.X = Player.MaxXPosition;
            #endregion

            #region Skip if out of visible range
            float distance = Mission.LookAtPosition.Y - position.Y;
            const float MaxUnitDistance = 60;

            // Remove unit if it is out of visible range!
            if (distance > MaxUnitDistance)
                return true;
            bool visible = distance > -35;
            #endregion

            #region Render
            Vector3 shipPos = new Vector3(position, Mission.AllShipsZHeight);
            Mission.ShipModelTypes shipModelType =
                unitType == UnitTypes.Corvette ? Mission.ShipModelTypes.Corvette :
                unitType == UnitTypes.SmallTransporter ? Mission.ShipModelTypes.SmallTransporter :
                unitType == UnitTypes.Firebird ? Mission.ShipModelTypes.Firebird :
                unitType == UnitTypes.RocketFrigate ? Mission.ShipModelTypes.RocketFrigate :
                Mission.ShipModelTypes.Asteroid;
            float shipSize = Mission.ShipModelSize[(int)shipModelType];
            //Note: rotation could be implemented here, but game is fine without
            float shipRotation = 0;
            Matrix rotationMatrix = Matrix.CreateRotationZ(shipRotation);
            if (unitType == UnitTypes.Asteroid)
                rotationMatrix *=
                    Matrix.CreateRotationX(position.X / 10 + Player.gameTimeMs / 1539.0f) *
                    Matrix.CreateRotationY(position.Y / 20 + Player.gameTimeMs / 1839.0f);
            mission.AddModelToRender(
                mission.shipModels[(int)shipModelType],
                Matrix.CreateScale(shipSize) *
                rotationMatrix *
                Matrix.CreateTranslation(shipPos));
            // Add rocket smoke
            if (unitType != UnitTypes.Asteroid)
                EffectManager.AddRocketOrShipFlareAndSmoke(
                    shipPos + shipSize *
                    new Vector3(0, 0.6789f + ((int)unitType > 3 ? 0.25f : 0.0f), 0),
                    shipSize / 4.0f, 12 * maxSpeed);
            #endregion

            #region Shooting?
            if (Player.GameOver)
                return false;

            // Peng peng?
            bool fireProjectile = false;
            if (cooldownTime > 0 &&
                visible)
            {
                // Ready to shoot again?
                if (shootTime <= 0)
                {
                    fireProjectile = true;
                    shootTime += cooldownTime;
                } // if (shootTime)
            } // if (GameForm.Mouse.LeftButtonPressed)

            // Weapon needs cooldown time?
            if (shootTime > 0)
                shootTime -= BaseGame.ElapsedTimeThisFrameInMs;

            if (fireProjectile)
            {
                Vector3 shootPos = Vector3.Zero;
                Vector3 enemyUnitPos = Player.shipPos;
                switch (unitType)
                {
                    case UnitTypes.Corvette:
                        if ((int)(lifeTimeMs / 700) % 3 == 0)
                        {
                            // Just shoot straight ahead.
                            bool hitUnit = Math.Abs(Player.position.X - shipPos.X) < 4.5f &&
                                Player.shipPos.Y < shipPos.Y;
                            shootPos = shipPos +
                            new Vector3(-2.35f, -3.5f, +0.2f);
                            EffectManager.AddMgEffect(shootPos,
                                hitUnit ? enemyUnitPos : shootPos + new Vector3(0, -40, 0),
                                10, 12, hitUnit, false);//shootNum % 2 == 0);
                            shootPos = shipPos +
                            new Vector3(+2.35f, -3.5f, +0.2f);
                            EffectManager.AddMgEffect(shootPos,
                                hitUnit ? enemyUnitPos : shootPos + new Vector3(0, -40, 0),
                                10, 13, hitUnit, false);
                            EffectManager.PlaySoundEffect(
                                EffectManager.EffectSoundType.MgShootEnemy);
                            if (hitUnit)
                            {
                                Player.health -= damage / 1000.0f;
                            } // if
                        } // if
                        break;

                    case UnitTypes.Firebird:
                        shootPos = shipPos + new Vector3(0, -1.5f, 3);
                        Mission.AddWeaponProjectile(
                            Projectile.WeaponTypes.Fireball, shootPos, false);
                        EffectManager.AddFireFlash(shootPos);
                        EffectManager.PlaySoundEffect(
                            EffectManager.EffectSoundType.EnemyShoot);
                        break;

                    case UnitTypes.RocketFrigate:
                        shootPos = shipPos + new Vector3(
                            RandomHelper.GetRandomInt(2) == 0 ? -2.64f : +2.64f, 0, 0);
                        Mission.AddWeaponProjectile(
                            Projectile.WeaponTypes.Rocket, shootPos, false);
                        EffectManager.AddFireFlash(shootPos);
                        EffectManager.PlaySoundEffect(
                            EffectManager.EffectSoundType.RocketShoot);
                        break;
                } // switch
            } // if
            #endregion

            #region Explode if out of hitpoints
            // Destroy ship and damage player if colliding!
            // Near enough to our ship?
            Vector2 distVec =
                new Vector2(Player.shipPos.X, Player.shipPos.Y) -
                new Vector2(position.X, position.Y);
            if (distVec.Length() < 4.75f)//5.5f)
            {
                // Explode and do damage!
                EffectManager.AddFlameExplosion(Player.shipPos);
                Player.health -= explosionDamage / 1000.0f;
                hitpoints = 0;
            } // else

            if (maxHitpoints > 0 &&
                hitpoints <= 0)
            {
                // Explode and kill unit!
                float size = 10 + (int)unitType * 3;
                if (unitType == UnitTypes.Asteroid)
                    size = 12;
                EffectManager.AddExplosion(
                    new Vector3(position, Mission.AllShipsZHeight),
                    size * 0.425f);
                    //size * 0.55f);
                return true;
            } // if (hitpoints.X)
            #endregion

            // Don't remove unit from units list
            return false;
        }
Esempio n. 6
0
        /// <summary>
        /// Handle game logic
        /// </summary>
        public static void HandleGameLogic(Mission mission)
        {
            //tst: if (Input.KeyboardF1JustPressed)
            //	gameOver = true;

            // Don't handle any more game logic if game is over.
            if (Player.GameOver)
            {
                if (victory)
                {
                    // Display Victory message
                    TextureFont.WriteTextCentered(
                        BaseGame.Width / 2, BaseGame.Height / 3,
                        "Victory! You won.");
                    TextureFont.WriteTextCentered(
                        BaseGame.Width / 2, BaseGame.Height / 3 + 40,
                        "Your Highscore: " + Player.score +
                        " (#" + Highscores.GetRankFromCurrentScore(Player.score) + ")");
                } // if
                else
                {
                    // Display game over message
                    TextureFont.WriteTextCentered(
                        BaseGame.Width / 2, BaseGame.Height / 3,
                        "Game Over! You lost.");
                    TextureFont.WriteTextCentered(
                        BaseGame.Width / 2, BaseGame.Height / 3 + 40,
                        "Your Highscore: " + Player.score +
                        " (#" + Highscores.GetRankFromCurrentScore(Player.score) + ")");
                } // else

                return;
            } // if

            // Increase game time
            gameTimeMs += BaseGame.ElapsedTimeThisFrameInMs;

            // Control our ship position with the keyboard or gamepad.
            // Use keyboard cursor keys and the left thumb stick. The
            // right hand is used for fireing (ctrl, space, a, b).
            Vector2 lastPosition = position;
            Vector2 lastRotation = shipRotation;
            float moveFactor = mouseSensibility *
                MovementSpeedPerSecond * BaseGame.MoveFactorPerSecond;
            // Left/Right
            if (Input.Keyboard.IsKeyDown(moveLeftKey) ||
                Input.Keyboard.IsKeyDown(Keys.Left) ||
                Input.Keyboard.IsKeyDown(Keys.NumPad4) ||
                Input.GamePad.DPad.Left == ButtonState.Pressed)
            {
                position.X -= moveFactor;
            } // if
            if (Input.Keyboard.IsKeyDown(moveRightKey) ||
                Input.Keyboard.IsKeyDown(Keys.Right) ||
                Input.Keyboard.IsKeyDown(Keys.NumPad6) ||
                Input.GamePad.DPad.Right == ButtonState.Pressed)
            {
                position.X += moveFactor;
            } // if
            if (Input.GamePad.ThumbSticks.Left.X != 0.0f)
            {
                position.X += Input.GamePad.ThumbSticks.Left.X;// *0.75f;
            } // if

            // Down/Up
            if (Input.Keyboard.IsKeyDown(moveDownKey) ||
                Input.Keyboard.IsKeyDown(Keys.Down) ||
                Input.Keyboard.IsKeyDown(Keys.NumPad2) ||
                Input.GamePad.DPad.Down == ButtonState.Pressed)
            {
                position.Y -= moveFactor;
            } // if
            if (Input.Keyboard.IsKeyDown(moveUpKey) ||
                Input.Keyboard.IsKeyDown(Keys.Up) ||
                Input.Keyboard.IsKeyDown(Keys.NumPad8) ||
                Input.GamePad.DPad.Up == ButtonState.Pressed)
            {
                position.Y += moveFactor;
            } // if
            if (Input.GamePad.ThumbSticks.Left.Y != 0.0f)
            {
                position.Y += Input.GamePad.ThumbSticks.Left.Y;// *0.75f;
            } // if

            // Keep position in bounds
            if (position.X < -MaxXPosition)
                position.X = -MaxXPosition;
            if (position.X > MaxXPosition)
                position.X = MaxXPosition;
            if (position.Y < -MaxYPosition)
                position.Y = -MaxYPosition;
            if (position.Y > MaxYPosition)
                position.Y = MaxYPosition;

            // Calculate ship rotation based on the current movement
            if (lastPosition.X > position.X)
                shipRotation.X = -0.5f;
            else if (lastPosition.X < position.X)
                shipRotation.X = +0.5f;
            else
                shipRotation.X = 0;

            if (lastPosition.Y > position.Y)
                shipRotation.Y = +0.53f;
            else if (lastPosition.Y < position.Y)
                shipRotation.Y = -0.33f;
            else
                shipRotation.Y = 0;
            shipRotation = lastRotation * 0.95f + shipRotation * 0.05f;

            // Fire?
            if (Input.GamePadAPressed ||
                Input.GamePad.Triggers.Right > 0.5f ||
                Input.Keyboard.IsKeyDown(Keys.LeftControl) ||
                Input.Keyboard.IsKeyDown(Keys.RightControl))
            {
                switch (currentWeapon)
                {
                    case WeaponTypes.MG:
                        if (gameTimeMs - lastShootTimeMs >= 150)
                        {
                            bool hitUnit = false;
                            Vector3 enemyUnitPos = new Vector3(0, 100000, 0);
                            // Hit enemy units, check all of them
                            for (int num = 0; num < Mission.units.Count; num++)
                            {
                                Unit enemyUnit = Mission.units[num];
                                // Near enough to enemy ship?
                                if (Math.Abs(enemyUnit.position.X - shipPos.X) < 5.0f &&
                                    enemyUnit.position.Y > shipPos.Y &&
                                    (enemyUnit.position.Y - shipPos.Y) < 60)
                                {
                                    // Hit and do damage!
                                    if (enemyUnit.position.Y < enemyUnitPos.Y)
                                        enemyUnitPos = new Vector3(enemyUnit.position, Mission.AllShipsZHeight);
                                    hitUnit = true;
                                    Player.score += (int)enemyUnit.hitpoints / 10;
                                    enemyUnit.hitpoints -= 150; // do about 1000 damage/sec
                                } // if
                            } // for

                            Vector3 shootPos = shipPos + new Vector3(-1.2f, 0, 0);
                            EffectManager.AddMgEffect(shootPos,
                                hitUnit ? enemyUnitPos : shootPos + new Vector3(0, 100, 0),
                                0, 2, hitUnit, true);//shootNum % 2 == 0);
                            shootPos = shipPos + new Vector3(+1.2f, 0, 0);
                            EffectManager.AddMgEffect(shootPos,
                                hitUnit ? enemyUnitPos : shootPos + new Vector3(0, 100, 0),
                                1, 3, hitUnit, false);
                            shootNum++;
                            lastShootTimeMs = gameTimeMs;
                            Input.GamePadRumble(0.015f, 0.125f);
                            Player.score += 1;
                        } // if
                        else
                        {
                            Vector3 shootPos = shipPos + new Vector3(-1.2f, 0, 0);
                            EffectManager.UpdateMgEffect(shootPos, 0);
                            shootPos = shipPos + new Vector3(+1.2f, 0, 0);
                            EffectManager.UpdateMgEffect(shootPos, 1);
                        } // else
                        break;
                    case WeaponTypes.Plasma:
                        if (gameTimeMs - lastShootTimeMs >= 250)
                        {
                            Vector3 shootPos = shipPos +
                                new Vector3(shootNum % 2 == 0 ? -2.4f : +2.4f, 0, 0);
                            Mission.AddWeaponProjectile(
                                Projectile.WeaponTypes.Plasma, shootPos, true);
                            EffectManager.AddFireFlash(shootPos);
                            EffectManager.PlaySoundEffect(
                                EffectManager.EffectSoundType.PlasmaShoot);
                            shootNum++;
                            lastShootTimeMs = gameTimeMs;
                            Input.GamePadRumble(0.05f, 0.175f);
                            Player.score += 5;
                        } // if
                        break;
                    case WeaponTypes.Gattling:
                        if (gameTimeMs - lastShootTimeMs >= 100)
                        {
                            bool hitUnit = false;
                            Vector3 enemyUnitPos = new Vector3(0, 100000, 0);
                            // Hit enemy units, check all of them
                            for (int num = 0; num < Mission.units.Count; num++)
                            {
                                Unit enemyUnit = Mission.units[num];
                                // Near enough to enemy ship?
                                if (Math.Abs(enemyUnit.position.X - shipPos.X) < 5.0f &&
                                    enemyUnit.position.Y > shipPos.Y &&
                                    (enemyUnit.position.Y - shipPos.Y) < 60)
                                {
                                    // Hit and do damage!
                                    if (enemyUnit.position.Y < enemyUnitPos.Y)
                                        enemyUnitPos = new Vector3(enemyUnit.position, Mission.AllShipsZHeight);
                                    hitUnit = true;
                                    Player.score += (int)enemyUnit.hitpoints / 10;
                                    enemyUnit.hitpoints -= 175; // do about 1750 damage/sec
                                } // if
                            } // for

                            Vector3 shootPos = shipPos + new Vector3(-1.2f, 0, 0);
                            EffectManager.AddGattlingEffect(shootPos,
                                hitUnit ? enemyUnitPos : shootPos + new Vector3(0, 100, 0),
                                0, 2, hitUnit, shootNum % 2 == 0);
                            shootPos = shipPos + new Vector3(+1.2f, 0, 0);
                            EffectManager.AddGattlingEffect(shootPos,
                                hitUnit ? enemyUnitPos : shootPos + new Vector3(0, 100, 0),
                                1, 3, hitUnit, false);
                            if (RandomHelper.GetRandomInt(2) == 0)
                            {
                                shootPos = shipPos + new Vector3(-2.54f, 0, 0);
                                EffectManager.AddGattlingEffect(shootPos,
                                    hitUnit ? enemyUnitPos : shootPos + new Vector3(0, 100, 0),
                                    4, 2, hitUnit, false);
                                shootPos = shipPos + new Vector3(+2.54f, 0, 0);
                                EffectManager.AddGattlingEffect(shootPos,
                                    hitUnit ? enemyUnitPos : shootPos + new Vector3(0, 100, 0),
                                    5, 2, hitUnit, false);
                            } // if
                            shootNum++;
                            lastShootTimeMs = gameTimeMs;
                            Input.GamePadRumble(0.05f, 0.225f);
                            Player.score += 1;
                        } // if
                        break;
                    case WeaponTypes.Rockets:
                        if (gameTimeMs - lastShootTimeMs >= 500)
                        {
                            Vector3 shootPos = shipPos +
                                new Vector3(shootNum % 2 == 0 ? -2.64f : +2.64f, 0, 0);
                            Mission.AddWeaponProjectile(
                                Projectile.WeaponTypes.Rocket, shootPos, true);
                            EffectManager.AddFireFlash(shootPos);
                            EffectManager.PlaySoundEffect(
                                EffectManager.EffectSoundType.RocketShoot);
                            shootNum++;
                            lastShootTimeMs = gameTimeMs;
                            Input.GamePadRumble(0.10f, 0.15f);
                            Player.score += 10;
                        } // if
                        break;
                } // switch
            } // if

            // Fire EMP?
            if (empBombs > 0 &&
                empBombTimeout <= 0 &&
                (Input.GamePadBJustPressed ||
                Input.KeyboardSpaceJustPressed))
            {
                empBombs--;
                empBombTimeout = MaxEmpTimeout;
                empPosition = shipPos + new Vector3(0, 10, 0);

                // Give some extra points to player
                Player.score += 1000;

                // Big badda boom
                //tst: EffectManager.AddExplosion(empPosition, 10);
                for (int num=0; num<5; num++)
                    EffectManager.AddEffect(shipPos+ new Vector3(0, 5, 0),
                        EffectManager.EffectType.ExplosionRing, 2+2.5f*num, 0);
                Sound.Play(Sound.Sounds.EMP);
            } // if

            if (empBombTimeout > 0)
            {
                empBombTimeout -= BaseGame.ElapsedTimeThisFrameInMs;

                if (empBombTimeout <= 0)
                    empBombTimeout = 0;

                // EMP Distance
                float distance = 64 *
                    (MaxEmpTimeout - empBombTimeout) / MaxEmpTimeout;

                // Show effect
                BaseGame.DrawLine(
                    empPosition +new Vector3(-100, distance, 0),
                    empPosition +new Vector3(+100, distance, 0));
                BaseGame.DrawLine(
                    empPosition +new Vector3(-100, -distance, 0),
                    empPosition +new Vector3(+100, -distance, 0));
                BaseGame.DrawLine(
                    empPosition +new Vector3(distance, -100, 0),
                    empPosition +new Vector3(distance, +100, 0));
                BaseGame.DrawLine(
                    empPosition +new Vector3(-distance, -100, 0),
                    empPosition +new Vector3(-distance, +100, 0));

                // Kill all units on screen that are in range!
                for (int num = 0; num < Mission.units.Count; num++)
                {
                    // Only include units on the screen and don't kill asteroids.
                    // Projectiles and effects stay active too.
                    if ((Mission.units[num].position -
                        new Vector2(empPosition.X, empPosition.Y)).Length() < distance + 10)// &&
                        //Mission.units[num].position.Y - Mission.LookAtPosition.Y < 15 &&
                        //looks cooler with: Mission.units[num].unitType != Unit.UnitTypes.Asteroid)
                    {
                        Player.score += (int)Mission.units[num].hitpoints / 10;
                        Mission.units[num].hitpoints = 0;
                    } // if
                } // for
            } // if (emp bomb active)

            if (Player.health <= 0)
            {
                victory = false;
                EffectManager.AddExplosion(shipPos, 20);
                for (int num = 0; num<8; num++)
                    EffectManager.AddFlameExplosion(shipPos+
                        RandomHelper.GetRandomVector3(-12, +12), false);
                Sound.PlayDefeatSound();
                Player.SetGameOverAndUploadHighscore();
            } // if
        }
Esempio n. 7
0
        /// <summary>
        /// Render projectile, returns false if we are done with it.
        /// Has to be removed then. Else it updates just position.
        /// </summary>
        /// <returns>True if done, false otherwise</returns>
        public bool Render(Mission mission)
        {
            #region Update movement
            lifeTimeMs += BaseGame.ElapsedTimeThisFrameInMs;
            float moveSpeed = BaseGame.MoveFactorPerSecond;
            if (Player.GameOver)
                moveSpeed = 0;
            switch (weaponType)
            {
                case WeaponTypes.Fireball:
                    position += moveDirection * maxSpeed * moveSpeed;
                    break;
                case WeaponTypes.Rocket:
                    if (ownProjectile)
                        position += new Vector3(0, +1, 0) * maxSpeed * moveSpeed * 1.1f;
                    else
                    {
                        // Fly to player
                        Vector3 targetMovement = Player.shipPos - position;
                        targetMovement.Normalize();
                        moveDirection = moveDirection * 0.95f + targetMovement * 0.05f;
                        moveDirection.Normalize();
                        position += moveDirection * maxSpeed * moveSpeed;
                    } // else
                    break;
                case WeaponTypes.Plasma:
                    if (ownProjectile)
                        position += new Vector3(0, +1, 0) * maxSpeed * moveSpeed * 1.25f;
                    else
                        position += new Vector3(0, -1, 0) * maxSpeed * moveSpeed;
                    break;
                // Rest are items, they just stay around
            } // switch(movementPattern)
            #endregion

            #region Skip if out of visible range
            float distance = Mission.LookAtPosition.Y - position.Y;
            const float MaxUnitDistance = 35;

            // Remove unit if it is out of visible range!
            if (distance > MaxUnitDistance ||
                distance < -MaxUnitDistance * 2 ||
                position.Z < 0 ||
                lifeTimeMs > 6000)
                return true;
            #endregion

            #region Render
            switch (weaponType)
            {
                case WeaponTypes.Rocket:
                    float rocketRotation = MathHelper.Pi;
                    if (ownProjectile == false)
                        rocketRotation = MathHelper.PiOver2 + GetRotationAngle(moveDirection);
                    mission.AddModelToRender(
                        mission.shipModels[(int)Mission.ShipModelTypes.Rocket],
                        Matrix.CreateScale(
                        (ownProjectile ? 1.25f : 0.75f) *
                        Mission.ShipModelSize[(int)Mission.ShipModelTypes.Rocket]) *
                        Matrix.CreateRotationZ(rocketRotation) *
                        Matrix.CreateTranslation(position));
                    // Add rocket smoke
                    EffectManager.AddRocketOrShipFlareAndSmoke(position, 1.5f, 6 * maxSpeed);
                    break;
                case WeaponTypes.Plasma:
                    EffectManager.AddPlasmaEffect(position, effectRotation, 1.25f);
                    break;
                case WeaponTypes.Fireball:
                    EffectManager.AddFireBallEffect(position, effectRotation, 1.25f);
                    break;
            } // switch
            #endregion

            #region Explode if hitting unit
            // Own projectile?
            if (ownProjectile)
            {
                // Hit enemy units, check all of them
                for (int num = 0; num < Mission.units.Count; num++)
                {
                    Unit enemyUnit = Mission.units[num];
                    // Near enough to enemy ship?
                    Vector2 distVec =
                        new Vector2(enemyUnit.position.X, enemyUnit.position.Y) -
                        new Vector2(position.X, position.Y);
                    if (distVec.Length() < 7 &&
                        (enemyUnit.position.Y - Player.shipPos.Y) < 60)
                    {
                        // Explode and do damage!
                        EffectManager.AddFlameExplosion(position);
                        Player.score += (int)enemyUnit.hitpoints / 10;
                        enemyUnit.hitpoints -= damage;
                        return true;
                    } // if
                } // for
            } // if
            // Else this is an enemy projectile?
            else
            {
                // Near enough to our ship?
                Vector2 distVec =
                    new Vector2(Player.shipPos.X, Player.shipPos.Y) -
                    new Vector2(position.X, position.Y);
                if (distVec.Length() < 2.75f)//3)
                {
                    // Explode and do damage!
                    EffectManager.AddFlameExplosion(position);
                    Player.health -= damage / 1000.0f;
                    return true;
                } // if
            } // else
            #endregion

            // Don't remove unit from units list
            return false;
        }