示例#1
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();
                });
        }