Merge branch 'kuchy-robust-formation' into 'Development'

# Conflicts:
#   theProject/Assets/Scripts/Managers/TilemapManager.cs
This commit is contained in:
Krzysztof Stefan Rudnicki 2022-06-03 11:05:21 +00:00
commit a1c85a04e5
4 changed files with 119 additions and 12 deletions

55
theProject/.vscode/settings.json vendored Normal file
View File

@ -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
}
}

View File

@ -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

View File

@ -5,7 +5,7 @@ using UnityEngine;
public class Formation : MonoBehaviour
{
[SerializeField] Squad squad;
[SerializeField] Squad squad;
void Awake()
{
@ -15,24 +15,71 @@ public class Formation : MonoBehaviour
public Dictionary<Entity, Vector2Int> CalculatePositions(Vector2Int coordinates)
{
List<Entity> soldiers = squad.GetSoldiers();
List<Entity> soldiers = new List<Entity>(squad.GetSoldiers());
Dictionary<Entity, Vector2Int> soldiersNewCoordinates = new Dictionary<Entity, Vector2Int>();
int soldierNumber = 0;
foreach (Entity Entity in soldiers)
int numberOfSoldiers = soldiers.Count;
for(int i = numberOfSoldiers / 2; i >= 0; i--)
{
soldiersNewCoordinates.Add(Entity, CalculateSoldierCoordinates(soldierNumber, coordinates));
soldierNumber++;
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(in int soldierNumber, in Vector2Int coordinates)
private Vector2Int CalculateSoldierCoordinates(int soldierNumber, in Vector2Int coordinates)
{
// Horizontal line we change x
Vector2Int soldierCoordinates = new Vector2Int(coordinates.x + soldierNumber, coordinates.y);
return soldierCoordinates;
if(soldierNumber % 2 == 1) soldierNumber = -1 * soldierNumber;
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;
}
}
}

View File

@ -174,12 +174,12 @@ public class TilemapManager : MonoBehaviour
// ---------- private methods
public 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;