mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-06 20:23:10 +02:00
Merge branch 'kuchy-robust-formation' into 'Development'
# Conflicts: # theProject/Assets/Scripts/Managers/TilemapManager.cs
This commit is contained in:
commit
a1c85a04e5
55
theProject/.vscode/settings.json
vendored
Normal file
55
theProject/.vscode/settings.json
vendored
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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
|
||||||
|
|||||||
@ -5,7 +5,7 @@ using UnityEngine;
|
|||||||
|
|
||||||
public class Formation : MonoBehaviour
|
public class Formation : MonoBehaviour
|
||||||
{
|
{
|
||||||
[SerializeField] Squad squad;
|
[SerializeField] Squad squad;
|
||||||
|
|
||||||
void Awake()
|
void Awake()
|
||||||
{
|
{
|
||||||
@ -15,24 +15,71 @@ 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;
|
|
||||||
foreach (Entity Entity in soldiers)
|
int numberOfSoldiers = soldiers.Count;
|
||||||
|
for(int i = numberOfSoldiers / 2; i >= 0; i--)
|
||||||
{
|
{
|
||||||
soldiersNewCoordinates.Add(Entity, CalculateSoldierCoordinates(soldierNumber, coordinates));
|
float shortestDistance = Mathf.Infinity;
|
||||||
soldierNumber++;
|
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;
|
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(in int soldierNumber, in Vector2Int coordinates)
|
private Vector2Int CalculateSoldierCoordinates(int soldierNumber, in Vector2Int coordinates)
|
||||||
{
|
{
|
||||||
// Horizontal line we change x
|
// Horizontal line we change x
|
||||||
Vector2Int soldierCoordinates = new Vector2Int(coordinates.x + soldierNumber, coordinates.y);
|
if(soldierNumber % 2 == 1) soldierNumber = -1 * soldierNumber;
|
||||||
return soldierCoordinates;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -174,12 +174,12 @@ public class TilemapManager : MonoBehaviour
|
|||||||
|
|
||||||
// ---------- private methods
|
// ---------- 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;
|
return TileState.outOfBounds;
|
||||||
|
|
||||||
if (tiles[x, y].standingEntity == null)
|
if (ins.tiles[x, y].standingEntity == null)
|
||||||
return TileState.free;
|
return TileState.free;
|
||||||
|
|
||||||
return TileState.taken;
|
return TileState.taken;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user