diff --git a/DepthsBelow/DepthsBelow/Component/Collision.cs b/DepthsBelow/DepthsBelow/Component/Collision.cs index 53b0149..c61f7fa 100644 --- a/DepthsBelow/DepthsBelow/Component/Collision.cs +++ b/DepthsBelow/DepthsBelow/Component/Collision.cs @@ -1,8 +1,10 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; namespace DepthsBelow.Component { @@ -21,11 +23,12 @@ namespace DepthsBelow.Component this.Height = height; this.Rectangle = new Rectangle(0, 0, width, height); } + public override void Update(GameTime gameTime) { - PixelTransform pt = this.Parent.GetComponent(); - this.Rectangle.X = (int)pt.X; - this.Rectangle.Y = (int)pt.Y; + Transform transform = this.Parent.GetComponent(); + this.Rectangle.X = (int)transform.World.X; + this.Rectangle.Y = (int)transform.World.Y; } } } diff --git a/DepthsBelow/DepthsBelow/Component/GridTransform.cs b/DepthsBelow/DepthsBelow/Component/GridTransform.cs index aa8b7ab..823aeba 100644 --- a/DepthsBelow/DepthsBelow/Component/GridTransform.cs +++ b/DepthsBelow/DepthsBelow/Component/GridTransform.cs @@ -6,6 +6,7 @@ using Microsoft.Xna.Framework; namespace DepthsBelow.Component { + [Obsolete("Deprecated in favour of Component.Transform")] public class GridTransform : Component { public Point Position; diff --git a/DepthsBelow/DepthsBelow/Component/PathFinder.cs b/DepthsBelow/DepthsBelow/Component/PathFinder.cs index 072d55b..8bd0530 100755 --- a/DepthsBelow/DepthsBelow/Component/PathFinder.cs +++ b/DepthsBelow/DepthsBelow/Component/PathFinder.cs @@ -3,50 +3,51 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; +using AStar; using Microsoft.Xna.Framework; namespace DepthsBelow.Component { - class PathFinder : Component + public class PathFinder : Component { + public class Node + { + public Point Position; + public int F; + public int G; + + public static explicit operator Node(AStar.PathFinderNode aStarNode) + { + return new Node + { + Position = new Point(aStarNode.X, aStarNode.Y), + F = aStarNode.F, + G = aStarNode.G + }; + } + } + public Point Start { get; private set; } + + private Point goal; public Point Goal { get { return goal; } set { this.goal = value; - this.Start = this.Parent.GetComponent().Position; + this.Start = this.Parent.GetComponent().Grid.Position; this.FindPath(this.Start, value); } } - - private Point goal; + + private AStar.PathFinderFast pathFinder; private List path; public PathFinder(Entity parent) : base(parent) { - - } - - public override void Update(GameTime gameTime) - { - if (path != null && path.Count != 0) - { - var nextNode = path.Last(); - var nodePos = new Point(nextNode.X, nextNode.Y); - Parent.gridTransform.Position = nodePos; - if (Parent.pixelTransform.Position == Parent.gridTransform.ToWorld()) - path.Remove(nextNode); - } - - base.Update(gameTime); - } - - public void FindPath(Point start, Point goal) - { - AStar.PathFinderFast pathFinder = new AStar.PathFinderFast(Core.Map.GetCollisionMap()); + pathFinder = new AStar.PathFinderFast(Core.Map.GetCollisionMap()); pathFinder.Formula = AStar.HeuristicFormula.Manhattan; pathFinder.Diagonals = false; pathFinder.HeavyDiagonals = false; @@ -56,13 +57,45 @@ namespace DepthsBelow.Component pathFinder.SearchLimit = 50000; pathFinder.DebugProgress = false; pathFinder.DebugFoundPath = false; + } + public override void Update(GameTime gameTime) + { + /*if (path != null && path.Count != 0) + { + var nextNode = path.Last(); + var nodePos = new Point(nextNode.X, nextNode.Y); + Parent.Transform.Grid.Position = nodePos; + if (Parent.Transform.World == (Vector2)Parent.Transform.Grid) + path.Remove(nextNode); + }*/ + + base.Update(gameTime); + } + + public Node Next() + { + if (path == null) + return null; + + if (path.Count == 0) + return null; + + var nextNode = path.Last(); + path.Remove(nextNode); + return (Node)nextNode; + } + + public List FindPath(Point start, Point goal) + { var _start = new System.Drawing.Point(start.X, start.Y); var _goal = new System.Drawing.Point(goal.X, goal.Y); //path = Core.PathFinder.FindPath(_start, _goal); path = pathFinder.FindPath(_start, _goal); if (path == null) Debug.WriteLine(this.Parent.ToString() + ": Path not found!"); + + return path; } } } diff --git a/DepthsBelow/DepthsBelow/Component/PixelTransform.cs b/DepthsBelow/DepthsBelow/Component/PixelTransform.cs index e9332d3..865b6f0 100644 --- a/DepthsBelow/DepthsBelow/Component/PixelTransform.cs +++ b/DepthsBelow/DepthsBelow/Component/PixelTransform.cs @@ -6,6 +6,7 @@ using Microsoft.Xna.Framework; namespace DepthsBelow.Component { + [Obsolete("Deprecated in favour of Component.Transform")] public class PixelTransform : Component { public Vector2 Position; diff --git a/DepthsBelow/DepthsBelow/Component/SpriteRenderer.cs b/DepthsBelow/DepthsBelow/Component/SpriteRenderer.cs index ab0972b..e7a2093 100644 --- a/DepthsBelow/DepthsBelow/Component/SpriteRenderer.cs +++ b/DepthsBelow/DepthsBelow/Component/SpriteRenderer.cs @@ -23,9 +23,9 @@ namespace DepthsBelow.Component public void Draw(SpriteBatch spriteBatch) { - PixelTransform transform = this.Parent.GetComponent(); + Transform transform = this.Parent.GetComponent(); //spriteBatch.Draw(Texture, tc.Position, this.Color); - spriteBatch.Draw(Texture, transform.Position + transform.Origin, null, Color, transform.Rotation, transform.Origin, Scale, SpriteEffects, 0); + spriteBatch.Draw(Texture, transform.World.Position + transform.World.Origin, null, Color, transform.World.Rotation, transform.World.Origin, Scale, SpriteEffects, 0); } } } diff --git a/DepthsBelow/DepthsBelow/Component/Transform.cs b/DepthsBelow/DepthsBelow/Component/Transform.cs new file mode 100755 index 0000000..6385688 --- /dev/null +++ b/DepthsBelow/DepthsBelow/Component/Transform.cs @@ -0,0 +1,145 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Text; +using Microsoft.Xna.Framework; + +namespace DepthsBelow.Component +{ + public class Transform : Component + { + public class GridTransform + { + private Point position; + public Point Position + { + get { return position; } + set + { + position = value; + masterTransform.World.SetWithoutRelation((Vector2)this); + } + } + + public int X + { + get { return position.X; } + set + { + position.X = value; + masterTransform.World.SetWithoutRelation((Vector2)this); + } + } + public int Y + { + get { return position.Y; } + set + { + position.Y = value; + masterTransform.World.SetWithoutRelation((Vector2)this); + } + } + + public static implicit operator Point(GridTransform gridTransform) + { + return gridTransform.position; + } + + public static explicit operator Vector2(GridTransform gridTransform) + { + var worldPosition = DepthsBelow.Grid.GridToWorld(gridTransform.Position); + return new Vector2(worldPosition.X, worldPosition.Y); + } + + private readonly Transform masterTransform; + public GridTransform(Transform masterTransform) + { + this.masterTransform = masterTransform; + } + + public void SetWithoutRelation(Point position) + { + this.position = position; + } + } + + public class WorldTransform + { + private Vector2 position; + public Vector2 Position + { + get { return position; } + set + { + position = value; + masterTransform.Grid.SetWithoutRelation((Point)this); + } + } + + public float X + { + get { return position.X; } + set + { + position.X = value; + masterTransform.Grid.SetWithoutRelation((Point)this); + } + } + public float Y + { + get { return position.Y; } + set + { + position.Y = value; + masterTransform.Grid.SetWithoutRelation((Point)this); + } + } + + public float Rotation; + public Vector2 Origin; + + public static implicit operator Vector2(WorldTransform worldTransform) + { + return worldTransform.Position; + } + + public static explicit operator Point(WorldTransform worldTransform) + { + var gridPosition = DepthsBelow.Grid.WorldToGrid(worldTransform.Position); + return new Point(gridPosition.X, gridPosition.Y); + } + + private readonly Transform masterTransform; + public WorldTransform(Transform masterTransform) + { + this.masterTransform = masterTransform; + } + + public void SetWithoutRelation(Vector2 position) + { + this.position = position; + } + } + + public GridTransform Grid; + public WorldTransform World; + + public Transform(Entity parent) + : base(parent) + { + Grid = new GridTransform(this); + World = new WorldTransform(this); + } + + public static implicit operator Point(Transform transform) + { + return transform.Grid; + } + + public static implicit operator Vector2(Transform transform) + { + return transform.World; + } + } +} diff --git a/DepthsBelow/DepthsBelow/DepthsBelow.csproj b/DepthsBelow/DepthsBelow/DepthsBelow.csproj index c935820..fe80598 100644 --- a/DepthsBelow/DepthsBelow/DepthsBelow.csproj +++ b/DepthsBelow/DepthsBelow/DepthsBelow.csproj @@ -123,6 +123,7 @@ + diff --git a/DepthsBelow/DepthsBelow/Entity.cs b/DepthsBelow/DepthsBelow/Entity.cs index b35f26e..26935e9 100644 --- a/DepthsBelow/DepthsBelow/Entity.cs +++ b/DepthsBelow/DepthsBelow/Entity.cs @@ -9,24 +9,23 @@ namespace DepthsBelow public class Entity { List Components; + public Component.Transform Transform; public Component.PixelTransform pixelTransform; public Component.GridTransform gridTransform; public int X { - get { return this.gridTransform.X; } + get { return this.Transform.Grid.X; } set - { - this.gridTransform.X = value; - this.pixelTransform.X = this.gridTransform.ToWorld().X; + { + this.Transform.Grid.X = value; } } public int Y { - get { return this.gridTransform.Y; } + get { return this.Transform.Grid.Y; } set { - this.gridTransform.Y = value; - this.pixelTransform.Y = this.gridTransform.ToWorld().Y; + this.Transform.Grid.Y = value; } } @@ -37,6 +36,8 @@ namespace DepthsBelow this.core = core; Components = new List(); + Transform = new Component.Transform(this); + AddComponent(Transform); pixelTransform = new Component.PixelTransform(this); AddComponent(pixelTransform); gridTransform = new Component.GridTransform(this); diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index bda0099..4b5f9e2 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -13,16 +13,14 @@ namespace DepthsBelow public static Point Origin; private bool _selected; - private KeyboardState lastKeyboardState; + private PathFinder.Node nextNode; public Soldier(Core core) : base(core) { if (Texture == null) LoadContent(core); - pixelTransform.Origin = new Vector2(16, 16); - //gridTransform.Position = new Point(7, 3); - //pixelTransform.Position = gridTransform.ToWorld(); + Transform.World.Origin = new Vector2(16, 16); this.Color = Color.White; var rc = new SpriteRenderer(this) {Texture = Texture, Color = Color.White}; @@ -53,39 +51,30 @@ namespace DepthsBelow public override void Update(GameTime gameTime) { base.Update(gameTime); - KeyboardState ks = Keyboard.GetState(); - if (ks.IsKeyDown(Keys.Right) && lastKeyboardState.IsKeyUp(Keys.Right)) + + if (nextNode == null) + nextNode = GetComponent().Next(); + + if (nextNode != null) { - gridTransform.X += 1; - pixelTransform.Rotation = (float)0; + var nodeWorldPos = Grid.GridToWorld(nextNode.Position); + // HACK: Make this work properly with other speeds... + int speed = 2*4; + if (Transform.World.X < nodeWorldPos.X) + Transform.World.X += speed; + if (Transform.World.X > nodeWorldPos.X) + Transform.World.X -= speed; + if (Transform.World.Y < nodeWorldPos.Y) + Transform.World.Y += speed; + if (Transform.World.Y > nodeWorldPos.Y) + Transform.World.Y -= speed; + + if (Transform.World == nodeWorldPos) + nextNode = GetComponent().Next(); + } - if (ks.IsKeyDown(Keys.Left) && lastKeyboardState.IsKeyUp(Keys.Left)) - { - gridTransform.X -= 1; - pixelTransform.Rotation = (float)Math.PI; - } - if (ks.IsKeyDown(Keys.Up) && lastKeyboardState.IsKeyUp(Keys.Up)) - { - gridTransform.Y -= 1; - pixelTransform.Rotation = (float)(3.0f*Math.PI/2.0f); - } - if (ks.IsKeyDown(Keys.Down) && lastKeyboardState.IsKeyUp(Keys.Down)) - { - gridTransform.Y += 1; - pixelTransform.Rotation = (float)(Math.PI / 2.0f); - } - lastKeyboardState = ks; - - int speed = 1; - if (pixelTransform.X < gridTransform.ToWorld().X) - pixelTransform.X += speed; - if (pixelTransform.X > gridTransform.ToWorld().X) - pixelTransform.X -= speed; - if (pixelTransform.Y < gridTransform.ToWorld().Y) - pixelTransform.Y += speed; - if (pixelTransform.Y > gridTransform.ToWorld().Y) - pixelTransform.Y -= speed; + /**/ } } } \ No newline at end of file