Skip to content

Commit dc1a2c8

Browse files
committed
Merge remote-tracking branch 'origin/dev' into zachr/2043/match-mode-config
2 parents 8fe1111 + b21585c commit dc1a2c8

File tree

20 files changed

+110
-154
lines changed

20 files changed

+110
-154
lines changed

fission/src/mirabuf/EjectableSceneObject.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,7 @@ class EjectableSceneObject extends SceneObject {
8080
World.physicsSystem.disablePhysicsForBody(this._gamePieceBodyId)
8181

8282
// Remove from any scoring zones
83-
const zones = [...World.sceneRenderer.sceneObjects.entries()]
84-
.filter(x => x[1] instanceof ScoringZoneSceneObject)
85-
.map(x => x[1]) as ScoringZoneSceneObject[]
86-
83+
const zones = World.sceneRenderer.filterSceneObjects(x => x instanceof ScoringZoneSceneObject)
8784
zones.forEach(x => {
8885
if (this._gamePieceBodyId) ScoringZoneSceneObject.removeGamepiece(x, this._gamePieceBodyId)
8986
})

fission/src/systems/World.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,8 @@ class World {
119119
World._analyticsSystem?.update(this._currentDeltaT)
120120
World._performanceMonitorSystem?.update(this._currentDeltaT)
121121

122-
RobotDimensionTracker.update(World._sceneRenderer)
123-
RobotPositionTracker.update(World._sceneRenderer)
122+
RobotDimensionTracker.update()
123+
RobotPositionTracker.update()
124124
}
125125

126126
public static get currentDeltaT(): number {

fission/src/systems/match_mode/RobotDimensionTracker.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import { MiraType } from "@/mirabuf/MirabufLoader"
2-
import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
31
import SimulationSystem from "@/systems/simulation/SimulationSystem"
4-
import World from "@/systems/World"
5-
import type SceneRenderer from "../scene/SceneRenderer"
2+
import World from "@/systems/World.ts"
63
import MatchMode from "./MatchMode"
74

85
const BUFFER_HEIGHT = 0.1
@@ -31,14 +28,10 @@ class RobotDimensionTracker {
3128
this._sideExtensionPenalty = sideExtensionPenalty
3229
}
3330

34-
public static update(sceneRenderer: SceneRenderer): void {
31+
public static update(): void {
3532
if (!MatchMode.getInstance().isMatchEnabled()) return
3633

37-
const robots = [...sceneRenderer.sceneObjects.values()].filter(
38-
(obj): obj is MirabufSceneObject => obj instanceof MirabufSceneObject && obj.miraType === MiraType.ROBOT
39-
)
40-
41-
robots.forEach(robot => {
34+
World.sceneRenderer.mirabufSceneObjects.getRobots().forEach(robot => {
4235
const dimensions = this._ignoreRotation ? robot.getDimensionsWithoutRotation() : robot.getDimensions()
4336

4437
if (dimensions.height > this._maxHeight + BUFFER_HEIGHT) {
@@ -69,11 +62,7 @@ class RobotDimensionTracker {
6962
this._robotSize.clear()
7063
this._robotLastFramePenalty.clear()
7164

72-
const robots = [...World.sceneRenderer.sceneObjects.values()].filter(
73-
(obj): obj is MirabufSceneObject => obj instanceof MirabufSceneObject && obj.miraType === MiraType.ROBOT
74-
)
75-
76-
robots.forEach(robot => {
65+
World.sceneRenderer.mirabufSceneObjects.getRobots().forEach(robot => {
7766
this._robotSize.set(robot.id, robot.getDimensions())
7867
})
7968
}

fission/src/systems/scene/CameraControls.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as THREE from "three"
22
import { MiraType } from "@/mirabuf/MirabufLoader"
3-
import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
3+
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
44
import PreferencesSystem from "@/systems/preferences/PreferencesSystem"
55
import World from "../World"
66
import type ScreenInteractionHandler from "./ScreenInteractionHandler"
@@ -171,15 +171,12 @@ export class CustomOrbitControls extends CameraControls {
171171
* Prioritizes robots first, then fields, then any other MirabufSceneObject.
172172
*/
173173
private findFallbackFocus(mirabufObjects?: MirabufSceneObject[]): MirabufSceneObject | undefined {
174-
if (!mirabufObjects) {
175-
const sceneObjects = Array.from(World.sceneRenderer.sceneObjects.values())
176-
mirabufObjects = sceneObjects.filter(obj => obj instanceof MirabufSceneObject) as MirabufSceneObject[]
177-
}
174+
mirabufObjects ??= World.sceneRenderer.mirabufSceneObjects.getAll()
178175

179176
const robots = mirabufObjects.filter(obj => obj.miraType === MiraType.ROBOT)
180177
const fields = mirabufObjects.filter(obj => obj.miraType === MiraType.FIELD)
181178

182-
return robots[0] || fields[0] || mirabufObjects[0]
179+
return robots[0] ?? fields[0] ?? mirabufObjects[0]
183180
}
184181

185182
/**
@@ -190,9 +187,7 @@ export class CustomOrbitControls extends CameraControls {
190187
if (!World.sceneRenderer?.sceneObjects || World.dragModeSystem.isTransitioning) {
191188
return
192189
}
193-
194-
const allSceneObjects = Array.from(World.sceneRenderer.sceneObjects.values())
195-
const mirabufObjects = allSceneObjects.filter(obj => obj instanceof MirabufSceneObject) as MirabufSceneObject[]
190+
const mirabufObjects = World.sceneRenderer.mirabufSceneObjects.getAll()
196191

197192
if (this._focusProvider) {
198193
if (!mirabufObjects.includes(this._focusProvider)) {

fission/src/systems/scene/DragModeSystem.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type Jolt from "@azaleacolburn/jolt-physics"
22
import * as THREE from "three"
33
import { MiraType } from "@/mirabuf/MirabufLoader"
4-
import MirabufSceneObject, { type RigidNodeAssociate } from "@/mirabuf/MirabufSceneObject"
4+
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
5+
import type { RigidNodeAssociate } from "@/mirabuf/MirabufSceneObject"
56
import InputSystem from "@/systems/input/InputSystem.ts"
67
import JOLT from "@/util/loading/JoltSyncLoader"
78
import { convertJoltVec3ToThreeVector3, convertThreeVector3ToJoltVec3 } from "@/util/TypeConversions"
@@ -242,7 +243,7 @@ class DragModeSystem extends WorldSystem {
242243
const hitResult = this.raycastFromMouse(interaction.position)
243244
if (hitResult) {
244245
const association = World.physicsSystem.getBodyAssociation(hitResult.data.mBodyID) as RigidNodeAssociate
245-
if (association?.sceneObject && association.sceneObject instanceof MirabufSceneObject) {
246+
if (association?.sceneObject) {
246247
const body = World.physicsSystem.getBody(hitResult.data.mBodyID)
247248
if (body) {
248249
const isStatic = body.GetMotionType() === JOLT.EMotionType_Static

fission/src/systems/scene/SceneRenderer.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,18 @@ class SceneRenderer extends WorldSystem {
5555
return this._sceneObjects
5656
}
5757

58+
public filterSceneObjects<T extends SceneObject>(predicate: (obj: SceneObject) => obj is T): T[] {
59+
return [...this._sceneObjects.values()].filter(predicate)
60+
}
61+
62+
public readonly mirabufSceneObjects = {
63+
getAll: () => this.filterSceneObjects(obj => obj instanceof MirabufSceneObject),
64+
findWhere: (predicate: Parameters<(typeof Array<MirabufSceneObject>)["prototype"]["find"]>[0]) =>
65+
this.mirabufSceneObjects.getAll().find(predicate),
66+
getField: () => this.mirabufSceneObjects.findWhere(obj => obj.miraType == MiraType.FIELD),
67+
getRobots: () => this.mirabufSceneObjects.getAll().filter(obj => obj.miraType == MiraType.ROBOT),
68+
} as const
69+
5870
public get mainCamera() {
5971
return this._mainCamera
6072
}
@@ -514,9 +526,9 @@ class SceneRenderer extends WorldSystem {
514526
*/
515527
public onContextMenu(e: InteractionEnd) {
516528
// Cast ray into physics scene.
517-
const origin = World.sceneRenderer.mainCamera.position
529+
const origin = this.mainCamera.position
518530

519-
const worldSpace = World.sceneRenderer.pixelToWorldSpace(e.position[0], e.position[1])
531+
const worldSpace = this.pixelToWorldSpace(e.position[0], e.position[1])
520532
const dir = worldSpace.sub(origin).normalize().multiplyScalar(40.0)
521533

522534
const res = World.physicsSystem.rayCast(

fission/src/systems/simulation/RobotPositionTracker.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
import * as THREE from "three"
2-
import { MiraType } from "@/mirabuf/MirabufLoader"
3-
import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
42
import SimulationSystem from "@/systems/simulation/SimulationSystem"
53
import JOLT from "@/util/loading/JoltSyncLoader"
64
import { convertJoltMat44ToThreeMatrix4 } from "@/util/TypeConversions"
7-
import type SceneRenderer from "../scene/SceneRenderer"
85
import World from "../World"
96

107
class RobotPositionTracker {
118
private static _mapBoundaryY: number = -4
129
private static _offMapPenalty: number = 0
1310

14-
public static update(sceneRenderer: SceneRenderer): void {
15-
const robots = [...sceneRenderer.sceneObjects.values()].filter(
16-
(obj): obj is MirabufSceneObject => obj instanceof MirabufSceneObject && obj.miraType === MiraType.ROBOT
17-
)
18-
19-
robots.forEach(robot => {
11+
public static update(): void {
12+
World.sceneRenderer.mirabufSceneObjects.getRobots().forEach(robot => {
2013
const rootNodeId = robot.getRootNodeId()
2114
if (!rootNodeId) {
2215
return

fission/src/systems/simulation/SimulationSystem.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
22
import { OnScoreChangedEvent } from "@/mirabuf/ScoringZoneSceneObject"
3+
import World from "@/systems/World.ts"
34
import { globalAddToast } from "@/ui/components/GlobalUIControls"
45
import JOLT from "@/util/loading/JoltSyncLoader"
56
import type Mechanism from "../physics/Mechanism"
6-
import World from "../World"
77
import WorldSystem from "../WorldSystem"
88
import type Brain from "./Brain"
99
import type Driver from "./driver/Driver"
@@ -113,9 +113,7 @@ class SimulationLayer {
113113
constructor(mechanism: Mechanism) {
114114
this._mechanism = mechanism
115115

116-
const assembly = [...World.sceneRenderer.sceneObjects.values()].find(
117-
x => (x as MirabufSceneObject).mechanism == mechanism
118-
) as MirabufSceneObject
116+
const assembly = World.sceneRenderer.mirabufSceneObjects.findWhere(obj => obj.mechanism == mechanism)
119117

120118
// Generate standard drivers and stimuli
121119
this._drivers = new Map()

fission/src/systems/simulation/wpilib_brain/WPILibBrain.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
1+
import type MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
22
import World from "@/systems/World"
33
import { random } from "@/util/Random"
44
import Brain from "../Brain"
@@ -101,8 +101,8 @@ class WPILibBrain extends Brain {
101101

102102
this.loadSimConfig()
103103

104-
World.sceneRenderer.sceneObjects.forEach(v => {
105-
if (v instanceof MirabufSceneObject && v.brain?.brainType == "wpilib") {
104+
World.sceneRenderer.mirabufSceneObjects.getRobots().forEach(v => {
105+
if (v.brain?.brainType == "wpilib") {
106106
v.brain = new SynthesisBrain(v, v.assemblyName)
107107
}
108108
})

fission/src/test/RobotDimensionsTracker.test.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { afterEach, beforeEach, describe, expect, test, vi } from "vitest"
22
import { MiraType } from "@/mirabuf/MirabufLoader"
33
import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
44
import RobotDimensionTracker from "@/systems/match_mode/RobotDimensionTracker"
5-
import type SceneObject from "@/systems/scene/SceneObject"
65
import SimulationSystem from "@/systems/simulation/SimulationSystem"
76
import World from "@/systems/World"
87

@@ -35,12 +34,6 @@ interface MockNonRobotObject {
3534
dispose: () => void
3635
}
3736

38-
interface MockSceneRenderer {
39-
sceneObjects: Map<number, MockRobotObject | MockNonRobotObject>
40-
}
41-
42-
type TrackerUpdateParam = Parameters<typeof RobotDimensionTracker.update>[0]
43-
4437
const mockMatchModeInstance = {
4538
isMatchEnabled: vi.fn(() => true),
4639
}
@@ -63,16 +56,21 @@ vi.mock("@/systems/simulation/SimulationSystem", () => ({
6356
},
6457
}))
6558

66-
vi.mock("@/systems/World", () => ({
59+
type RecursivePartial<T> = {
60+
[P in keyof T]?: RecursivePartial<T[P]>
61+
}
62+
63+
vi.mock("@/systems/World", (): { default: RecursivePartial<typeof World> } => ({
6764
default: {
6865
sceneRenderer: {
69-
sceneObjects: new Map(),
66+
mirabufSceneObjects: {
67+
getRobots: vi.fn(),
68+
},
7069
},
7170
},
7271
}))
7372

7473
describe("RobotDimensionTracker", () => {
75-
let mockSceneRenderer: MockSceneRenderer
7674
let mockRobot1: MockRobotObject
7775
let mockRobot2: MockRobotObject
7876
let mockNonRobot: MockNonRobotObject
@@ -124,18 +122,10 @@ describe("RobotDimensionTracker", () => {
124122
update: vi.fn(),
125123
dispose: vi.fn(),
126124
}
127-
128-
mockSceneRenderer = {
129-
sceneObjects: new Map([
130-
[1, mockRobot1],
131-
[2, mockRobot2],
132-
[3, mockNonRobot],
133-
]),
134-
}
135-
136-
World.sceneRenderer.sceneObjects.set(1, mockRobot1 as unknown as SceneObject)
137-
World.sceneRenderer.sceneObjects.set(2, mockRobot2 as unknown as SceneObject)
138-
World.sceneRenderer.sceneObjects.set(3, mockNonRobot as unknown as SceneObject)
125+
;(World.sceneRenderer.mirabufSceneObjects.getRobots as ReturnType<typeof vi.fn>).mockReturnValue([
126+
mockRobot1,
127+
mockRobot2,
128+
])
139129
})
140130

141131
afterEach(() => {
@@ -144,7 +134,7 @@ describe("RobotDimensionTracker", () => {
144134

145135
test("config values determine which dimension method is used", () => {
146136
RobotDimensionTracker.setConfigValues(false, 2, 15, 1.5, 15)
147-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
137+
RobotDimensionTracker.update()
148138

149139
expect(mockRobot1.getDimensions).toHaveBeenCalled()
150140
expect(mockRobot1.getDimensionsWithoutRotation).not.toHaveBeenCalled()
@@ -156,7 +146,7 @@ describe("RobotDimensionTracker", () => {
156146
mockRobot1.getDimensionsWithoutRotation = vi.fn().mockReturnValue({ height: 2.0, width: 1.0, depth: 1.0 })
157147
mockRobot2.getDimensionsWithoutRotation = vi.fn().mockReturnValue({ height: 3.5, width: 1.0, depth: 1.0 })
158148

159-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
149+
RobotDimensionTracker.update()
160150

161151
expect(SimulationSystem.robotPenalty).toHaveBeenCalledWith(mockRobot2, 5, expect.any(String))
162152
expect(SimulationSystem.robotPenalty).not.toHaveBeenCalledWith(
@@ -179,7 +169,7 @@ describe("RobotDimensionTracker", () => {
179169

180170
mockRobot2.getDimensionsWithoutRotation = vi.fn().mockReturnValue({ height: 2.0, width: 1.7, depth: 1.0 })
181171

182-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
172+
RobotDimensionTracker.update()
183173

184174
expect(SimulationSystem.robotPenalty).toHaveBeenCalledWith(mockRobot2, 2, expect.any(String))
185175
expect(SimulationSystem.robotPenalty).not.toHaveBeenCalledWith(
@@ -202,7 +192,7 @@ describe("RobotDimensionTracker", () => {
202192

203193
mockRobot2.getDimensionsWithoutRotation = vi.fn().mockReturnValue({ height: 2.0, width: 1.0, depth: 1.7 })
204194

205-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
195+
RobotDimensionTracker.update()
206196

207197
expect(SimulationSystem.robotPenalty).toHaveBeenCalledWith(mockRobot2, 3, expect.any(String))
208198
expect(SimulationSystem.robotPenalty).not.toHaveBeenCalledWith(
@@ -218,7 +208,7 @@ describe("RobotDimensionTracker", () => {
218208
mockRobot1.getDimensions = vi.fn().mockReturnValue({ height: 2.0, width: 1.0, depth: 1.2 })
219209
mockRobot2.getDimensions = vi.fn().mockReturnValue({ height: 2.0, width: 1.0, depth: 1.2 })
220210

221-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
211+
RobotDimensionTracker.update()
222212

223213
expect(SimulationSystem.robotPenalty).not.toHaveBeenCalled()
224214
})
@@ -228,8 +218,8 @@ describe("RobotDimensionTracker", () => {
228218

229219
mockRobot1.getDimensionsWithoutRotation = vi.fn().mockReturnValue({ height: 12, width: 1.0, depth: 1.0 })
230220

231-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
232-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
221+
RobotDimensionTracker.update()
222+
RobotDimensionTracker.update()
233223

234224
expect(SimulationSystem.robotPenalty).toHaveBeenCalledTimes(1)
235225
})
@@ -240,7 +230,7 @@ describe("RobotDimensionTracker", () => {
240230
mockRobot1.getDimensionsWithoutRotation = vi.fn().mockReturnValue({ height: 12, width: 1.0, depth: 1.0 })
241231
mockRobot2.getDimensionsWithoutRotation = vi.fn().mockReturnValue({ height: 12, width: 1.0, depth: 1.0 })
242232

243-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
233+
RobotDimensionTracker.update()
244234

245235
expect(SimulationSystem.robotPenalty).toHaveBeenCalledTimes(2)
246236
})
@@ -250,7 +240,7 @@ describe("RobotDimensionTracker", () => {
250240

251241
mockNonRobot.getDimensions = vi.fn().mockReturnValue({ height: 12, width: 1.0, depth: 1.0 })
252242

253-
RobotDimensionTracker.update(mockSceneRenderer as unknown as TrackerUpdateParam)
243+
RobotDimensionTracker.update()
254244

255245
expect(SimulationSystem.robotPenalty).not.toHaveBeenCalled()
256246
})

0 commit comments

Comments
 (0)