Exemplo n.º 1
0
 public void CheckInGameMenuKeys()
 {
     if (input.GetState(input.escapeKey) == InputState.JustPressed)
     {
         //Return to menu!
         gameMusic.stopSound();
         gameMusic       = null;
         core.menuIndex  = 0;
         core.menuOption = 0;
     }
 }
Exemplo n.º 2
0
        public void GameLoop()
        {
#if DEBUG
            procFPSStopwatch.Start();
#endif
            lock (core)
            {
                if (gameMusic == null)
                {
                    gameMusic = core.PlaySound(gameplayLoopSound, true, 0.6f);
                }

                if (gs.currentPlayer.isAlive)
                {
                    CheckGameKeys();
                }
                CheckInGameMenuKeys();

                //rotateEnemies(); //TODO: Move to enemy ship's Process function. Need a behavior enum.

                checkProjectileLifetime();

                //Enemy spawning
                //TODO: You can check this less often
                gs.regionID = (int)Math.Floor(Geometry.DistanceFromOrigin(gs.currentPlayer.posX, gs.currentPlayer.posY) / gs.ringThickness);

                //Generate regions that don't already exist
                //TODO: Instead of bools, they should be lists (initially null references) to hold inactive enemies that were in the region when deactivated.
                while (gs.regionSpawnRecord.Count < gs.regionID + 2)                    //Note: Generates for 2 regions at the start
                {
                    gs.regionSpawnRecord.Add(new bool[gs.regionSpawnRecord.Count + 2]); //Sector count is equal to regionID + 2
                }

                for (int i = Math.Max(1, gs.regionID - 1); i <= gs.regionID + 1; i++)
                {
                    GenerateFoesForRegion(i);
                }


                foreach (var ship in gs.playerShips.Union(gs.enemyShips).Where(p => p.isAlive))
                {
                    ship.Process(core.frameCounter);
                }

                foreach (var projectile in gs.playerProjectiles)
                {
                    projectile.Process(core.frameCounter);
                    //Check bullet collision with enemies
                    foreach (var ship in gs.enemyShips.Where(p => p.isAlive))
                    {
                        if (ship.CollidesWith(projectile))
                        {
                            ship.Damage(projectile.damage, core.frameCounter);
                            projectile.Kill();
                        }
                    }
                }

                foreach (var projectile in gs.enemyProjectiles)
                {
                    projectile.Process(core.frameCounter);
                    //Check bullet collision with players
                    foreach (var ship in gs.playerShips.Where(p => p.isAlive))
                    {
                        if (ship.CollidesWith(projectile))
                        {
                            ship.Damage(projectile.damage, core.frameCounter);
                            projectile.Kill();
                        }
                    }
                }

                //Check if players ran into each other
                for (var x = 0; x < gs.playerShips.Count; x++)
                {
                    if (!gs.playerShips[x].isAlive)
                    {
                        continue;
                    }
                    for (var y = x + 1; y < gs.playerShips.Count; y++)
                    {
                        if (!gs.playerShips[y].isAlive)
                        {
                            continue;
                        }
                        if (gs.playerShips[x].CollidesWith(gs.playerShips[y]))
                        {
                            //Make ships bounce apart (get angle between ships and send them in opposite directions)
                            var targetAngle = Geometry.Face(gs.playerShips[x].posX, gs.playerShips[x].posY, gs.playerShips[y].posX, gs.playerShips[y].posY);
                            gs.playerShips[x].ApplyForce(5, targetAngle);
                            gs.playerShips[y].ApplyForce(5, -targetAngle);
                            gs.playerShips[x].Damage(1, core.frameCounter);
                            gs.playerShips[y].Damage(1, core.frameCounter);
                        }
                    }
                }

                //Check if players ran into enemies
                foreach (var ship in gs.playerShips.Where(p => p.isAlive))
                {
                    foreach (var foe in gs.enemyShips.Where(p => p.isAlive))
                    {
                        if (ship.CollidesWith(foe))
                        {
                            ship.Damage(1, core.frameCounter);
                            foe.Damage(20, core.frameCounter);
                        }
                        //TODO: Deactivate enemies that are far away; you can move them to a separate list and check less frequently to see if they should be readded to active list
                    }
                }
                //TODO: Other collisions, player inputs, stuff, things, etc.
            }

            var maxDist = 1000;
            //if frame number & 63 is 0
            var subCounter = core.frameCounter & 63;
            if (subCounter == 0)
            {
                //for each enemy
                for (int i = gs.enemyShips.Count - 1; i >= 0; i--)
                {
                    //If enemy is far away
                    if (Math.Abs(gs.enemyShips[i].posX - gs.currentPlayer.posX) >= maxDist && Math.Abs(gs.enemyShips[i].posY - gs.currentPlayer.posY) >= maxDist)
                    {
                        gs.inactivatedEnemies.Add(gs.enemyShips[i]);
                        gs.enemyShips.RemoveAt(i);
                        //deactivate enemy
                    }
                }
            }
            else if (subCounter == 32)
            {
                //else if frame number & 63 is 32
                //for each enemy in inactive list
                for (int i = gs.inactivatedEnemies.Count - 1; i >= 0; i--)
                {
                    //if enemy is close
                    if (Math.Abs(gs.inactivatedEnemies[i].posX - gs.currentPlayer.posX) < maxDist && Math.Abs(gs.inactivatedEnemies[i].posY - gs.currentPlayer.posY) < maxDist)
                    {
                        //activate enemy
                        gs.enemyShips.Add(gs.inactivatedEnemies[i]);
                        gs.inactivatedEnemies.RemoveAt(i);
                    }
                }
            }

#if DEBUG
            procFPSStopwatch.Stop();
            frameSeconds    = (double)procFPSStopwatch.ElapsedTicks / Stopwatch.Frequency;
            avgFrameSeconds = (9 * avgFrameSeconds + frameSeconds) / 10; //Moving average (estimate of last 10 frames' average; not exact)
            procFPSStopwatch.Reset();
#endif
        } // End of Gameloop
