@@ -11,13 +11,15 @@ protected PlayerManager () { }
1111 //listeners
1212 void OnEnable ( )
1313 {
14- EventManager . StartListening ( "ResetPlayers" , ResetPlayers ) ;
15- EventManager . StartListening ( "ClearNonActivePlayers" , ClearNonActivePlayers ) ;
14+ EventManager . StartListening ( "AddAllPlayers" , AddAllPlayers ) ;
15+ EventManager . StartListening ( "RemoveAllPlayers" , RemoveAllPlayers ) ;
16+ EventManager . StartListening ( "CheckUpdatePlayers" , CheckUpdatePlayers ) ;
1617 }
1718 void OnDisable ( )
1819 {
19- EventManager . StopListening ( "ResetPlayers" , ResetPlayers ) ;
20- EventManager . StopListening ( "ClearNonActivePlayers" , ClearNonActivePlayers ) ;
20+ EventManager . StopListening ( "AddAllPlayers" , AddAllPlayers ) ;
21+ EventManager . StopListening ( "RemoveAllPlayers" , RemoveAllPlayers ) ;
22+ EventManager . StopListening ( "CheckUpdatePlayers" , CheckUpdatePlayers ) ;
2123 }
2224
2325
@@ -38,6 +40,7 @@ void OnDisable ()
3840 public BoxCollider worldContainerCollider ;
3941 public GameObject playerPrefab ;
4042 public Dictionary < string , GameObject > playerDict ;
43+ public Dictionary < string , GameObject > playersToRemoveDict ;
4144 public CameraManager cameraManager ;
4245
4346 // temp sprites for assigning avatars
@@ -51,6 +54,7 @@ void OnDisable ()
5154 // player currently showing an event
5255 public GameObject currentPlayerObj ;
5356 public Player currentPlayerScript ;
57+ public string currentEventType ;
5458
5559
5660
@@ -82,37 +86,94 @@ private void Awake ()
8286 }
8387
8488
89+
90+
91+
92+
93+
94+
95+
96+
97+
98+
99+
85100 /**
86- * Remove all players from stage, reset dict
101+ * Add all players to screen and dict - called at start or after data reset
102+ * - called when Timeline.status == TimelineStatus.newDataReceived
87103 */
88- public void ResetPlayers ( )
104+ void AddAllPlayers ( )
89105 {
90- // remove any players first?
106+ Debug . Log ( "PlayerManager.AddAllPlayers()" ) ;
91107
92- // clear the dictionary
93- playerDict . Clear ( ) ;
94-
95- // loop through the buffer and add players
108+ // loop through the buffer and add players to scene and dict
96109 foreach ( var feed in Timeline . Instance . buffer ) {
110+ // max hasn't been reached
111+ if ( playerDict . Count > maxPlayersAllowed ) break ;
112+ // check if player exists and add if not
97113 CreateNewPlayer ( feed . username , feed . avatarPath ) ;
98114 }
99115 foreach ( var feed in Timeline . Instance . history ) {
116+ // max hasn't been reached
117+ if ( playerDict . Count > maxPlayersAllowed ) break ;
118+ // check if player exists and add if not
100119 CreateNewPlayer ( feed . username , feed . avatarPath ) ;
101120 }
102121
103- // update player count
104- playerCount = playerDict . Count ;
122+ UpdateCounts ( ) ;
105123
106- // trigger data updated event
107- EventManager . TriggerEvent ( "PlayersUpdated" ) ;
108124 }
109125
126+
110127 /**
111- * Remove players who haven't been active in a while
112- */
113- public void ClearNonActivePlayers ( )
128+ * Remove all players from screen and dict
129+ * - called from Timeline.OnStartBtnClick() to stop timeline / reset data
130+ */
131+ void RemoveAllPlayers ( )
114132 {
133+ Debug . Log ( "PlayerManager.RemoveAllPlayers()" ) ;
134+
135+ // for each in playerDict, remove from scene
136+ foreach ( KeyValuePair < string , GameObject > kvp in playerDict ) {
137+ Destroy ( kvp . Value ) ;
138+ }
115139
140+ // clear the dictionary
141+ playerDict . Clear ( ) ;
142+ // update the count
143+ UpdateCounts ( ) ;
144+ }
145+
146+
147+ /**
148+ * - called when Timeline.status == TimelineStatus.newDataReceived
149+ */
150+ void CheckUpdatePlayers ( )
151+ {
152+ Debug . Log ( "PlayerManager.CheckUpdatePlayers()" ) ;
153+
154+ // - make a copy of the current player dict -> playersToRemoveDict
155+ // - for each event/player in current buffer and history
156+ // - if player found in playerDict
157+ // - remove from playersToRemoveDict
158+ // - else
159+ // - add to playerDict / scene
160+ // - add ALL players until max per resolution
161+ // - if players still left in playersToRemoveDict
162+ // - remove those players from scene and clear dict
163+ // - update counts
164+
165+
166+
167+ UpdateCounts ( ) ;
168+ }
169+
170+
171+
172+
173+ void UpdateCounts ( )
174+ {
175+ // update player count
176+ playerCount = playerDict . Count ;
116177
117178 // trigger data updated event
118179 EventManager . TriggerEvent ( "PlayersUpdated" ) ;
@@ -123,13 +184,16 @@ public void ClearNonActivePlayers ()
123184
124185
125186
187+
188+
189+
126190 /**
127191 * Create a new player
128192 */
129- public void CreateNewPlayer ( string username , string avatarPath )
193+ public bool CreateNewPlayer ( string username , string avatarPath )
130194 {
131195 // make sure the player doesn't already exist
132- if ( playerDict . ContainsKey ( username ) ) return ;
196+ if ( playerDict . ContainsKey ( username ) ) return false ;
133197
134198 // get a position that doesn't contain any other colliders
135199 Vector3 spawnPosition = GetClearSpawnPosition ( ) ;
@@ -159,6 +223,7 @@ public void CreateNewPlayer (string username, string avatarPath)
159223 // Allow the player to be selected by the camera
160224 cameraManager . AddPlayer ( username ) ;
161225 }
226+ return true ;
162227 }
163228
164229
@@ -181,6 +246,12 @@ public void PlayEvent (FeedData feed)
181246 // reference to script (contains all the other references we need)
182247 currentPlayerScript = currentPlayerObj . GetComponent < Player > ( ) ;
183248
249+ // show event in public var
250+ currentEventType = feed . eventType ;
251+
252+
253+
254+
184255
185256 // EFFECTS
186257
0 commit comments