Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 31 additions & 30 deletions fission/src/mirabuf/MirabufSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as THREE from "three"
import type { mirabuf } from "@/proto/mirabuf"
import type {
FieldConfiguration,
MetadataUpdateData,
LocalSceneObjectId,
RemoteSceneObjectId,
RobotConfiguration,
UpdateObjectData,
} from "@/systems/multiplayer/types"
Expand Down Expand Up @@ -118,19 +119,6 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {

private _collision?: (event: OnContactAddedEvent) => void

public get multiplayerInfo(): MetadataUpdateData {
return {
sceneObjectKey: this.id,
alliance: this._alliance,
station: this._station,
}
}

public set multiplayerInfo(info: MetadataUpdateData) {
this._alliance = info.alliance
this._station = info.station
}

public get scoringZones(): Readonly<ScoringZoneSceneObject[]> {
return this._scoringZones
}
Expand Down Expand Up @@ -242,10 +230,16 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
this._station = station
}

public constructor(mirabufInstance: MirabufInstance, assemblyName: string, progressHandle?: ProgressHandle) {
public constructor(
mirabufInstance: MirabufInstance,
assemblyName: string,
progressHandle?: ProgressHandle,
multiplayerOwnerId?: string
) {
super()
this._mirabufInstance = mirabufInstance
this._assemblyName = assemblyName
this._multiplayerOwningClientId = multiplayerOwnerId

progressHandle?.update("Creating mechanism...", 0.9)

Expand Down Expand Up @@ -343,7 +337,9 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
this.updateScoringZones()
this.updateProtectedZones()

setSpotlightAssembly(this)
if (this.isOwnObject) {
setSpotlightAssembly(this)
}

this.updateBatches()

Expand All @@ -353,7 +349,7 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {

const cameraControls = World.sceneRenderer.currentCameraControls as CustomOrbitControls

if (this.miraType === MiraType.ROBOT || !cameraControls.focusProvider) {
if (this.isOwnObject && (this.miraType === MiraType.ROBOT || !cameraControls.focusProvider)) {
cameraControls.focusProvider = this
}

Expand Down Expand Up @@ -848,12 +844,12 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {

public async sendPreferences() {
if (!World.multiplayerSystem) return

const data = this.getPreferenceData()
await World.multiplayerSystem.broadcast({
type: "configureObject",
data: {
sceneObjectKey: this.id,
objectConfigurationData: this.getPreferenceData(),
sceneObjectKey: this.id as RemoteSceneObjectId,
objectConfigurationData: data,
},
})
}
Expand Down Expand Up @@ -896,20 +892,25 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
: {
intakePreferences: JSON.stringify(this._intakePreferences),
ejectorPreferences: JSON.stringify(this._ejectorPreferences),
alliance: this._alliance,
station: this.station,
}
}

public setPreferenceData(preferences: FieldConfiguration | RobotConfiguration) {
if (this.miraType === MiraType.FIELD) {
const config = preferences as FieldConfiguration
this._fieldPreferences = JSON.parse(config.fieldPreferences)
// this.updateScoringZones()
// this.updateProtectedZones()
} else {
const config = preferences as RobotConfiguration
this._intakePreferences = JSON.parse(config.intakePreferences)
this._ejectorPreferences = JSON.parse(config.ejectorPreferences)
this._alliance = config.alliance
this._station = config.station
}
this.updateScoringZones()
this.updateProtectedZones()
this.updateIntakeSensor()
}

public updateSimConfig(config: SimConfigData | undefined) {
Expand All @@ -923,8 +924,8 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
}

public enablePhysics() {
if (World.multiplayerSystem?.getOwnSceneObjectIDs().includes(this.id)) {
World.multiplayerSystem.broadcast({ type: "enableObjectPhysics", data: this.id })
if (World.multiplayerSystem?.getOwnSceneObjectIDs().includes(this.id as LocalSceneObjectId)) {
World.multiplayerSystem.broadcast({ type: "enableObjectPhysics", data: this.id as RemoteSceneObjectId })
}

this._mirabufInstance.parser.rigidNodes.forEach(rn => {
Expand All @@ -934,8 +935,8 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
}

public disablePhysics() {
if (World.multiplayerSystem?.getOwnSceneObjectIDs().includes(this.id)) {
World.multiplayerSystem.broadcast({ type: "disableObjectPhysics", data: this.id })
if (World.multiplayerSystem?.getOwnSceneObjectIDs().includes(this.id as LocalSceneObjectId)) {
World.multiplayerSystem.broadcast({ type: "disableObjectPhysics", data: this.id as RemoteSceneObjectId })
}

this._mirabufInstance.parser.rigidNodes.forEach(rn => {
Expand Down Expand Up @@ -1034,7 +1035,6 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
data.items.push({
name: "Remove",
func: () => {
World.multiplayerSystem?.broadcast({ type: "deleteObject", data: this.id })
World.sceneRenderer.removeSceneObject(this.id)
},
})
Expand Down Expand Up @@ -1063,7 +1063,7 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
.filter(n => n != null)

return {
sceneObjectKey: this.id,
sceneObjectKey: this.id as RemoteSceneObjectId,
gamePiecesControlled,
bodies,
}
Expand All @@ -1088,15 +1088,16 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {

export async function createMirabuf(
assembly: mirabuf.Assembly,
progressHandle?: ProgressHandle
progressHandle?: ProgressHandle,
multiplayerOwnerId?: string
): Promise<MirabufSceneObject | null | undefined> {
const parser = new MirabufParser(assembly, progressHandle)
if (parser.maxErrorSeverity >= ParseErrorSeverity.UNIMPORTABLE) {
console.error(`Assembly Parser produced significant errors for '${assembly.info!.name!}'`)
return
}

return new MirabufSceneObject(new MirabufInstance(parser), assembly.info!.name!, progressHandle)
return new MirabufSceneObject(new MirabufInstance(parser), assembly.info!.name!, progressHandle, multiplayerOwnerId)
}

/**
Expand Down
Loading
Loading