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/Component/Transform.cs b/DepthsBelow/DepthsBelow/Component/Transform.cs index 6385688..c7a451c 100755 --- a/DepthsBelow/DepthsBelow/Component/Transform.cs +++ b/DepthsBelow/DepthsBelow/Component/Transform.cs @@ -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) 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 b57cb28..18af796 100755 --- a/DepthsBelow/DepthsBelow/MouseInput.cs +++ b/DepthsBelow/DepthsBelow/MouseInput.cs @@ -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().Goal = Grid.WorldToGrid(mouseWorldPos); + } + } + if (ms.RightButton == ButtonState.Pressed) + { + foreach (var unit in core.Squad) + { + if (unit.Selected && gridRectangle.Intersects(unit.GetComponent().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().World.Rotation = (float)Math.Atan(directionY / directionX); + } + else + { + */unit.GetComponent().World.Rotation = (float)Math.Atan2(mouseWorldPos.Y - unit.Transform.World.Y, mouseWorldPos.X - unit.Transform.World.X);/* + }*/ + //Console.WriteLine(angle); + //unit.GetComponent().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); + } } } 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(); + } }