public IEnumerator BombTimer() { MapGenerator.Coord myPosition = MapGenerator.ConvertWorldToIndex(transform.position); Vector2 spawnPos = MapGenerator.initialPos + new Vector2(TILE_SIZE * myPosition.i, -TILE_SIZE * myPosition.j); GameObject bomb = Instantiate(m_BombPrefab); bomb.transform.position = spawnPos; m_Inventory.RemoveBomb(m_SpawnedBomb); yield return(new WaitForSeconds(m_BombDuration)); Destroy(bomb); GameObject explosion = Instantiate(m_ExplosionPrefab); explosion.transform.position = spawnPos; yield return(new WaitForSeconds(m_ExplosionDuration)); Destroy(explosion); MapGenerator.Coord bombPosition = MapGenerator.ConvertWorldToIndex(spawnPos); int bombPos = GetTile(bombPosition.i, bombPosition.j); if ((m_FoeMovement.ActualFoePosition.i == bombPosition.i - 2) && (m_FoeMovement.ActualFoePosition.j == bombPosition.j - 4)) { MapGenerator.m_FoeCopy.SetActive(false); m_enemyDefeated = true; } }
private void PreparePlayerStartPosition(ushort[,] map, MeshGenerator.SquareGrid squareGrid) { if (mainRoom == null) { return; } int tries = 0; bool valid = false; do { MapGenerator.Coord playerStartCoord = mainRoom.tiles[UnityEngine.Random.Range(0, mainRoom.tiles.Count - 1)]; playerStartNode = squareGrid.controlNodes[playerStartCoord.tileX, playerStartCoord.tileY]; valid = playerStartNode != null && !playerStartNode.active && // active means wall map[playerStartNode.mapCoords.tileX, playerStartNode.mapCoords.tileY] == (ushort)EnvironmentObjectType.Void; } while (!valid && tries++ <= mainRoom.tiles.Count); if (playerStartNode == null || playerStartNode.active) { Debug.Log("Can't find player start node"); } else { map[playerStartNode.mapCoords.tileX, playerStartNode.mapCoords.tileY] = (ushort)EnvironmentObjectType.PlayerStart; } }
public void PickItems() { MapGenerator.Coord myPosition = MapGenerator.ConvertWorldToIndex(transform.position); int myActualPosition = GetTile(myPosition.i, myPosition.j); Vector2 spawnPos = MapGenerator.initialPos + new Vector2(TILE_SIZE * myPosition.i, -TILE_SIZE * myPosition.j); if ((myActualPosition == (int)MapGenerator.KeyTile) && (Input.GetKeyDown(KeyCode.Space))) { int itemindex = GetTile(myPosition.i, myPosition.j); m_Inventory.AddKey(new Key()); MapGenerator.m_PlayerPos[myPosition.i, myPosition.j] = MapGenerator.WalkableTile; GameObject blank = Instantiate(m_BlankPrefab); blank.transform.position = spawnPos; } else if ((myActualPosition == (int)MapGenerator.RupeeTile) && (Input.GetKeyDown(KeyCode.Space))) { int itemindex = GetTile(myPosition.i, myPosition.j); m_Inventory.AddRupee(new Rupee()); MapGenerator.m_PlayerPos[myPosition.i, myPosition.j] = MapGenerator.WalkableTile; GameObject blank = Instantiate(m_BlankPrefab); blank.transform.position = spawnPos; } else if ((myActualPosition == (int)MapGenerator.BombTile) && (Input.GetKeyDown(KeyCode.Space))) { int itemindex = GetTile(myPosition.i, myPosition.j); m_Inventory.AddBomb(m_SpawnedBomb); MapGenerator.m_PlayerPos[myPosition.i, myPosition.j] = MapGenerator.WalkableTile; GameObject blank = Instantiate(m_BlankPrefab); blank.transform.position = spawnPos; } }
public void SpeakToPeople() { IInteractiveObject otherObject = null; MapGenerator.Coord myPosition = MapGenerator.ConvertWorldToIndex(transform.position); switch (currentDirection) { case direction.up: otherObject = GetInteractiveObject(myPosition.i, myPosition.j - 1); break; case direction.down: otherObject = GetInteractiveObject(myPosition.i, myPosition.j + 1); break; case direction.left: otherObject = GetInteractiveObject(myPosition.i - 1, myPosition.j); break; case direction.right: otherObject = GetInteractiveObject(myPosition.i + 1, myPosition.j); break; } if (otherObject != null && Input.GetKey(KeyCode.Space)) { otherObject.StartInteraction(); } }
public ControlNode(Vector3 _pos, MapGenerator.Coord _mapCoords, bool _active, float squareSize) : base(_pos) { mapCoords = _mapCoords; active = _active; above = new Node(position + Vector3.forward * squareSize / 2f); right = new Node(position + Vector3.right * squareSize / 2f); }
bool MapIsFullyAccessible(bool[,] obstacleMap) { int currentObstacleCount = 0; for (int x = 0; x < mapSizex; x++) { for (int y = 0; y < mapSizey; y++) { Tile tile = tileMap[x, y].GetComponent <Tile>(); if (tile.isOccuped) { obstacleMap[x, y] = true; currentObstacleCount++; } else { obstacleMap[x, y] = false; } } } bool[,] mapFlags = new bool[obstacleMap.GetLength(0), obstacleMap.GetLength(1)]; Queue <MapGenerator.Coord> queue = new Queue <MapGenerator.Coord>(); queue.Enqueue(currentMap.mapCenter); mapFlags[currentMap.mapCenter.x, currentMap.mapCenter.y] = true; int accessibleTileCount = 1; while (queue.Count > 0) { MapGenerator.Coord tile = queue.Dequeue(); for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { int neighbourX = tile.x + x; int neighbourY = tile.y + y; if (x == 0 || y == 0) { if (neighbourX >= 0 && neighbourX < obstacleMap.GetLength(0) && neighbourY >= 0 && neighbourY < obstacleMap.GetLength(1)) { if (!mapFlags[neighbourX, neighbourY] && !obstacleMap[neighbourX, neighbourY]) { mapFlags[neighbourX, neighbourY] = true; queue.Enqueue(new MapGenerator.Coord(neighbourX, neighbourY)); accessibleTileCount++; } } } } } } int targetAccessibleTileCount = (int)(mapSizex * mapSizey - currentObstacleCount); return(targetAccessibleTileCount == accessibleTileCount); }
public LinkedList <Vector3> GetPath(Vector3 from, Vector3 to) { MapGenerator.Coord start = mapGen.WorldToCoordPoint(from); MapGenerator.Coord end = mapGen.WorldToCoordPoint(to); Node startNode = nodeMap[start.tileX, start.tileY]; Node endNode = nodeMap[end.tileX, end.tileY]; LinkedList <Vector3> path = null; System.Threading.Thread newThread = new System.Threading.Thread(() => path = startNode.FindPathTo(endNode)); newThread.Start(); newThread.Join(); return(path); }
public Vector3 GetPosition(MapGenerator.Coord mapCoords) { if (CommonUtils.IsInRange( mapCoords.tileX, mapCoords.tileY, controlNodes.GetLength(0), controlNodes.GetLength(1)) && controlNodes[mapCoords.tileX, mapCoords.tileY] != null) { return(controlNodes[mapCoords.tileX, mapCoords.tileY].position); } else { return(Vector3.negativeInfinity); } }
public void SwitchLevel() { MapGenerator.Coord myPosition = MapGenerator.ConvertWorldToIndex(transform.position); int myActualPosition = GetTile(myPosition.i, myPosition.j); if ((myPosition.i == 2 && myPosition.j == 9) || (myPosition.i == 3 && myPosition.j == 9)) { SceneManager.LoadScene("Game2"); SauvegardeManager.ImportData("GameContentLvl2.json"); } if ((myPosition.i == 2 && myPosition.j == 2) || (myPosition.i == 3 && myPosition.j == 2)) { SceneManager.LoadScene("Game"); SauvegardeManager.ImportData("GameContentLvl1.json"); } }
private void SpawnTrees() { MapTileType[,] map = this.mapGenerator.GetMap(); for (int x = 0; x < this.mapGenerator.width; x++) { for (int y = 0; y < this.mapGenerator.height; y++) { if (map[x, y] == MapTileType.WALL) { if ((x % this.treeSparseness == 0 && y % this.treeSparseness == 0) || (x == 0 || y == 0 || x == this.mapGenerator.width - 1 || y == this.mapGenerator.height - 1)) { MapGenerator.Coord coord = new MapGenerator.Coord(x, y); Vector3 spawnLocation = this.mapGenerator.CoordToWorldPoint(coord); GameObject tree = Instantiate(treePrefab, spawnLocation, Quaternion.identity) as GameObject; float randScale = Random.Range(0.5f, 2f); tree.transform.localScale = new Vector3(randScale, randScale, 1); tree.transform.SetParent(this.treeParent); } } } } }
internal void InitSpawn( ushort[,] map, MeshGenerator.SquareGrid squareGrid, List <MapGenerator.Room> rooms, HashSet <MapGenerator.Passage> passages) { foreach (MapGenerator.Room room in rooms) { MapGenerator.Coord enemySpawnCoord = default(MapGenerator.Coord); int tries = 0; bool match = false; do { MapGenerator.Coord curCoord = room.tiles[UnityEngine.Random.Range(0, room.tiles.Count)]; if (map[curCoord.tileX, curCoord.tileY] == (ushort)EnvironmentObjectType.Void) { enemySpawnCoord = curCoord; match = true; break; } } while (tries++ <= 5); if (!match) { continue; } Vector3 enemyPosition = squareGrid.controlNodes[enemySpawnCoord.tileX, enemySpawnCoord.tileY].position; GameObject enemyObject = Instantiate(enemyPrefab, enemyPosition + enemyPrefab.transform.position, enemyPrefab.transform.rotation); enemyObject.transform.parent = enemiesHolder.transform; enemyObject.layer = enemiesHolder.layer; } }
private MapGenerator.Coord GetAvailableSpot(MapGenerator.Coord spawnCoord, int[,] map) { Vector2 newPos = GenerateRandomPos(spawnCoord.x, spawnCoord.y); int r = 2; if (!IsInsideMapRange((int)newPos.x, (int)newPos.y, r, map)) { return(GetAvailableSpot(spawnCoord, map)); } for (int x = (int)newPos.x - r; x <= (int)newPos.x + r; x++) { for (int y = (int)newPos.y - r; y <= (int)newPos.y + r; y++) { if ((Mathf.Pow(x - newPos.x, 2) + Mathf.Pow(y - newPos.y, 2)) <= Mathf.Pow(r, 2)) { if (map[x, y] == 0) { return(GetAvailableSpot(spawnCoord, map)); } } } } if (map[(int)newPos.x, (int)newPos.y] == 1) { MapGenerator.Coord randomCoord = new MapGenerator.Coord((int)newPos.x, (int)newPos.y); return(randomCoord); } else { spawnRange += Time.deltaTime; return(GetAvailableSpot(spawnCoord, map)); } }
public void SetCost(Vector3 position, int cost) { MapGenerator.Coord tile = mapGen.WorldToCoordPoint(position); nodeMap[tile.tileX, tile.tileY].SetCost(cost); }
public Vector3 CoordToWorldPoint(MapGenerator.Coord tile) { return(new Vector3(-mapGenerator.Width / 2 + mapGenerator.unitSize / 2 + tile.x, 1f, -mapGenerator.Height / 2 + mapGenerator.unitSize / 2 + tile.y)); }
public bool AreNeighbours(Vector3 a, Vector3 b) { MapGenerator.Coord tileA = mapGen.WorldToCoordPoint(a); MapGenerator.Coord tileB = mapGen.WorldToCoordPoint(b); return(nodeMap[tileA.tileX, tileA.tileY].IsNeighbour(nodeMap[tileB.tileX, tileB.tileY])); }
public void ToMove() { MapGenerator.Coord FoePosition = MapGenerator.ConvertWorldToIndex(transform.position); int rand = Random.Range(-1, 2); int rand2 = Random.Range(-1, 2); float askMoveHorizontal = rand; float askMoveVertical = rand2; int indexvalueleft = GetTile(FoePosition.i - 1, FoePosition.j); int indexvalueright = GetTile(FoePosition.i + 1, FoePosition.j); int indexvaluetop = GetTile(FoePosition.i, FoePosition.j - 1); int indexvaluedown = GetTile(FoePosition.i, FoePosition.j + 1); if (!m_IsMoving) { m_Animator.SetFloat("x", askMoveHorizontal); m_Animator.SetFloat("y", askMoveVertical); m_Animator.SetBool("isWalking", false); if (askMoveHorizontal != 0) { m_PercentageCompletion = 0; m_IsMoving = true; if (askMoveHorizontal < 0) { if (indexvalueleft == (int)MapGenerator.WalkableTile || indexvalueleft == ((int)MapGenerator.KeyTile) || indexvalueleft == ((int)MapGenerator.RupeeTile) || indexvalueleft == ((int)MapGenerator.BombTile)) { m_IsMoving = true; m_InitialPos = m_RigidBody.transform.position; Vector2 offset = (m_Xvelocity * TILE_SIZE) * askMoveHorizontal; m_WantedPos = m_InitialPos + offset; m_Animator.SetBool("isWalking", true); m_SpriteRenderer.flipX = false; } else { m_IsMoving = false; } } else if (askMoveHorizontal > 0) { if (indexvalueright == ((int)MapGenerator.WalkableTile) || indexvalueright == ((int)MapGenerator.KeyTile) || indexvalueright == ((int)MapGenerator.RupeeTile) || indexvalueright == ((int)MapGenerator.BombTile)) { m_IsMoving = true; m_InitialPos = m_InitialPos = m_RigidBody.transform.position; Vector2 offset = (m_Xvelocity * TILE_SIZE) * askMoveHorizontal; m_WantedPos = m_InitialPos + offset; m_Animator.SetBool("isWalking", true); m_SpriteRenderer.flipX = true; } else { m_IsMoving = false; } } else if (askMoveHorizontal == 0) { m_IsMoving = false; m_Animator.SetBool("isWalking", false); } } else if (askMoveVertical != 0) { m_PercentageCompletion = 0; m_IsMoving = true; if (askMoveVertical < 0) { if (indexvaluedown == (int)MapGenerator.WalkableTile || indexvaluedown == ((int)MapGenerator.KeyTile) || indexvaluedown == ((int)MapGenerator.RupeeTile) || indexvaluedown == ((int)MapGenerator.BombTile)) { m_IsMoving = true; m_InitialPos = m_RigidBody.transform.position; Vector2 offset = (m_Yvelocity * TILE_SIZE) * askMoveVertical; m_WantedPos = m_InitialPos + offset; m_Animator.SetBool("isWalking", true); } else { m_IsMoving = false; } } else if (askMoveVertical > 0) { if (indexvaluetop == (int)MapGenerator.WalkableTile || indexvaluetop == ((int)MapGenerator.KeyTile) || indexvaluetop == ((int)MapGenerator.RupeeTile) || indexvaluetop == ((int)MapGenerator.BombTile)) { m_IsMoving = true; m_InitialPos = m_RigidBody.transform.position; Vector2 offset = (m_Yvelocity * TILE_SIZE) * askMoveVertical; m_WantedPos = m_InitialPos + offset; m_Animator.SetBool("isWalking", true); } else { m_IsMoving = false; } } else if (askMoveVertical == 0) { m_IsMoving = false; m_Animator.SetBool("isWalking", false); } } } else { m_PercentageCompletion += Time.fixedDeltaTime * m_FoeSpeed; m_PercentageCompletion = Mathf.Clamp(m_PercentageCompletion, 0, 1); m_RigidBody.transform.position = Vector2.Lerp(m_InitialPos, m_WantedPos, m_PercentageCompletion); if (Vector2.Distance(m_RigidBody.transform.position, m_WantedPos) == 0f) { m_IsMoving = false; } } }
public void FixedUpdate() { ActualFoePosition = MapGenerator.ConvertWorldToIndex(transform.position); ToMove(); }
// Update is called once per frame void Update() { // Test World to map coord if (Input.GetMouseButton(0) && testMouse) { RaycastHit hitInfo; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hitInfo)) { MapGenerator.Coord nearestPoint = mapGenerator.NearestWorldPointToCoord(hitInfo.point); MapTileType tile = mapGenerator.GetMapCoord(nearestPoint); if (tile != MapTileType.OUT_OF_BOUNDS) { this.debugCubes[nearestPoint.tileX, nearestPoint.tileY].GetComponent <MeshRenderer>().material.color = Color.red; } } } // Restart if (Input.GetKeyDown(KeyCode.R)) { this.Initialize(); } // Spawn random stuff if (Input.GetKeyDown(KeyCode.T)) { int numCount = 5; int radius = 1; List <MapGenerator.Coord> loc = mapGenerator.GetRandomOpenCoords(numCount, radius, true); foreach (MapGenerator.Coord coord in loc) { List <MapGenerator.Coord> cubesForSquare = new List <MapGenerator.Coord>(); for (int x = -radius; x <= radius; x++) { for (int y = -radius; y <= radius; y++) { int drawX = coord.tileX + x; int drawY = coord.tileY + y; this.debugCubes[drawX, drawY].GetComponent <MeshRenderer>().material.color = Color.green; MapGenerator.Coord newCoord = new MapGenerator.Coord(drawX, drawY); mapGenerator.SetMapTile(newCoord, MapTileType.OTHER); cubesForSquare.Add(newCoord); } } this.activeGreenCubes.Add(cubesForSquare); } } // Remove a patch of random stuff if (Input.GetKeyDown(KeyCode.Y) && this.activeGreenCubes.Count > 0) { List <MapGenerator.Coord> selectedCube = this.activeGreenCubes[0]; this.activeGreenCubes.RemoveAt(0); foreach (MapGenerator.Coord coord in selectedCube) { this.debugCubes[coord.tileX, coord.tileY].GetComponent <MeshRenderer>().material.color = Color.white; this.mapGenerator.SetMapTile(coord, MapTileType.EMPTY); } } }