public void CreatePath(Vector3 targetPos) { int num = FindClosestVisibleNode(transform); int goal = FindClosestNode(targetPos); if (num == -1) { print("Can't find closest"); transform.LookAt(graph.nodes[FindClosestNode(transform.position)].transform.position); num = FindClosestVisibleNode(transform); } pathToFollow = graph.FindPath(num, goal); testString = num + " - " + goal; }
void CreatePath() { target = graph.nodes[Random.Range(0, graph.nodes.Count - 1)].transform.position; int startPath = FindClosestVisibleNode(transform); int endPath = FindClosestNode(target, startPath); if (ownedNodes.Count > 0 && Random.Range(0, 100) > 30) { int chosenNode = Random.Range(0, ownedNodes.Count - 1); if (!nodeHistory.Contains(chosenNode)) { endPath = ownedNodes[chosenNode]; } } if ((transform.position - player.transform.position).magnitude < 3F) { print(startPath + " -> " + endPath); } pathToFollow = graph.FindPath(startPath, endPath); }
private void Update() { if (currentlyWants == wants.toEnter) { if (pathToFollow == null || pathToFollow.Count == 0) { CreatePath(graph.enterance.transform.position); } else { if ((pathToFollow[0].transform.position + randomDistanceFromNodeBias - transform.position).magnitude < 10f) { if (Enterance.npcsWaiting < 6) { pathToFollow.RemoveAt(0); } else { setWants(wants.toExitScene); pathToFollow = new List <NavigationScript>(); FindExit(false); CreatePath(exitNode.transform.position); } if (UnityEngine.Random.value > 0.5f) { randomDistanceFromNodeBias = UnityEngine.Random.insideUnitSphere * 5f; randomDistanceFromNodeBias.y = 0f; } } RaycastHit raycastHit; if (pathToFollow.Count == 0) { Enterance.npcsWaiting++; // System if (SceneManager.GetSceneAt(0).buildIndex == 2) { setWants(wants.toFindSeat); } else { pathToFollow.Add(graph.nodes.ToArray()[12]); setWants(wants.toOrder); //setWants(wants.toFindSeat); } } else if (Time.time > lastLinecastCheck + linecastCheckDelay && Physics.Linecast(transform.position, pathToFollow[0].transform.position, out raycastHit, pathfindLayerMask) && raycastHit.distance < 3f) { if (timesStuckPathfinding > 4) { gameObject.layer = 16; } randomDistanceFromNodeBias = UnityEngine.Random.insideUnitSphere * 0.2f; randomDistanceFromNodeBias.y = 0f; CreatePath(graph.enterance.transform.position); timesStuckPathfinding++; lastLinecastCheck = Time.time; } else { Vector3 vector = Vector3.zero; Vector3 a = Vector3.zero; vector += Seek(pathToFollow[0].transform.position + randomDistanceFromNodeBias) * followWeight; a = vector; a.y = 0f; vector += SeparateAvoidNodes() * avoidNodesWeight; vector.y = 0f; if (vector != Vector3.zero) { transform.rotation = Quaternion.LookRotation(a + transform.forward); } GetComponent <Rigidbody>().AddForce(vector.normalized * maxSpeed * 10f * Time.deltaTime); } } } else if (currentlyWants == NPC.wants.toExitScene) { if (pathToFollow == null || pathToFollow.Count == 0) { CreatePath(exitNode.transform.position); } else { if ((pathToFollow[0].transform.position + randomDistanceFromNodeBias - transform.position).magnitude < 10f) { pathToFollow.RemoveAt(0); if (UnityEngine.Random.value > 0.5f) { randomDistanceFromNodeBias = UnityEngine.Random.insideUnitSphere * 5f; randomDistanceFromNodeBias.y = 0f; } } RaycastHit raycastHit2; if (pathToFollow.Count == 0) { SpawnNPC.currentNPCs--; Destroy(gameObject); } else if (Time.time > lastLinecastCheck + linecastCheckDelay && Physics.Linecast(transform.position, pathToFollow[0].transform.position, out raycastHit2, layerMask) && raycastHit2.distance < 3f) { if (timesStuckPathfinding > 4) { gameObject.layer = 15; } randomDistanceFromNodeBias = UnityEngine.Random.insideUnitSphere * 0.2f; randomDistanceFromNodeBias.y = 0f; CreatePath(exitNode.transform.position); timesStuckPathfinding++; lastLinecastCheck = Time.time; } else { Vector3 vector2 = Vector3.zero; Vector3 a2 = Vector3.zero; vector2 += Seek(pathToFollow[0].transform.position + randomDistanceFromNodeBias) * followWeight; a2 = vector2; a2.y = 0f; vector2 += SeparateAvoidNodes() * avoidNodesWeight; vector2.y = 0f; if (vector2 != Vector3.zero) { transform.rotation = Quaternion.LookRotation(a2 + transform.forward); } GetComponent <Rigidbody>().AddForce(vector2.normalized * maxSpeed * 10f * Time.deltaTime); } } } else if (currentlyWants == wants.toIdle) { transform.rotation = Quaternion.LookRotation(transform.forward); Vector3 zero = Vector3.zero; GetComponent <Rigidbody>().AddForce(zero.normalized * maxSpeed * 10f * Time.deltaTime); if (Time.time > idleTimeMin + idleTimeRand + idleStartTime) { currentlyWants = previouslyWanted; if (currentlyWants == wants.toExitScene) { FindExit(false); CreatePath(exitNode.transform.position); } } } else if (currentlyWants == wants.toOrder) { GameObject orderPoint = GameObject.Find("OrderPoint"); Vector3 pointOrder = Vector3.zero; for (int i = 0; i < orderPoint.GetComponent <OrderScript>().npcs.ToArray().Length; i++) { if (orderPoint.GetComponent <OrderScript>().npcs.ToArray()[i] == gameObject) { pointOrder += Vector3.forward * i * 3; } } Vector3 vector3 = Vector3.zero; Vector3 a3 = Vector3.zero; if (pathToFollow.Count > 0) { vector3 += Seek(pathToFollow[0].transform.position + pointOrder) * followWeight * 1.5f; a3 = vector3; a3.y = 0f; vector3 += SeparateAvoidNodes() * avoidNodesWeight; vector3.y = 0f; if (vector3 != Vector3.zero) { transform.rotation = Quaternion.LookRotation(a3 + transform.forward); } GetComponent <Rigidbody>().AddForce(vector3.normalized * maxSpeed * 10f * Time.deltaTime); // Move if ((transform.position - pathToFollow[0].transform.position).magnitude < 3) { pathToFollow.RemoveAt(0); } } else { vector3 += Seek(orderPoint.transform.position + pointOrder) * followWeight * 1.5f; a3 = vector3; a3.y = 0f; vector3 += SeparateAvoidNodes() * avoidNodesWeight; vector3.y = 0f; if ((transform.position - (orderPoint.transform.position + pointOrder)).magnitude > 1) { if (vector3 != Vector3.zero) { transform.rotation = Quaternion.LookRotation(a3 + transform.forward); } GetComponent <Rigidbody>().AddForce(vector3.normalized * maxSpeed * 10f * Time.deltaTime); // Move } else { Transform tryGetObject = LookAtPlayer(15); if (tryGetObject) { transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(0, Quaternion.LookRotation(tryGetObject.position - transform.position).eulerAngles.y, 0), 4 * Time.deltaTime); } else { transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(180, 0, 180), 4 * Time.deltaTime); } } //Debug.Log((transform.position - (orderPoint.transform.position + pointOrder)).magnitude); } } else if (currentlyWants == wants.toFindSeat) { List <NPC> list = new List <NPC>(); int num = 0; int num2 = 0; list.Add(this); if (this.desiredGroupSize > 1) { GameObject[] array = GameObject.FindGameObjectsWithTag("NPC"); for (int i = 0; i < array.Length; i++) { GameObject gameObject = array[i]; //Debug.Log(gameObject.name + "; " + base.gameObject.name); //Debug.Log((gameObject != base.gameObject) + "; " + ((transform.position - gameObject.transform.position).magnitude < 40f)); if (gameObject != base.gameObject && (transform.position - gameObject.transform.position).magnitude < 40f) { NPC component = gameObject.GetComponent <NPC>(); if (num2 < desiredGroupSize - 1 && (component.currentlyWants == wants.toFindSeat || component.currentlyWants == wants.toIdle) && component.desiredGroupSize == desiredGroupSize) { list.Add(component); num2++; } } } } // System if (SceneManager.GetSceneAt(0).buildIndex == 3) { int randomNumber = 0; while (randomNumber != int.Parse(orderNumber.name.Substring(orderNumber.name.Length - 1))) { if (randomNumber == int.Parse(orderNumber.name.Substring(orderNumber.name.Length - 1))) { break; } randomNumber = UnityEngine.Random.Range(1, TableGraph.tables.Length + 1); } myNPCGroup = list; if (tableGraph == null) { tableGraph = GameObject.Find("!TableNodes").GetComponent <TableGraph>(); } num = randomNumber; print("Goal table: " + num); if (num >= 0) { foreach (NPC current in list) { if (current != this) { current.myNPCGroupLeader = this; current.setWants(wants.toGetToSeat); } foreach (TableNodes current2 in TableGraph.tables[num - 1].tableNodes) { if (!current2.occupied) { current.targetSeat = current2; current2.SetOccupied(true); current.setWants(wants.toGetToSeat); current.pathToFollow = graph.FindPath(FindClosestNode(current.transform.position), FindClosestVisibleNode(current.targetSeat.transform)); print(string.Concat(new object[] { "Found a seat for id[", list.IndexOf(current), "] @ ", num, " : ", TableGraph.tables[num - 1].tableNodes.IndexOf(current2) })); Enterance.npcsWaiting--; break; } } if (targetSeat == null) { print("Error: no free seat found?"); } } } else { if (desiredGroupSize == 4 && UnityEngine.Random.value > 0.8f) { setGroupSize(gameObject, 2); } myNPCGroup = new List <NPC>(); myNPCGroupLeader = null; setWants(wants.toIdle); idleStartTime = Time.time; idleTimeRand = UnityEngine.Random.Range(20f, 40f); } } else { if ((list.Count > 1 && list.Count == desiredGroupSize) || SceneManager.GetSceneAt(0).buildIndex == 3) { myNPCGroup = list; if (tableGraph == null) { tableGraph = GameObject.Find("!TableNodes").GetComponent <TableGraph>(); } num = TableGraph.FindUnoccupiedTableForGroup(myNPCGroup.Count); print("Goal table: " + num); if (num >= 0) { foreach (NPC current in list) { if (current != this) { current.myNPCGroupLeader = this; current.setWants(wants.toGetToSeat); } foreach (TableNodes current2 in TableGraph.tables[num - 1].tableNodes) { if (!current2.occupied) { current.targetSeat = current2; current2.SetOccupied(true); current.setWants(wants.toGetToSeat); current.pathToFollow = graph.FindPath(FindClosestNode(current.transform.position), FindClosestVisibleNode(current.targetSeat.transform)); print(string.Concat(new object[] { "Found a seat for id[", list.IndexOf(current), "] @ ", num, " : ", TableGraph.tables[num - 1].tableNodes.IndexOf(current2) })); Enterance.npcsWaiting--; break; } } if (targetSeat == null) { print("Error: no free seat found?"); } } } else { if (/*Network.isServer && */ desiredGroupSize == 4 && UnityEngine.Random.value > 0.8f) { setGroupSize(gameObject, 2); } myNPCGroup = new List <NPC>(); myNPCGroupLeader = null; setWants(wants.toIdle); idleStartTime = Time.time; idleTimeRand = UnityEngine.Random.Range(20f, 40f); } } else if (desiredGroupSize == 1) { if (tableGraph == null) { tableGraph = GameObject.Find("!TableNodes").GetComponent <TableGraph>(); } num = TableGraph.FindUnoccupiedTableForGroup(myNPCGroup.Count); if (num >= 0) { foreach (TableNodes current3 in TableGraph.tables[num - 1].tableNodes) { if (!current3.occupied) { targetSeat = current3; current3.SetOccupied(true); setWants(wants.toGetToSeat); pathToFollow = graph.FindPath(FindClosestNode(transform.position), FindClosestVisibleNode(targetSeat.transform)); Enterance.npcsWaiting--; break; } } } setWants(wants.toIdle); idleStartTime = Time.time; idleTimeRand = UnityEngine.Random.Range(20f, 40f); } else { myNPCGroup = new List <NPC>(); myNPCGroupLeader = null; setWants(wants.toIdle); idleStartTime = Time.time; idleTimeRand = UnityEngine.Random.Range(10f, 30f); } } //Debug.Log((list.Count > 1) + " " + (list.Count == desiredGroupSize)); //Debug.Log(list.Count + " " + desiredGroupSize); } else if (currentlyWants == wants.toGetToSeat) { Vector3 vector3 = Vector3.zero; Vector3 a3 = Vector3.zero; if (pathToFollow.Count > 0 && (pathToFollow[0].transform.position - transform.position).magnitude < 3f) { pathToFollow.RemoveAt(0); } RaycastHit raycastHit3; if (Physics.Linecast(transform.position, targetSeat.transform.position, out raycastHit3, layerMask)) { if (pathToFollow.Count > 0) { vector3 += Seek(pathToFollow[0].transform.position) * followWeight; a3 = vector3; a3.y = 0f; vector3 += SeparateAvoidNodes() * avoidNodesWeight; vector3.y = 0f; if (vector3 != Vector3.zero) { transform.rotation = Quaternion.LookRotation(a3 + transform.forward); } GetComponent <Rigidbody>().AddForce(vector3.normalized * maxSpeed * 8f * Time.deltaTime); } else { float num3 = 1f; if ((transform.position - targetSeat.transform.position).magnitude < 6f) { num3 = 0.2f; } if ((transform.position - targetSeat.transform.position).magnitude < 1f) { targetSeat.table.SetNPCAtTable(targetSeat.table.gameObject, gameObject); GetComponent <Rigidbody>().velocity = Vector3.zero; setWants(wants.toEat); /*if (Network.peerType == NetworkPeerType.Server) * { * targetSeat.table.GenerateFoodOrder(); * }*/ if (SceneManager.GetSceneAt(0).buildIndex == 3) { targetSeat.table.GenerateFoodOrder(burgerIndex); } else { targetSeat.table.GenerateFoodOrder(-1); } //targetSeat.table.GenerateFoodOrder(); foodWaitStartTime = Time.time; } vector3 += Seek(targetSeat.transform.position) * followWeight; a3 = vector3; a3.y = 0f; vector3 += SeparateAvoidNodes() * (avoidNodesWeight * num3); vector3.y = 0f; if (vector3 != Vector3.zero) { transform.rotation = Quaternion.LookRotation(a3 + transform.forward); } GetComponent <Rigidbody>().AddForce(vector3.normalized * maxSpeed * 10f * Time.deltaTime); } } else { if ((transform.position - targetSeat.transform.position).magnitude < 1f) { targetSeat.table.SetNPCAtTable(targetSeat.table.gameObject, gameObject); GetComponent <Rigidbody>().velocity = Vector3.zero; setWants(wants.toEat); /*if (Network.peerType == NetworkPeerType.Server) * { * targetSeat.table.GenerateFoodOrder(); * }*/ if (SceneManager.GetSceneAt(0).buildIndex == 3) { targetSeat.table.GenerateFoodOrder(burgerIndex); } else { targetSeat.table.GenerateFoodOrder(-1); } //targetSeat.table.GenerateFoodOrder(); foodWaitStartTime = Time.time; } vector3 += Seek(targetSeat.transform.position) * followWeight * 1.5f; a3 = vector3; a3.y = 0f; vector3 += SeparateAvoidNodes() * avoidNodesWeight; vector3.y = 0f; if (vector3 != Vector3.zero) { transform.rotation = Quaternion.LookRotation(a3 + transform.forward); } GetComponent <Rigidbody>().AddForce(vector3.normalized * maxSpeed * 10f * Time.deltaTime); } } else if (currentlyWants == wants.toLeave) { if (myNPCGroupLeader == null) { int num4 = 0; foreach (NPC current4 in myNPCGroup) { if (current4.currentlyWants == wants.toLeave) { num4++; } } if (num4 == myNPCGroup.Count) { print("Entire group wants to leave"); if (desiredGroupSize > 1) { foreach (NPC current5 in myNPCGroup) { current5.targetSeat.table.npcAtTable = null; //current5.targetSeat.table.ClearOrder(); current5.targetSeat.SetOccupied(false); current5.FindExit(false); current5.CreatePath(current5.exitNode.transform.position); current5.setWants(wants.toExitScene); } } else { targetSeat.table.npcAtTable = null; //targetSeat.table.ClearOrder(); targetSeat.SetOccupied(false); FindExit(false); CreatePath(exitNode.transform.position); setWants(wants.toExitScene); } } } } else if (currentlyWants == wants.toGetMenu) { if (Time.time > menuWaitStartTime + menuWaitBaseDuration) { currentlyWants = wants.toLeave; } } else if (currentlyWants == wants.toEat && Time.time > foodWaitStartTime + foodWaitBaseDuration) { currentlyWants = wants.toLeave; } if (SceneManager.GetSceneAt(0).buildIndex == 3) { gameObject.GetComponent <Collider>().enabled = currentlyWants != wants.toEat; gameObject.GetComponent <Rigidbody>().isKinematic = currentlyWants == wants.toEat; if (currentlyWants == wants.toEat) { transform.rotation = Quaternion.Euler(0, Quaternion.LookRotation(orderNumber.transform.position - transform.position).eulerAngles.y, 0); } } // System Scenes if (orderNumber) { switch (currentlyWants) { case wants.toGetToSeat: orderNumber.GetComponent <Rigidbody>().isKinematic = true; orderNumber.GetComponent <Collider>().enabled = false; orderNumber.transform.position = Vector3.Lerp(orderNumber.transform.position, transform.position + transform.forward * 2, 0.5f); orderNumber.transform.rotation = Quaternion.Lerp(orderNumber.transform.rotation, transform.rotation, 0.5f); break; case wants.toEat: orderNumber.transform.position = Vector3.Lerp(orderNumber.transform.position, targetSeat.table.transform.position + targetSeat.table.transform.up * 1.1f + targetSeat.table.transform.right * 3, 0.5f); orderNumber.transform.rotation = Quaternion.Lerp(orderNumber.transform.rotation, targetSeat.table.transform.rotation, 0.5f); break; case wants.toExitScene: orderNumber.GetComponent <Rigidbody>().isKinematic = false; orderNumber.GetComponent <Collider>().enabled = true; orderNumber = null; break; } } // If he got Order Number }