public class FlockSpawnTest : MonoBehaviour
{
public FlockAgent agentPrefab;
List<FlockAgent> agents = new List<FlockAgent>();
public CommandPost commandPostPrefab;
List<CommandPost> commandPosts = new List<CommandPost>();
int[] numOfEachRank = new int[5];
[Range(0, 500)]
public int unitCount = 250; //number of frontline units
const float AgentDensity = 0.08f;
[SerializeField] private bool topDownCreation = false;
// Start is called before the first frame update
void Start()
{
int rankToAssign;
int unitsSpawned = 0;
if (unitCount > 0)
{
// create formation of division or general HQ via public bool topdown
if (topDownCreation)
{
rankToAssign = 4; // General
}
else
{
rankToAssign = 0; // Division
unitsSpawned++;
}
// Set up: make first formation & set to currentFormation
CommandPost currentFormation = SpawnCommandPost(rankToAssign, unitsSpawned);
// run if more units need to be created
for (; unitsSpawned < unitCount;)
{
// Step 1: if current formation subordinate limit is not reached:
if (currentFormation.Subordinates.Count < currentFormation.SubordinateLimit)
{
// create one command post
CommandPost newSubordinate = SpawnCommandPost(rankToAssign - 1, unitsSpawned);
currentFormation.AddSubordinate(newSubordinate);
// if a division, add to division count else add to subCount
if (newSubordinate.myRank == 0)
{
// spawn flock agent
//FlockAgent newAgent = Instantiate(agentPrefab, currentFormation.transform.position, Quaternion.identity);
//agents.Add(newAgent);
//Destroy(newSubordinate.gameObject);
//currentFormation.AddSubordinate(newAgent); //FIX should be able to add agent using function.
unitsSpawned++;
}
}
// Step 2: if subordinate limit is not full aready
if (currentFormation.Subordinates.Count <= currentFormation.SubordinateLimit)
{
bool movedDown = false;
foreach (CommandPost subordinate in currentFormation.Subordinates)
{
// populate branch by moving down the chain of command
if (subordinate.Subordinates.Count < subordinate.SubordinateLimit)
{
currentFormation = subordinate;
movedDown = true;
break;
}
}
// Step 3: if no space for subordinates, move up the chain of command
if (!movedDown)
{
// if there is no parent formation, create one and add the current formation to it
if (currentFormation.Superior == null && currentFormation.myRank != 4)
{
CommandPost superior = SpawnCommandPost(rankToAssign + 1, unitsSpawned);
superior.AddSubordinate(currentFormation);
}
currentFormation = currentFormation.Superior;
}
}
}
}
//spawning command post
CommandPost SpawnCommandPost(int rank, int numAgentsSpawned)
{
float x = numAgentsSpawned / 10;//Placeholder, HELP
float y = (rank - 5) * 2 / 3;
Vector3 pos = transform.position + new Vector3(x, y);
CommandPost newCommandPost = Instantiate(commandPostPrefab, pos, Quaternion.identity);
newCommandPost.myRank = rank;
newCommandPost.name = numOfEachRank[rank] + 1 + newCommandPost.GetCommandLevelName(rank);
numOfEachRank[rank]++;
commandPosts.Add(newCommandPost);
return newCommandPost;
}
}
}