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

Kuchy robust formation

See merge request gskowron/eopsy-lab-567-project!19
This commit is contained in:
Krzysztof Stefan Rudnicki 2022-06-03 14:02:41 +02:00
commit e83f01586a
2 changed files with 137 additions and 17 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
@ -891,6 +966,7 @@ Transform:
- {fileID: 282616949}
- {fileID: 910185106}
- {fileID: 1678876130}
- {fileID: 794133338}
m_Father: {fileID: 0}
m_RootOrder: 1
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

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)