mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-20 22:52:28 +01:00
feat: reformat formation assignment
toDo: fix middle squares not being taken
This commit is contained in:
parent
4cdc53a377
commit
0e36930777
@ -30,6 +30,11 @@ public class Entity : MonoBehaviour
|
|||||||
return myTeam;
|
return myTeam;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Vector2Int GetTileCoord()
|
||||||
|
{
|
||||||
|
return tileCoord;
|
||||||
|
}
|
||||||
|
|
||||||
// Start is called before the first frame update
|
// Start is called before the first frame update
|
||||||
protected virtual void Start(){
|
protected virtual void Start(){
|
||||||
healthPoints = maxHealthPoints; // initialize health
|
healthPoints = maxHealthPoints; // initialize health
|
||||||
|
|||||||
@ -15,24 +15,55 @@ public class Formation : MonoBehaviour
|
|||||||
public Dictionary<Entity, Vector2Int> CalculatePositions(Vector2Int coordinates)
|
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>();
|
Dictionary<Entity, Vector2Int> soldiersNewCoordinates = new Dictionary<Entity, Vector2Int>();
|
||||||
int soldierNumber = 0;
|
|
||||||
int add = 1;
|
int numberOfSoldiers = soldiers.Count;
|
||||||
foreach (Entity Entity in soldiers)
|
for(int i = numberOfSoldiers / 2; i >= 0; i--)
|
||||||
{
|
{
|
||||||
soldiersNewCoordinates.Add(Entity, CalculateSoldierCoordinates(soldierNumber, coordinates, add));
|
float shortestDistance = Mathf.Infinity;
|
||||||
soldierNumber++;
|
Entity nearestSoldier = null;
|
||||||
add *= -1;
|
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;
|
return soldiersNewCoordinates;
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier
|
// 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
|
// Horizontal line we change x
|
||||||
soldierNumber = add * soldierNumber;
|
if(soldierNumber % 2 == 1) soldierNumber = -1 * soldierNumber;
|
||||||
TilemapManager.TileState tileState = TilemapManager.GetTileState(coordinates.x + soldierNumber, coordinates.y);
|
TilemapManager.TileState tileState = TilemapManager.GetTileState(coordinates.x + soldierNumber, coordinates.y);
|
||||||
if ( tileState == TilemapManager.TileState.free)
|
if ( tileState == TilemapManager.TileState.free)
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user