summaryrefslogtreecommitdiff
path: root/TankBattleCore/Objects/Game.cs
blob: 75f25e5d30802a84883278350ebbab15c8b471e2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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
            )
        );
    }
}