private bool Pathfind(PathmapTile fromTile, PathmapTile toTile, List <PathmapTile> path) { fromTile.visited = true; if (fromTile.blocking) { return(false); } path.Add(fromTile); if (fromTile == toTile) { return(true); } List <PathmapTile> neighbours = new List <PathmapTile>(); PathmapTile up = GetTile(fromTile.posX, fromTile.posY - 1); if (up != null && !up.visited && !up.blocking && !path.Contains(up)) { neighbours.Insert(0, up); } PathmapTile down = GetTile(fromTile.posX, fromTile.posY + 1); if (down != null && !down.visited && !down.blocking && !path.Contains(down)) { neighbours.Insert(0, down); } PathmapTile right = GetTile(fromTile.posX + 1, fromTile.posY); if (right != null && !right.visited && !right.blocking && !path.Contains(right)) { neighbours.Insert(0, right); } PathmapTile left = GetTile(fromTile.posX - 1, fromTile.posY); if (left != null && !left.visited && !left.blocking && !path.Contains(left)) { neighbours.Insert(0, left); } for (int n = 0; n < neighbours.Count; n++) { PathmapTile tile = neighbours[n]; path.Add(tile); if (Pathfind(tile, toTile, path)) { return(true); } path.Remove(tile); } return(false); }
private void Chase() { if (timer >= changeDirectionTimer) { if (UnityEngine.Random.Range(0, 100) > 70) { path.Clear(); path = MapManager.Get().GetPath(currentTileX, currentTileY, GameManager2.Get().GetPlayer().currentTileX, GameManager2.Get().GetPlayer().currentTileY); } timer = 0f; } else { timer += Time.deltaTime; } if (IsAtDestination()) { if (path.Count > 0) { PathmapTile nextTile = path[0]; path.RemoveAt(0); SetNextTile(new Vector2Int(nextTile.posX, nextTile.posY)); } else { path.Clear(); path = MapManager.Get().GetPath(currentTileX, currentTileY, GameManager2.Get().GetPlayer().currentTileX, GameManager2.Get().GetPlayer().currentTileY); } } MoveToDestination(); }
public bool InitPathmap() { string[] lines = System.IO.File.ReadAllLines("Assets/Data/Map.txt"); for (int y = 0; y < lines.Length; y++) { char[] line = lines[y].ToCharArray(); for (int x = 0; x < line.Length; x++) { PathmapTile tile = new PathmapTile(); tile.posX = x; tile.posY = -y; tiles.Add(tile); switch (line[x]) { case 'x': tile.blocking = true; break; case 'p': playerStartPos = new Vector2(tile.posX, tile.posY); break; case 'g': ghostStartPos = new Vector2(tile.posX, tile.posY); break; case 'G': ghostExitPos = new Vector2Int(tile.posX, tile.posY); break; case '.': SmallDot smallDot = GameObject.Instantiate(smallDotPrefab).GetComponent <SmallDot>(); smallDot.transform.SetParent(smallDotGroup.transform); smallDot.SetPosition(new Vector2(x * MapManager.Get().tileSize, -y * MapManager.Get().tileSize)); smallDot.OnCollected += OnItemColleted; smallDots.Add(smallDot); dotCount++; break; case 'o': PowerDot powerDot = GameObject.Instantiate(powerDotPrefab).GetComponent <PowerDot>(); powerDot.transform.SetParent(powerDotGroup.transform); powerDot.SetPosition(new Vector2(x * MapManager.Get().tileSize, -y * MapManager.Get().tileSize)); powerDot.OnCollected += OnItemColleted; powerDots.Add(powerDot); dotCount++; break; } } } canSpawnCherry = true; return(true); }
public bool InitPathmap() { string[] lines = System.IO.File.ReadAllLines("Assets/Data/map.txt"); for (int y = 0; y < lines.Length; y++) { char[] line = lines[y].ToCharArray(); for (int x = 0; x < line.Length; x++) { PathmapTile tile = new PathmapTile(); tile.posX = x; tile.posY = y; tile.blocking = line[x] == 'x'; tiles.Add(tile); } } return(true); }
public List <PathmapTile> GetPath(int currentTileX, int currentTileY, int targetX, int targetY) { PathmapTile fromTile = GetTile(currentTileX, currentTileY); PathmapTile toTile = GetTile(targetX, targetY); for (int t = 0; t < tiles.Count; t++) { tiles[t].visited = false; } List <PathmapTile> path = new List <PathmapTile>(); if (Pathfind(fromTile, toTile, path)) { return(path); } return(null); }
private void SpawnCherry() { if (canSpawnCherry) { if (timer >= cherryTimer) { PathmapTile tile = tiles[UnityEngine.Random.Range(0, tiles.Count)]; if (!tile.blocking) { cherry.gameObject.SetActive(true); cherry.SetPosition(new Vector2(tile.posX * MapManager.Get().tileSize, tile.posY * MapManager.Get().tileSize)); timer = 0f; canSpawnCherry = false; } } else { timer += Time.deltaTime; } } }
// Update is called once per frame public void onUpdate(Map map, PacMan avatar) { float speed = 30.0f; int nextTileX = currentTileX + desiredMovementX; int nextTileY = currentTileY + desiredMovementY; if (isDead) { speed = 120.0f; } if (IsAtDestination()) { if (path.Count > 0) { PathmapTile nextTile = path[0]; path.RemoveAt(0); SetNextTile(new Vector2Int(nextTile.posX, nextTile.posY)); } else if (map.TileIsValid(nextTileX, nextTileY)) { SetNextTile(new Vector2Int(nextTileX, nextTileY)); } else { if (isClaimable) { BehaveVulnerable(); } else { switch (myBehaviour) { case Behaviour.Chase: BehaveChase(map, avatar); break; case Behaviour.Intercept: case Behaviour.Wander: default: BehaveWander(); break; } } isDead = false; } } int tileSize = 22; Vector2 destination = new Vector2(nextTileX * tileSize, nextTileY * tileSize); Vector2 direction = destination - GetPosition(); float distanceToMove = Time.deltaTime * speed; if (distanceToMove > direction.magnitude) { SetPosition(destination); currentTileX = nextTileX; currentTileY = nextTileY; } else { direction.Normalize(); SetPosition(GetPosition() + direction * distanceToMove); } }