internal void ReceiveShootResult(int score, int x = -1, int y = -1, bool endGame = false, int orientation = -1, bool destroyed = false, int shipId = -1, Vector2 shipCenter = new Vector2()) { Debug.Log("Receiveeeeeeeeeeeeeeeeeee"); Debug.Log("score " + score); gridAnimPos = new Vector3(x, y, (float)shootTurn); if (score > 0) { shootStatus = ShootStatus.TARGETHIT; if (destroyed) { destroyShipState = DestroyShipState.DESTROYONE; destroyedShipId = shipId; destroyedShipOrient = orientation; destroyedShipPos = shipCenter; } } else { shootStatus = ShootStatus.TARGETMISS; } if (!endGame) { PostReceiveDamage(); SwitchTurn(); } }
// Update is called once per frame void Update() { //Update for UI and Start the TURN for SHOOTING if (uiState == UIState.GAMESTART) { canvas.SetActive(false); opponentField.SetActive(true); uiState = UIState.UIDONE; for (int i = 0; i < 5; i++) { arrOppoShip[i] = Instantiate(arrShip[i], arrShip[i].transform.position, Quaternion.identity) as GameObject; } //START SHOOTING HERE canvasTurn.SetActive(true); if (shootTurn == ShootTurn.PLAYERTURN) { Debug.Log("update turn ne"); curTurnText = "YOUR TURN"; canShoot = true; } else { Debug.Log("update turn ne"); curTurnText = "OPPONENT'S TURN"; canShoot = false; PostReceiveDamage(); } } else if (uiState == UIState.UIDONE) //Game is already starting { //Update Turn Status textTurn.text = curTurnText; //Get animation position Vector3 animPos = new Vector3(); if (shootStatus != ShootStatus.WAITFORSHOOT) { if (gridAnimPos.z == (float)ShootTurn.PLAYERTURN) { animPos = oppoGridLayout.Grid[(int)gridAnimPos.x][(int)gridAnimPos.y].transform.position; } else { animPos = gridLayout.Grid[(int)gridAnimPos.x][(int)gridAnimPos.y].transform.position; } Debug.Log("Animation Pos " + animPos); Debug.Log("Animation Grid Pos " + gridAnimPos); //Animation for shoot GameObject animObject = null; if (shootStatus == ShootStatus.TARGETHIT) { animObject = Instantiate(goExplosion, animPos, Quaternion.identity) as GameObject; shootStatus = ShootStatus.WAITFORSHOOT; queue[idQueue].Enqueue(animObject); } else if (shootStatus == ShootStatus.TARGETMISS) { animObject = Instantiate(goWater, animPos, Quaternion.identity) as GameObject; shootStatus = ShootStatus.WAITFORSHOOT; queue[idQueue].Enqueue(animObject); } } //Animation Playing Check while (queue[idQueue].Count > 0) { GameObject animObject = queue[idQueue].Dequeue(); if (animObject != null) { if (animObject.GetComponentInChildren <Animator>().GetCurrentAnimatorStateInfo(0).tagHash == Animator.StringToHash("EndFire")) { GameObject g = Instantiate(goCross, animObject.transform.position, Quaternion.identity) as GameObject; GameObject h = Instantiate(goSmoke, animObject.transform.position, Quaternion.identity) as GameObject; h.transform.localEulerAngles = new Vector3(0, 0, -45); Destroy(animObject); animObject = null; } else if (animObject.GetComponentInChildren <Animator>().GetCurrentAnimatorStateInfo(0).tagHash == Animator.StringToHash("EndWater")) { GameObject g = Instantiate(goBlueDot, animObject.transform.position, Quaternion.identity) as GameObject; Destroy(animObject); animObject = null; } if (animObject != null) { queue[1 - idQueue].Enqueue(animObject); } else if (winningState != -1) { uiState = UIState.GAMEEND; } } } idQueue = 1 - idQueue; //Display ship if it is destroyed if (destroyShipState == DestroyShipState.DESTROYONE) { //Change rotation if (destroyedShipOrient == 1) { arrOppoShip[destroyedShipId].transform.localEulerAngles = new Vector3(0, 0, -90); } else { arrOppoShip[destroyedShipId].transform.localEulerAngles = new Vector3(0, 0, 0); } arrOppoShip[destroyedShipId].transform.localScale = arrShip[destroyedShipId].transform.localScale; //Transform ship to the position GameObject shipCell = oppoGridLayout.Grid[(int)destroyedShipPos.x][(int)destroyedShipPos.y]; Vector2 cellPos = shipCell.transform.position; Debug.Log("cell Pos " + cellPos); Vector2 offsetShip = new Vector2((1 - destroyedShipOrient) * (arrOppoShip[destroyedShipId].GetComponent <SpriteRenderer>().bounds.size.x / 2 - shipCell.GetComponent <SpriteRenderer>().bounds.size.x / 2), destroyedShipOrient * (-arrOppoShip[destroyedShipId].GetComponent <SpriteRenderer>().bounds.size.y / 2 + shipCell.GetComponent <SpriteRenderer>().bounds.size.y / 2)); arrOppoShip[destroyedShipId].transform.position = cellPos + offsetShip; arrOppoShip[destroyedShipId].transform.SetParent(arrShip[destroyedShipId].transform.parent.transform, true); destroyShipState = DestroyShipState.NOTYET; } } else if (uiState == UIState.GAMEEND) { if (winningState == 1) { textResult.text = "YOU WIN"; textResultStatus.text = "You destroyed all opponent's ships"; } else if (winningState == 0) { textResult.text = "YOU LOSE"; textResultStatus.text = "Opponent destroyed all your ships"; } else { textResult.text = "YOU WIN"; textResultStatus.text = "Opponent has surrendered"; } canvasResult.SetActive(true); canvas.SetActive(false); uiState = UIState.UIFINAL; } }