1111use TeeFrame \Game \Entities \AbstractEntity ;
1212use TeeFrame \Game \Entities \Character \AbstractCharacterEntity ;
1313use TeeFrame \Game \Entities \Character \PvpCharacterEntity ;
14+ use TeeFrame \Game \Entities \FlagEntity ;
15+ use TeeFrame \Game \Entities \PickupEntity ;
1416use TeeFrame \Game \Tees \AbstractTee ;
1517use TeeFrame \Game \Tees \PlayerTee ;
1618use TeeFrame \Game \Vote \VoteController ;
2931use TeeFrame \Network \Chunks \Game \SvChatChunk ;
3032use TeeFrame \Network \Chunks \Game \SvBroadcastChunk ;
3133use TeeFrame \Network \Chunks \Game \SvEmoticonChunk ;
34+ use TeeFrame \Network \Chunks \Game \SvSoundGlobalChunk ;
3235use TeeFrame \Network \NetworkParams ;
3336use TeeFrame \Network \SnapItems \AbstractPositionedSnapItem ;
3437use TeeFrame \Network \SnapItems \AbstractSnapItem ;
@@ -70,6 +73,9 @@ abstract class AbstractWorld implements SnapableObject, TickableObject
7073
7174 protected TuneController $ tuneController ;
7275
76+ protected bool $ paused = false ;
77+ protected bool $ resetRequested = false ;
78+
7379 public function __construct (
7480 public string $ identifier ,
7581 protected TickHandler $ tickHandler ,
@@ -84,7 +90,13 @@ public function __construct(
8490 $ this ->bootTuneController ();
8591
8692 $ this ->bootGameController ();
87- $ this ->collectSpawnPoints ();
93+
94+ $ this ->getGameController ()->setWorld ($ this );
95+
96+ if (($ gameLayer = $ this ->getMap ()->getGameLayer ()) !== null ) {
97+ $ this ->getGameController ()->collectSpawnPoints ($ gameLayer );
98+ $ this ->getGameController ()->collectFlagPoints ($ gameLayer );
99+ }
88100 }
89101
90102 abstract protected function bootGameController (): void ;
@@ -102,14 +114,6 @@ public function registerCommand(AbstractCommand $command): void
102114 $ this ->commands [] = $ command ;
103115 }
104116
105- protected function collectSpawnPoints (): void
106- {
107- $ gameLayer = $ this ->map ->getGameLayer ();
108-
109- if ($ gameLayer !== null ) {
110- $ this ->gameController ->collectSpawnPoints ($ gameLayer );
111- }
112- }
113117
114118 protected function bootVoteController (): void
115119 {
@@ -179,6 +183,84 @@ public function getTuneController(): TuneController
179183 return $ this ->tuneController ;
180184 }
181185
186+ public function isPaused (): bool
187+ {
188+ return $ this ->paused ;
189+ }
190+
191+ public function setPaused (bool $ paused ): void
192+ {
193+ $ this ->paused = $ paused ;
194+ }
195+
196+ public function isResetRequested (): bool
197+ {
198+ return $ this ->resetRequested ;
199+ }
200+
201+ public function resetGame (): void
202+ {
203+ $ this ->resetRequested = true ;
204+ }
205+
206+ protected function resetRound (int $ currentTick ): void
207+ {
208+ $ survivingEntities = [];
209+
210+ foreach ($ this ->entities as $ entity ) {
211+ if ($ entity instanceof PickupEntity) {
212+ $ entity ->reset ();
213+ $ survivingEntities [] = $ entity ;
214+ } elseif ($ entity instanceof FlagEntity) {
215+ $ entity ->reset ();
216+ $ survivingEntities [] = $ entity ;
217+ } else {
218+ $ this ->removeEntity ($ entity );
219+ }
220+ }
221+
222+ $ this ->entities = $ survivingEntities ;
223+
224+ foreach ($ this ->tees as $ tee ) {
225+ if (! $ tee instanceof PlayerTee) {
226+ continue ;
227+ }
228+
229+ // Detach any lingering character reference
230+ $ tee ->character = null ;
231+
232+ $ tee ->score = 0 ;
233+ $ tee ->scoreStartTick = $ currentTick ;
234+ $ tee ->respawnTick = $ currentTick + (int ) (NetworkParams::TICKS_PER_SECOND / 2 );
235+
236+ if ($ tee ->team !== GameConstants::TEAM_SPECTATORS ) {
237+ $ tee ->spawning = true ;
238+ }
239+ }
240+ }
241+
242+ public function createSoundGlobal (int $ soundId ): void
243+ {
244+ if ($ soundId < 0 ) {
245+ return ;
246+ }
247+
248+ $ chunk = new SvSoundGlobalChunk (soundId: $ soundId );
249+
250+ foreach ($ this ->tees as $ tee ) {
251+ $ this ->server ->sendToTee ($ this , $ tee ->teeIndex , $ chunk );
252+ }
253+ }
254+
255+ public function createSoundGlobalFor (int $ soundId , int $ teeIndex ): void
256+ {
257+ if ($ soundId < 0 ) {
258+ return ;
259+ }
260+
261+ $ this ->server ->sendToTee ($ this , $ teeIndex , new SvSoundGlobalChunk (soundId: $ soundId ));
262+ }
263+
182264 public function addEvent (AbstractPositionedSnapItem $ event ): void
183265 {
184266 $ this ->pendingEvents [] = $ event ;
@@ -229,7 +311,14 @@ public function addTee(AbstractTee $tee): void
229311 $ this ->tees [$ index ] = $ tee ;
230312
231313 if ($ tee instanceof PlayerTee) {
314+ $ autoTeam = $ this ->getGameController ()->getAutoTeam ($ tee ->teeIndex );
315+ if ($ tee ->team !== $ autoTeam ) {
316+ $ tee ->team = $ autoTeam ;
317+ }
318+
232319 $ tee ->spawning = true ;
320+ $ tee ->lastActionTick = $ this ->getCurrentTick ();
321+ $ tee ->scoreStartTick = $ this ->getCurrentTick ();
233322
234323 $ chunk = new SvChatChunk (
235324 team: 0 ,
@@ -245,28 +334,34 @@ public function addTee(AbstractTee $tee): void
245334
246335 public function removeTee (AbstractTee $ tee , ?string $ reason = null ): void
247336 {
248- if ($ tee ->character !== null && $ tee ->character ->alive ) {
249- $ tee ->character ->die ();
250- }
337+ foreach ($ this ->tees as $ index => $ existingTee ) {
338+ if ($ existingTee !== $ tee ) {
339+ continue ;
340+ }
251341
252- if ($ tee instanceof PlayerTee) {
253- $ text = $ reason !== null && $ reason !== ''
254- ? "' {$ tee ->name }' has left the game ( {$ reason }) "
255- : "' {$ tee ->name }' has left the game " ;
342+ if ($ existingTee ->character !== null && $ existingTee ->character ->alive ) {
343+ $ existingTee ->character ->die ();
344+ }
256345
257- $ chunk = new SvChatChunk (
258- team: 0 ,
259- clientId: -1 ,
260- text: $ text ,
261- );
346+ if ($ existingTee instanceof PlayerTee) {
347+ $ text = $ reason !== null && $ reason !== ''
348+ ? "' {$ tee ->name }' has left the game ( {$ reason }) "
349+ : "' {$ tee ->name }' has left the game " ;
262350
263- foreach ($ this ->tees as $ tee ) {
264- $ this ->server ->sendToTee ($ this , $ tee ->teeIndex , $ chunk );
351+ $ chunk = new SvChatChunk (
352+ team: 0 ,
353+ clientId: -1 ,
354+ text: $ text ,
355+ );
356+
357+ foreach ($ this ->tees as $ recipientTee ) {
358+ $ this ->server ->sendToTee ($ this , $ recipientTee ->teeIndex , $ chunk );
359+ }
265360 }
266- }
267361
268- $ this ->releasedTeeIndexes [] = $ tee ->teeIndex ;
269- unset($ this ->tees [$ tee ->teeIndex ]);
362+ $ this ->releasedTeeIndexes [] = $ index ;
363+ unset($ this ->tees [$ index ]);
364+ }
270365 }
271366
272367 public function onMessage (AbstractTee $ tee , AbstractChunk $ chunk ): void
@@ -292,8 +387,9 @@ public function onMessage(AbstractTee $tee, AbstractChunk $chunk): void
292387
293388 public function doTick (): void
294389 {
295- // apply new input
296390 $ currentTick = $ this ->getCurrentTick ();
391+
392+ // apply new input
297393 foreach ($ this ->tees as $ tee ) {
298394 if (! $ tee instanceof PlayerTee) {
299395 continue ;
@@ -321,56 +417,68 @@ public function doTick(): void
321417 // Vote ticking (CGameContext::OnTick vote section)
322418 $ this ->getVoteController ()->tick ($ this );
323419
324- // Game controller tick: handle win conditions...
420+ // Game controller tick
325421 $ this ->getGameController ()->doTick ();
326422
327- // Entity tick: handle pickups, collisions...
328- foreach ($ this ->entities as $ entity ) {
329- $ entity ->doTick ();
330-
331- if ($ entity ->isToDestroy ()) {
332- $ this ->removeEntity ($ entity );
333- }
423+ // Handle a reset requested by the game controller (ResetGame/StartRound).
424+ if ($ this ->resetRequested ) {
425+ $ this ->resetRequested = false ;
426+ $ this ->resetRound ($ currentTick );
334427 }
335428
336- // Player tick: handle respawns (CPlayer::Tick)
337- foreach ($ this ->tees as $ tee ) {
338- if (! $ tee instanceof PlayerTee) {
339- continue ;
429+ if ($ this ->paused ) {
430+ foreach ($ this ->entities as $ entity ) {
431+ $ entity ->tickPaused ();
340432 }
433+ } else {
434+ // Entity tick: handle pickups, collisions...
435+ foreach ($ this ->entities as $ entity ) {
436+ $ entity ->doTick ();
341437
342- // Spectators don't respawn
343- if ( $ tee -> team === GameConstants:: TEAM_SPECTATORS ) {
344- continue ;
438+ if ( $ entity -> isToDestroy ()) {
439+ $ this -> removeEntity ( $ entity );
440+ }
345441 }
346442
347- if ($ tee ->character === null ) {
348- // Auto-respawn after 3s of being dead
349- // (CPlayer::Tick: !m_pCharacter && m_DieTick+TickSpeed*3 <= Tick)
350- if (! $ tee ->spawning && $ tee ->dieTick > 0 && $ tee ->dieTick + 3 * 50 <= $ currentTick ) {
351- $ tee ->spawning = true ;
443+ // Player tick: handle respawns (CPlayer::Tick)
444+ foreach ($ this ->tees as $ tee ) {
445+ if (! $ tee instanceof PlayerTee) {
446+ continue ;
352447 }
353448
354- // Try to respawn once the respawn tick has elapsed
355- // (CPlayer::Tick: m_Spawning && m_RespawnTick <= Tick)
356- if ($ tee ->spawning && $ tee ->respawnTick <= $ currentTick ) {
357- $ this ->tryRespawnTee ($ tee );
449+ // Spectators don't respawn
450+ if ($ tee ->team === GameConstants::TEAM_SPECTATORS ) {
451+ continue ;
358452 }
359- }
360- }
361453
362- // Player post-tick: update spectator view positions (CPlayer::PostTick)
363- foreach ($ this ->tees as $ tee ) {
364- if (! $ tee instanceof PlayerTee) {
365- continue ;
454+ if ($ tee ->character === null ) {
455+ // Auto-respawn after 3s of being dead
456+ // (CPlayer::Tick: !m_pCharacter && m_DieTick+TickSpeed*3 <= Tick)
457+ if (! $ tee ->spawning && $ tee ->dieTick > 0 && $ tee ->dieTick + 3 * 50 <= $ currentTick ) {
458+ $ tee ->spawning = true ;
459+ }
460+
461+ // Try to respawn once the respawn tick has elapsed
462+ // (CPlayer::Tick: m_Spawning && m_RespawnTick <= Tick)
463+ if ($ tee ->spawning && $ tee ->respawnTick <= $ currentTick ) {
464+ $ this ->tryRespawnTee ($ tee );
465+ }
466+ }
366467 }
367468
368- // Spectators following a player use that player's view position
369- if ($ tee ->team === GameConstants::TEAM_SPECTATORS && $ tee ->spectatorId !== GameConstants::SPEC_FREEVIEW ) {
370- foreach ($ this ->tees as $ targetTee ) {
371- if ($ targetTee ->teeIndex === $ tee ->spectatorId ) {
372- $ tee ->viewPosition = clone $ targetTee ->viewPosition ;
373- break ;
469+ // Player post-tick: update spectator view positions (CPlayer::PostTick)
470+ foreach ($ this ->tees as $ tee ) {
471+ if (! $ tee instanceof PlayerTee) {
472+ continue ;
473+ }
474+
475+ // Spectators following a player use that player's view position
476+ if ($ tee ->team === GameConstants::TEAM_SPECTATORS && $ tee ->spectatorId !== GameConstants::SPEC_FREEVIEW ) {
477+ foreach ($ this ->tees as $ targetTee ) {
478+ if ($ targetTee ->teeIndex === $ tee ->spectatorId ) {
479+ $ tee ->viewPosition = clone $ targetTee ->viewPosition ;
480+ break ;
481+ }
374482 }
375483 }
376484 }
@@ -467,11 +575,14 @@ protected function doEntitySnap(AbstractTee $requestingTee): array
467575
468576 $ entitySnaps = $ entity ->doSnap ($ requestingTee );
469577
470- // Character entities must use the tee's index as snap ID
471- if ($ entity instanceof AbstractCharacterEntity && $ entity ->tee !== null && $ entity ->tee ->teeIndex >= 0 ) {
578+ if ($ entity instanceof AbstractCharacterEntity && $ entity ->tee !== null && $ entity ->tee ->teeIndex >= 0 ) { // Character entities must use the tee's index as snap ID
472579 foreach ($ entitySnaps as $ snap ) {
473580 $ snap ->setId ($ entity ->tee ->teeIndex );
474581 }
582+ } elseif ($ entity instanceof FlagEntity) { // Flags use their team as the snap ID
583+ foreach ($ entitySnaps as $ snap ) {
584+ $ snap ->setId ($ entity ->team );
585+ }
475586 } else {
476587 $ entityAllocatedIds = $ entity ->getAllocatedSnapIds ();
477588
@@ -653,7 +764,7 @@ protected function handleSetSpectatorModeMessage(AbstractTee $tee, ClSetSpectato
653764
654765 protected function tryRespawnTee (PlayerTee $ tee ): bool
655766 {
656- $ spawnPos = $ this ->getGameController ()->canSpawn ($ this , 0 );
767+ $ spawnPos = $ this ->getGameController ()->canSpawn ($ this , $ tee -> team );
657768 if ($ spawnPos === null ) {
658769 return false ;
659770 }
0 commit comments