protected override void LoadContent() { Cameras.Main.DrawSize = new Vector2(Resolution.X / 2f, Resolution.Y); Camera cam2 = Cameras.Create(0, 0, Graphics.WorldScale.X, Graphics.WorldScale.Y); cam2.DrawPosition = new Vector2(Resolution.X / 2f, 0); cam2.DrawSize = new Vector2(Resolution.X / 2f, Resolution.Y); Content.AddImage("../../Content/GameOver.png", "GameOver"); Content.AddImage("../../Content/Asteroid.png", "Asteroid"); Content.AddImage("../../Content/Cursor.png", "Cursor"); Sprites.Add("cursor", new SImage(this, 0, 0, 50, 50, "Cursor")); Sprites.Scale("cursor", 0.5); Sprites.Display("cursor", true); cursorSize = Sprites.GetSize("cursor"); Content.AddSound("../../Content/pew.mp3", "Pew"); Content.AddFont("../../Content/heav.ttf", "Heavy Data"); test = new SharpSlugsEngine.Physics.PolygonCollider(new Vector2(0, -34.375f), new Vector2(25, 15.625f), new Vector2(0, 3.125f), new Vector2(-25, 15.625f)); }
protected override void LoadContent() { Content.AddImage("../../images/redTank.png", "tank1"); Content.AddImage("../../images/blueTank.png", "tank2"); Content.AddFont("../../images/heav.ttf", "Heavy Data"); Sprites.Add("Player1", new PlayerTank(this, "tank1", 100, 695, true)); Sprites.Add("Player2", new PlayerTank(this, "tank2", 1100, 695, false)); Sprites.Add("Ground", new Rect(0, 715, 1280, 5, Color.Brown)); Sprites.Add("Wall", new Rect(640, 500, 10, 220, Color.Brown)); Sprites.Display("Wall", true); Sprites.Display("Ground", true); }
protected override void Update(GameTime gameTime) { if (gameOver) { return; } if (mousePos != Graphics.ToWorldScale(Mouse.State.Location)) { usingMouse = true; mousePos = Graphics.ToWorldScale(Mouse.State.Location); } shooting = usingMouse && Mouse.State.Left.IsClicked; //Search for sticks outside of a modest deadzone //Left stick for movement and right stick for shooting Vector2 moveVec = new Vector2(0, 0); Vector2 shootVec = new Vector2(0, 0); foreach (XboxController controller in Controllers.XboxControllers) { if (controller.LeftStick.State.Length >= 0.25) { moveVec = controller.LeftStick.State; } if (controller.RightStick.State.Length >= 0.25) { shootVec = controller.RightStick.State; shooting = true; usingMouse = false; } } Sprites.Display("cursor", usingMouse); //Apply InputAction bindings to this vector if (inputActions["Left"].IsPressed) { moveVec = new Vector2(-1, moveVec.Y); } if (inputActions["Right"].IsPressed) { moveVec = new Vector2(1, moveVec.Y); } if (inputActions["Up"].IsPressed) { moveVec = new Vector2(moveVec.X, -1); } if (inputActions["Down"].IsPressed) { moveVec = new Vector2(moveVec.X, 1); } //Make sure the vector isn't too long if (moveVec.Length > 1f) { moveVec = moveVec.Normalize(); } //Move the player playerPos += moveVec * 250 * (float)gameTime.DeltaTime.TotalSeconds; //Keep the player on the screen if (playerPos.X < shipSize.X / 2f) { playerPos = new Vector2(shipSize.X / 2f, playerPos.Y); } if (playerPos.X > Graphics.WorldScale.X - shipSize.X / 2f) { playerPos = new Vector2(Graphics.WorldScale.X - shipSize.X / 2f, playerPos.Y); } if (playerPos.Y < shipSize.Y / 2f) { playerPos = new Vector2(playerPos.X, shipSize.Y / 2f); } if (playerPos.Y > Graphics.WorldScale.Y - shipSize.Y / 2f) { playerPos = new Vector2(playerPos.X, Graphics.WorldScale.Y - shipSize.Y / 2f); } Sprites.MoveTo("cursor", (int)(mousePos.X - cursorSize.X / 2f), (int)(mousePos.Y - cursorSize.Y / 2f)); if (usingMouse) { shootDir = (playerPos - mousePos).Normalize(); } else if (shootVec != Vector2.Zero) { shootDir = -shootVec.Normalize(); } float angleRadians = (float)Math.Atan2(shootDir.Y, shootDir.X); float angleDegrees = angleRadians * (float)(180f / Math.PI) - 90; bullets.RemoveAll(bullet => bullet.Dead); asteroids.RemoveAll(asteroid => asteroid.Dead); bulletCooldown -= (float)gameTime.DeltaTime.TotalSeconds; asteroidCooldown -= (float)gameTime.DeltaTime.TotalSeconds; if (shooting && bulletCooldown <= 0f) { bulletCooldown = 0.25f; for (int i = -1; i <= 1; i++) { bullets.Add(new Bullet(this, playerPos - shootDir * Vector2.One * 34.75f, shootDir.Rotate(Vector2.Zero, i * 15) * -500)); } Sound snd = Content.GetSound("Pew"); if (snd != null) { snd.Volume = 10; snd.DisposeOnFinished = true; snd.Play(); } } if (asteroidCooldown <= 0f) { asteroidCooldown = (float)(rnd.NextDouble() + 0.5f); //0 - top //1 - left //2 - bottom //3 - right Vector2 pos = new Vector2(0, 0); Vector2 vel = new Vector2(0, 0); switch (rnd.Next(4)) { case 0: pos = new Vector2(rnd.Next(0, (int)Graphics.WorldScale.X), 0); vel = new Vector2(rnd.Next(100) - 50, rnd.Next(50)); break; case 1: pos = new Vector2(0, rnd.Next(0, (int)Graphics.WorldScale.Y)); vel = new Vector2(rnd.Next(50), rnd.Next(100) - 50); break; case 2: pos = new Vector2(rnd.Next(0, (int)Graphics.WorldScale.X), Graphics.WorldScale.Y); vel = new Vector2(rnd.Next(100) - 50, -rnd.Next(50)); break; case 3: pos = new Vector2(Graphics.WorldScale.X, rnd.Next(0, (int)Graphics.WorldScale.Y)); vel = new Vector2(-rnd.Next(50), rnd.Next(100) - 50); break; } asteroids.Add(new Asteroid(this, pos, vel.Normalize() * rnd.Next(100, 200))); } List <Asteroid> newAsteroids = new List <Asteroid>(); foreach (Asteroid asteroid in asteroids) { foreach (Bullet bullet in bullets) { if (!asteroid.Dead && asteroid.CheckCollision(bullet.Position)) { bullet.Dead = true; asteroid.Damage(); if (asteroid.Dead && asteroid.Vertices.Length >= 6 && asteroid.Size > 1.25) { Vector2 pos = asteroid.Position; asteroid.Position = Vector2.Zero; int mid = asteroid.Vertices.Length / 2; Vector2[] first = new Vector2[mid]; Vector2[] second = new Vector2[asteroid.Vertices.Length - mid]; for (int i = 0; i < asteroid.Vertices.Length; i++) { if (i < mid) { first[i] = asteroid.Vertices[i]; } else { second[i - mid] = asteroid.Vertices[i]; } } Vector2 vel = new Vector2(rnd.Next(100) - 50, rnd.Next(100) - 50).Normalize() * rnd.Next(100, 200); newAsteroids.Add(new Asteroid(this, first, pos, vel)); newAsteroids.Add(new Asteroid(this, second, pos, -vel)); } } } } asteroids.AddRange(newAsteroids); foreach (Asteroid asteroid in asteroids) { if (asteroid.CheckCollision(playerPos)) { Sprites.Display("cursor", false); ShowCursor = true; LockCursor = false; gameOver = true; break; } } test.Position = playerPos; test.Rotation = angleDegrees; }