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:
@@ -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<PixelTransform>();
|
||||
this.Rectangle.X = (int)pt.X;
|
||||
this.Rectangle.Y = (int)pt.Y;
|
||||
Transform transform = this.Parent.GetComponent<Transform>();
|
||||
this.Rectangle.X = (int)transform.World.X;
|
||||
this.Rectangle.Y = (int)transform.World.Y;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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<GridTransform>().Position;
|
||||
this.Start = this.Parent.GetComponent<Transform>().Grid.Position;
|
||||
this.FindPath(this.Start, value);
|
||||
}
|
||||
}
|
||||
|
||||
private Point goal;
|
||||
|
||||
private AStar.PathFinderFast pathFinder;
|
||||
private List<AStar.PathFinderNode> 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<AStar.PathFinderNode> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -23,9 +23,9 @@ namespace DepthsBelow.Component
|
||||
|
||||
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, 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
@@ -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>
|
||||
<Compile Include="Camera.cs" />
|
||||
<Compile Include="Component\PathFinder.cs" />
|
||||
<Compile Include="Component\Transform.cs" />
|
||||
<Compile Include="Grid.cs" />
|
||||
<Compile Include="Component\Collision.cs" />
|
||||
<Compile Include="Component\Component.cs" />
|
||||
|
||||
@@ -9,24 +9,23 @@ namespace DepthsBelow
|
||||
public class Entity
|
||||
{
|
||||
List<Component.Component> 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<Component.Component>();
|
||||
Transform = new Component.Transform(this);
|
||||
AddComponent(Transform);
|
||||
pixelTransform = new Component.PixelTransform(this);
|
||||
AddComponent(pixelTransform);
|
||||
gridTransform = new Component.GridTransform(this);
|
||||
|
||||
@@ -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<PathFinder>().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<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;
|
||||
/**/
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user