summaryrefslogtreecommitdiff
path: root/TankBattleCore/Objects/Game.cs
diff options
context:
space:
mode:
Diffstat (limited to 'TankBattleCore/Objects/Game.cs')
-rw-r--r--TankBattleCore/Objects/Game.cs106
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