/** * called by any GUI component to join a remote game. * The remote host should tell us what game we're in and where our paddle is in world coordinates **/ public void JoinGame(string url) { Debug.Log("joining game"); status = CoordinatorStatus.JOINED_WAITING; playerInfo = new Dictionary<string, PongPlayer>(); pongWebSockets.Reconnect(url); SendPlayerJoin(localPlayerName); }
/** * called by any GUI component to start hosting a game. * We will start listening for "playerjoined" requests and start a game. **/ public void HostGame(string url) { Debug.Log("Host starting game"); status = CoordinatorStatus.HOSTING_WAITING_FOR_PLAYERS; playerInfo = new Dictionary<string, PongPlayer>(); pongWebSockets.Reconnect(url); //join my own game: SendPlayerJoin(localPlayerName); chatWindowController.addLine("server", "You're the host! Press the 'fire' button to start the game"); }
private void UpdateJoinedStarting() { //we're starting a new game this.playerInfo.Clear(); //@TODO: destroy gameobject paddles from previous game ArrayList players = (ArrayList)parsedGameSetup[PongPlayer.ARRAY_PLAYERS]; PongPlayer previousPlayer = null; for (int p = 0; p < players.Count; ++p) { PongPlayer newPlayer = new PongPlayer((Hashtable)players[p]); //now create the walls & goals: GameObject goal = Instantiate<GameObject>(PrefabGoal); createWall(goal, newPlayer.goalLeft, newPlayer.goalRight); goal.name = "Goal for " + newPlayer.playerid; newPlayer.paddle = CreatePaddle(newPlayer.playerid, goal.transform.rotation, newPlayer.playerLeft, newPlayer.playerRight); //@TODO: for local paddle, bind keys to the newly instantiated paddle this.playerInfo.Add(newPlayer.playerid, newPlayer); //@TODO: link goal to correct user, for points and awards if (previousPlayer != null) { GameObject wall = Instantiate<GameObject>(PrefabWall); createWall(wall, previousPlayer.goalRight, newPlayer.goalLeft); } previousPlayer = newPlayer; } this.localPlayer = this.playerInfo[this.localPlayerName]; //some ugly shenanigans to create the last wall (between first and last player) GameObject lastwall = Instantiate<GameObject>(PrefabWall); createWall(lastwall, previousPlayer.goalRight, PongSerializer.toVector((Hashtable) ((Hashtable)players[0])[PongPlayer.FIELD_GOALLEFT])); //now let's play ball! GameObject ballobject = Instantiate<GameObject>(PrefabBall); ballobject.name = "PongTestBall"; this.ball = new PongBall(ballobject.GetComponent<Rigidbody>()); //next frame, we can start playing! status = CoordinatorStatus.JOINED_PLAYING; }
private void StartNewGame() { Debug.Log("initializing playfield"); Vector3[] points = CreateEllipse(radius, radius, new Vector3(0f, 0f, 0f), this.playerInfo.Count * 2); GameObject wall; GameObject goal; var playerEnum = this.playerInfo.GetEnumerator(); for (int i = 0; i < points.Length - 2; i += 2) { //@TODO: should this stay here, or move to the corresponding receiveGameSetup? The server may not see its own gamesetup message wall = Instantiate<GameObject>(PrefabWall); createWall(wall, points[i + 1], points[i + 2]); goal = Instantiate<GameObject>(PrefabGoal); createWall(goal, points[i], points[i + 1]); if (playerEnum.MoveNext()) { var p = playerEnum.Current.Value; p.goalLeft = points[i]; p.goalRight = points[i + 1]; p.height = PADDLE_HEIGHT; p.length = PADDLE_LENGTH; p.playerLeft = p.goalLeft + (goal.transform.forward * PADDLE_DISTANCE); p.playerRight = p.goalRight + (goal.transform.forward * PADDLE_DISTANCE); p.paddle = CreatePaddle(p.playerid, goal.transform.rotation, p.playerLeft, p.playerRight); goal.name = p.playerid; } else { Debug.LogError("mismatch between playfield size(" + (points.Length/2) + ") and playercount(" + this.playerInfo.Count + ")"); } } //now let's play ball! GameObject ballobject = Instantiate<GameObject>(PrefabBall); ballobject.name = "PongTestBall"; this.ball = new PongBall(ballobject.GetComponent<Rigidbody>()); SendGameSetup(this.playerInfo); status = CoordinatorStatus.HOSTING_PLAYING; }
private void ReceivePlayerJoined(Hashtable parsedPlayer) { //construct a place-holder player info. The coordinates and other details will be communicated by the host (later) PongPlayer playa = new PongPlayer((string)parsedPlayer[PongPlayer.FIELD_PLAYERID]); Debug.Log("player joined: " + playa.playerid); if (status == CoordinatorStatus.HOSTING_PLAYING) { status = CoordinatorStatus.HOSTING_WAITING_FOR_PLAYERS; } if (this.localPlayerName.Equals(playa.playerid)) { Debug.Log("hey, that's me joining!"); if (status == CoordinatorStatus.JOINING) { status = CoordinatorStatus.JOINED_STARTING; } //@TODO: bind this player's paddle to local keys //CORRECTION: binding should only happen during setup of the game (when host is building playfield) localPlayer = playa; } playerInfo.Add(playa.playerid, playa); Debug.Log("Total #players joined: " + playerInfo.Count); }
private bool ReceiveGameSetup(Hashtable parsed) { if (status == CoordinatorStatus.HOSTING_PLAYING) { Debug.Log("PongCoordinator received echo of my own GameSetup -- ignoring. (it's all good)"); return true; } if (status == CoordinatorStatus.JOINED_PLAYING || status == CoordinatorStatus.JOINED_WAITING) { //since I can't create unity objects outside the FixedUpdate() method, //I'm simply setting the status and storing the parsedGameSetup; //it will be used in UpdateJoinedStarting() this.parsedGameSetup = parsed; status = CoordinatorStatus.JOINED_STARTING; return true; } Debug.LogError("PongCoordinator should not be in status '" + status + "' when receiving gamesetup message"); return false; }