Website Summary Files Commits Branches @commit @diff @stats
path: root / src / Web / PUSG.Web / Models
diff options
context:
space:
mode:
Commite8ee7b69663a95d97b226266ebe3ccc95e13c309 (patch)
Author
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:42:30 +0200
Committer
 ivaylo.zhelev <ivaylo.zhelev@solarwinds.com>
2021-01-31 16:42:30 +0200
Files9eed7de1b817c812d61514950a9299aa2cb8d887 /src/Web/PUSG.Web/Models
pusg-api-e8ee7b69663a95d97b226266ebe3ccc95e13c309.tar
pusg-api-e8ee7b69663a95d97b226266ebe3ccc95e13c309.tar.gz
pusg-api-e8ee7b69663a95d97b226266ebe3ccc95e13c309.zip
Parent6fb05a2a6f94e008dc689291d0522f39f87a6622 (diff)
Major Business Logic Implementation
* Added GameHub * Added Back-End Game Loop and Game Mechanics * Added End Game and Result message
diff --git a/src/Web/PUSG.Web/Models/BaseGameObject.cs b/src/Web/PUSG.Web/Models/BaseGameObject.cs
new file mode 100644
index 0000000..2a76f39
--- /dev/null
+++ b/src/Web/PUSG.Web/Models/BaseGameObject.cs
@@ -0,0 +1,9 @@
+namespace PUSG.Web.Models
+{
+ public abstract class BaseGameObject
+ {
+ public double X { get; set; }
+
+ public double Y { get; set; }
+ }
+}
diff --git a/src/Web/PUSG.Web/Models/CollisionEngine.cs b/src/Web/PUSG.Web/Models/CollisionEngine.cs
new file mode 100644
index 0000000..4ae2e13
--- /dev/null
+++ b/src/Web/PUSG.Web/Models/CollisionEngine.cs
@@ -0,0 +1,16 @@
+using System.Drawing;
+
+namespace PUSG.Web.Models
+{
+ public class CollisionEngine
+ {
+ public static bool CollidesTankWithShell(BaseGameObject tank, BaseGameObject shell)
+ {
+ Rectangle targetRectangle = new Rectangle((int)tank.X, (int)tank.Y, 50, 40);
+
+ Rectangle colliderRectangle = new Rectangle((int)shell.X, (int)shell.Y, 25, 25);
+
+ return colliderRectangle.IntersectsWith(targetRectangle);
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Models/GameSession.cs b/src/Web/PUSG.Web/Models/GameSession.cs
new file mode 100644
index 0000000..21c65d0
--- /dev/null
+++ b/src/Web/PUSG.Web/Models/GameSession.cs
@@ -0,0 +1,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;
+ }
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Models/GameState.cs b/src/Web/PUSG.Web/Models/GameState.cs
new file mode 100644
index 0000000..aeea038
--- /dev/null
+++ b/src/Web/PUSG.Web/Models/GameState.cs
@@ -0,0 +1,13 @@
+using System.Collections.Generic;
+
+namespace PUSG.Web.Models
+{
+ public class GameState
+ {
+ public Player Blue { get; set; }
+
+ public Player Red { get; set; }
+
+ public List<Shell> Shells { get; set; }
+ }
+}
diff --git a/src/Web/PUSG.Web/Models/Player.cs b/src/Web/PUSG.Web/Models/Player.cs
new file mode 100644
index 0000000..cb0cc5a
--- /dev/null
+++ b/src/Web/PUSG.Web/Models/Player.cs
@@ -0,0 +1,73 @@
+using System.Collections.Generic;
+
+namespace PUSG.Web.Models
+{
+ public class Player : BaseGameObject
+ {
+ private Dictionary<string, string> directionsRight = new Dictionary<string, string>
+ {
+ ["up"] = "right",
+ ["right"] = "down",
+ ["down"] = "left",
+ ["left"] = "up"
+ };
+
+ private Dictionary<string, string> directionsLeft = new Dictionary<string, string>
+ {
+ ["up"] = "left",
+ ["left"] = "down",
+ ["down"] = "right",
+ ["right"] = "up"
+ };
+
+ public string Color { get; set; }
+
+ public string Direction { get; set; }
+
+ public string Type { get; set; }
+
+ public bool IsDead { get; set; } = false;
+
+ public void MoveUp()
+ {
+ if (this.Y - 20 >= 0)
+ {
+ this.Y -= 20;
+ }
+ }
+
+ public void MoveRight()
+ {
+ if (this.X + 20 <= 1366)
+ {
+ this.X += 20;
+ }
+ }
+
+ public void MoveDown()
+ {
+ if (this.Y + 20 <= 1366)
+ {
+ this.Y += 20;
+ }
+ }
+
+ public void MoveLeft()
+ {
+ if (this.X - 20 >= 0)
+ {
+ this.X -= 20;
+ }
+ }
+
+ public void RotateLeft()
+ {
+ this.Direction = this.directionsLeft[this.Direction];
+ }
+
+ public void RotateRight()
+ {
+ this.Direction = this.directionsRight[this.Direction];
+ }
+ }
+}
diff --git a/src/Web/PUSG.Web/Models/Shell.cs b/src/Web/PUSG.Web/Models/Shell.cs
new file mode 100644
index 0000000..f005423
--- /dev/null
+++ b/src/Web/PUSG.Web/Models/Shell.cs
@@ -0,0 +1,40 @@
+using System;
+
+namespace PUSG.Web.Models
+{
+ public class Shell : BaseGameObject
+ {
+ public string Id { get; set; } = Guid.NewGuid().ToString();
+
+ public string Direction { get; set; }
+
+ public string Color { get; set; }
+
+ public bool IsDestroyable { get; set; } = false;
+
+ public void Update()
+ {
+ if(this.Direction == "up")
+ {
+ this.Y -= 7;
+ }
+ else if(this.Direction == "right")
+ {
+ this.X += 7;
+ }
+ else if(this.Direction == "down")
+ {
+ this.Y += 7;
+ }
+ else if(this.Direction == "left")
+ {
+ this.X -= 7;
+ }
+
+ if(this.Y < 0 || this.Y > 1366 || this.X < 0 || this.X > 1366)
+ {
+ this.IsDestroyable = true;
+ }
+ }
+ }
+}