Merge branch 'master' of github.com:sippeangelo/DepthsBelow
This commit is contained in:
@@ -29,6 +29,8 @@ namespace DepthsBelow.Component
|
||||
|
||||
public Point Start { get; private set; }
|
||||
|
||||
public byte[,] CollisionMap;
|
||||
|
||||
private Point goal;
|
||||
public Point Goal
|
||||
{
|
||||
@@ -36,8 +38,8 @@ namespace DepthsBelow.Component
|
||||
set
|
||||
{
|
||||
this.goal = value;
|
||||
this.Start = this.Parent.GetComponent<Transform>().Grid.Position;
|
||||
this.FindPath(this.Start, value);
|
||||
//this.Start = this.Parent.GetComponent<Transform>().Grid.Position;
|
||||
this.FindPath(this.Parent.GetComponent<Transform>().Grid.Position, value, null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,16 +49,7 @@ namespace DepthsBelow.Component
|
||||
public PathFinder(Entity parent)
|
||||
: base(parent)
|
||||
{
|
||||
pathFinder = new AStar.PathFinderFast(Core.Map.GetCollisionMap());
|
||||
pathFinder.Formula = AStar.HeuristicFormula.Manhattan;
|
||||
pathFinder.Diagonals = false;
|
||||
pathFinder.HeavyDiagonals = false;
|
||||
pathFinder.HeuristicEstimate = 2;
|
||||
pathFinder.PunishChangeDirection = true;
|
||||
pathFinder.TieBreaker = false;
|
||||
pathFinder.SearchLimit = 50000;
|
||||
pathFinder.DebugProgress = false;
|
||||
pathFinder.DebugFoundPath = false;
|
||||
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
@@ -86,8 +79,25 @@ namespace DepthsBelow.Component
|
||||
return (Node)nextNode;
|
||||
}
|
||||
|
||||
public List<AStar.PathFinderNode> FindPath(Point start, Point goal)
|
||||
private List<AStar.PathFinderNode> FindPath(Point start, Point goal, List<Point> appendCollisionMap)
|
||||
{
|
||||
byte[,] mapCollisionMap = Core.Map.GetCollisionMap();
|
||||
|
||||
if (appendCollisionMap != null)
|
||||
foreach (var point in appendCollisionMap)
|
||||
mapCollisionMap[point.X, point.Y] = 0;
|
||||
|
||||
pathFinder = new AStar.PathFinderFast(mapCollisionMap);
|
||||
pathFinder.Formula = AStar.HeuristicFormula.Manhattan;
|
||||
pathFinder.Diagonals = false;
|
||||
pathFinder.HeavyDiagonals = false;
|
||||
pathFinder.HeuristicEstimate = 2;
|
||||
pathFinder.PunishChangeDirection = false;
|
||||
pathFinder.TieBreaker = false;
|
||||
pathFinder.SearchLimit = 50000;
|
||||
pathFinder.DebugProgress = false;
|
||||
pathFinder.DebugFoundPath = false;
|
||||
|
||||
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);
|
||||
@@ -95,7 +105,14 @@ namespace DepthsBelow.Component
|
||||
if (path == null)
|
||||
Debug.WriteLine(this.Parent.ToString() + ": Path not found!");
|
||||
|
||||
this.CollisionMap = new byte[100, 100];
|
||||
|
||||
return path;
|
||||
}
|
||||
|
||||
public void RecreatePath(List<Point> appendCollisionMap)
|
||||
{
|
||||
FindPath(this.Parent.GetComponent<Transform>().Grid.Position, this.Goal, appendCollisionMap);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,6 +97,12 @@ namespace DepthsBelow.Component
|
||||
}
|
||||
|
||||
public float Rotation;
|
||||
|
||||
public void Rotate(int directionX, int directionY)
|
||||
{
|
||||
Rotation = (float)Math.Atan(directionY/directionX);
|
||||
}
|
||||
|
||||
public Vector2 Origin;
|
||||
|
||||
public static implicit operator Vector2(WorldTransform worldTransform)
|
||||
|
||||
@@ -29,7 +29,7 @@ namespace DepthsBelow
|
||||
}
|
||||
}
|
||||
|
||||
private Core core;
|
||||
protected Core core;
|
||||
|
||||
public Entity(Core core)
|
||||
{
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace DepthsBelow
|
||||
{
|
||||
if (mapObject.Type == "SquadStart")
|
||||
{
|
||||
var soldier = new Soldier(core);
|
||||
var soldier = new Soldier(core, 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);
|
||||
|
||||
@@ -19,6 +19,12 @@ namespace DepthsBelow
|
||||
Rectangle gridRectangle;
|
||||
Texture2D gridTexture;
|
||||
|
||||
Vector2 directionStart;
|
||||
Vector2 directionNow;
|
||||
|
||||
bool checkingDirection = false;
|
||||
|
||||
|
||||
public MouseInput(Core core)
|
||||
{
|
||||
this.core = core;
|
||||
@@ -27,6 +33,7 @@ namespace DepthsBelow
|
||||
gridRectangle = Rectangle.Empty;
|
||||
}
|
||||
|
||||
|
||||
public void LoadContent()
|
||||
{
|
||||
selectionTexture = new Texture2D(core.GraphicsDevice, 1, 1);
|
||||
@@ -80,9 +87,52 @@ namespace DepthsBelow
|
||||
if (ms.RightButton == ButtonState.Released && lastMouseState.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
if (unit.Selected)
|
||||
if (unit.Selected)
|
||||
{
|
||||
unit.GetComponent<Component.PathFinder>().Goal = Grid.WorldToGrid(mouseWorldPos);
|
||||
}
|
||||
|
||||
}
|
||||
if (ms.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
{
|
||||
if (unit.Selected && gridRectangle.Intersects(unit.GetComponent<Component.Collision>().Rectangle))
|
||||
{
|
||||
checkingDirection = true;
|
||||
directionStart = unit.Transform.World + unit.Transform.World.Origin;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (checkingDirection == true && ms.RightButton == ButtonState.Pressed)
|
||||
{
|
||||
foreach (var unit in core.Squad)
|
||||
if (unit.Selected)
|
||||
{
|
||||
directionNow.X = mouseWorldPos.X;
|
||||
directionNow.Y = mouseWorldPos.Y;
|
||||
float directionX = mouseWorldPos.X - unit.Transform.World.X;
|
||||
float directionY = mouseWorldPos.Y - unit.Transform.World.Y;
|
||||
var mouseDirection = mouseWorldPos;
|
||||
mouseDirection.Normalize();
|
||||
//float angle = (float)Math.Acos(Vector2.Dot(new Vector2(0, 1), mouseDirection)) * MathHelper.TwoPi;
|
||||
/*if (directionX < 0)
|
||||
{
|
||||
directionX *= -1;
|
||||
unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan(directionY / directionX);
|
||||
}
|
||||
else
|
||||
{
|
||||
*/unit.GetComponent<Component.Transform>().World.Rotation = (float)Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y, mouseWorldPos.X - unit.Transform.World.X);/*
|
||||
}*/
|
||||
//Console.WriteLine(angle);
|
||||
//unit.GetComponent<Component.Transform>().World.Rotation = angle;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
checkingDirection = false;
|
||||
}
|
||||
|
||||
// Grid highlighting
|
||||
Point mouseGridPos = Grid.WorldToGrid(mouseWorldPos);
|
||||
@@ -96,6 +146,24 @@ namespace DepthsBelow
|
||||
{
|
||||
spriteBatch.Draw(selectionTexture, selectionRectangle, Color.Red * 0.5f);
|
||||
spriteBatch.Draw(gridTexture, gridRectangle, Color.Yellow * 0.3f);
|
||||
|
||||
if (checkingDirection == true)
|
||||
{
|
||||
Texture2D blank = new Texture2D(core.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
|
||||
blank.SetData(new[] { Color.White });
|
||||
DrawLine(spriteBatch, blank, 2, Color.White, directionStart, directionNow);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DrawLine(SpriteBatch batch, Texture2D blank, float width, Color color, Vector2 point1, Vector2 point2)
|
||||
{
|
||||
float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
|
||||
float length = Vector2.Distance(point1, point2);
|
||||
|
||||
batch.Draw(blank, point1, null, color,
|
||||
angle, Vector2.Zero, new Vector2(length, width),
|
||||
SpriteEffects.None, 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using DepthsBelow.Component;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
@@ -13,13 +14,17 @@ namespace DepthsBelow
|
||||
public static Point Origin;
|
||||
private bool _selected;
|
||||
|
||||
public List<Soldier> Squad;
|
||||
|
||||
private PathFinder.Node nextNode;
|
||||
|
||||
public Soldier(Core core) : base(core)
|
||||
public Soldier(Core core, ref List<Soldier> squad) : base(core)
|
||||
{
|
||||
if (Texture == null)
|
||||
LoadContent(core);
|
||||
|
||||
this.Squad = squad;
|
||||
|
||||
Transform.World.Origin = new Vector2(16, 16);
|
||||
|
||||
this.Color = Color.White;
|
||||
@@ -52,25 +57,48 @@ namespace DepthsBelow
|
||||
{
|
||||
base.Update(gameTime);
|
||||
|
||||
float elapsed = gameTime.ElapsedGameTime.Milliseconds / 1000.0f;
|
||||
|
||||
if (nextNode == null)
|
||||
nextNode = GetComponent<PathFinder>().Next();
|
||||
|
||||
if (nextNode != null)
|
||||
{
|
||||
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;
|
||||
List<Point> soldierCollisions = new List<Point>();
|
||||
|
||||
if (Transform.World == nodeWorldPos)
|
||||
nextNode = GetComponent<PathFinder>().Next();
|
||||
foreach (var soldier in Squad)
|
||||
{
|
||||
if (soldier != this)
|
||||
{
|
||||
if (soldier.Transform.Grid == nextNode.Position)
|
||||
{
|
||||
soldierCollisions.Add(soldier.Transform.Grid);
|
||||
GetComponent<PathFinder>().RecreatePath(soldierCollisions);
|
||||
nextNode = GetComponent<PathFinder>().Next();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
if (nextNode != null)
|
||||
{
|
||||
var nodeWorldPos = Grid.GridToWorld(nextNode.Position);
|
||||
// HACK: Make this work properly with other speeds...
|
||||
float speed = 4f;
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user