public void DrawGame() { g.BeforeFrame(); g.TranslateTransform((float)-gs.currentPlayer.posX + gameWidth / 2, (float)-gs.currentPlayer.posY + gameHeight / 2); var oldTransform = g.GetMatrix(); //Draw a starry background using a predictable pseudorandom number sequence var starSeed = new PseudoRandom(backgroundSeed); for (double x = gs.currentPlayer.posX - (gameWidth / 2); x < gs.currentPlayer.posX + (gameWidth / 2) + 256; x += 256) { int squareX = (int)Math.Floor(x / 256); for (double y = gs.currentPlayer.posY - (gameHeight / 2); y < gs.currentPlayer.posY + (gameHeight / 2) + 256; y += 256) { int squareY = (int)Math.Floor(y / 256); starSeed.lastValue = (uint)(((long)squareX * 13 + squareY * 58) & uint.MaxValue); int numberOfStars = Math.Min(8 + ((int)(starSeed.Next() & 0xF00) >> 8), 25); //10 to 25 stars for (int i = 0; i < numberOfStars; i++) { var xc = (float)squareX * 256 + (starSeed.Next() & 255); var yc = (float)squareY * 256 + (starSeed.Next() & 255); g.DrawLine(Pens.White, xc, yc, xc, yc + 1); } } } #if DEBUG DrawRegionSector(gs.regionID); g.ResetMatrix(); g.DrawString(String.Format("Sector Area: {0:0.00}", gs.regionSectorArea), new Font(MenuFont.FontFamily, 12), Brushes.Azure, 10, gameHeight - 40); #endif //Draw projectiles foreach (var projectile in gs.playerProjectiles.Union(gs.enemyProjectiles)) { g.SetMatrix(oldTransform); g.TranslateTransform((float)projectile.posX, (float)projectile.posY); g.DrawLines(projectile.uGraphics.color, projectile.uGraphics.points.ToArray()); #if DEBUG foreach (var circle in projectile.collider.dCircles) { g.DrawEllipse(Pens.Aqua, (float)(-circle.Radius + circle.X), (float)(-circle.Radius + circle.Y), (float)circle.Radius * 2, (float)circle.Radius * 2); } #endif } //Draw ships (and in debug mode, draw their colliders) foreach (var ship in gs.playerShips.Union(gs.enemyShips).Where(p => p.isAlive)) { g.SetMatrix(oldTransform); g.TranslateTransform((float)ship.posX, (float)ship.posY); g.RotateTransform((float)(ship.facing / Math.PI * 180)); if (ship.lastDamagedFrame <= frameCounter - 8) { g.DrawLines(ship.uGraphics.color, ship.uGraphics.points.ToArray()); } else //Invert ship color when recently damaged { var tempColor = ship.uGraphics.color.Color.ToArgb(); var tempPen = new Pen(Color.FromArgb(tempColor ^ 0x00FFFFFF)); g.DrawLines(tempPen, ship.uGraphics.points.ToArray()); } #if DEBUG foreach (var circle in ship.collider.dCircles) { g.DrawEllipse(Pens.Aqua, (float)(-circle.Radius + circle.X), (float)(-circle.Radius + circle.Y), (float)circle.Radius * 2, (float)circle.Radius * 2); } #endif g.ResetMatrix(); } if (input.GetState(input.shiftKey) == InputState.Held) { g.DrawLine(new Pen(Color.FromArgb(255, 0, 39, 45)), (float)(gameWidth / 2 - gs.currentPlayer.posX), (float)(gameHeight / 2 - gs.currentPlayer.posY), gameWidth / 2, gameHeight / 2); } g.AfterFrame(); }
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; } }