diff options
| author | BoredGuy <osome3717@gmail.com> | 2026-03-27 16:29:32 -0700 |
|---|---|---|
| committer | BoredGuy <osome3717@gmail.com> | 2026-03-27 16:29:32 -0700 |
| commit | 25fdc90968a14ffd44dae65d6fdb3d50e6df9082 (patch) | |
| tree | 3e11812a480ff173a7c65fd9d53e0efc69f5be27 /TankBattleCore/Objects/Game.cs | |
Initial Commit
Diffstat (limited to 'TankBattleCore/Objects/Game.cs')
| -rw-r--r-- | TankBattleCore/Objects/Game.cs | 106 |
1 files changed, 106 insertions, 0 deletions
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<Tank> tanks = []; + private List<Bullet> 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 |
