Tic Tac Toe GameInstance - TogetherGames/Public-Unity-CSharp GitHub Wiki
The GameInstance screen contains all the gameplay logic for a 2 player Tie-Tac-Toe game including a ChatRoom and a list of Users in the ChatRoom and ChatMessages 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);
}