private void respawn() { if (Random.value < p) { int index = Random.Range(0, n - 1); zombies[index].active = false; Corners.CornerIndex ci = cn.randomCorner(); zombies[index].transform.localEulerAngles = new Vector3(0, 90 * ((ci.j - 2) % 4), 0); zombies[index].initialize(randomType(), ci, cn.getCorner(ci.i, (4 + ci.j - 1) % 4)); } }
// initialize the type and states of the zombie public void initialize(ZombieBehavior.ZombieType t, Corners.CornerIndex ci, Vector2 pos) { transform.Find("character").renderer.material = zb.getColor(t); direction = 1; active = true; moving = true; type = t; speed = zb.getSpeed(t); nextCorner = new Corners.CornerIndex(ci.i, ci.j); position = pos; nextPos = Vector2.zero; distanceLeft = cn.distanceLeft(nextCorner, position, direction == 1); unitVelocity = cn.getCorner(ci) - position; unitVelocity.Normalize(); transform.localPosition = new Vector3(pos.x, 0, pos.y); }
void Start() { // compute the probability density for a uniform initialization of the zombie position // at the start of the game zombies = new Zombie[n]; float sum = 0; Vector2[,] diff = new Vector2[3, 3]; for (int i = 0; i < 3; i++) { for (int j = 1; j < 4; j++) { diff[i, j - 1] = cn.getCorner(i, j - 1) - cn.getCorner(i, j); sum += Vector2.SqrMagnitude(diff[i, j - 1]); } } Dictionary <float, Corners.CornerIndex> cdf = new Dictionary <float, Corners.CornerIndex> (); float cumu = 0; for (int i = 0; i < 3; i++) { for (int j = 1; j < 4; j++) { cumu += Vector2.SqrMagnitude(diff[i, j - 1]) / sum; cdf.Add(cumu, new Corners.CornerIndex(i, j)); } } for (int i = 0; i < n; i++) { float rnd = Random.value; foreach (float f in cdf.Keys) { if (rnd < f) { // create a zombie here zombies[i] = (Zombie)Instantiate(zombie); zombies[i].name = i.ToString(); Corners.CornerIndex idx = cdf[f]; zombies[i].transform.parent = transform; zombies[i].transform.localEulerAngles = new Vector3(0, 90 * ((idx.j - 2) % 4), 0); zombies[i].initialize(randomType(), idx, diff[idx.i, idx.j - 1] * Random.value + cn.getCorner(idx)); break; } } } }