@@ -20,10 +20,13 @@ export const DEFAULT_ENDGAME_TIME = 20
2020
2121class MatchMode {
2222 private static _instance : MatchMode
23- private _matchEnabled : boolean = false
2423 private _endgame : boolean = false
2524 private _matchModeType : MatchModeType = MatchModeType . SANDBOX
2625
26+ private setMatchModeType ( val : MatchModeType ) {
27+ this . _matchModeType = val
28+ new MatchStateChangeEvent ( val ) . dispatch ( )
29+ }
2730 private _initialTime : number = 0
2831 private _timeLeft : number = 0
2932 private _intervalId : number | null = null
@@ -77,7 +80,7 @@ class MatchMode {
7780
7881 autonomousModeStart ( openModal : ( modalName : string ) => void ) {
7982 SoundPlayer . play ( MatchStart )
80- this . _matchModeType = MatchModeType . AUTONOMOUS
83+ this . setMatchModeType ( MatchModeType . AUTONOMOUS )
8184 this . startTimer ( this . _matchModeConfig . autonomousTime , ( ) => this . autonomousModeEnd ( openModal ) )
8285 }
8386
@@ -88,7 +91,7 @@ class MatchMode {
8891
8992 teleopModeStart ( openModal : ( modalName : string ) => void ) {
9093 SoundPlayer . play ( MatchResume )
91- this . _matchModeType = MatchModeType . TELEOP
94+ this . setMatchModeType ( MatchModeType . TELEOP )
9295 this . startTimer ( this . _matchModeConfig . teleopTime , ( ) => this . matchEnded ( openModal ) )
9396 }
9497
@@ -98,22 +101,19 @@ class MatchMode {
98101 }
99102
100103 start ( openModal : ( modalName : string ) => void ) {
101- this . _matchEnabled = true
102104 this . autonomousModeStart ( openModal )
103105 SimulationSystem . resetScores ( )
104106 }
105107
106108 matchEnded ( openModal : ( modalName : string ) => void ) {
107109 SoundPlayer . play ( MatchEnd )
108110 clearInterval ( this . _intervalId as number )
109- this . _matchEnabled = false
110- this . _matchModeType = MatchModeType . MATCH_ENDED
111+ this . setMatchModeType ( MatchModeType . MATCH_ENDED )
111112 if ( openModal ) openModal ( "match-results" )
112113 }
113114
114115 sandboxModeStart ( ) {
115- this . _matchEnabled = false
116- this . _matchModeType = MatchModeType . SANDBOX
116+ this . setMatchModeType ( MatchModeType . SANDBOX )
117117 clearInterval ( this . _intervalId as number )
118118 this . _initialTime = 0
119119 this . _timeLeft = 0
@@ -122,7 +122,7 @@ class MatchMode {
122122 }
123123
124124 isMatchEnabled ( ) : boolean {
125- return this . _matchEnabled
125+ return ! ( this . _matchModeType == MatchModeType . SANDBOX || this . _matchModeType == MatchModeType . MATCH_ENDED )
126126 }
127127
128128 isEndgame ( ) : boolean {
@@ -158,3 +158,25 @@ export class UpdateTimeLeft extends Event {
158158 window . removeEventListener ( UpdateTimeLeft . EVENT_KEY , func as ( e : Event ) => void )
159159 }
160160}
161+
162+ export class MatchStateChangeEvent extends Event {
163+ public static readonly EVENT_KEY = "MatchEnd"
164+
165+ public readonly matchModeType : MatchModeType
166+ constructor ( matchModeType : MatchModeType ) {
167+ super ( MatchStateChangeEvent . EVENT_KEY )
168+ this . matchModeType = matchModeType
169+ }
170+
171+ public dispatch ( ) : void {
172+ window . dispatchEvent ( this )
173+ }
174+
175+ public static addListener ( func : ( e : MatchStateChangeEvent ) => void ) {
176+ window . addEventListener ( MatchStateChangeEvent . EVENT_KEY , func as ( e : Event ) => void )
177+ }
178+
179+ public static removeListener ( func : ( e : MatchStateChangeEvent ) => void ) {
180+ window . removeEventListener ( MatchStateChangeEvent . EVENT_KEY , func as ( e : Event ) => void )
181+ }
182+ }
0 commit comments