Exemplo n.º 3
0
        public void MenuLoop()
        {
            if (menuMusic == null)
            {
                menuMusic = core.PlaySound(menuLoopSound, true);
            }
            if (input.GetState(input.enterKey) == InputState.JustPressed)
            {
                if (core.menuIndex == (int)MenuIndex.Main) //Main menu
                {
                    if (core.menuOption == 0)
                    {
                        ResetGameState();
                        core.menuIndex = -1;

                        core.PlaySound(startSound);
                        //Stop the menu music and set it to null so it can play again if the menu loop ever gets called again
                        menuMusic.stopSound();
                        menuMusic = null;
                    }
                    else if (core.menuOption == 1)
                    {
                        core.menuIndex  = (int)MenuIndex.HostGame; //Host game menu
                        core.menuOption = 0;
                        core.Connected  = (coreVer, gameVer) => {
                            if (coreVer != core.CoreVersion || gameVer != core.GameVersion)
                            {
                                //TODO: Tell the user the versions don't match
                                core.Disconnect();
                            }
                            else //When connection succeeds, start the game
                            {
                                core.menuIndex = -1;
                            }
                        };
                        //Start listening for incoming connections
                        core.ListenForIncomingConnection();
                    }
                    else if (core.menuOption == 2)
                    {
                        core.menuIndex  = (int)MenuIndex.JoinGame; //Join game menu
                        core.menuOption = 0;
                        //TODO: allow user to input an IP address
                        core.Connected = (coreVer, gameVer) => {
                            if (coreVer != core.CoreVersion || gameVer != core.GameVersion)
                            {
                                //TODO: Tell the user the versions don't match
                                core.Disconnect();
                            }
                            else //When connection succeeds, start the game
                            {
                                core.menuIndex = -1;
                            }
                        };
                        core.Connect(new System.Net.IPAddress(new byte[] { 127, 0, 0, 1 }));
                    }
                    else if (core.menuOption == 3)
                    {
                        core.menuIndex  = (int)MenuIndex.Options; //Options menu
                        core.menuOption = 0;
                    }
                    else if (core.menuOption == 4)
                    {
                        core.menuIndex         = (int)MenuIndex.Credits; //Credits menu
                        core.menuOption        = 0;
                        renderer.creditsScroll = pictureBox1.Height;
                    }
                    else if (core.menuOption == 5)
                    {
                        core.Exit();
                    }
                }
                else if (core.menuIndex == (int)MenuIndex.HostGame) //Host game menu
                {
                    core.menuIndex  = 0;
                    core.menuOption = 1;
                }
                else if (core.menuIndex == (int)MenuIndex.JoinGame) //Join game menu
                {
                    core.menuIndex  = 0;
                    core.menuOption = 2;
                }
                else if (core.menuIndex == (int)MenuIndex.Options) //Options menu
                {
                    core.menuIndex  = 0;
                    core.menuOption = 3;
                }
                else if (core.menuIndex == (int)MenuIndex.Credits) //Credits menu
                {
                    core.menuIndex  = 0;
                    core.menuOption = 4;
                }
            }

            if (input.GetState(input.downArrowKey) == InputState.JustPressed)
            {
                core.menuOption = (core.menuOption + 1) % menuItems[core.menuIndex];
            }
            if (input.GetState(input.upArrowKey) == InputState.JustPressed)
            {
                core.menuOption = core.menuOption == 0 ? menuItems[core.menuIndex] - 1 : core.menuOption - 1;
            }
        }