Tic Tac Toe Inviting Friends - TogetherGames/Public-Unity-CSharp GitHub Wiki
When the Invite Friend button is pressed, the OnInviteFriendButtonClicked() method is called. The OnInviteFriendButtonClicked() method displays the FriendLobby screen allowing the User to invite a friend to the Game.
void OnInviteFriendButtonClicked()
{
Debug.Log("OnInviteFriendButtonClicked()");
FriendLobby.PreviousScreen = "GameInstance";
Application.LoadLevel("FriendLobby");
}
When the Clear Board button is pressed, the OnClearBoardButtonClicked() method is called. The OnClearBoardButtonClicked() method clears out the GameInstance's properties, effectively clearing the board. This is strictly for test purposes.
void OnClearBoardButtonClicked()
{
Debug.Log("OnClearBoardButtonClicked()");
m_GameData = "_________";
m_GameInstance.Properties.Set("Data", m_GameData);
m_GameInstance.Modify (-1, onRefreshGame);
}
When the Forfeit Board button is pressed, the OnForfeitGameButtonClicked() method is called. The OnForfeitGameButtonClicked() method makes the User forfeit the GameInstance by calling. GameInstance.Forfeit().
void OnForfeitGameButtonClicked()
{
Debug.Log("OnForfeitGameButtonClicked()");
m_GameInstance.Forfeit(onGameFinished);
}
If it is your turn, clicking on an empty board cell will either place an 'X' or 'O'. The MakeTurnInGame() method sends a MakeMove message to the Together server by using the GameInstance.MakeMove() method.
// Makes a turn in the game.
void MakeTurnInGame(int row, int col)
{
Debug.Log ("GameInstance.MakeTurnInGame(" + row + ", " + col + ")");
if (!IsMyTurn())
{
Debug.Log("Uh oh, Waiting on user to take there turn");
}
else
{
Together.Instance.Print(m_GameInstance.Properties.ToString());
m_GameInstance.MakeMove(onTurnMade); // callbackFunc
}
}
Whenever you win, the opposing player wins, or a tie is recognized, a popup stating such is displayed while asking you if you want to create a rematch.
When the popup is dismissed, the PopupDismissed() method is called. If the User said yes to a rematch, the current GameInstance is finished and a rematch created using the GameInstanceManager.CreateRematch() method. If the User said no to a rematch, the current GameInstance is finished using the GameInstance.Finish() method.
void PopupDismissed(string buttonText)
{
Debug.Log("GameInstance.PopupDismissed(" + buttonText + ")");
if (m_DisplayDialog == "UserWonDialog" || m_DisplayDialog == "OpponentWonDialog" ||
m_DisplayDialog == "TiedDialog")
{
GameInstanceUser myGameInstanceUser = this.GetMyGameInstanceUser();
GameInstanceUser opposingGameInstanceUser = this.GetOpposingGameInstanceUser();
long winningUserID = 0;
if (m_DisplayDialog == "UserWonDialog")
winningUserID = myGameInstanceUser.UserID;
else if (m_DisplayDialog == "OpponentWonDialog")
winningUserID = opposingGameInstanceUser.UserID;
else if (m_DisplayDialog == "TiedDialog")
winningUserID = 0;
// Want rematch?
if (buttonText == "Yes")
{
PlaysTogether.PropertyCollection rematchGameProps = new PlaysTogether.PropertyCollection();
rematchGameProps.Set("Data", "_________");
// Finish and create a rematch GameInstance.
Together.Instance.GameInstanceManager.CreateRematch(m_GameInstance.GameInstanceID, "", 0, 2,
winningUserID, winningUserID, true, "",
m_GameInstance.Properties, myGameInstanceUser.Properties,
rematchGameProps, onGameFinished);
}
else
{
// Just finish the GameInstance.
m_GameInstance.Finish(winningUserID, onGameFinished);
}
}
}