protected override void OnStart() { if (XPlayerTile == null) { Debug.LogError("X Player Tile GameObject is null!"); } else { XPlayer = XPlayerTile.GetComponent <TicTacToePlayer>(); } if (OPlayerTile == null) { Debug.LogError("O Player Tile GameObject is null!"); } else { OPlayer = OPlayerTile.GetComponent <TicTacToePlayer>(); } LocalPlayers.Add(XPlayer); LocalPlayers.Add(OPlayer); Reset(); }
protected override void OnUpdateGamepad(SocketGamepad gamepad) { switch (CurrentMode) { case CurrentModeType.WaitForPlayers: break; case CurrentModeType.WaitForReady: //Check if player is present if (ActiveGamepads.ContainsKey(gamepad)) { if (DateTime.Now.Ticks > ActiveGamepads[gamepad].InputWait) { if (!ActiveGamepads[gamepad].Ready) { ActiveGamepads[gamepad].Ready = true; } } } //Check if all players have readied if (ActiveGamepads.Values.Count(p => p.Ready) >= 2) { StartGame(); ActiveGamepads[gamepad].InputWait = DateTime.Now.Ticks + (InputWaitMs * TimeSpan.TicksPerMillisecond); } break; case CurrentModeType.Playing: if (ActiveGamepads.ContainsKey(gamepad)) { TicTacToePlayer player = (TicTacToePlayer)ActiveGamepads[gamepad]; if (player.isMyTurn) { for (int b = 0; b < 9; b++) { if (gamepad.GetButton("button " + b.ToString()) && Grid[b].CurrentMark == 0) { ActiveGamepads[gamepad].InputWait = DateTime.Now.Ticks + (InputWaitMs * TimeSpan.TicksPerMillisecond); SetCell(b, player.Xs ? 1 : 2); break; } } } } break; } }
protected override void OnGamepadUnplugged(SocketGamepad gamepad) { if (ActiveGamepads.ContainsKey(gamepad)) { //Remove from players TicTacToePlayer player = (TicTacToePlayer)ActiveGamepads[gamepad]; player.PlayerIcon.SetActive(false); ActiveGamepads.Remove(gamepad); LocalPlayers.Remove(player); player.Gamepad = null; if (XPlayer.Gamepad == null && OPlayer.Gamepad == null) { CurrentMode = CurrentModeType.WaitForPlayers; } } }
protected void SendGameState(SocketGamepad gamepad) { SGUpdateMsg GameStateMsg = new SGUpdateMsg(); TicTacToePlayer tttPlayer = (TicTacToePlayer)ActiveGamepads[gamepad]; GameStateMsg.SGID = tttPlayer.PlayerId; GameStateMsg.MessageType = "gamestate"; GameStateMsg.Data = new JSONObject(); JSONObject CellJSON = new JSONObject(JSONObject.Type.ARRAY); GameStateMsg.Data.AddField("mark", tttPlayer.Xs ? 1 : 2); for (int m = 0; m < 9; m++) { CellJSON.list.Add(new JSONObject(Grid[m].CurrentMark)); } GameStateMsg.Data.AddField("cells", CellJSON); GameStateMsg.Serialize(); Debug.Log("Sending Game State: " + GameStateMsg.ToString()); socket.Emit("SGUpdateMsg", GameStateMsg); }