Tic Tac Toe Example GameInstance - TogetherGames/Public-Unity-CSharp GitHub Wiki
The GameInstance screen contains all the gameplay logic for a 2 player Tie-Tac-Toe game. a ChatRoom. A list of Users in the ChatRoom and ChatMessages belonging to the ChatRoom are all displayed. A series of buttons is displayed on the left allowing the User to: Join the ChatRoom, Leave the ChatRoom, Delete the ChatRoom, Delete the last ChatMessage, Mark the top ChatMessage as read, Display the Friend Lobby screen so the User can invite a friend, and Send a Message.
The Start() method calls RefreshGame(). The RefreshGame() method gets the details of the current GameInstance via the GameInstance.GetDetails() method.
void RefreshGame()
{
Debug.Log("Refresh game");
m_GameInstance.LastModifyTimestamp = 0;
m_GameInstance.GetDetails(m_GameInstance.GameInstanceID, onRefreshGame);
}
The DisplayText() method displays some info on the GameInstance and the list of Users participating in the 2 player Tie-Tac-Toe match.
void DisplayText()
{
int userTurnIndex = GetTurnIndexForUser(Together.Instance.User.UserID);
bool myTurn = IsMyTurn ();
//Create and set the Labels
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 15, 300,100), "Game Instance ", m_TitleStyle);
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 80, 300, 50), "ID = " + m_ID , m_TextStyle);
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 110, 300, 50), "UserIndex = " + userTurnIndex, m_TextStyle);
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 140, 300, 50), "TurnIndex= " + m_GameInstance.TurnIndex, m_TextStyle);
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 170, 300, 50), "PlayCount = " + m_playCount, m_TextStyle);
if (myTurn)
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 230, 300, 50), "Your Turn", m_TextStyle);
else
GUI.Label(new Rect((Screen.width - 300) * 0.5f, 230, 300, 50), "Not Your Turn", m_TextStyle);
int labelY = 270;
GameInstanceUser gameInstanceUser;
for (int i=0; i<=m_GameInstance.GetGameInstanceUserCount()-1; i++)
{
gameInstanceUser = m_GameInstance.GetGameInstanceUser(i);
GUI.Label(new Rect((Screen.width - 300) * 0.5f, labelY, 300, 50),
gameInstanceUser.Name + ", Score=" + gameInstanceUser.Properties.GetEx("Score", "0"), m_TextStyle);
labelY += 30;
}
}
The DisplayButtons() method displays various buttons on the left allowing the User to Leave the Game, Invite a friend to play, Clear the board, and Forfeit the game.
void DisplayButtons()
{
if( GUI.Button(new Rect(10, 50, 100, 40), "Back"))
Application.LoadLevel("GameLobby");
if( GUI.Button(new Rect(10, 100, 100, 40), "Leave Game"))
OnLeaveButtonClicked();
if( GUI.Button(new Rect(10, 150, 100, 40), "Invite Friend"))
OnInviteFriendButtonClicked();
if( GUI.Button(new Rect(10, 200, 100, 40), "Clear Board"))
OnClearBoardButtonClicked();
if( GUI.Button(new Rect(10, 250, 100, 40), "Forfeit Game"))
OnForfeitGameButtonClicked();
int buttonWidth = 100;
int buttonHeight = 50;
int buttonX = 0;
int buttonY = buttonYStart;
int buttonXSep = 10;
int buttonYSep = 10;
for (int row = 0; row <= m_nGridWidth-1; row++)
{
buttonX = (Screen.width - buttonWidth*3 - buttonXSep*2) / 2;
for (int col = 0; col <= m_nGridHeight-1; col++)
{
//Create our buttons with a 2d array of there textures
if (GUI.Button(new Rect(buttonX, buttonY, buttonWidth, buttonHeight), m_gridTextures[col,row]))
{
OnBoardButtonClicked(row, col);
}
buttonX += buttonWidth + buttonXSep;
}
buttonY += buttonHeight + buttonYSep;
}
if (m_CreateDialog)
{
ShowDialogToDisplay();
m_CreateDialog = false;
}
}
When the Leave Game button is pressed, the OnLeaveButtonClicked() method is called. The OnLeaveButtonClicked() method makes the User leave the GameInstance by calling. GameInstance.Leave().
void OnLeaveButtonClicked()
{
m_GameInstance.Leave(onGameLeft);
}
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);
}
}
}