Skip to content

Instantly share code, notes, and snippets.

@zacyzacy
Created September 19, 2016 19:04
Show Gist options
  • Save zacyzacy/ddd23dec10d9bbfa44ebbb25aa5b02e7 to your computer and use it in GitHub Desktop.
Save zacyzacy/ddd23dec10d9bbfa44ebbb25aa5b02e7 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Crowd : MonoBehaviour
{
//each frame increment the npc index to only calculate one path per frame if length of npc array npc index = 0
public GameObject npc, assassin, preacher;
public List<NPC> assassinationTargets;
public int NumberOfNPCS;
public Transform avoidTarget;
private int npcIndex;
private NPC[] npcs;
// Use this for initialization
void OnEnable()
{
//temporary ints to tell them to spawn in formation
int zVal = 0;
int xVal = 0;
npcIndex = 0;
npcs = new NPC[NumberOfNPCS];
for (int x = 0; x < npcs.Length; x++)
{
if (x % 50 == 0)
{
zVal++;
xVal = 0;
}
xVal++;
GameObject a = null;
if (x < 5)
{
a = Instantiate(assassin.gameObject, new Vector3(transform.position.x + xVal * 2, 3, zVal * 10), Quaternion.identity) as GameObject;
}
else if (x > npcs.Length - 5)
{
a = Instantiate(preacher.gameObject, new Vector3(transform.position.x + xVal * 2, 3, zVal * 10), Quaternion.identity) as GameObject;
}
else
{
a = Instantiate(npc.gameObject, new Vector3(transform.position.x + xVal * 2, 3, zVal * 10), Quaternion.identity) as GameObject;
}
a.transform.parent = transform;
npcs[x] = a.GetComponent<NPC>();
npcs[x].crowd = this; // a little hacky
}
}
// Update is called once per frame
void Update()
{
CrowdUpdate();
incrementNPCIndex();
}
private void CrowdUpdate()
{
npcs[npcIndex].decide();
}
private void incrementNPCIndex()
{
if (npcIndex < NumberOfNPCS - 1)
{
npcIndex++;
}
else
{
npcIndex = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment