public Vector2 OppositeSideOfScreenLoc() { Vector2 npcSpawnPoint = parent.transform.position; var grid = NodeGridManager.GetGrid(PathFinding.Resolution.Medium); Camera camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent <Camera>(); System.Random rnd = new System.Random(); var npcLoc = camera.WorldToViewportPoint(npcSpawnPoint); float locXOffset = (float)rnd.NextDouble() * (.8f - .65f) + .65f; if (npcLoc.x >= 1) { locXOffset = (float)rnd.NextDouble() * (.35f - .2f) + .2f; } float locYOffset = (float)rnd.NextDouble() * (.8f - .65f) + .65f; if (npcLoc.y >= 1) { locYOffset = (float)rnd.NextDouble() * (.35f - .2f) + .2f; } Vector3 targetLoc = camera.ViewportToWorldPoint(new Vector3(locXOffset, locYOffset, camera.nearClipPlane)); Vector2 newLoc = new Vector2(targetLoc.x, targetLoc.y); return(newLoc); }
void SpawnOutOfView() { var camPos = (Vector2) GameLibOfMethods.player.transform.position; var sqrOrthoSize = cam.orthographicSize * cam.orthographicSize; var grid = NodeGridManager.GetGrid(PathFinding.Resolution.Low); int currentAttempt = 0; while (currentAttempt <= maxAttemptsForNewLocation) { currentAttempt++; //var rndNode = grid.GetRandomWalkable(); //Test Camera camera = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>(); random rnd = new random(); float spawnX = rnd.Next(0, 2); float spawnY = rnd.Next(0, 2); spawnX = AdjustSpawnPoints(spawnX); spawnY = AdjustSpawnPoints(spawnY); Vector3 spawnPoint = camera.ViewportToWorldPoint(new Vector3(spawnX, spawnY, camera.nearClipPlane)); Vector2 newLoc = new Vector2(spawnPoint.x, spawnPoint.y); var rndNode = grid.NodeFromWorldPoint(newLoc); //Test End // We don't want nodes that are occupied if (rndNode.isCurrentlyOccupied != null) { continue; } if (!rndNode.walkable) { continue; } var sqrMag = Vector2.SqrMagnitude(newLoc - camPos); // We don't want nodes that are within camera view; if (sqrMag <= 2 * sqrOrthoSize) { continue; } parent.transform.position = newLoc; return; } // We reach here if the max attempts limit has been exceeded // TODO: Make sure it never happens.. somehow.. Debug.Log("Max Attempts limit on spawning NPC outside the camera's frustum has been exceeded."); // Safety for not appearing on the first frame // .. has to happen since the physics won't update until FixedUpdate, and we do this in Update parent.transform.position = Vector3.one * 1000; }
void DisappearSafely() { var camPos = cam.transform.position; var pos = parent.transform.position; var sqrOrthoSize = cam.orthographicSize * cam.orthographicSize; var sqrMag = Vector2.SqrMagnitude(pos - camPos); if (sqrMag >= 3 * sqrOrthoSize) { return; } var randomNode = NodeGridManager.GetGrid(PathFinding.Resolution.Medium).GetRandomWalkable(); var newFadePos = NodeGridManager.GetGrid(PathFinding.Resolution.Medium).PosFromNode(randomNode); parent.commandQueue.Enqueue(new HangAroundCommand(parent, Random.Range(1, 5f))); parent.commandQueue.Enqueue(new MoveToCommand(parent, newFadePos)); parent.commandQueue.Enqueue(new DisappearCommand(parent)); }
void FireNext() { if (npcPool.Count <= 0) { return; } if (npcPool.Count <= PoolAmount - MaxActive) { return; } //T RandomFromList<T>(List<T> list) => list[Random.Range(0, list.Count)]; //Vector2 target = RandomFromList(npcLocationHelper.GoToLocations); //Vector2 origin = RandomFromList(npcLocationHelper.SpawnLocations); //target += new Vector2(Random.Range(-5f, 5f), Random.Range(5f, 5f)); //float time = Random.Range(1f, 20f); var grid = NodeGridManager.GetGrid(PathFinding.Resolution.Medium); var disappearNode = grid.GetRandomWalkable(); var disappearPos = grid.PosFromNode(disappearNode); var nextNPC = npcPool.Dequeue(); // If the command queue is not empty there is an error in code. if (nextNPC.commandQueue.Count != 0) { Debug.LogWarning($"Command queue of {nextNPC} is not empty."); } nextNPC.commandQueue.Enqueue(new AppearCommand(nextNPC)); nextNPC.commandQueue.Enqueue(new MoveToCommand(nextNPC, disappearPos)); //nextNPC.commandQueue.Enqueue(new MoveToCommand(nextNPC, target)); //nextNPC.commandQueue.Enqueue(new HangAroundCommand(nextNPC, time)); //nextNPC.commandQueue.Enqueue(new MoveToCommand(nextNPC, origin)); nextNPC.commandQueue.Enqueue(new DisappearCommand(nextNPC)); // Patch for the single frame that the NPC appears on the screen before searching for a new location. nextNPC.transform.position = Vector3.one * 1000; nextNPC.gameObject.SetActive(true); nextNPC.GetNextCommand(); }
void SpawnOutOfView() { var camPos = (Vector2)GameLibOfMethods.player.transform.position; var sqrOrthoSize = cam.orthographicSize * cam.orthographicSize; var grid = NodeGridManager.GetGrid(PathFinding.Resolution.Medium); int currentAttempt = 0; while (currentAttempt <= maxAttemptsForNewLocation) { currentAttempt++; var rndNode = grid.GetRandomWalkable(); // We don't want nodes that are occupied if (rndNode.isCurrentlyOccupied != null) { continue; } var loc = grid.PosFromNode(rndNode); var sqrMag = Vector2.SqrMagnitude(loc - camPos); // We don't want nodes that are within camera view; if (sqrMag <= 3 * sqrOrthoSize) { continue; } parent.transform.position = loc; return; } // We reach here if the max attempts limit has been exceeded // TODO: Make sure it never happens.. somehow.. Debug.Log("Max Attempts limit on spawning NPC outside the camera's frustum has been exceeded."); // Safety for not appearing on the first frame // .. has to happen since the physics won't update until FixedUpdate, and we do this in Update parent.transform.position = Vector3.zero; }
public void Initialize() { GetPath(true); grid = NodeGridManager.GetGrid(resolution); PlayAnim(AnimationType.Walk); if (toScreen) { var getNodeTries = 0; Target = OppositeSideOfScreenLoc(); Node targetNode = grid.NodeFromWorldPoint(Target); while (!targetNode.walkable) { Target = OppositeSideOfScreenLoc(); targetNode = grid.NodeFromWorldPoint(Target); getNodeTries++; if (getNodeTries > 20) { break; } } } }
public void ExecuteCommand() { FrameSafeEnsurance(); if (gotValidPath && index >= path.Count) { IsFinished = true; PlayAnim(AnimationType.Idle); return; } // Local avoidance implementation if (ShouldGetNewPath()) { GetPath(); return; } if (!gotValidPath) { PlayAnim(AnimationType.Idle); return; } var NPCAdjustedMultiplier = Mathf.Clamp(GameTime.Clock.TimeMultiplier, 1, 2); var spd = Speed * GameTime.Time.fixedDeltaTime * NPCAdjustedMultiplier; PlayAnim(AnimationType.Walk); var pos = (Vector2)parent.transform.position; var targetPos = grid.PosFromNode(path[index]); var offset = targetPos - pos; var posAfterMovement = Vector2.MoveTowards(pos, targetPos, spd); if (posAfterMovement == targetPos) { index++; if (index < path.Count) { if (!CanWalkAt(path[index])) { } else { targetPos = grid.PosFromNode(path[index]); offset = targetPos - pos; posAfterMovement = Vector2.MoveTowards(pos, targetPos, spd); } } } rb.MovePosition(posAfterMovement); if (!hasUpdatedThisFrame) { NodeGridManager.SetPosUnwalkable(pos, col); } if (index >= path.Count) { IsFinished = true; PlayAnim(AnimationType.Idle); } else { CalculateFacing(offset); } hasUpdatedThisFrame = true; }
public static Node GetRandomWalkable(PathFinding.Resolution resolution) => NodeGridManager.GetGrid(resolution).GetRandomWalkable();