From 3b718e34a376c415274bfb69c4b05c08d7733650 Mon Sep 17 00:00:00 2001 From: Simon Holmberg Date: Mon, 1 Oct 2012 17:35:38 +0200 Subject: [PATCH] Almost working pathfinding --- .../DepthsBelow/Component/PathFinder.cs | 43 +++++++++----- DepthsBelow/DepthsBelow/Entity.cs | 2 +- DepthsBelow/DepthsBelow/Map.cs | 2 +- DepthsBelow/DepthsBelow/MouseInput.cs | 6 +- DepthsBelow/DepthsBelow/Soldier.cs | 56 ++++++++++++++----- 5 files changed, 77 insertions(+), 32 deletions(-) diff --git a/DepthsBelow/DepthsBelow/Component/PathFinder.cs b/DepthsBelow/DepthsBelow/Component/PathFinder.cs index 8bd0530..4dad9d7 100755 --- a/DepthsBelow/DepthsBelow/Component/PathFinder.cs +++ b/DepthsBelow/DepthsBelow/Component/PathFinder.cs @@ -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().Grid.Position; - this.FindPath(this.Start, value); + //this.Start = this.Parent.GetComponent().Grid.Position; + this.FindPath(this.Parent.GetComponent().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 FindPath(Point start, Point goal) + private List FindPath(Point start, Point goal, List 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 appendCollisionMap) + { + FindPath(this.Parent.GetComponent().Grid.Position, this.Goal, appendCollisionMap); + } } } diff --git a/DepthsBelow/DepthsBelow/Entity.cs b/DepthsBelow/DepthsBelow/Entity.cs index 26935e9..838dc97 100644 --- a/DepthsBelow/DepthsBelow/Entity.cs +++ b/DepthsBelow/DepthsBelow/Entity.cs @@ -29,7 +29,7 @@ namespace DepthsBelow } } - private Core core; + protected Core core; public Entity(Core core) { diff --git a/DepthsBelow/DepthsBelow/Map.cs b/DepthsBelow/DepthsBelow/Map.cs index d410a77..b32861a 100755 --- a/DepthsBelow/DepthsBelow/Map.cs +++ b/DepthsBelow/DepthsBelow/Map.cs @@ -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); diff --git a/DepthsBelow/DepthsBelow/MouseInput.cs b/DepthsBelow/DepthsBelow/MouseInput.cs index 92ff766..a303c92 100755 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -25,7 +25,7 @@ namespace DepthsBelow bool checkingDirection = false; -public MouseInput(Core core) + public MouseInput(Core core) { this.core = core; @@ -104,8 +104,8 @@ public MouseInput(Core core) } if (checkingDirection == true && ms.RightButton == ButtonState.Pressed) { - directionNow.X = ms.X; - directionNow.Y = ms.Y; + directionNow.X = mouseWorldPos.X; + directionNow.Y = mouseWorldPos.Y; } else { diff --git a/DepthsBelow/DepthsBelow/Soldier.cs b/DepthsBelow/DepthsBelow/Soldier.cs index 4b5f9e2..336e7c6 100644 --- a/DepthsBelow/DepthsBelow/Soldier.cs +++ b/DepthsBelow/DepthsBelow/Soldier.cs @@ -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 Squad; + private PathFinder.Node nextNode; - public Soldier(Core core) : base(core) + public Soldier(Core core, ref List 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().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 soldierCollisions = new List(); - if (Transform.World == nodeWorldPos) - nextNode = GetComponent().Next(); + foreach (var soldier in Squad) + { + if (soldier != this) + { + if (soldier.Transform.Grid == nextNode.Position) + { + soldierCollisions.Add(soldier.Transform.Grid); + GetComponent().RecreatePath(soldierCollisions); + nextNode = GetComponent().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().Next(); + } }