Deprecated Component.GridTransform and Component.PixelTransform in favour of new Component.Transform which keeps track of both the grid position and the world position, while keeping them related.

This commit is contained in:
Simon Holmberg
2012-09-30 13:51:04 +02:00
parent 57789c4600
commit f8e9ec4630
9 changed files with 244 additions and 70 deletions
@@ -1,8 +1,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace DepthsBelow.Component namespace DepthsBelow.Component
{ {
@@ -21,11 +23,12 @@ namespace DepthsBelow.Component
this.Height = height; this.Height = height;
this.Rectangle = new Rectangle(0, 0, width, height); this.Rectangle = new Rectangle(0, 0, width, height);
} }
public override void Update(GameTime gameTime) public override void Update(GameTime gameTime)
{ {
PixelTransform pt = this.Parent.GetComponent<PixelTransform>(); Transform transform = this.Parent.GetComponent<Transform>();
this.Rectangle.X = (int)pt.X; this.Rectangle.X = (int)transform.World.X;
this.Rectangle.Y = (int)pt.Y; this.Rectangle.Y = (int)transform.World.Y;
} }
} }
} }
@@ -6,6 +6,7 @@ using Microsoft.Xna.Framework;
namespace DepthsBelow.Component namespace DepthsBelow.Component
{ {
[Obsolete("Deprecated in favour of Component.Transform")]
public class GridTransform : Component public class GridTransform : Component
{ {
public Point Position; public Point Position;
+56 -23
View File
@@ -3,50 +3,51 @@ using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using AStar;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
namespace DepthsBelow.Component 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; } public Point Start { get; private set; }
private Point goal;
public Point Goal public Point Goal
{ {
get { return goal; } get { return goal; }
set set
{ {
this.goal = value; this.goal = value;
this.Start = this.Parent.GetComponent<GridTransform>().Position; this.Start = this.Parent.GetComponent<Transform>().Grid.Position;
this.FindPath(this.Start, value); this.FindPath(this.Start, value);
} }
} }
private Point goal; private AStar.PathFinderFast pathFinder;
private List<AStar.PathFinderNode> path; private List<AStar.PathFinderNode> path;
public PathFinder(Entity parent) public PathFinder(Entity parent)
: base(parent) : base(parent)
{ {
pathFinder = new AStar.PathFinderFast(Core.Map.GetCollisionMap());
}
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.Formula = AStar.HeuristicFormula.Manhattan; pathFinder.Formula = AStar.HeuristicFormula.Manhattan;
pathFinder.Diagonals = false; pathFinder.Diagonals = false;
pathFinder.HeavyDiagonals = false; pathFinder.HeavyDiagonals = false;
@@ -56,13 +57,45 @@ namespace DepthsBelow.Component
pathFinder.SearchLimit = 50000; pathFinder.SearchLimit = 50000;
pathFinder.DebugProgress = false; pathFinder.DebugProgress = false;
pathFinder.DebugFoundPath = 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<AStar.PathFinderNode> FindPath(Point start, Point goal)
{
var _start = new System.Drawing.Point(start.X, start.Y); var _start = new System.Drawing.Point(start.X, start.Y);
var _goal = new System.Drawing.Point(goal.X, goal.Y); var _goal = new System.Drawing.Point(goal.X, goal.Y);
//path = Core.PathFinder.FindPath(_start, _goal); //path = Core.PathFinder.FindPath(_start, _goal);
path = pathFinder.FindPath(_start, _goal); path = pathFinder.FindPath(_start, _goal);
if (path == null) if (path == null)
Debug.WriteLine(this.Parent.ToString() + ": Path not found!"); Debug.WriteLine(this.Parent.ToString() + ": Path not found!");
return path;
} }
} }
} }
@@ -6,6 +6,7 @@ using Microsoft.Xna.Framework;
namespace DepthsBelow.Component namespace DepthsBelow.Component
{ {
[Obsolete("Deprecated in favour of Component.Transform")]
public class PixelTransform : Component public class PixelTransform : Component
{ {
public Vector2 Position; public Vector2 Position;
@@ -23,9 +23,9 @@ namespace DepthsBelow.Component
public void Draw(SpriteBatch spriteBatch) public void Draw(SpriteBatch spriteBatch)
{ {
PixelTransform transform = this.Parent.GetComponent<PixelTransform>(); Transform transform = this.Parent.GetComponent<Transform>();
//spriteBatch.Draw(Texture, tc.Position, this.Color); //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);
} }
} }
} }
+145
View File
@@ -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;
}
}
}
@@ -123,6 +123,7 @@
<ItemGroup> <ItemGroup>
<Compile Include="Camera.cs" /> <Compile Include="Camera.cs" />
<Compile Include="Component\PathFinder.cs" /> <Compile Include="Component\PathFinder.cs" />
<Compile Include="Component\Transform.cs" />
<Compile Include="Grid.cs" /> <Compile Include="Grid.cs" />
<Compile Include="Component\Collision.cs" /> <Compile Include="Component\Collision.cs" />
<Compile Include="Component\Component.cs" /> <Compile Include="Component\Component.cs" />
+7 -6
View File
@@ -9,24 +9,23 @@ namespace DepthsBelow
public class Entity public class Entity
{ {
List<Component.Component> Components; List<Component.Component> Components;
public Component.Transform Transform;
public Component.PixelTransform pixelTransform; public Component.PixelTransform pixelTransform;
public Component.GridTransform gridTransform; public Component.GridTransform gridTransform;
public int X public int X
{ {
get { return this.gridTransform.X; } get { return this.Transform.Grid.X; }
set set
{ {
this.gridTransform.X = value; this.Transform.Grid.X = value;
this.pixelTransform.X = this.gridTransform.ToWorld().X;
} }
} }
public int Y public int Y
{ {
get { return this.gridTransform.Y; } get { return this.Transform.Grid.Y; }
set set
{ {
this.gridTransform.Y = value; this.Transform.Grid.Y = value;
this.pixelTransform.Y = this.gridTransform.ToWorld().Y;
} }
} }
@@ -37,6 +36,8 @@ namespace DepthsBelow
this.core = core; this.core = core;
Components = new List<Component.Component>(); Components = new List<Component.Component>();
Transform = new Component.Transform(this);
AddComponent(Transform);
pixelTransform = new Component.PixelTransform(this); pixelTransform = new Component.PixelTransform(this);
AddComponent(pixelTransform); AddComponent(pixelTransform);
gridTransform = new Component.GridTransform(this); gridTransform = new Component.GridTransform(this);
+23 -34
View File
@@ -13,16 +13,14 @@ namespace DepthsBelow
public static Point Origin; public static Point Origin;
private bool _selected; private bool _selected;
private KeyboardState lastKeyboardState; private PathFinder.Node nextNode;
public Soldier(Core core) : base(core) public Soldier(Core core) : base(core)
{ {
if (Texture == null) if (Texture == null)
LoadContent(core); LoadContent(core);
pixelTransform.Origin = new Vector2(16, 16); Transform.World.Origin = new Vector2(16, 16);
//gridTransform.Position = new Point(7, 3);
//pixelTransform.Position = gridTransform.ToWorld();
this.Color = Color.White; this.Color = Color.White;
var rc = new SpriteRenderer(this) {Texture = Texture, 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) public override void Update(GameTime gameTime)
{ {
base.Update(gameTime); base.Update(gameTime);
KeyboardState ks = Keyboard.GetState();
if (ks.IsKeyDown(Keys.Right) && lastKeyboardState.IsKeyUp(Keys.Right)) if (nextNode == null)
nextNode = GetComponent<PathFinder>().Next();
if (nextNode != null)
{ {
gridTransform.X += 1; var nodeWorldPos = Grid.GridToWorld(nextNode.Position);
pixelTransform.Rotation = (float)0; // 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<PathFinder>().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;
} }
} }
} }