public LFLabyrinthNode RandomFreeNode() { List <LFLabyrinthNode> notWallNodes = GetAllNotWallNodes(); LFLabyrinthNode randomNode = notWallNodes [Random.Range(0, notWallNodes.Count)]; return(randomNode); }
private void CreatePlayer() { LFLabyrinthNode spawnNode = _labyrinth.GridNodeFromWorldPosition(playerSpawn.transform.position); _player = Instantiate(playerPrefab, spawnNode.WorldPosition, Quaternion.identity); _player.transform.parent = labyrinth.transform; }
private void InitPath() { int startX = Random.Range(0, _gridSizeX); int startY = Random.Range(0, _gridSizeY); LFLabyrinthNode currentNode = _grid [startX, startY]; int step = 0; while (step < pathStepCount) { currentNode.IsWall = false; List <LFLabyrinthNode> neighbours = NeighbourNodes(currentNode); LFLabyrinthNode newNeighbour = neighbours[0]; List <LFLabyrinthNode> newNexNeighbours = NeighbourNodes(newNeighbour); int maxWallCount = NeighboursWallCount(newNexNeighbours); for (int i = 1; i < neighbours.Count; i++) { LFLabyrinthNode neighbour = neighbours[i]; List <LFLabyrinthNode> nextNeighbours = NeighbourNodes(neighbour); int newWallCount = NeighboursWallCount(nextNeighbours); if (newWallCount >= maxWallCount) { newNeighbour = neighbour; maxWallCount = newWallCount; } } currentNode = newNeighbour; step += 1; } }
private List <LFLabyrinthNode> NeighbourNodes(LFLabyrinthNode node) { List <LFLabyrinthNode> neighbours = new List <LFLabyrinthNode> (); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if ((x == 0 && y == 0) || (x == 1 && y == 1) || (x == -1 && y == -1) || (x == -1 && y == 1) || (x == 1 && y == -1)) { continue; } int checkX = node.GridX + x; int checkY = node.GridY + y; if (checkX >= 0 && checkX < _gridSizeX && checkY >= 0 && checkY < _gridSizeY) { neighbours.Add(_grid [checkX, checkY]); } } } return(neighbours); }
private void CreateCoin() { if (_labyrinth == null) { return; } GameObject coin = Instantiate(coinPrefab, gameObject.transform); LFLabyrinthNode spawnNode = _labyrinth.RandomFreeNode(); //int z = 0; coin.transform.position = spawnNode.WorldPosition; //new Vector3 (spawnNode.WorldPosition.x, spawnNode.WorldPosition.y, z); _coins.Add(coin); }
private void InitGrid() { _grid = new LFLabyrinthNode[_gridSizeX, _gridSizeY]; Vector3 leftBottomCorner = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.up * gridWorldSize.y / 2; for (int x = 0; x < _gridSizeX; x++) { for (int y = 0; y < _gridSizeY; y++) { Vector3 worldPoint = leftBottomCorner + Vector3.right * (x * nodeWidth + nodeWidth / 2) + Vector3.up * (y * nodeWidth + nodeWidth / 2); _grid[x, y] = new LFLabyrinthNode(false, worldPoint, x, y); } } }
public LFLabyrinthNode RandomFreeNodeWithOutPosition(Vector3 deletedPosition) { List <LFLabyrinthNode> notWallNodes = GetAllNotWallNodes(); LFLabyrinthNode deletedNode = GridNodeFromWorldPosition(deletedPosition); if (notWallNodes.Contains(deletedNode)) { notWallNodes.Remove(deletedNode); } LFLabyrinthNode randomNode = notWallNodes [Random.Range(0, notWallNodes.Count)]; return(randomNode); }
public void CreateMummy() { if (_labyrinth == null || player == null || mummyPrefab == null) { return; } LFLabyrinthNode spawnNode = _labyrinth.RandomFreeNodeWithOutPosition(player.transform.position); GameObject mummy = Instantiate(mummyPrefab, spawnNode.WorldPosition, Quaternion.identity); mummy.transform.parent = gameObject.transform; mummy.GetComponent <LFEnemyMove>().pathFinder = _pathFinder; mummy.GetComponent <LFEnemyMove>().speed = startMommySpeed; _enemyList.Add(mummy); }
public void CreateZombie() { if (_labyrinth == null || player == null || zombiePrefab == null) { return; } LFLabyrinthNode spawnNode = _labyrinth.RandomFreeNodeWithOutPosition(player.transform.position); GameObject zombie = Instantiate(zombiePrefab, spawnNode.WorldPosition, Quaternion.identity); zombie.transform.parent = enemyContainer.transform; zombie.GetComponent <LFEnemyMove>().pathFinder = _pathFinder; zombie.GetComponent <LFEnemyMove>().speed = startZombieSpeed; _enemyList.Add(zombie); }
public void CreatePing() { if (_labyrinth == null || pingPrefab == null) { return; } LFLabyrinthNode spawnNode = _labyrinth.RandomFreeNode(); GameObject pig = Instantiate(pingPrefab, spawnNode.WorldPosition, Quaternion.identity); pig.transform.parent = animalContainer.transform; pig.GetComponent <LFAnimalMove>().pathFinder = _pathFinder; pig.GetComponent <LFAnimalMove>().speed = startPingSpeed; pig.SetActive(true); _animalList.Add(pig); }
private int NeighboursWallCount(List <LFLabyrinthNode> neighbours) { int wallCount = 0; for (int i = 0; i < neighbours.Count; i++) { LFLabyrinthNode neighbour = neighbours[i]; if (neighbour.IsWall) { wallCount += 1; } } return(wallCount); }
private void CreateWallBlocks() { _wallBlocks = new List <GameObject>(); for (int x = 0; x < _gridSizeX; x++) { for (int y = 0; y < _gridSizeY; y++) { LFLabyrinthNode node = _grid [x, y]; if (node.IsWall) { GameObject wall = Instantiate(wallBlockPrefab, new Vector3(node.WorldPosition.x, node.WorldPosition.y, 0.5f), Quaternion.identity); wall.transform.parent = ground.transform; _wallBlocks.Add(wall); } } } }