diff --git a/DepthsBelow/DepthsBelow/Component/GridTransform.cs b/DepthsBelow/DepthsBelow/Component/GridTransform.cs deleted file mode 100644 index 823aeba..0000000 --- a/DepthsBelow/DepthsBelow/Component/GridTransform.cs +++ /dev/null @@ -1,46 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.Xna.Framework; - -namespace DepthsBelow.Component -{ - [Obsolete("Deprecated in favour of Component.Transform")] - public class GridTransform : Component - { - public Point Position; - public int X - { - get - { - return Position.X; - } - set - { - Position.X = value; - } - } - public int Y - { - get - { - return Position.Y; - } - set - { - Position.Y = value; - } - } - - public GridTransform(Entity parent) : base(parent) - { - Position = Point.Zero; - } - - public Vector2 ToWorld() - { - return Grid.GridToWorld(Position); - } - } -} diff --git a/DepthsBelow/DepthsBelow/Component/PixelTransform.cs b/DepthsBelow/DepthsBelow/Component/PixelTransform.cs deleted file mode 100644 index 865b6f0..0000000 --- a/DepthsBelow/DepthsBelow/Component/PixelTransform.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Microsoft.Xna.Framework; - -namespace DepthsBelow.Component -{ - [Obsolete("Deprecated in favour of Component.Transform")] - public class PixelTransform : Component - { - public Vector2 Position; - public float X - { - get - { - return Position.X; - } - set - { - Position.X = value; - } - } - public float Y - { - get - { - return Position.Y; - } - set - { - Position.Y = value; - } - } - public float Rotation; - public Vector2 Origin; - - public PixelTransform(Entity parent) - : base(parent) - { - Position = Vector2.Zero; - Rotation = 0.0f; - Origin = Vector2.Zero; - } - - public Point ToGrid() - { - return Grid.WorldToGrid(Position); - } - } -} diff --git a/DepthsBelow/DepthsBelow/Core.cs b/DepthsBelow/DepthsBelow/Core.cs index a170908..1ae2f5d 100755 --- a/DepthsBelow/DepthsBelow/Core.cs +++ b/DepthsBelow/DepthsBelow/Core.cs @@ -22,7 +22,9 @@ namespace DepthsBelow public static GraphicsDeviceManager GraphicsDeviceManager; SpriteBatch spriteBatch; - public Lua Lua; + public Lua Lua; + + public EntityManager EntityManager; public Camera Camera; @@ -63,14 +65,13 @@ namespace DepthsBelow /// protected override void Initialize() { - // TODO: Add your initialization logic here + // TODO: Add your initialization logic here + EntityManager = new EntityManager(); Camera = new Camera(this); Squad = new List(); Swarm = new List(); Volley = new List(); - TestMonster = new SmallEnemy(this, ref Swarm); - // Run scripts after everything is initialized Lua = new Lua(); Lua.LoadScripts(); @@ -92,20 +93,18 @@ namespace DepthsBelow Map.ParseObjects(this); // TODO: use this.Content to load your game content here - Soldier.LoadContent(this); - SmallEnemy.LoadContent(this); - Shot.LoadContent(this); + Soldier.LoadContent(); + SmallEnemy.LoadContent(); + Shot.LoadContent(); MouseInput = new MouseInput(this); MouseInput.LoadContent(); - KeyboardInput = new KeyboardInput(this); - + KeyboardInput = new KeyboardInput(this); + + TestMonster = new SmallEnemy(EntityManager, ref Swarm); + TestMonster.X = 12; + TestMonster.Y = 4; Swarm.Add(TestMonster); - - TestMonster.X = 12; - TestMonster.Y = 4; - - //frame.AddChild(text); } /// @@ -132,17 +131,8 @@ namespace DepthsBelow MouseInput.Update(gameTime); KeyboardInput.Update(gameTime); - // Update soldiers in squad - foreach (var soldier in Squad) - soldier.Update(gameTime); + EntityManager.Update(gameTime); - foreach (var body in Swarm) - body.Update(gameTime); - - foreach (var bullet in Volley) - bullet.Update(gameTime); - - TestMonster.Update(gameTime); GUIManager.Update(gameTime); base.Update(gameTime); @@ -163,25 +153,8 @@ namespace DepthsBelow // Draw the level Map.Draw(spriteBatch); - // Draw units - foreach (var soldier in Squad) - { - var sr = soldier.GetComponent(); - if (sr != null) - sr.Draw(spriteBatch); - } - foreach (var body in Swarm) - { - var sr = body.GetComponent(); - if (sr != null) - sr.Draw(spriteBatch); - } - foreach (var bullet in Volley) - { - var sr = bullet.GetComponent(); - if (sr != null) - sr.Draw(spriteBatch); - } + // Draw entities + EntityManager.Draw(spriteBatch); // Draw mouse input visuals MouseInput.Draw(spriteBatch); @@ -197,7 +170,5 @@ namespace DepthsBelow base.Draw(gameTime); } - - } } diff --git a/DepthsBelow/DepthsBelow/DepthsBelow.csproj b/DepthsBelow/DepthsBelow/DepthsBelow.csproj index 8a968e4..fca0875 100644 --- a/DepthsBelow/DepthsBelow/DepthsBelow.csproj +++ b/DepthsBelow/DepthsBelow/DepthsBelow.csproj @@ -1,204 +1,203 @@ - - - - {94683C5B-052D-4143-96D8-35C67E831000} - {6D335F3A-9D43-41b4-9D22-F6F17C4BE596};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} - Debug - x86 - WinExe - Properties - DepthsBelow - DepthsBelow - v4.0 - Client - v4.0 - Windows - HiDef - faa0577e-a03e-43cf-89c7-0a03f4c22717 - Game - Game.ico - GameThumbnail.png - false - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 1 - 1.0.0.%2a - false - true - true - - - true - full - false - bin\x86\Debug - DEBUG;TRACE;WINDOWS - prompt - 4 - true - false - x86 - false - - - pdbonly - true - bin\x86\Release - TRACE;WINDOWS - prompt - 4 - true - false - x86 - true - - - ECE66DEF071576E8D2F7D57232C0569623E3E2DF - - - DepthsBelow_TemporaryKey.pfx - - - true - - - true - - - - ..\..\LuaInterface\LuaInterface.dll - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - False - - - - False - - - False - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - {CD3F949D-54AA-4D38-99DB-92905A375D84} - AStar - - - DepthsBelowContent %28Content%29 - Content - {433A77A2-DE52-4875-A4EF-232CF59C2DD3} - - - - - False - Microsoft .NET Framework 4 Client Profile %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - false - - - False - Windows Installer 3.1 - true - - - False - Microsoft XNA Framework Redistributable 4.0 - true - - - - - - - - - + + + + {94683C5B-052D-4143-96D8-35C67E831000} + {6D335F3A-9D43-41b4-9D22-F6F17C4BE596};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} + Debug + x86 + WinExe + Properties + DepthsBelow + DepthsBelow + v4.0 + Client + v4.0 + Windows + HiDef + faa0577e-a03e-43cf-89c7-0a03f4c22717 + Game + Game.ico + GameThumbnail.png + false + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 1 + 1.0.0.%2a + false + true + true + + + true + full + false + bin\x86\Debug + DEBUG;TRACE;WINDOWS + prompt + 4 + true + false + x86 + false + + + pdbonly + true + bin\x86\Release + TRACE;WINDOWS + prompt + 4 + true + false + x86 + true + + + ECE66DEF071576E8D2F7D57232C0569623E3E2DF + + + DepthsBelow_TemporaryKey.pfx + + + true + + + true + + + + ..\..\LuaInterface\LuaInterface.dll + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + False + + + + False + + + False + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {CD3F949D-54AA-4D38-99DB-92905A375D84} + AStar + + + DepthsBelowContent %28Content%29 + Content + {433A77A2-DE52-4875-A4EF-232CF59C2DD3} + + + + + False + Microsoft .NET Framework 4 Client Profile %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + False + Microsoft XNA Framework Redistributable 4.0 + true + + + + + + + + + + --> \ No newline at end of file diff --git a/DepthsBelow/DepthsBelow/Entity.cs b/DepthsBelow/DepthsBelow/Entity.cs index 572d108..5941f63 100644 --- a/DepthsBelow/DepthsBelow/Entity.cs +++ b/DepthsBelow/DepthsBelow/Entity.cs @@ -6,13 +6,11 @@ using Microsoft.Xna.Framework; namespace DepthsBelow { - public class Entity + public class Entity : IDisposable { List Components; public Component.Transform Transform; - public Component.PixelTransform pixelTransform; - public Component.GridTransform gridTransform; - public Component.Stat stat; + public int X { get { return this.Transform.Grid.X; } @@ -28,23 +26,35 @@ namespace DepthsBelow { this.Transform.Grid.Y = value; } - } - - protected Core core; - - public Entity(Core core) - { - this.core = core; + } + + private EntityManager entityManager; + public Entity(EntityManager entityManager) + { + this.entityManager = entityManager; + entityManager.Add(this); + Components = new List(); + Transform = new Component.Transform(this); AddComponent(Transform); - pixelTransform = new Component.PixelTransform(this); - AddComponent(pixelTransform); - gridTransform = new Component.GridTransform(this); - AddComponent(gridTransform); - stat = new Component.Stat(this); - AddComponent(stat); + } + + public virtual void Dispose() + { + + } + + public virtual void Remove() + { + entityManager.Remove(this); + Dispose(); + } + + public virtual void Kill() + { + Remove(); } public T GetComponent() where T : Component.Component diff --git a/DepthsBelow/DepthsBelow/EntityManager.cs b/DepthsBelow/DepthsBelow/EntityManager.cs new file mode 100755 index 0000000..c064d7b --- /dev/null +++ b/DepthsBelow/DepthsBelow/EntityManager.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; + +namespace DepthsBelow +{ + public class EntityManager : IDisposable + { + private List entities; + + public EntityManager() + { + entities = new List(); + } + + public void Dispose() + { + foreach (var entity in entities) + entity.Dispose(); + } + + public void Add(Entity entity) + { + entities.Add(entity); + } + + public void Remove(Entity entity) + { + entities.Remove(entity); + } + + public void Reset() + { + foreach (var entity in entities) + entity.Dispose(); + + entities.Clear(); + } + + public void Update(GameTime gameTime) + { + foreach (var entity in entities) + entity.Update(gameTime); + } + + public void Draw(SpriteBatch spriteBatch) + { + foreach (var entity in entities) + entity.GetComponent().Draw(spriteBatch); + } + } +} diff --git a/DepthsBelow/DepthsBelow/KeyboardInput.cs b/DepthsBelow/DepthsBelow/KeyboardInput.cs index b0b2f59..d40133d 100644 --- a/DepthsBelow/DepthsBelow/KeyboardInput.cs +++ b/DepthsBelow/DepthsBelow/KeyboardInput.cs @@ -82,7 +82,7 @@ namespace DepthsBelow { if (soldier.Selected == true && soldier.Fired == false) { - core.Volley.Add(new Shot(core)); + core.Volley.Add(new Shot(core.EntityManager)); soldier.Fired = true; foreach (var shot in core.Volley) { @@ -139,7 +139,7 @@ namespace DepthsBelow current = 0; if (soldier.Selected == true && soldier.Fired == false && hit == true) { - core.Volley.Add(new Shot(core)); + core.Volley.Add(new Shot(core.EntityManager)); soldier.Fired = true; foreach (var shot in core.Volley) { diff --git a/DepthsBelow/DepthsBelow/Map.cs b/DepthsBelow/DepthsBelow/Map.cs index b32861a..e089083 100755 --- a/DepthsBelow/DepthsBelow/Map.cs +++ b/DepthsBelow/DepthsBelow/Map.cs @@ -72,7 +72,7 @@ namespace DepthsBelow { if (mapObject.Type == "SquadStart") { - var soldier = new Soldier(core, ref core.Squad); + var soldier = new Soldier(core.EntityManager, ref core.Squad); // HACK: For some reason, the tile object coordinates are offset by one tile on the Y-axis in the Tiled map file (https://github.com/bjorn/tiled/issues/91) var mapObjectPos = new Vector2(mapObject.Bounds.X, mapObject.Bounds.Y - Grid.TileSize); var mapObjectGridPos = Grid.WorldToGrid(mapObjectPos); diff --git a/DepthsBelow/DepthsBelow/Shot.cs b/DepthsBelow/DepthsBelow/Shot.cs index c9878b4..8f2e229 100644 --- a/DepthsBelow/DepthsBelow/Shot.cs +++ b/DepthsBelow/DepthsBelow/Shot.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using DepthsBelow.Component; -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -28,11 +29,11 @@ namespace DepthsBelow public Vector2 direction = Vector2.Zero; - public Shot(Core core) - : base(core) + public Shot(EntityManager entityManager) + : base(entityManager) { if (Texture == null) - LoadContent(core); + LoadContent(); Transform.World.Origin = new Vector2(16, 16); @@ -44,9 +45,9 @@ namespace DepthsBelow AddComponent(cc); } - public static void LoadContent(Core core) - { - Texture = core.Content.Load("images/Shot"); + public static void LoadContent() + { + Texture = GameServices.GetService().Load("images/Shot"); } public override void Update(GameTime gameTime) { @@ -55,21 +56,6 @@ namespace DepthsBelow float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f; List soldierCollisions = new List(); - foreach (var body in core.Swarm) - { - if (this.GetComponent().Rectangle.Intersects(body.GetComponent().Rectangle)) - { - - } - } - - foreach (var soldier in core.Squad) - { - if (this.GetComponent().Rectangle.Intersects(soldier.GetComponent().Rectangle)) - { - - } - } // HACK: Make this work properly with other speeds... float speed = 4f; Transform.World.Position += speed * direction; diff --git a/DepthsBelow/DepthsBelow/SmallEnemy.cs b/DepthsBelow/DepthsBelow/SmallEnemy.cs index bee04bd..49763f5 100644 --- a/DepthsBelow/DepthsBelow/SmallEnemy.cs +++ b/DepthsBelow/DepthsBelow/SmallEnemy.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using DepthsBelow.Component; -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -30,11 +31,11 @@ namespace DepthsBelow } } - public SmallEnemy(Core core, ref List swarm) - : base(core) + public SmallEnemy(EntityManager entityManager, ref List swarm) + : base(entityManager) { if (Texture == null) - LoadContent(core); + LoadContent(); this.Swarm = swarm; @@ -48,13 +49,17 @@ namespace DepthsBelow AddComponent(cc); var pfc = new PathFinder(this); - AddComponent(pfc); - - stat.Life = 2; - stat.Defence = 0; - stat.Strength = 5000; - stat.GetAim = 100; - stat.GetDodge = 50; + AddComponent(pfc); + + var stat = new Component.Stat(this) + { + Life = 2, + Defence = 0, + Strength = 5000, + GetAim = 100, + GetDodge = 50 + }; + AddComponent(stat); } public bool Selected @@ -67,9 +72,9 @@ namespace DepthsBelow } } - public static void LoadContent(Core core) - { - Texture = core.Content.Load("images/Monster"); + public static void LoadContent() + { + Texture = GameServices.GetService().Load("images/Monster"); } public override void Update(GameTime gameTime) @@ -85,6 +90,7 @@ namespace DepthsBelow { List soldierCollisions = new List(); + // Collision with moving units foreach (var body in Swarm) { if (body != this) @@ -99,20 +105,6 @@ namespace DepthsBelow } } - foreach (var soldier in core.Squad) - { - var pathFinder = soldier.GetComponent(); - var position = nextNode.Position; - if (soldier.Transform.Grid == position) - { - soldierCollisions.Add(soldier.Transform.Grid); - GetComponent().RecreatePath(soldierCollisions); - nextNode = GetComponent().Next(); - break; - } - Utility.HitTest(this, soldier, Utility.CalculateHitChance(this, soldier)); - } - if (nextNode != null) { var nodeWorldPos = Grid.GridToWorld(nextNode.Position); diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index 509f5e3..21e9a96 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -1,7 +1,8 @@ using System; using System.Collections.Generic; using DepthsBelow.Component; -using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; @@ -41,12 +42,13 @@ namespace DepthsBelow { currentStep = value; } - } - - public Soldier(Core core, ref List squad) : base(core) + } + + public Soldier(EntityManager entityManager, ref List squad) + : base(entityManager) { if (Texture == null) - LoadContent(core); + LoadContent(); this.Squad = squad; @@ -60,13 +62,17 @@ namespace DepthsBelow AddComponent(cc); var pfc = new PathFinder(this); - AddComponent(pfc); - - stat.Life = 10; - stat.Defence = 10; - stat.Strength = 10; - stat.GetAim = 10; - stat.GetDodge = 20; + AddComponent(pfc); + + var stat = new Component.Stat(this) + { + Life = 10, + Defence = 10, + Strength = 10, + GetAim = 10, + GetDodge = 20 + }; + AddComponent(stat); } public bool Selected @@ -79,9 +85,9 @@ namespace DepthsBelow } } - public static void LoadContent(Core core) + public static void LoadContent() { - Texture = core.Content.Load("images/soldier2"); + Texture = GameServices.GetService().Load("images/soldier2"); } public override void Update(GameTime gameTime) @@ -97,6 +103,7 @@ namespace DepthsBelow { List soldierCollisions = new List(); + // Collision with units at a standstill foreach (var soldier in Squad) { if (soldier != this && !soldier.GetComponent().IsMoving) @@ -104,14 +111,8 @@ namespace DepthsBelow soldierCollisions.Add(soldier.Transform.Grid); } } - foreach (var enemy in core.Swarm) - { - if (!enemy.GetComponent().IsMoving) - { - soldierCollisions.Add(enemy.Transform.Grid); - } - } + // Collision with moving units foreach (var soldier in Squad) { if (soldier != this) @@ -127,18 +128,6 @@ namespace DepthsBelow } } } - foreach (var enemy in core.Swarm) - { - var pathFinder = enemy.GetComponent(); - var position = nextNode.Position; - if (enemy.Transform.Grid == position) - { - soldierCollisions.Add(enemy.Transform.Grid); - GetComponent().RecreatePath(soldierCollisions); - nextNode = GetComponent().Next(); - break; - } - } if (nextNode != null) { @@ -169,10 +158,7 @@ namespace DepthsBelow } } } - } - - /**/ } } } \ No newline at end of file