Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Web / PUSG.Web / Models / GameSession.cs
blob: 21c65d04035d3e9f1256a15cc38191486d4896c3 (plain) (blame)
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
using PUSG.Web.Extensions;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;

namespace PUSG.Web.Models
{
    public class GameSession
    {
        public Player BluePlayer { get; set; }

        public Player RedPlayer { get; set; }

        public bool IsDone { get; private set;  } = false;

        public string Winner { get; private set; } = null;

        public ConcurrentDictionary<string, Shell> Shells { get; set; } = new ConcurrentDictionary<string, Shell>();

        public Player this[string color]
        {
            get
            {
                if (color == "blue") return this.BluePlayer;
                if (color == "red") return this.RedPlayer;
                return null;
            }
        }

        public void Update()
        {
            lock (this.Shells)
            {
                Stack<Shell> shellsToDelete = new Stack<Shell>();

                foreach (var shell in this.Shells)
                {
                    shell.Value.Update();

                    if(shell.Value.IsDestroyable)
                    {
                        shellsToDelete.Push(shell.Value);
                    }
                }

                foreach (var item in shellsToDelete)
                {
                    this.Shells.Delete(item.Id);
                }

                foreach (var shell in this.Shells.Values.Where(shell => shell.Color == "red"))
                {
                    if (CollisionEngine.CollidesTankWithShell(this.BluePlayer, shell))
                    {
                        this.IsDone = true; // RED WINS
                        this.Winner = "red";
                    }
                }

                foreach (var shell in this.Shells.Values.Where(shell => shell.Color == "blue"))
                {
                    if (CollisionEngine.CollidesTankWithShell(this.RedPlayer, shell))
                    {
                        this.IsDone = true; // BLUE WINS
                        this.Winner = "blue";
                    }
                }
            }
        }

        public void Shoot(string playerColor)
        {
            if(playerColor == "blue")
            {
                Shell shell = new Shell
                {
                    Direction = this[playerColor].Direction,
                    Color = playerColor,
                    X = this[playerColor].X,
                    Y = this[playerColor].Y
                };

                this.Shells[shell.Id] = shell;
            }
            else if(playerColor == "red")
            {
                Shell shell = new Shell
                {
                    Direction = this[playerColor].Direction,
                    Color = playerColor,
                    X = this[playerColor].X,
                    Y = this[playerColor].Y
                };
                
                this.Shells[shell.Id] = shell;
            }
        }
    }
}