fix: even uneven formations

This commit is contained in:
Krzysztof Rudnicki 2022-06-03 13:51:30 +02:00
parent 3e39ca54f0
commit de4764a13b
4 changed files with 141 additions and 21 deletions

View File

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

View File

@ -24,7 +24,7 @@ public class Pathfinding : MonoBehaviour
public bool FindPath(Vector2Int startCoords, Vector2Int endCoords, out List<Vector2Int> path, int maxPathPointDistanceFromStart = int.MaxValue)
{
path = new List<Vector2Int>();
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);
}

View File

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

View File

@ -17,13 +17,15 @@ public class Formation : MonoBehaviour
List<Entity> soldiers = new List<Entity>(squad.GetSoldiers());
Dictionary<Entity, Vector2Int> soldiersNewCoordinates = new Dictionary<Entity, Vector2Int>();
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<Entity, Vector2Int> CalculateNewCoordinates(Dictionary<Entity, Vector2Int> soldiersNewCoordinates, List<Entity> 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)