void OnTriggerEnter(Collider col) { if (col.gameObject.tag == "Shopper") { ShopperController sc = col.gameObject.GetComponent <ShopperController>(); if (sc.isAlive) { sc.killShopper(false); health -= 5; if (health <= 0) { gm.GameOver(); Destroy(gameObject); } updateLightColour(); } } }
void reservePath(ShopperController s, bool firstCall) { // first set a new destination if the shopper has reached their previous target Vector2 d = new Vector2(0, 0); if (s.atDest || firstCall) { bool distFlag = true; while (distFlag) { if (Random.value > 0.5f) // Move objective { bool flag = true; int x = 0; int y = 0; while (flag) { x = Random.Range(0, 35); y = Random.Range(4, 14); // choose as dest if location unoccupied if (y < 9 && !getCell(x, y, 0)) { flag = false; } else if (y >= 9 && !getCell(x, y + 6, 0)) { flag = false; y += 6; } } d = new Vector2(x, y); } else // Shop objective { int shopNum = Random.Range(0, 12); int localX = Random.Range(1, 4); int localY = Random.Range(0, 3); // NOTE: two shoppers may choose same point in same shop but 1) unlikely they actually do and 2) unlikely they arrive simultaneously if (shopNum > 5) // upper level shop, large Y offset { localY += 21; shopNum -= 6; // essentially modulo 6 } localX += shopNum * 6; d = new Vector2(localX, localY); } distFlag = false; /*if (manhattanDist (s.transform.position, d) > window-1) { * distFlag = true; * }*/ } s.setDest(d); } else { d = s.getDest(); } // then do A* to find a (window-length segment of a) path to the destination List <Node> close = new List <Node>(); List <Node> fringe = new List <Node> (); Node starting = default(Node); if (s.transform.position.z < 9) { starting = new Node(new Vector2(s.transform.position.x, s.transform.position.z), 0); } else { starting = new Node(new Vector2(s.transform.position.x, s.transform.position.z - 6 + stairLength), 0); } starting.g = 0f; starting.f = manhattanDist(starting.position, d); fringe.Add(starting); while (fringe.Count > 0) { Node current = fringe[0]; for (int i = 0; i < fringe.Count; i++) { if (fringe [i].f < current.f) { current = fringe [i]; } /* else if (fringe [i].f - fringe [i].g < current.f - current.g) { * current = fringe [i]; * }*/ } if (current.position == d) { // pathCreate and assign to s s.setPath(pathCreate(current, close)); return; } else if (current.time >= window - 1) { Debug.Log("Failed to reach destination in window."); for (int t = window - 1; t > current.time; t--) { grid[(int)current.position.x][(int)current.position.y][t] = true; } s.setPath(pathCreate(current, close)); return; } /*} else if (current.time >= 4) { * for (int j=0; j<close.Count; j++) { * if (manhattanDist(close[j].position, d) < manhattanDist(current.position, d)) { * current = close[j]; * } * } * s.setPath (pathCreate (current, close)); * }*/ fringe.Remove(current); close.Add(current); Node[] neighbours = new Node[5]; // 5 neighbours at current.time +1 // move up neighbours [0] = new Node(new Vector2(current.position.x, current.position.y + 1), current.time + 1); // move down neighbours [1] = new Node(new Vector2(current.position.x, current.position.y - 1), current.time + 1); // move right neighbours [2] = new Node(new Vector2(current.position.x + 1, current.position.y), current.time + 1); // move left neighbours [3] = new Node(new Vector2(current.position.x - 1, current.position.y), current.time + 1); // wait at cell neighbours [4] = new Node(current.position, current.time + 1); for (int i = 0; i < neighbours.Length; i++) { if (close.Contains(neighbours[i])) { continue; } else if (getCell((int)neighbours[i].position.x, (int)neighbours[i].position.y, neighbours[i].time)) { // if neighbour is impassable, add to close and ignore close.Add(neighbours[i]); continue; } fringe.Add(neighbours[i]); float gNB = current.g + manhattanDist(current.position, neighbours[i].position); if (gNB >= neighbours[i].g) { continue; } fringe.Remove(neighbours [i]); neighbours [i].parent = current.position; neighbours [i].g = gNB; neighbours [i].f = neighbours[i].g + manhattanDist(neighbours[i].position, d); if (i == 4) { neighbours [i].f++; } fringe.Add(neighbours [i]); } } }