Skip to content

Commit d2d2aca

Browse files
Allow Cancelling Match Mode [AARD-2009] (#1231)
Co-authored-by: Zach Rutman <92497727+rutmanz@users.noreply.github.com> Co-authored-by: Brandon Pacewic <92102436+BrandonPacewic@users.noreply.github.com>
2 parents 3fac06c + 6a3adb0 commit d2d2aca

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-25
lines changed

fission/src/systems/MatchMode.ts

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@ export const DEFAULT_ENDGAME_TIME = 20
2020

2121
class 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+
}

fission/src/ui/components/MainHUD.tsx

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Box } from "@mui/material"
1313
import { TouchControlsEvent, TouchControlsEventKeys } from "./TouchControls"
1414
import { setAddToast } from "./GlobalUIControls"
1515
import { SoundPlayer } from "@/systems/sound/SoundPlayer"
16-
import MatchMode from "@/systems/MatchMode"
16+
import MatchMode, { MatchStateChangeEvent } from "@/systems/MatchMode"
1717
import { globalAddToast } from "@/components/GlobalUIControls.ts"
1818

1919
type ButtonProps = {
@@ -66,13 +66,20 @@ const MainHUD: React.FC = () => {
6666
setAddToast(addToast)
6767

6868
const [userInfo, setUserInfo] = useState(APS.userInfo)
69+
const [matchModeRunning, setMatchModeRunning] = useState(MatchMode.getInstance().isMatchEnabled())
6970

7071
useEffect(() => {
7172
document.addEventListener(APS_USER_INFO_UPDATE_EVENT, () => {
7273
setUserInfo(APS.userInfo)
7374
})
7475
}, [])
7576

77+
useEffect(() => {
78+
MatchStateChangeEvent.addListener(() => {
79+
setMatchModeRunning(MatchMode.getInstance().isMatchEnabled())
80+
})
81+
}, [])
82+
7683
return (
7784
<>
7885
{!isOpen && (
@@ -193,21 +200,27 @@ const MainHUD: React.FC = () => {
193200
onClick={() => APS.requestAuthCode()}
194201
/>
195202
)}
196-
<MainHUDButton
197-
value={"Start Match Mode"}
198-
icon={SynthesisIcons.GAMEPAD}
199-
larger={true}
200-
onClick={() => {
201-
MatchMode.getInstance().isMatchEnabled()
202-
? globalAddToast(
203-
"error",
204-
"Match Mode Already Running",
205-
"You can't start match mode if its already running"
206-
)
207-
: openPanel("match-mode-config")
208-
setIsOpen(false)
209-
}}
210-
/>
203+
{!matchModeRunning ? (
204+
<MainHUDButton
205+
value={"Start Match Mode"}
206+
icon={SynthesisIcons.GAMEPAD}
207+
larger={true}
208+
onClick={() => {
209+
openPanel("match-mode-config")
210+
setIsOpen(false)
211+
}}
212+
/>
213+
) : (
214+
<MainHUDButton
215+
value={"Abort Match Mode"}
216+
icon={SynthesisIcons.XMARK_LARGE}
217+
larger={true}
218+
onClick={() => {
219+
MatchMode.getInstance().sandboxModeStart()
220+
globalAddToast("info", "Match Mode Cancelled", "")
221+
}}
222+
/>
223+
)}
211224
</motion.div>
212225
</>
213226
)

0 commit comments

Comments
 (0)