From 25fdc90968a14ffd44dae65d6fdb3d50e6df9082 Mon Sep 17 00:00:00 2001 From: BoredGuy Date: Fri, 27 Mar 2026 16:29:32 -0700 Subject: Initial Commit --- TankBattleCore/Objects/Game.cs | 106 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 TankBattleCore/Objects/Game.cs (limited to 'TankBattleCore/Objects/Game.cs') diff --git a/TankBattleCore/Objects/Game.cs b/TankBattleCore/Objects/Game.cs new file mode 100644 index 0000000..75f25e5 --- /dev/null +++ b/TankBattleCore/Objects/Game.cs @@ -0,0 +1,106 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; +using TankBattleCore; +using TankBattleCore.Objects; + +internal class GameScene : IScene +{ + private const int PlayAreaOffset = 100; + private static readonly Rectangle PlayArea = new Rectangle( + -PlayAreaOffset, + -PlayAreaOffset, + Game1.Instance.WindowWidth + PlayAreaOffset, + Game1.Instance.WindowHeight + PlayAreaOffset + ); + + private List tanks = []; + private List bullets = []; + private bool paused = false; + + public GameScene() + { + Clear(); + + AddTank( + new Tank( + Vector2.Zero, + Game1.Instance.TankBodyTextures[TankColor.Black] + ) + ); + } + + internal void Clear() + { + tanks.Clear(); + } + + public void Update(float dt) + { + if (paused) + { + return; + } + + var keyboardState = Keyboard.GetState(); + + foreach (Tank t in tanks) + { + t.Update(dt); + + if (keyboardState.IsKeyDown(Keys.Enter)) + { + ShootBullet(t.GetNextShot()); + } + } + + foreach (Bullet b in bullets) + { + b.Update(dt); + } + + bullets.RemoveAll(b => !PlayArea.Contains(b.Position)); + } + + public void Draw(SpriteBatch spriteBatch) + { + spriteBatch.Begin(); + + foreach (Tank t in tanks) + { + t.Draw(spriteBatch); + } + + foreach (Bullet b in bullets) + { + b.Draw(spriteBatch); + } + + spriteBatch.End(); + } + + private void AddTank(Tank tank) + { + tanks.Add(tank); + } + + private void AddBullet(Bullet bullet) + { + bullets.Add(bullet); + } + + public void ShootBullet(BulletData bulletData) + { + AddBullet( + new Bullet( + bulletData.Position, + Game1.Instance.TankBodyTextures[TankColor.Black], + bulletData.Speed, + bulletData.Angle + ) + ); + } +} \ No newline at end of file -- cgit v1.2.3