From 6146d64dba55e259a9a7f4757e806beed37c6b8a Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 30 May 2022 13:13:39 +0200 Subject: [PATCH 1/3] feat: detect edges in formation --- theProject/.vscode/settings.json | 55 +++++++++++++++++++ theProject/Assets/Scripts/Formation.cs | 20 +++++-- .../Assets/Scripts/Managers/TilemapManager.cs | 6 +- 3 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 theProject/.vscode/settings.json diff --git a/theProject/.vscode/settings.json b/theProject/.vscode/settings.json new file mode 100644 index 00000000..e232cd65 --- /dev/null +++ b/theProject/.vscode/settings.json @@ -0,0 +1,55 @@ +{ + "files.exclude": + { + "**/.DS_Store":true, + "**/.git":true, + "**/.gitmodules":true, + "**/*.booproj":true, + "**/*.pidb":true, + "**/*.suo":true, + "**/*.user":true, + "**/*.userprefs":true, + "**/*.unityproj":true, + "**/*.dll":true, + "**/*.exe":true, + "**/*.pdf":true, + "**/*.mid":true, + "**/*.midi":true, + "**/*.wav":true, + "**/*.gif":true, + "**/*.ico":true, + "**/*.jpg":true, + "**/*.jpeg":true, + "**/*.png":true, + "**/*.psd":true, + "**/*.tga":true, + "**/*.tif":true, + "**/*.tiff":true, + "**/*.3ds":true, + "**/*.3DS":true, + "**/*.fbx":true, + "**/*.FBX":true, + "**/*.lxo":true, + "**/*.LXO":true, + "**/*.ma":true, + "**/*.MA":true, + "**/*.obj":true, + "**/*.OBJ":true, + "**/*.asset":true, + "**/*.cubemap":true, + "**/*.flare":true, + "**/*.mat":true, + "**/*.meta":true, + "**/*.prefab":true, + "**/*.unity":true, + "build/":true, + "Build/":true, + "Library/":true, + "library/":true, + "obj/":true, + "Obj/":true, + "ProjectSettings/":true, + "temp/":true, + "Temp/":true + } +} \ No newline at end of file diff --git a/theProject/Assets/Scripts/Formation.cs b/theProject/Assets/Scripts/Formation.cs index a731e3d4..56bc505c 100644 --- a/theProject/Assets/Scripts/Formation.cs +++ b/theProject/Assets/Scripts/Formation.cs @@ -5,7 +5,7 @@ using UnityEngine; public class Formation : MonoBehaviour { - [SerializeField] Squad squad; + [SerializeField] Squad squad; void Awake() { @@ -29,9 +29,21 @@ public class Formation : MonoBehaviour // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier private Vector2Int CalculateSoldierCoordinates(in int soldierNumber, in Vector2Int coordinates) { - // Horizontal line we change x - Vector2Int soldierCoordinates = new Vector2Int(coordinates.x + soldierNumber, coordinates.y); - return soldierCoordinates; + // Horizontal line we change x + TilemapManager.TileState tileState = TilemapManager.GetTileState(coordinates.x + soldierNumber, coordinates.y); + if ( tileState == TilemapManager.TileState.free) + { + Vector2Int soldierCoordinates = new Vector2Int(coordinates.x + soldierNumber, coordinates.y); + return soldierCoordinates; + } else if (tileState == TilemapManager.TileState.taken) + { + Vector2Int soldierCoordinates = new Vector2Int(coordinates.x, coordinates.y); + return soldierCoordinates; + } else + { + Vector2Int soldierCoordinates = new Vector2Int(coordinates.x, coordinates.y); + return soldierCoordinates; + } } diff --git a/theProject/Assets/Scripts/Managers/TilemapManager.cs b/theProject/Assets/Scripts/Managers/TilemapManager.cs index 576273fa..efcafb41 100644 --- a/theProject/Assets/Scripts/Managers/TilemapManager.cs +++ b/theProject/Assets/Scripts/Managers/TilemapManager.cs @@ -174,12 +174,12 @@ public class TilemapManager : MonoBehaviour // ---------- private methods - private TileState GetTileState(int x, int y) + public static TileState GetTileState(int x, int y) { - if (x < 0 || y < 0 || x >= mapSize.x || y >= mapSize.y) + if (x < 0 || y < 0 || x >= ins.mapSize.x || y >= ins.mapSize.y) return TileState.outOfBounds; - if (tiles[x, y].standingEntity == null) + if (ins.tiles[x, y].standingEntity == null) return TileState.free; return TileState.taken; From 4cdc53a3774cdef1d9ffadbec30208b9e181396b Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 30 May 2022 13:48:37 +0200 Subject: [PATCH 2/3] feat: formation in middle soldiers allign on the left and right of where we clicked --- theProject/Assets/Scripts/Formation.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/theProject/Assets/Scripts/Formation.cs b/theProject/Assets/Scripts/Formation.cs index 56bc505c..8513659b 100644 --- a/theProject/Assets/Scripts/Formation.cs +++ b/theProject/Assets/Scripts/Formation.cs @@ -18,18 +18,21 @@ public class Formation : MonoBehaviour List soldiers = squad.GetSoldiers(); Dictionary soldiersNewCoordinates = new Dictionary(); int soldierNumber = 0; + int add = 1; foreach (Entity Entity in soldiers) { - soldiersNewCoordinates.Add(Entity, CalculateSoldierCoordinates(soldierNumber, coordinates)); + soldiersNewCoordinates.Add(Entity, CalculateSoldierCoordinates(soldierNumber, coordinates, add)); soldierNumber++; + add *= -1; } return soldiersNewCoordinates; } // https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier - private Vector2Int CalculateSoldierCoordinates(in int soldierNumber, in Vector2Int coordinates) + private Vector2Int CalculateSoldierCoordinates(int soldierNumber, in Vector2Int coordinates,in int add) { // Horizontal line we change x + soldierNumber = add * soldierNumber; TilemapManager.TileState tileState = TilemapManager.GetTileState(coordinates.x + soldierNumber, coordinates.y); if ( tileState == TilemapManager.TileState.free) { @@ -47,4 +50,5 @@ public class Formation : MonoBehaviour } + } From 0e369307770f078350807ee8dee20b9b3a6fcda6 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 30 May 2022 15:00:56 +0200 Subject: [PATCH 3/3] feat: reformat formation assignment toDo: fix middle squares not being taken --- theProject/Assets/Scripts/Entities/Entity.cs | 5 ++ theProject/Assets/Scripts/Formation.cs | 51 ++++++++++++++++---- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/theProject/Assets/Scripts/Entities/Entity.cs b/theProject/Assets/Scripts/Entities/Entity.cs index 2a593dce..2424c302 100644 --- a/theProject/Assets/Scripts/Entities/Entity.cs +++ b/theProject/Assets/Scripts/Entities/Entity.cs @@ -30,6 +30,11 @@ public class Entity : MonoBehaviour return myTeam; } + public Vector2Int GetTileCoord() + { + return tileCoord; + } + // Start is called before the first frame update protected virtual void Start(){ healthPoints = maxHealthPoints; // initialize health diff --git a/theProject/Assets/Scripts/Formation.cs b/theProject/Assets/Scripts/Formation.cs index 8513659b..d1775e63 100644 --- a/theProject/Assets/Scripts/Formation.cs +++ b/theProject/Assets/Scripts/Formation.cs @@ -15,24 +15,55 @@ public class Formation : MonoBehaviour public Dictionary CalculatePositions(Vector2Int coordinates) { - List soldiers = squad.GetSoldiers(); + List soldiers = new List(squad.GetSoldiers()); Dictionary soldiersNewCoordinates = new Dictionary(); - int soldierNumber = 0; - int add = 1; - foreach (Entity Entity in soldiers) + + int numberOfSoldiers = soldiers.Count; + for(int i = numberOfSoldiers / 2; i >= 0; i--) { - soldiersNewCoordinates.Add(Entity, CalculateSoldierCoordinates(soldierNumber, coordinates, add)); - soldierNumber++; - add *= -1; + 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,in int add) + private Vector2Int CalculateSoldierCoordinates(int soldierNumber, in Vector2Int coordinates) { - // Horizontal line we change x - soldierNumber = add * soldierNumber; + // Horizontal line we change x + if(soldierNumber % 2 == 1) soldierNumber = -1 * soldierNumber; TilemapManager.TileState tileState = TilemapManager.GetTileState(coordinates.x + soldierNumber, coordinates.y); if ( tileState == TilemapManager.TileState.free) {