public void GameStateChanged(GameLogic.GameState gameState) { AudioClip newClip; if (gameState == GameLogic.GameState.Completed) { newClip = completed; } else if (gameState == GameLogic.GameState.Destroyed) { newClip = destroyed; } else if (gameState == GameLogic.GameState.Playing) { int indexOfClip = GameLogic.GetInstance().GetCurrentTotalNumberOfRounds() % playing.Length; newClip = playing[indexOfClip]; } else if (gameState == GameLogic.GameState.Initializing) { newClip = completed; } else if (gameState == GameLogic.GameState.Menu) { newClip = completed; } else { Debug.LogError("Unexpected GameState: " + gameState); return; } availableAudioSource.clip = newClip; availableAudioSource.time = playingAudioSource.time; availableAudioSource.pitch = playingAudioSource.pitch; availableAudioSource.volume = 0.0f; availableAudioSource.Play(); AudioSource temp = playingAudioSource; playingAudioSource = availableAudioSource; availableAudioSource = temp; }
/// <summary> /// This is used to check if the Ball goes offscreen and unprevent it. /// If the ball goes left or right side without colliding with Paddle /// It means that one of the players have scored and sends a SetGameStatus command to the GameLogic object to make them aware /// </summary> public void CheckCollisionWithScreen() { Vector3 HalfExtents = this.GetComponent <BoxCollider2D>().bounds.extents; Vector3 PositionMinusHExtents = new Vector3(m_vBallPosition.x - HalfExtents.x, m_vBallPosition.y - HalfExtents.y); Vector3 PositionPlusHExtents = new Vector3(m_vBallPosition.x + HalfExtents.x, m_vBallPosition.y + HalfExtents.y); if (m_vScreenBoundary.x <= ToScreen(m_vBallPosition).x || ToScreen(m_vBallPosition).x <= 0) { float winner = (ToScreen(m_vBallPosition).x / m_vScreenBoundary.x); winner = Mathf.Sign(winner); GameLogic.GameState gameStatus = new GameLogic.GameState(true, (int)winner, false); GameObject.Find("GameLogic").SendMessage("SetGameStatus", gameStatus); m_vSpeedMultiplier = m_vInitialBallSpeed; m_vBallPosition = new Vector2(0, 0); } if (m_vScreenBoundary.y <= ToScreen(PositionPlusHExtents).y || ToScreen(PositionMinusHExtents).y <= 0) { m_vSpeedMultiplier.y *= -1; } }
//-------------------------------------------------------------------------------- public void CheckGameStatus(object status) { GameLogic.GameState desiredState = (GameLogic.GameState)status; m_bPaused = desiredState.Paused; }