From de4764a13bfcef05f1901b539664625e11e981b5 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Fri, 3 Jun 2022 13:51:30 +0200 Subject: [PATCH 1/2] fix: even uneven formations --- theProject/Assets/Scenes/Main.unity | 78 ++++++++++++++++++- .../Assets/Scripts/Algorithms/Pathfinding.cs | 6 +- theProject/Assets/Scripts/Entities/Soldier.cs | 2 +- theProject/Assets/Scripts/Formation.cs | 76 ++++++++++++++---- 4 files changed, 141 insertions(+), 21 deletions(-) diff --git a/theProject/Assets/Scenes/Main.unity b/theProject/Assets/Scenes/Main.unity index be29aa57..27ea96df 100644 --- a/theProject/Assets/Scenes/Main.unity +++ b/theProject/Assets/Scenes/Main.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 0} - m_IndirectSpecularColor: {r: 0.17920087, g: 0.22438505, b: 0.3061323, a: 1} + m_IndirectSpecularColor: {r: 0.17920034, g: 0.2243842, b: 0.30613118, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -306,6 +306,81 @@ MonoBehaviour: m_RequiresDepthTexture: 0 m_RequiresColorTexture: 0 m_Version: 2 +--- !u!1001 &794133337 +PrefabInstance: + m_ObjectHideFlags: 0 + serializedVersion: 2 + m_Modification: + m_TransformParent: {fileID: 1773236753} + m_Modifications: + - target: {fileID: 3289194458089359769, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_Name + value: PathfindingSingleton + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_RootOrder + value: 2 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalPosition.x + value: 2.9636345 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalPosition.y + value: 6.377094 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalPosition.z + value: 3.499856 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalRotation.w + value: 1 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalRotation.x + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalRotation.y + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalRotation.z + value: -0 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalEulerAnglesHint.x + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalEulerAnglesHint.y + value: 0 + objectReference: {fileID: 0} + - target: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + propertyPath: m_LocalEulerAnglesHint.z + value: 0 + objectReference: {fileID: 0} + m_RemovedComponents: [] + m_SourcePrefab: {fileID: 100100000, guid: 61a3c7da406cc5d488ddd48802d51021, type: 3} +--- !u!4 &794133338 stripped +Transform: + m_CorrespondingSourceObject: {fileID: 3289194458089359783, guid: 61a3c7da406cc5d488ddd48802d51021, + type: 3} + m_PrefabInstance: {fileID: 794133337} + m_PrefabAsset: {fileID: 0} --- !u!1 &905024696 GameObject: m_ObjectHideFlags: 0 @@ -815,6 +890,7 @@ Transform: m_Children: - {fileID: 282616949} - {fileID: 1678876130} + - {fileID: 794133338} m_Father: {fileID: 0} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} diff --git a/theProject/Assets/Scripts/Algorithms/Pathfinding.cs b/theProject/Assets/Scripts/Algorithms/Pathfinding.cs index 3afb0b02..52b1c379 100644 --- a/theProject/Assets/Scripts/Algorithms/Pathfinding.cs +++ b/theProject/Assets/Scripts/Algorithms/Pathfinding.cs @@ -24,7 +24,7 @@ public class Pathfinding : MonoBehaviour public bool FindPath(Vector2Int startCoords, Vector2Int endCoords, out List path, int maxPathPointDistanceFromStart = int.MaxValue) { path = new List(); - if (TilemapManager.ins.GetTileState(endCoords.x, endCoords.y) == TilemapManager.TileState.outOfBounds) + if (TilemapManager.GetTileState(endCoords.x, endCoords.y) == TilemapManager.TileState.outOfBounds) { return false; } @@ -99,7 +99,7 @@ public class Pathfinding : MonoBehaviour private int GetTargetCost(int cost, Vector2Int targetCoords) { - TilemapManager.TileState targetState = TilemapManager.ins.GetTileState(targetCoords.x, targetCoords.y); + TilemapManager.TileState targetState = TilemapManager.GetTileState(targetCoords.x, targetCoords.y); if(targetState == TilemapManager.TileState.free) { return cost + EMPTY_TILE_COST; @@ -119,7 +119,7 @@ public class Pathfinding : MonoBehaviour neighbors.Add(coords + Vector2Int.right); foreach(Vector2Int neighbor in neighbors) { - if(TilemapManager.ins.GetTileState(neighbor.x, neighbor.y) != TilemapManager.TileState.outOfBounds) + if(TilemapManager.GetTileState(neighbor.x, neighbor.y) != TilemapManager.TileState.outOfBounds) { final.Add(neighbor); } diff --git a/theProject/Assets/Scripts/Entities/Soldier.cs b/theProject/Assets/Scripts/Entities/Soldier.cs index 158b6235..fcfe7155 100644 --- a/theProject/Assets/Scripts/Entities/Soldier.cs +++ b/theProject/Assets/Scripts/Entities/Soldier.cs @@ -30,7 +30,7 @@ public class Soldier : Entity return; // cannot find path: do nothing (for now) } - Vector2Int movementStepDestination = path[0]; + Vector2Int movementStepDestination = path[path.Count - 1]; if (!TilemapManager.MoveSoldierS(soldier.tileCoord.x, soldier.tileCoord.y, movementStepDestination.x, movementStepDestination.y)) diff --git a/theProject/Assets/Scripts/Formation.cs b/theProject/Assets/Scripts/Formation.cs index d1775e63..45271422 100644 --- a/theProject/Assets/Scripts/Formation.cs +++ b/theProject/Assets/Scripts/Formation.cs @@ -17,13 +17,15 @@ public class Formation : MonoBehaviour List soldiers = new List(squad.GetSoldiers()); Dictionary soldiersNewCoordinates = new Dictionary(); + bool isEven = false; - int numberOfSoldiers = soldiers.Count; - for(int i = numberOfSoldiers / 2; i >= 0; i--) + + if(soldiers.Count % 2 == 0) { + isEven = true; float shortestDistance = Mathf.Infinity; Entity nearestSoldier = null; - Vector2Int newCoordinates = new Vector2Int(coordinates.x + i, coordinates.y); + Vector2Int newCoordinates = new Vector2Int(coordinates.x, coordinates.y); foreach (Entity Entity in soldiers) { float distanceToTile = Vector2.Distance(Entity.GetTileCoord(), newCoordinates); @@ -38,26 +40,68 @@ public class Formation : MonoBehaviour soldiersNewCoordinates.Add(nearestSoldier, newCoordinates); soldiers.Remove(nearestSoldier); } - shortestDistance = Mathf.Infinity; - nearestSoldier = null; - newCoordinates = new Vector2Int(coordinates.x - i, coordinates.y); - foreach (Entity Entity in soldiers) + } + int numberOfSoldiers = soldiers.Count; + /* numberOfSoldiers = 4 + i = 4 / 2 + i = 2 + soldier.Count = 2 + i = 3 + i = 3 / 2 = 1 + sol + */ + if(isEven) + { + for(int i = numberOfSoldiers; i >= 0; i-=2) { - float distanceToTile = Vector2.Distance(Entity.GetTileCoord(), newCoordinates); - if (distanceToTile < shortestDistance) - { - shortestDistance = distanceToTile; - nearestSoldier = Entity; - } + soldiersNewCoordinates = CalculateNewCoordinates(soldiersNewCoordinates, soldiers, coordinates, i); } - if (nearestSoldier != null) + }else{ + for(int i = numberOfSoldiers / 2; i >= 0; i--) { - soldiersNewCoordinates.Add(nearestSoldier, newCoordinates); - soldiers.Remove(nearestSoldier); + soldiersNewCoordinates = CalculateNewCoordinates(soldiersNewCoordinates, soldiers, coordinates, i); } } return soldiersNewCoordinates; } + private Dictionary CalculateNewCoordinates(Dictionary soldiersNewCoordinates, List soldiers, Vector2Int coordinates, int i) + { + float shortestDistance = Mathf.Infinity; + Entity nearestSoldier = null; + Vector2Int newCoordinates = new Vector2Int(coordinates.x + i, coordinates.y); + foreach (Entity Entity in soldiers) + { + float distanceToTile = Vector2.Distance(Entity.GetTileCoord(), newCoordinates); + if (distanceToTile < shortestDistance) + { + shortestDistance = distanceToTile; + nearestSoldier = Entity; + } + } + if (nearestSoldier != null) + { + soldiersNewCoordinates.Add(nearestSoldier, newCoordinates); + soldiers.Remove(nearestSoldier); + } + shortestDistance = Mathf.Infinity; + nearestSoldier = null; + newCoordinates = new Vector2Int(coordinates.x - i, coordinates.y); + foreach (Entity Entity in soldiers) + { + float distanceToTile = Vector2.Distance(Entity.GetTileCoord(), newCoordinates); + if (distanceToTile < shortestDistance) + { + shortestDistance = distanceToTile; + nearestSoldier = Entity; + } + } + if (nearestSoldier != null) + { + soldiersNewCoordinates.Add(nearestSoldier, newCoordinates); + soldiers.Remove(nearestSoldier); + } + return soldiersNewCoordinates; + } // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier private Vector2Int CalculateSoldierCoordinates(int soldierNumber, in Vector2Int coordinates) From 46ab64aa48cb6336e1131e6e746479b4a65ec563 Mon Sep 17 00:00:00 2001 From: Krzysztof Stefan Rudnicki Date: Fri, 3 Jun 2022 14:02:20 +0200 Subject: [PATCH 2/2] restore old path finding value --- theProject/Assets/Scripts/Entities/Soldier.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/theProject/Assets/Scripts/Entities/Soldier.cs b/theProject/Assets/Scripts/Entities/Soldier.cs index fcfe7155..158b6235 100644 --- a/theProject/Assets/Scripts/Entities/Soldier.cs +++ b/theProject/Assets/Scripts/Entities/Soldier.cs @@ -30,7 +30,7 @@ public class Soldier : Entity return; // cannot find path: do nothing (for now) } - Vector2Int movementStepDestination = path[path.Count - 1]; + Vector2Int movementStepDestination = path[0]; if (!TilemapManager.MoveSoldierS(soldier.tileCoord.x, soldier.tileCoord.y, movementStepDestination.x, movementStepDestination.y))