コード例 #1
0
ファイル: Game1.cs プロジェクト: er1/c376p1
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
            #region Menus (GAMESTATES)

            #region Main Menu
            //Credits
            if (gameState == -1)
            {
                if (!creditsMusik)
                {
                    MediaPlayer.Stop();
                    menuMusik = false;
                    MediaPlayer.Play(creditsMusic);
                    creditsMusik = true;
                }
                if ((Keyboard.GetState().IsKeyDown(Keys.Enter) || gamePadState.Buttons.B == ButtonState.Pressed) && !aButton)
                {
                    aButton = true;
                    gameState = 0;

                }

                if ((gamePadState.Buttons.B == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Enter)))
                {
                    aButton = false;
                }

            }
            if (gameState == 0)
            {
                if (!menuMusik)
                {
                    MediaPlayer.Stop();
                    creditsMusik = false;
                    gameMusik = false;
                    MediaPlayer.Play(menuMusic);
                    menuMusik = true;
                }

                if (!started)
                {
                    Components.Remove(cameraMain);
                    Components.Remove(spriteManager);
                    Components.Remove(spriteManager.frames);

                    cameraMain = new Camera(this, new Vector3(0, 200, 199), new Vector3(0, -5, 1), Vector3.Up, MainScreen, true, worldSize);
                    spriteManager = new SpriteManager(this);

                    Components.Add(cameraMain);
                    Components.Add(spriteManager);
                    started = true;
                }
                if((Keyboard.GetState().IsKeyDown(Keys.Up)||gamePadState.DPad.Up== ButtonState.Pressed) && !dPadUp)
                {
                    dPadUp = true;
                    menuState--;
                    if (menuState < 0)
                        menuState = 2;

                }
                if ((Keyboard.GetState().IsKeyDown(Keys.Down)||gamePadState.DPad.Down == ButtonState.Pressed) && !dPadDown)
                {
                    dPadDown = true;
                    menuState++;

                    if (menuState > 2)
                        menuState = 0;

                }
                if ((Keyboard.GetState().IsKeyDown(Keys.Enter) || gamePadState.Buttons.A == ButtonState.Pressed) && !aButton)
                {
                    aButton = true;
                    if (menuState == 0)
                    {
                        gameState = 1;
                    }
                    if (menuState == 1)
                    {
                        gameState = -1;
                    }
                    if (menuState == 2)
                    {
                        this.Exit();
                    }

                }
                if ((Keyboard.GetState().IsKeyDown(Keys.Escape)|| gamePadState.Buttons.A == ButtonState.Pressed) && !aButton )
                {
                    aButton = true;
                    if (menuState == 2)
                    {
                        this.Exit();
                    }
                }

                if ((gamePadState.Buttons.B == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Z)))
                {
                    bButton = false;
                }
                if ((gamePadState.Buttons.A == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Enter)))
                {
                    aButton = false;
                }
                if ((gamePadState.DPad.Up == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Up)))
                {
                    dPadUp = false;
                }
                if ((gamePadState.DPad.Down == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Down)))
                {
                    dPadDown = false;
                }

            }
            #endregion
            // game

            #region Win/Lose
            else if ((gameState == 3) || (gameState == 4))
            {
                if ((Keyboard.GetState().IsKeyDown(Keys.Enter) || gamePadState.Buttons.A == ButtonState.Pressed) && !aButton)
                {
                    wavesLevel.Clear();

                    aButton = true;
                    LIFE = 1000;
                    gameState = 0;
                    wavesLevel.Clear();
                    towers.Clear();
                    projectiles.Clear();
                    monsters.Clear();
                    resourcemanager = new ResourceManager();
                    gatherzone = new GatherZone(resourcemanager, this);
                    chosenTile = new TileCoord(0, 0);
                    started = false;
                    base.Initialize();
                }

                if ((gamePadState.Buttons.A == ButtonState.Released) && Keyboard.GetState().IsKeyUp(Keys.Enter))
                {
                    aButton = false;
                }
                if ((gamePadState.DPad.Up == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Up)))
                {
                    dPadUp = false;
                }
                if ((gamePadState.DPad.Down == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Down)))
                {
                    dPadDown = false;
                }

            }
            #endregion

            #region Main Game
            else if (gameState == 1)
            {

                if (!gameMusik)
                {
                    MediaPlayer.Stop();
                    menuMusik = false;
                    MediaPlayer.Play(gameMusic);
                    gameMusik = true;
                }
                base.Update(gameTime);

                if (Keyboard.GetState().IsKeyDown(Keys.Escape))
                {
                    gameState = 2;
                }
                if (gamePadState.Buttons.Start == ButtonState.Pressed)
                {
                    gameState = 2;
                }

            }
            #endregion

            #region Pause Menu
            //pause
            else if (gameState == 2)
            {
                if ((Keyboard.GetState().IsKeyDown(Keys.Up) || gamePadState.DPad.Up == ButtonState.Pressed) && !dPadUp)
                {
                    dPadUp = true;
                    pauseState--;
                    if (pauseState < 0)
                        pauseState = 1;

                }
                if ((Keyboard.GetState().IsKeyDown(Keys.Down) || gamePadState.DPad.Down == ButtonState.Pressed) && !dPadDown)
                {
                    dPadDown = true;
                    pauseState++;

                    if (pauseState > 1)
                        pauseState = 0;

                }
                if ((Keyboard.GetState().IsKeyDown(Keys.Enter) || gamePadState.Buttons.A == ButtonState.Pressed) && !aButton)
                {
                    aButton = true;
                    if (pauseState == 0)
                    {
                        gameState = 1;
                    }
                    if (pauseState == 1)
                    {
                        LIFE = 1000;
                        gameState = 0;
                        wavesLevel.Clear();
                        towers.Clear();
                        projectiles.Clear();
                        monsters.Clear();
                        resourcemanager = new ResourceManager();
                        gatherzone = new GatherZone(resourcemanager, this);
                        chosenTile = new TileCoord(0, 0);
                        started = false;
                        base.Initialize();
                    }

                }

                if ((gamePadState.Buttons.A == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Enter)))
                {
                    aButton = false;
                }
                if ((gamePadState.DPad.Up == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Up)))
                {
                    dPadUp = false;
                }
                if ((gamePadState.DPad.Down == ButtonState.Released) && (Keyboard.GetState().IsKeyUp(Keys.Down)))
                {
                    dPadDown = false;
                }

            }
            #endregion

            #endregion

            #region GAMESTATE 1 = playing
            if (gameState == 1)
            {
                if (gamePadState.Buttons.Start == ButtonState.Pressed)
                    gameState = 2;
                #region Update Level
                //Level 1
                if (currentWave < wavesLevel.Count)
                {
                    wavesLevel[currentWave].UpdateWave(gameTime);
                    spriteManager.drawHUD(wavesLevel[currentWave].levelTimer, wavesLevel[currentWave].level, curResource);
                    //Check if this Waves Timer is done or monsters are all dead (so wave is done)
                    if (((wavesLevel[currentWave].levelDone && wavesLevel[currentWave].spawn <= 1) && monsters.Count<1 ))
                    {
                        //Game.Exit();
                        //System.GC.Collect();
                        currentWave++;
                    }
                    //If Level isn't done then check the Timer to add monsters at invervals
                    else if (wavesLevel[currentWave].canSpawn && wavesLevel[currentWave].spawn > 0)
                    {

                        int z = RandomNumber(-80, 60);
                        wavesLevel[currentWave].spawn--;
                        wavesLevel[currentWave].canSpawn = false;
                        chance = RandomNumber(1, wavesLevel[currentWave].level);

                        if (chance == 1)
                        {
                            monsters.Add(new monster1(ref Monster1, new Vector3(-390 + 1, 5, z), new Vector3(1, 0, 0), this), true);
                            spriteManager.addLifeBarsMonsters(new Vector2(-390 + 1, z));
                        }
                        if (chance == 2)
                        {
                            monsters.Add(new monster2(ref Monster2, new Vector3(-390 + 1, 5, z), new Vector3(1, 0, 0), this), true);
                            spriteManager.addLifeBarsMonsters(new Vector2(-390 + 1, z));
                        }
                        if (chance == 3)
                        {
                            monsters.Add(new monster3(ref Monster3, new Vector3(-390 + 1, 5, z), new Vector3(1, 0, 0), this), true);
                            spriteManager.addLifeBarsMonsters(new Vector2(-390 + 1, z));
                        }
                        if (chance >= 4)
                        {
                            monsters.Add(new monster4(ref Monster4, new Vector3(-390 + 1, 5, z), new Vector3(1, 0, 0), this), true);
                            spriteManager.addLifeBarsMonsters(new Vector2(-390 + 1, z));
                        }
                    }
                }
                else
                {
                    gameState = 3;
                }

                if (LIFE <= 0)
                {
                    gameState = 4;
                }
                #endregion

                #region Update Drawing the Map
                //update Map
                map.Update();
                //Selected tile
                chosenTile = cameraMain.getCurrentTC();
                SelectionTile.Update(gameTime);

                #endregion

                RemoveDeadEntities();
                UpdateExplosions(gameTime);
                //Has some key input here
                //Selecting Resources
                if (curResource > 0)
                {
                    if (((gamePadState.Buttons.X == ButtonState.Pressed) || (Keyboard.GetState().IsKeyDown(Keys.Q))) && !SpaceBar)//
                    {
                        curResource--;
                        SpaceBar = true;
                    }
                }
                if (curResource < 5)
                {
                    if (((gamePadState.Buttons.Y == ButtonState.Pressed) || (Keyboard.GetState().IsKeyDown(Keys.E))) && !SpaceBar)//
                    {
                        curResource++;
                        SpaceBar = true;
                    }
                }

                #region Update Monster, Tower, bullets + a little logic

                #region monster
                foreach (KeyValuePair<monster, bool> pair in monsters)
                {
                    /*
                    percentage = 0;

                    if ((pair.Key.life / 100) * 100 == 100)
                    {
                        percentage = 100;
                    }
                    else if ((pair.Key.life / 100) * 100 > 75 && (pair.Key.life / 100) * 100 <= 99)
                    {
                        percentage = 100;
                    }
                    else if ((pair.Key.life / 100) * 100 > 51 && (pair.Key.life / 100) * 100 <= 75)
                    {
                        percentage = 75;
                    }
                    else if ((pair.Key.life / 100) * 100 > 25 && (pair.Key.life / 100) * 100 <= 51)
                    {
                        percentage = 50;
                    }
                    else if ((pair.Key.life / 100) * 100 <= 25)
                    {
                        percentage = 25;
                    }
                    */

                    pair.Key.Update(gameTime);
                    //spriteManager.updateLifeBarsMonsters(0, percentage, pair.Key.getPosition(), cameraMain, MainScreen);
                    TileCoord monsterLocation
                        =
                        new TileCoord((int)Math.Floor((pair.Key.getPosition().X + 10) / 20.0), (int)Math.Floor((pair.Key.getPosition().Z + 10) / 20.0));

                    map.GetTile(monsterLocation).addEntity(pair.Key);
                    // HIT THE COLONY NOT FINISHED ( REMOVE LIFE AND BLAH BLAH)
                    //if (pair.Key.hitColony)
                    //{

                    //    //pair.Key.life -= 100;
                    //    //spriteManager.removeLifeBarsMonsters(pair.Key);
                    //    //LIFE -= 10;
                    //}
                }
                #endregion

                #region towers
                //Draws Tower list
                foreach (KeyValuePair<tower, bool> pair in towers)
                {
                    pair.Key.Update();
                    pair.Key.game = this;

                    TileCoord towerLocation
                        =
                        new TileCoord((int)Math.Floor((pair.Key.getPosition().X + 10) / 20.0), (int)Math.Floor((pair.Key.getPosition().Z + 10) / 20.0));

                    map.GetTile(towerLocation).addEntity(pair.Key);

                    //Shoots a projectile based on a Timer from tower
                    if ((pair.Key.iWantToShoot(gameTime)) && (pair.Key.lookForTarget(map)))
                    {
                        if (pair.Key.shooting)
                        {
                            pair.Key.shooting = false;
                            //towers[i].timer = TimeSpan.FromSeconds(3.0);
                            pair.Key.Shoot();
                            //addProject(towers[i].getPosition() + new Vector3(0, 25, 0), new Vector3(-1, 0, 0));
                        }
                    }
                }
                #endregion

                #region projectiles
                //updates projectile list
                foreach (KeyValuePair<projectile, bool> pair in projectiles)
                {
                    pair.Key.Update(gameTime);

                    TileCoord projectileLocation
                        =
                        new TileCoord((int)Math.Floor((pair.Key.getPosition().X + 10) / 20.0), (int)Math.Floor((pair.Key.getPosition().Z + 10) / 20.0));

                    map.GetTile(projectileLocation).addEntity(pair.Key);

                    //Check if projectile Timer is at zero to remove
                    if (pair.Key.removeProject(gameTime))
                    {
                        projectiles.Remove(pair.Key);
                        break;
                    }
                }
                #endregion

                #region Collision detection
                //CheckBoxCollision();
                foreach (KeyValuePair<TileCoord, Tile> pair in map.getDictionary())
                {
                    if ((pair.Value.anyMonster()) && (pair.Value.anyTower()))
                    {
                        List<model> test = pair.Value.getEntities();
                        for (int i = 0; i < test.Count(); i++)
                        {
                            if (test[i] is tower)
                            {
                                foreach (KeyValuePair<tower, bool> pairA in towers)
                                {
                                    if (pairA.Key.getID() == test[i].getID())
                                    {
                                        ((tower)pairA.Key).life -= 25;
                                        break;
                                    }
                                }
                            }
                            if (test[i] is monster)
                            {
                                ((monster)test[i]).life -= 1000;
                            }

                        }
                    }

                    if ((pair.Value.anyMonster()) && (pair.Value.anyProjectile()))
                    {
                        List<model> test = pair.Value.getEntities();
                        for (int i = 0; i < test.Count(); i++)
                        {
                            if (test[i] is monster)
                            {
                                ((monster)test[i]).life -= 25;
                            }
                            if (test[i] is projectile)
                            {
                                foreach (KeyValuePair<projectile, bool> pairA in projectiles)
                                {
                                    if (pairA.Key.getID() == test[i].getID())
                                    {
                                        projectiles.Remove(pairA.Key);
                                        break;
                                    }
                                }
                            }
                        }
                        //CheckTileCollision(pair.Value.getEntities());
                    }
                }
                #endregion

                #region Tower Adding

                //Add Gatherer
                if ((Keyboard.GetState().IsKeyDown(Keys.Z)) || (gamePadState.Buttons.B == ButtonState.Pressed)) {
                    if (bButton == false) {
                        if (resourcemanager.resourceA >= 8) {
                            Gatherer g = new Gatherer(gatherzone, new Vector3(100, 8, 0));
                            g.targetPosition = new Vector3(250, 8, 0);
                            gatherzone.add(g);
                            resourcemanager.resourceA -= 8;
                        }
                    }
                    bButton = true;
                }

                if ((Keyboard.GetState().IsKeyUp(Keys.Z)) && (gamePadState.Buttons.B == ButtonState.Released)) {
                    bButton = false;
                }

                if (((Keyboard.GetState().IsKeyDown(Keys.Space)) && (!map.GetTile(chosenTile).anyTower()) && !SpaceBar) ||
                    ((gamePadState.Buttons.A == ButtonState.Pressed) && (!map.GetTile(chosenTile).anyTower()) && !SpaceBar))
                {
                    SpaceBar = true;

                    tower towerToAdd;
                    //Add Gun Tower
                    if ((curResource == 0) && (resourcemanager.resourceA >= 5))
                    {
                        resourcemanager.resourceA -= 5;
                        towerToAdd = new GunTower(ref gunTower, (new Vector3(chosenTile.x * 20, 5, chosenTile.y * 20)), chosenTile);
                        towers.Add(towerToAdd, true);
                        map.GetTile(chosenTile).addEntity(towerToAdd);
                    }
                    //Add Cannon Tower
                    else if ((curResource == 1) && (resourcemanager.resourceA >= 5) && (resourcemanager.resourceB >= 8))
                    {
                        resourcemanager.resourceA -= 5;
                        resourcemanager.resourceB -= 8;
                        towerToAdd = new CanonTower(ref cannonTower, (new Vector3(chosenTile.x * 20, 5, chosenTile.y * 20)), chosenTile);
                        towers.Add(towerToAdd, true);
                        map.GetTile(chosenTile).addEntity(towerToAdd);
                    }
                    //Add missile tower
                    else if ((curResource == 2) && (resourcemanager.resourceB >= 5) && (resourcemanager.resourceC >= 10))
                    {
                        resourcemanager.resourceB -= 5;
                        resourcemanager.resourceC -= 10;
                        towerToAdd = new MissileTower(ref missileTower, (new Vector3(chosenTile.x * 20, 5, chosenTile.y * 20)), chosenTile);
                        towers.Add(towerToAdd, true);
                        map.GetTile(chosenTile).addEntity(towerToAdd);
                    }
                    //Add Fire Tower
                    else if ((curResource == 3) && (resourcemanager.resourceC >= 10) && (resourcemanager.resourceD >= 10))
                    {
                        resourcemanager.resourceC -= 10;
                        resourcemanager.resourceD -= 10;
                        towerToAdd = new FireTower(ref fireTower, (new Vector3(chosenTile.x * 20, 5, chosenTile.y * 20)), chosenTile);
                        towers.Add(towerToAdd, true);
                        map.GetTile(chosenTile).addEntity(towerToAdd);
                    }
                    //Add Electric Tower
                    else if ((curResource == 4) && (resourcemanager.resourceC >= 15))
                    {
                        resourcemanager.resourceC -= 15;
                        towerToAdd = new ElectricTower(ref electricTower, (new Vector3(chosenTile.x * 20, 5, chosenTile.y * 20)), chosenTile);
                        towers.Add(towerToAdd, true);
                        map.GetTile(chosenTile).addEntity(towerToAdd);
                    }
                    //Add Chicken tower
                    else if ((curResource == 5) && (resourcemanager.resourceD >= 20))
                    {
                        resourcemanager.resourceD -= 20;
                        towerToAdd = new ChickenTower(ref chickenTower, (new Vector3(chosenTile.x * 20, 5, chosenTile.y * 20)), chosenTile);
                        towers.Add(towerToAdd, true);
                        map.GetTile(chosenTile).addEntity(towerToAdd);
                    }

                }

                if (
                    (Keyboard.GetState().IsKeyUp(Keys.Space)) &&
                    (Keyboard.GetState().IsKeyUp(Keys.Q)) &&
                    (Keyboard.GetState().IsKeyUp(Keys.E)) &&
                    (gamePadState.Buttons.A == ButtonState.Released) &&
                    (gamePadState.Buttons.X == ButtonState.Released) &&
                    (gamePadState.Buttons.Y == ButtonState.Released)
                )
                {
                    SpaceBar = false;
                }

                #endregion

                #endregion

                #region Update Resource Manager
                gatherzone.update();
                #endregion
            }
            #endregion
        }
コード例 #2
0
ファイル: Game1.cs プロジェクト: er1/c376p1
        protected override void Initialize()
        {
            LIFE = 1000;

            worldHeight = graphics.PreferredBackBufferHeight;
            worldWidth = graphics.PreferredBackBufferWidth;
            map = new Map();
            #region Viewport stuff
            MainScreen = new Viewport();
            MainScreen.X = 0;
            MainScreen.Y = 0;
            MainScreen.Width = worldWidth;
            MainScreen.Height = worldHeight;
            MainScreen.MinDepth = 0;
            MainScreen.MaxDepth = 1;
            #endregion

            #region init Game components
            Components.Remove(cameraMain);
            Components.Remove(spriteManager);
            cameraMain = new Camera(this, new Vector3(0, 200, 199), new Vector3(0, -5, 1), Vector3.Up, MainScreen, true, worldSize);
            spriteManager = new SpriteManager(this);
            Components.Add(cameraMain);
            Components.Add(spriteManager);
            #endregion

            chosenTile = cameraMain.getCurrentTC();
            this.IsFixedTimeStep = false;
            this.TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 20);

            // resource management
            resourcemanager = new ResourceManager();
            gatherzone = new GatherZone(resourcemanager, this);

            base.Initialize();
        }