private bool MapIsFullyAccessible(bool[,] obstacleMap, int currentObstacleCount) { bool[,] mapFlags = new bool[obstacleMap.GetLength(0), obstacleMap.GetLength(1)]; Queue <Utility.Coord> queue = new Queue <Utility.Coord> (); queue.Enqueue(playerCoords); mapFlags [playerCoords.x, playerCoords.y] = true; int accessibleTileCount = 1; while (queue.Count > 0) { Utility.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 Utility.Coord(neighbourX, neighbourY)); accessibleTileCount++; } } } } } } int targetAccessibleTileCount = (int)(mapSize.x * mapSize.y - currentObstacleCount); return(targetAccessibleTileCount == accessibleTileCount); }
public Utility.Coord GetRandomCoord() { Utility.Coord randomCoord = shuffledTileCoords.Dequeue(); shuffledTileCoords.Enqueue(randomCoord); return(randomCoord); }
public TileClass getTile(Utility.Coord coord) { return(map[coord.x, coord.y].GetComponent <TileClass>()); }
// Update is called once per frame void Update() { switch (currentState) { case GameState.StandBy: break; case GameState.Idle: if (!Cooldown) { temporal = cursorCoords; //Debug.Log(temporal.x + " " + temporal.y); //Debug.Log(cursorCoords.x + " " + cursorCoords.y); //Debug.Log("Idle"); //if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A)) CursorMove("DUpLeft"); //else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) CursorMove("DUpRight"); //else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D)) CursorMove("DDownRight"); //else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) CursorMove("DDownLeft"); if (Input.GetKey(KeyCode.A)) { CursorMove("Left"); } else if (Input.GetKey(KeyCode.W)) { CursorMove("Up"); } else if (Input.GetKey(KeyCode.D)) { CursorMove("Right"); } else if (Input.GetKey(KeyCode.S)) { CursorMove("Down"); } Invoke("ResetCoodldown", 0.10f); Cooldown = true; } if (Input.GetKeyDown(KeyCode.Space)) { SetStateStandBy(); //for(int i = 0; i <= 1000; i++) {Debug.Log(i);} combatControllerScript.EndTurn(); } break; case GameState.PlayerMovement: //Debug.Log(position.x + " " + position.y); temporal = cursorCoords; if (!Cooldown) { //Debug.Log("PlayerMovement"); //Debug.Log(playerCoords.Difference(cursorCoords.Add(new Utility.Coord(-1,0)))); //Debug.Log(playerCoords.x + " " + playerCoords.y); //Debug.Log(temporal.x + " " + temporal.y); //Debug.Log(cursorCoords.x + " " + cursorCoords.y); //if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.A)) CursorMove("DUpLeft"); //else if (Input.GetKey(KeyCode.W) && Input.GetKey(KeyCode.D)) CursorMove("DUpRight"); //else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.D)) CursorMove("DDownRight"); //else if (Input.GetKey(KeyCode.S) && Input.GetKey(KeyCode.A)) CursorMove("DDownLeft"); (mapGeneratorScript.getTile(cursorCoords.Add(new Utility.Coord(-1, 0)))).getType(); if (Input.GetKey(KeyCode.A) && (mapGeneratorScript.getTile(cursorCoords.Add(new Utility.Coord(-1, 0)))).getType() != 2 && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(-1, 0))) <= maxplayerDistance) { //Debug.Log(playerCoords.Difference(position.Add(new Utility.Coord(-1,0)))); CursorMove("Left"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } else if (Input.GetKey(KeyCode.W) && (mapGeneratorScript.getTile(cursorCoords.Add(new Utility.Coord(0, 1)))).getType() != 2 && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(0, 1))) <= maxplayerDistance) { //Debug.Log(playerCoords.Difference(position.Add(new Utility.Coord(-1,0)))); CursorMove("Up"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } else if (Input.GetKey(KeyCode.D) && (mapGeneratorScript.getTile(cursorCoords.Add(new Utility.Coord(1, 0)))).getType() != 2 && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(1, 0))) <= maxplayerDistance) { //Debug.Log(playerCoords.Difference(position.Add(new Utility.Coord(-1,0)))); CursorMove("Right"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } else if (Input.GetKey(KeyCode.S) && (mapGeneratorScript.getTile(cursorCoords.Add(new Utility.Coord(0, -1)))).getType() != 2 && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(0, -1))) <= maxplayerDistance) { //Debug.Log(playerCoords.Difference(position.Add(new Utility.Coord(-1,0)))); //Debug.Log("MAX IS: "+ maxplayerDistance); CursorMove("Down"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } } if (Input.GetKeyDown(KeyCode.Alpha1) && !cursorCoords.Equals(playerCoords)) { //Debug.Log("Moving here!"); movePlayer(); goBack(); } if (Input.GetKey(KeyCode.Q)) { goBack(); } if (Input.GetKey(KeyCode.Space)) { SetStateStandBy(); combatControllerScript.EndTurn(); } break; case GameState.PlayerAttack: temporal = cursorCoords; if (!Cooldown) { //Debug.Log("PlayerAttack"); //Debug.Log(playerCoords.Difference(temporal.Add(new Utility.Coord(-1,0)))); //Debug.Log(playerCoords.x + " " + playerCoords.y); //Debug.Log(temporal.x + " " + temporal.y); //Debug.Log(cursorCoords.x + " " + cursorCoords.y); if (Input.GetKey(KeyCode.A) && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(-1, 0))) <= maxAttackRange) { //Debug.Log(playerCoords.Difference(temporal.Add(new Utility.Coord(-1,0)))); CursorMove("Left"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } else if (Input.GetKey(KeyCode.W) && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(0, 1))) <= maxAttackRange) { //Debug.Log(playerCoords.Difference(temporal.Add(new Utility.Coord(0,1)))); CursorMove("Up"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } else if (Input.GetKey(KeyCode.D) && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(1, 0))) <= maxAttackRange) { //Debug.Log(playerCoords.Difference(temporal.Add(new Utility.Coord(1,0)))); CursorMove("Right"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } else if (Input.GetKey(KeyCode.S) && playerCoords.Difference(cursorCoords.Add(new Utility.Coord(0, -1))) <= maxAttackRange) { //Debug.Log(playerCoords.Difference(temporal.Add(new Utility.Coord(0,-1)))); CursorMove("Down"); Invoke("ResetCoodldown", 0.10f); Cooldown = true; } } if (Input.GetKeyDown(KeyCode.Alpha2) && cursorCoords.Equals(enemyCoords)) { Debug.Log("Attack: 1d20 + bab + strengthMod"); playerScript.MeleeAttack(enemyScript); goBack(); } if (Input.GetKey(KeyCode.Q)) { goBack(); } if (Input.GetKey(KeyCode.Space)) { SetStateStandBy(); combatControllerScript.EndTurn(); } break; } }
private void GenerateMap() { roughTerrainPercent = Random.Range(0f, 1f); wallPercent = Random.Range(0f, 1f); mapSize.x = Random.Range(10, 20); mapSize.y = Random.Range(10, 20); //Creating List of tiles allTileCoords = new List <Utility.Coord> (); for (int x = 0; x < mapSize.x; x++) { for (int y = 0; y < mapSize.y; y++) { allTileCoords.Add(new Utility.Coord(x, y)); } } shuffledTileCoords = new Queue <Utility.Coord>(Utility.ShuffleArray(allTileCoords.ToArray())); //Populating map with tiles string holderName = "Generated Map"; if (transform.FindChild(holderName)) { Destroy(transform.FindChild(holderName).gameObject); } Transform mapHolder = new GameObject(holderName).transform; mapHolder.parent = transform; map = new GameObject[(int)mapSize.x, (int)mapSize.y]; playerCoords = GetRandomCoord(); for (int x = 0; x < mapSize.x; x++) { for (int y = 0; y < mapSize.y; y++) { Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f + x, 0, -mapSize.y / 2 + 0.5f + y); //Rough Terrain if (Random.value < roughTerrainPercent) { Transform newTile = (Transform)Instantiate(DirtPlants1Tile, tilePosition, Quaternion.Euler(Vector3.right * 90)); newTile.localScale = new Vector3(5, 5, 5) * (1 - outlinePercent); newTile.parent = mapHolder; GameObject tile = newTile.gameObject; map[x, y] = tile; tileclass = map[x, y].GetComponent <TileClass>(); tileclass.setType(1); tileclass.setPos(tilePosition); //Debug.Log(tileclass.getPos()); } //Basic Terrain else { Transform newTile = (Transform)Instantiate(Dirt1Tile, tilePosition, Quaternion.Euler(Vector3.right * 90)); newTile.localScale = new Vector3(5, 5, 5) * (1 - outlinePercent); newTile.parent = mapHolder; GameObject tile = newTile.gameObject; map[x, y] = tile; tileclass = map[x, y].GetComponent <TileClass>(); tileclass.setType(0); tileclass.setPos(tilePosition); //Debug.Log(tileclass.getType()); } } } //Walls bool[,] wallMap = new bool[(int)mapSize.x, (int)mapSize.y]; int wallCount = (int)(mapSize.x * mapSize.y * wallPercent); int currentWallCount = 0; for (int i = 0; i < wallCount; i++) { Utility.Coord randomCoord = GetRandomCoord(); wallMap[randomCoord.x, randomCoord.y] = true; currentWallCount++; //Wall if (MapIsFullyAccessible(wallMap, currentWallCount) && !playerCoords.Equals(randomCoord)) { Vector3 wallPosition = CoordToPosition(randomCoord.x, randomCoord.y); wallPosition = wallPosition + new Vector3(0, 0.001f, 0); Transform newTile = (Transform)Instantiate(DirtRock1Tile, wallPosition, Quaternion.Euler(Vector3.right * 90)); newTile.localScale = new Vector3(5, 5, 5) * (1 - outlinePercent); newTile.parent = mapHolder; GameObject tile = newTile.gameObject; map[randomCoord.x, randomCoord.y] = tile; tileclass = tile.GetComponent <TileClass>(); tileclass.setType(2); tileclass.setPos(wallPosition); //Debug.Log(tileclass.coverType); wallMap[randomCoord.x, randomCoord.y] = true; } else { wallMap[randomCoord.x, randomCoord.y] = false; currentWallCount--; } } }
private void CursorMove(string direction) { if (direction.Equals("Left") && (transform.position.x - Hoffset.x) >= -Mathf.Floor(mapSize.x / 2)) { transform.position = transform.position - Hoffset; combatControllerScript.UpdateCursorCoords(-1, 0); cursorCoords = combatControllerScript.getCursorCoords(); } else if (direction.Equals("Up") && (transform.position.z + Voffset.z) <= Mathf.Floor(mapSize.y / 2)) { transform.position = transform.position + Voffset; combatControllerScript.UpdateCursorCoords(0, 1); cursorCoords = combatControllerScript.getCursorCoords(); } else if (direction.Equals("Down") && (transform.position.z - Voffset.z) >= -Mathf.Floor(mapSize.y / 2)) { transform.position = transform.position - Voffset; combatControllerScript.UpdateCursorCoords(0, -1); cursorCoords = combatControllerScript.getCursorCoords(); } else if (direction.Equals("Right") && (transform.position.x + Hoffset.x) <= Mathf.Floor(mapSize.x / 2)) { transform.position = transform.position + Hoffset; combatControllerScript.UpdateCursorCoords(1, 0); cursorCoords = combatControllerScript.getCursorCoords(); } else if (direction.Equals("DUpLeft") && (transform.position.x - Hoffset.x) >= -Mathf.Floor(mapSize.x / 2) && (transform.position.z + Voffset.z) <= Mathf.Floor(mapSize.y / 2)) { transform.position = transform.position - Hoffset; transform.position = transform.position + Voffset; combatControllerScript.UpdateCursorCoords(-1, 1); cursorCoords = combatControllerScript.getCursorCoords(); } else if (direction.Equals("DUpRight") && (transform.position.x + Hoffset.x) <= Mathf.Floor(mapSize.x / 2) && (transform.position.z + Voffset.z) <= Mathf.Floor(mapSize.y / 2)) { transform.position = transform.position + Hoffset; transform.position = transform.position + Voffset; combatControllerScript.UpdateCursorCoords(1, 1); cursorCoords = combatControllerScript.getCursorCoords(); } else if (direction.Equals("DDownRight") && (transform.position.x + Hoffset.x) <= Mathf.Floor(mapSize.x / 2) && (transform.position.z - Voffset.z) >= -Mathf.Floor(mapSize.y / 2)) { transform.position = transform.position + Hoffset; transform.position = transform.position - Voffset; combatControllerScript.UpdateCursorCoords(1, -1); cursorCoords = combatControllerScript.getCursorCoords(); } else if (direction.Equals("DDownLeft") && (transform.position.x - Hoffset.x) >= -Mathf.Floor(mapSize.x / 2) && (transform.position.z - Voffset.z) >= -Mathf.Floor(mapSize.y / 2)) { transform.position = transform.position - Hoffset; transform.position = transform.position - Voffset; combatControllerScript.UpdateCursorCoords(-1, -1); cursorCoords = combatControllerScript.getCursorCoords(); } }
public void SetEnemyPosition(Utility.Coord _enemyCoords, EnemyClass _enemyScript) { enemyCoords = _enemyCoords; enemyScript = _enemyScript; }
public void SetPlayerPosition(Utility.Coord _playerCoords, PlayerClass _playerScript) { playerCoords = _playerCoords; playerScript = _playerScript; }
public void movePlayer(Utility.Coord newPlayerCoords) { playerCoords = newPlayerCoords; playerTransform.position = mapGeneratorScript.CoordToPosition(playerCoords.x, playerCoords.y); cursorScript.SetPlayerPosition(playerCoords, playerScript); }
public void setCursorCoords(Utility.Coord _cursorCoords) { cursorCoords = _cursorCoords; }