/// <summary> /// Sets the player ships. /// </summary> IEnumerator setPlayerShips() { //clear the arrays playerTiles = new List <int>(); playerShipsInScene = new List <GameObject>(); //get a random formation int playerFormation = Random.Range(0, FormationPresets.formations); //set the ships on their positions for (int i = 0; i < 5; i++) { //get the target tile ID for this ship from the selected formation Vector3 formation = FormationPresets.getNewFormation(playerFormation, i); //convert the tile ID to actual vector3 position Vector3 position = convertTileIdToPosition((int)formation.x, new Vector3(0, 0, 0.1f)); //create the ship on the result position GameObject playerShip = Instantiate(playerShips[i], position, Quaternion.Euler(0, 0, 0)) as GameObject; //rename the ship object playerShip.name = "PlayerShip-" + (i + 1).ToString(); //init the ship playerShip.GetComponent <ShipController>().anchorTile = (int)formation.x; //get the anchor tile ID (1 ~ 64) //add it to the playerShipsInScene array playerShipsInScene.Add(playerShip); //save its position on the map tiles for (int j = 0; j < playerShip.GetComponent <ShipController>().shipSize; j++) { playerTiles.Add((int)formation.x + j); } yield return(new WaitForSeconds(0.005f)); } //sort the occupied tiles array playerTiles.Sort(); //debug //for(int k = 0; k < playerTiles.Count; k++) print ("player Ship Index " + k.ToString() + ": " + playerTiles[k]); }
// Sắp xếp Tàu cho Người chơi IEnumerator setPlayerShips() { // Dọn danh sách playerTiles = new List <int>(); playerShipsInScene = new List <GameObject>(); // Lựa 1 đội hình ngẫu nhiên int playerFormation = Random.Range(0, FormationPresets.formations); // Gắn vị trí cho Tàu for (int i = 0; i < 5; i++) { // Lấy ID của Ô từ đội hình đã có Vector3 formation = FormationPresets.getNewFormation(playerFormation, i); // Chuyển đổi ID của Ô sang vị trí Vector3 thật Vector3 position = convertTileIdToPosition((int)formation.x, new Vector3(0, 0, 0.1f)); // Tạo tàu dựa trên vị trí vừa tính GameObject playerShip = Instantiate(playerShips[i], position, Quaternion.Euler(0, 0, 0)) as GameObject; // Đặt tên cho Tàu playerShip.name = "PlayerShip-" + (i + 1).ToString(); // Đặt tàu playerShip.GetComponent <ShipController>().anchorTile = (int)formation.x; //get the anchor tile ID (1 ~ 64) // Thêm Tàu vào danh sách playerShipsInScene.Add(playerShip); // Lưu vị trí Tàu trên Map for (int j = 0; j < playerShip.GetComponent <ShipController>().shipSize; j++) { playerTiles.Add((int)formation.x + j); } yield return(new WaitForSeconds(0.005f)); } // Sắp xếp lại Ô playerTiles.Sort(); //debug //for(int k = 0; k < playerTiles.Count; k++) print ("player Ship Index " + k.ToString() + ": " + playerTiles[k]); }
/// <summary> /// Sets the AI ships. /// </summary> IEnumerator setEnemyShips() { //clear the arrays enemyTiles = new List <int>(); enemyShipsInScene = new List <GameObject>(); //get a random formation int enemyFormation = Random.Range(0, FormationPresets.formations); for (int i = 0; i < 5; i++) { Vector3 formation = FormationPresets.getNewFormation(enemyFormation, i); Vector3 position = convertTileIdToPosition((int)formation.x, new Vector3(0, 0, 0.1f)); GameObject enemyShip = Instantiate(enemyShips[i], position, Quaternion.Euler(0, 0, 0)) as GameObject; //rename the ship object enemyShip.name = "EnemyShip-" + (i + 1).ToString(); //init the ship enemyShip.GetComponent <ShipController>().anchorTile = (int)formation.x; //get the anchor tile ID (1 ~ 64) //add it to the enemyShipsInScene array enemyShipsInScene.Add(enemyShip); //hide this ship enemyShip.transform.GetChild(0).GetComponent <Renderer>().enabled = false; //save its position on the map tiles for (int j = 0; j < enemyShip.GetComponent <ShipController>().shipSize; j++) { enemyTiles.Add((int)formation.x + j); } yield return(new WaitForSeconds(0.005f)); } //sort the occupied tiles array enemyTiles.Sort(); //debug //for(int k = 0; k < enemyTiles.Count; k++) print ("Enemy Ship Index " + k.ToString() + ": " + enemyTiles[k]); }
// Sắp xếp Tàu cho Máy IEnumerator setEnemyShips() { // Dọn danh sách enemyTiles = new List <int>(); enemyShipsInScene = new List <GameObject>(); // Lựa 1 đội hình ngẫu nhiên int enemyFormation = Random.Range(0, FormationPresets.formations); for (int i = 0; i < 5; i++) { Vector3 formation = FormationPresets.getNewFormation(enemyFormation, i); Vector3 position = convertTileIdToPosition((int)formation.x, new Vector3(0, 0, 0.1f)); GameObject enemyShip = Instantiate(enemyShips[i], position, Quaternion.Euler(0, 0, 0)) as GameObject; // Đặt tên cho Tàu enemyShip.name = "EnemyShip-" + (i + 1).ToString(); // Đặt Tàu enemyShip.GetComponent <ShipController>().anchorTile = (int)formation.x; //get the anchor tile ID (1 ~ 64) // Thêm Tàu vào danh sách enemyShipsInScene.Add(enemyShip); // Ẩn Tàu của Máy enemyShip.transform.GetChild(0).GetComponent <Renderer>().enabled = false; // Lưu vị trí Tàu trên Map for (int j = 0; j < enemyShip.GetComponent <ShipController>().shipSize; j++) { enemyTiles.Add((int)formation.x + j); } yield return(new WaitForSeconds(0.005f)); } // Sắp xếp lại Ô enemyTiles.Sort(); //debug //for(int k = 0; k < enemyTiles.Count; k++) print ("Enemy Ship Index " + k.ToString() + ": " + enemyTiles[k]); }