Created and implemented EntityManager. PixelTransform and GridTransform phased out.

This commit is contained in:
Simon Holmberg
2012-10-17 23:44:58 +02:00
parent 8e1f18d233
commit c59ff9778a
11 changed files with 352 additions and 450 deletions
@@ -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);
}
}
}
@@ -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);
}
}
}
+11 -40
View File
@@ -24,6 +24,8 @@ namespace DepthsBelow
public Lua Lua;
public EntityManager EntityManager;
public Camera Camera;
public bool PlayerTurn = true;
@@ -64,13 +66,12 @@ namespace DepthsBelow
protected override void Initialize()
{
// TODO: Add your initialization logic here
EntityManager = new EntityManager();
Camera = new Camera(this);
Squad = new List<Soldier>();
Swarm = new List<SmallEnemy>();
Volley = new List<Shot>();
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);
Swarm.Add(TestMonster);
TestMonster = new SmallEnemy(EntityManager, ref Swarm);
TestMonster.X = 12;
TestMonster.Y = 4;
//frame.AddChild(text);
Swarm.Add(TestMonster);
}
/// <summary>
@@ -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<Component.SpriteRenderer>();
if (sr != null)
sr.Draw(spriteBatch);
}
foreach (var body in Swarm)
{
var sr = body.GetComponent<Component.SpriteRenderer>();
if (sr != null)
sr.Draw(spriteBatch);
}
foreach (var bullet in Volley)
{
var sr = bullet.GetComponent<Component.SpriteRenderer>();
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);
}
}
}
+1 -2
View File
@@ -121,6 +121,7 @@
<ItemGroup>
<Compile Include="Camera.cs" />
<Compile Include="CustomExtensions.cs" />
<Compile Include="EntityManager.cs" />
<Compile Include="GameServices.cs" />
<Compile Include="Interface.cs" />
<Compile Include="Lua.cs" />
@@ -136,7 +137,6 @@
<Compile Include="Grid.cs" />
<Compile Include="Component\Collision.cs" />
<Compile Include="Component\Component.cs" />
<Compile Include="Component\GridTransform.cs" />
<Compile Include="Core.cs" />
<Compile Include="Entity.cs" />
<Compile Include="Map.cs" />
@@ -147,7 +147,6 @@
<Compile Include="RenderHelpers.cs" />
<Compile Include="SmallEnemy.cs" />
<Compile Include="Soldier.cs" />
<Compile Include="Component\PixelTransform.cs" />
<Compile Include="Utility.cs" />
</ItemGroup>
<ItemGroup>
+23 -13
View File
@@ -6,13 +6,11 @@ using Microsoft.Xna.Framework;
namespace DepthsBelow
{
public class Entity
public class Entity : IDisposable
{
List<Component.Component> 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; }
@@ -30,21 +28,33 @@ namespace DepthsBelow
}
}
protected Core core;
private EntityManager entityManager;
public Entity(Core core)
public Entity(EntityManager entityManager)
{
this.core = core;
this.entityManager = entityManager;
entityManager.Add(this);
Components = new List<Component.Component>();
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<T>() where T : Component.Component
+55
View File
@@ -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<Entity> entities;
public EntityManager()
{
entities = new List<Entity>();
}
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<Component.SpriteRenderer>().Draw(spriteBatch);
}
}
}
+2 -2
View File
@@ -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)
{
+1 -1
View File
@@ -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);
+6 -20
View File
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using DepthsBelow.Component;
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)
public static void LoadContent()
{
Texture = core.Content.Load<Texture2D>("images/Shot");
Texture = GameServices.GetService<ContentManager>().Load<Texture2D>("images/Shot");
}
public override void Update(GameTime gameTime)
{
@@ -55,21 +56,6 @@ namespace DepthsBelow
float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
List<Point> soldierCollisions = new List<Point>();
foreach (var body in core.Swarm)
{
if (this.GetComponent<Collision>().Rectangle.Intersects(body.GetComponent<Collision>().Rectangle))
{
}
}
foreach (var soldier in core.Squad)
{
if (this.GetComponent<Collision>().Rectangle.Intersects(soldier.GetComponent<Collision>().Rectangle))
{
}
}
// HACK: Make this work properly with other speeds...
float speed = 4f;
Transform.World.Position += speed * direction;
+16 -24
View File
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using DepthsBelow.Component;
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<SmallEnemy> swarm)
: base(core)
public SmallEnemy(EntityManager entityManager, ref List<SmallEnemy> swarm)
: base(entityManager)
{
if (Texture == null)
LoadContent(core);
LoadContent();
this.Swarm = swarm;
@@ -50,11 +51,15 @@ namespace DepthsBelow
var pfc = new PathFinder(this);
AddComponent(pfc);
stat.Life = 2;
stat.Defence = 0;
stat.Strength = 5000;
stat.GetAim = 100;
stat.GetDodge = 50;
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)
public static void LoadContent()
{
Texture = core.Content.Load<Texture2D>("images/Monster");
Texture = GameServices.GetService<ContentManager>().Load<Texture2D>("images/Monster");
}
public override void Update(GameTime gameTime)
@@ -85,6 +90,7 @@ namespace DepthsBelow
{
List<Point> soldierCollisions = new List<Point>();
// 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<PathFinder>();
var position = nextNode.Position;
if (soldier.Transform.Grid == position)
{
soldierCollisions.Add(soldier.Transform.Grid);
GetComponent<PathFinder>().RecreatePath(soldierCollisions);
nextNode = GetComponent<PathFinder>().Next();
break;
}
Utility.HitTest(this, soldier, Utility.CalculateHitChance(this, soldier));
}
if (nextNode != null)
{
var nodeWorldPos = Grid.GridToWorld(nextNode.Position);
+17 -31
View File
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using DepthsBelow.Component;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
@@ -43,10 +44,11 @@ namespace DepthsBelow
}
}
public Soldier(Core core, ref List<Soldier> squad) : base(core)
public Soldier(EntityManager entityManager, ref List<Soldier> squad)
: base(entityManager)
{
if (Texture == null)
LoadContent(core);
LoadContent();
this.Squad = squad;
@@ -62,11 +64,15 @@ namespace DepthsBelow
var pfc = new PathFinder(this);
AddComponent(pfc);
stat.Life = 10;
stat.Defence = 10;
stat.Strength = 10;
stat.GetAim = 10;
stat.GetDodge = 20;
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<Texture2D>("images/soldier2");
Texture = GameServices.GetService<ContentManager>().Load<Texture2D>("images/soldier2");
}
public override void Update(GameTime gameTime)
@@ -97,6 +103,7 @@ namespace DepthsBelow
{
List<Point> soldierCollisions = new List<Point>();
// Collision with units at a standstill
foreach (var soldier in Squad)
{
if (soldier != this && !soldier.GetComponent<PathFinder>().IsMoving)
@@ -104,14 +111,8 @@ namespace DepthsBelow
soldierCollisions.Add(soldier.Transform.Grid);
}
}
foreach (var enemy in core.Swarm)
{
if (!enemy.GetComponent<PathFinder>().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<PathFinder>();
var position = nextNode.Position;
if (enemy.Transform.Grid == position)
{
soldierCollisions.Add(enemy.Transform.Grid);
GetComponent<PathFinder>().RecreatePath(soldierCollisions);
nextNode = GetComponent<PathFinder>().Next();
break;
}
}
if (nextNode != null)
{
@@ -169,10 +158,7 @@ namespace DepthsBelow
}
}
}
}
/**/
}
}
}