public void addRock(int x, int y) { SetupTileScript placeTile = tiles[x, y].GetComponent <SetupTileScript>(); placeTile.GetComponent <SetupTileScript> ().occupied = true; GameObject rock = (GameObject)Instantiate(Rock, new Vector3(placeTile.transform.position.x, 5, placeTile.transform.position.z), new Quaternion()); rock.transform.parent = placeTile.transform; rock.transform.localScale = new Vector3(1.25f, 12.5f, 1.25f); rock.transform.eulerAngles = new Vector3(4.5f, 0, 0f); }
void Start() { gp = GameObject.Find("GameProcess").GetComponent <GameProcess> (); boardCapacity = 8; maxUnitCount = 14; //Tile Creation tiles = new GameObject[xTiles, yTiles]; for (int i = 0; i < xTiles; i++) { for (int j = 0; j < yTiles; j++) { Vector3 position = new Vector3((10 * i), 0, (10 * j)); //each square is 10 points away from each other //-60 offsets all these tiles to appear below the game board along the z direction GameObject newtile = (GameObject)Instantiate(tile, position, new Quaternion(0, 0, 0, 0)); newtile.AddComponent("SetupTileScript"); newtile.GetComponent <SetupTileScript>().tt = SetupTileScript.TileType.ONFIELD; tiles[i, j] = newtile; newtile.transform.parent = this.transform; } } //loop through the array of tiles and assign neighbors accordingly for (int i = 0; i < xTiles; i++) { for (int j = 0; j < yTiles; j++) { if (tiles[i, j] != null) { SetupTileScript script = tiles[i, j].GetComponent <SetupTileScript>(); //set tile id e.g. 5,2 script.x = i; script.y = j; if (i != 0) { script.down = tiles[i - 1, j]; } if (i != xTiles - 1) { script.up = tiles[i + 1, j]; } if (j != 0) { script.right = tiles[i, j - 1]; } if (j != yTiles - 1) { script.left = tiles[i, j + 1]; } } } } //_________Create the unit_storage_tiles's tiles:_________ //Tile Creation unit_storage_tiles = new GameObject[xStorage_Tiles, yStorage_Tiles]; for (int i = 0; i < xStorage_Tiles; i++) { for (int j = 0; j < yStorage_Tiles; j++) { Vector3 position = new Vector3((10 * i), 0, (10 * j) - 25); //each square is 10 points away from each other //-20 offsets all these tiles to appear below the game board along the z direction //NOTICE how a global variable ("public GameObject tile" in this case) has to have prefab attached to it in editor //THEN, to create a game object of that prefab, you must use the following syntax below: GameObject newtile = (GameObject)Instantiate(storage_tile, position, new Quaternion(0, 0, 0, 0)); newtile.AddComponent("SetupTileScript"); newtile.GetComponent <SetupTileScript>().tt = SetupTileScript.TileType.OFFFIELD; unit_storage_tiles[i, j] = newtile; newtile.transform.parent = this.transform; } } //loop through the array of tiles and assign neighbors accordingly for (int i = 0; i < xStorage_Tiles; i++) { for (int j = 0; j < yStorage_Tiles; j++) { SetupTileScript script = unit_storage_tiles[i, j].GetComponent <SetupTileScript>(); //set tile id e.g. 5,2 script.x = i; script.y = j; if (i != 0) { script.down = unit_storage_tiles[i - 1, j]; } if (i != xStorage_Tiles - 1) { script.up = unit_storage_tiles[i + 1, j]; } if (j != 0) { script.right = unit_storage_tiles[i, j - 1]; } if (j != yStorage_Tiles - 1) { script.left = unit_storage_tiles[i, j + 1]; } } } //Create five page objects for storing unit positions pages = new Page[5]; for (int i = 0; i < 5; i++) { pages[i] = new Page(); } //The stupd screen defaults to the first page (index "0" in the pages array, "1" on the server) activePage = 0; gp.returnSocket().SendTCPPacket("getBoardData\\1\\" + gp.playerName); addRock(0, 0); addRock(8, 0); }
void OnMouseUp() { currentTile.renderer.material.shader = Shader.Find("Toon/Basic"); trackMouse = false; pieceMoved = false; isTouched = false; oldScript = this.gameObject.GetComponentInParent <SetupTileScript> (); nearestTile = findNearestTile(); //if the tile closest to where the piece was dropped exists and is unoccupied if (nearestTile != null && !nearestTile.GetComponent <SetupTileScript>().occupied) { //guardian and soulstone must be kept on the field if (gameObject.GetComponent <Unit>().unitType == 10 || gameObject.GetComponent <Unit>().unitType == 11) { if (nearestTile.GetComponent <SetupTileScript>().tt == SetupTileScript.TileType.ONFIELD) { pieceMoved = true; onField = true; } else { if (gameObject.GetComponent <Unit>().unitType == 10) { showErrorMessage("Guardian must be on the field"); } else if (gameObject.GetComponent <Unit>().unitType == 11) { showErrorMessage("Soulstone must be on the field"); } am.playErrorSFX(); } } //if there is room on the board for units... else if (playerSetup.pages[playerSetup.activePage].onBoardPieces.Count < playerSetup.boardCapacity) { //...and the unit is being placed on the field... if (nearestTile.GetComponent <SetupTileScript>().tt == SetupTileScript.TileType.ONFIELD) { //...only add the unit if it is not already on the board if (!playerSetup.pages[playerSetup.activePage].onBoardPieces.Contains(this.gameObject)) { playerSetup.pages[playerSetup.activePage].offBoardPieces.Remove(this.gameObject); playerSetup.pages[playerSetup.activePage].onBoardPieces.Add(this.gameObject); } pieceMoved = true; onField = true; } else //unit is being placed off the field { //if the unit is being moved off the field (rather than being adjusted off the board) if (playerSetup.pages[playerSetup.activePage].onBoardPieces.Contains(gameObject)) { playerSetup.pages[playerSetup.activePage].offBoardPieces.Add(this.gameObject); playerSetup.pages[playerSetup.activePage].onBoardPieces.Remove(this.gameObject); } pieceMoved = true; onField = false; } } else //board is at max capacity { if (nearestTile.GetComponent <SetupTileScript>().tt == SetupTileScript.TileType.ONFIELD) { //the piece can only be moved in the case that it is already on the board and is being adjusted if (playerSetup.pages[playerSetup.activePage].onBoardPieces.Contains(gameObject)) { pieceMoved = true; onField = true; } else { showErrorMessage("Board at maximum capacity"); am.playErrorSFX(); } } else //offfield - player is moving a piece when the board is full { //the piece being moved off the field if (playerSetup.pages[playerSetup.activePage].onBoardPieces.Contains(gameObject)) { playerSetup.pages[playerSetup.activePage].offBoardPieces.Add(this.gameObject); playerSetup.pages[playerSetup.activePage].onBoardPieces.Remove(this.gameObject); } pieceMoved = true; onField = false; } } //Debug.Log(playerSetup.pages[playerSetup.activePage].onBoardPieces.Count); } //if a piece was moved to a new valid tile (i.e. didn't snap back to old position)... if (pieceMoved) { int oldX = oldScript.x; int oldY = oldScript.y; int newX = nearestTile.GetComponent <SetupTileScript>().x; int newY = nearestTile.GetComponent <SetupTileScript>().y; int unitType = this.gameObject.GetComponent <Unit>().unitType; bool oldOnField = oldScript.tt == SetupTileScript.TileType.ONFIELD ? true : false; //send the server movePiece\\playerName\\activePageNumber\\unitType\\oldX\\oldY\\newX\\newY\\onOrOffField gp.returnSocket().SendTCPPacket("movePiece\\" + gp.playerName + "\\" + (playerSetup.activePage + 1) + "\\" + unitType + "\\" + oldX + "\\" + oldY + "\\" + newX + "\\" + newY + "\\" + oldOnField + "\\" + onField); //set the current tile to unoccupied (the piece is moving away from this tile) this.gameObject.GetComponentInParent <SetupTileScript>().occupied = false; //attach the new tile's transform to be the parent of the unit this.gameObject.transform.parent = nearestTile.transform; //move the unit to it's new tile and set that tile to occupied transform.position = new Vector3(nearestTile.transform.position.x, 5.0f, nearestTile.transform.position.z); nearestTile.GetComponent <SetupTileScript>().occupied = true; } //this happens if the piece move attempt was illegal/not valid, the piece is returned to it's original position else { transform.position = playerSetup.prevPosition; } }