Created
July 31, 2022 20:33
-
-
Save effektsvk/b0c1bad8c1f282daf64f36a77bdaffc0 to your computer and use it in GitHub Desktop.
Dance system issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as utils from '@dcl/ecs-scene-utils' | |
import { triggerEmote, PredefinedEmote } from '@decentraland/RestrictedActions' | |
export class DanceSystem implements ISystem { | |
emotes: PredefinedEmote[] = [] | |
timer: number = 2 | |
length: number = 11 | |
danceFunction: () => void = () => { | |
return this.dance() | |
} | |
constructor(emotes: PredefinedEmote[]) { | |
this.emotes = emotes | |
} | |
update(dt: number) { | |
if (this.timer > 0) { | |
this.timer -= dt | |
} else { | |
return this.dance() | |
} | |
} | |
dance() { | |
this.timer = this.length | |
const rand = Math.floor(Math.random() * (this.emotes.length - 0) + 0) | |
const emoteToDance = this.emotes[rand] | |
log('dance', emoteToDance) | |
if (!emoteToDance) { | |
return | |
} | |
return triggerEmote({ predefined: this.emotes[rand] }) | |
} | |
addEvents() { | |
Input.instance.subscribe( | |
'BUTTON_UP', | |
ActionButton.FORWARD, | |
false, | |
this.danceFunction | |
) | |
Input.instance.subscribe( | |
'BUTTON_UP', | |
ActionButton.BACKWARD, | |
false, | |
this.danceFunction | |
) | |
Input.instance.subscribe( | |
'BUTTON_UP', | |
ActionButton.RIGHT, | |
false, | |
this.danceFunction | |
) | |
Input.instance.subscribe( | |
'BUTTON_UP', | |
ActionButton.LEFT, | |
false, | |
this.danceFunction | |
) | |
} | |
removeEvents() { | |
Input.instance.unsubscribe( | |
'BUTTON_UP', | |
ActionButton.FORWARD, | |
this.danceFunction | |
) | |
Input.instance.unsubscribe( | |
'BUTTON_UP', | |
ActionButton.BACKWARD, | |
this.danceFunction | |
) | |
Input.instance.unsubscribe( | |
'BUTTON_UP', | |
ActionButton.RIGHT, | |
this.danceFunction | |
) | |
Input.instance.unsubscribe( | |
'BUTTON_UP', | |
ActionButton.LEFT, | |
this.danceFunction | |
) | |
} | |
} | |
export class DanceSystemManager { | |
systemMap: Record<string, DanceSystem> = {} | |
areaMap: Record<string, Entity> = {} | |
createSystem ( | |
name: string, | |
transform: Transform, | |
emotes?: PredefinedEmote[] | |
) { | |
this.systemMap[name] = new DanceSystem(emotes) | |
this.areaMap[name] = new Entity(`danceArea-${name}`) | |
this.areaMap[name].addComponent(transform) | |
engine.addEntity(this.areaMap[name]) | |
// DEBUG | |
this.areaMap[name].addComponent(new BoxShape()) | |
this.areaMap[name].getComponent(BoxShape).withCollisions = false | |
const transparentMaterial = new Material() | |
// transparentMaterial.albedoColor = new Color4(1, 1, 1, 0.1) | |
transparentMaterial.albedoColor = new Color4(1, 1, 1, 0) | |
this.areaMap[name].addComponentOrReplace(transparentMaterial) | |
this._setTriggerComponent(name) | |
} | |
_setTriggerComponent(name: string) { | |
this.areaMap[name].addComponent( | |
new utils.TriggerComponent( | |
new utils.TriggerBoxShape( | |
new Vector3( | |
this.areaMap[name].getComponent(Transform).scale.x, | |
this.areaMap[name].getComponent(Transform).scale.y, | |
this.areaMap[name].getComponent(Transform).scale.z | |
), | |
new Vector3(0, 2.5, 0) | |
), { | |
enableDebug: false, | |
onCameraEnter: () => { | |
const danceSystem = this.systemMap[name] | |
engine.addSystem(danceSystem) | |
danceSystem.addEvents() | |
log(`DANCE SYSTEM ADDED - ${name}`, this.systemMap[name].emotes) | |
}, | |
onCameraExit: () => { | |
const danceSystem = this.systemMap[name] | |
engine.removeSystem(danceSystem) | |
danceSystem.removeEvents() | |
log(`DANCE SYSTEM REMOVED - ${name}`, this.systemMap[name].emotes) | |
} | |
} | |
) | |
) | |
} | |
setEmotes(name: string, emotes: PredefinedEmote[]) { | |
this.systemMap[name].emotes = emotes | |
} | |
} | |
export const danceSystemManager = new DanceSystemManager() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// rest of game.ts... | |
const test1DanceSystemTransform = new Transform({ | |
position: new Vector3(8, 0, 8), | |
scale: new Vector3(8, 5, 8), | |
}) | |
danceSystemManager.createSystem('test1', test1DanceSystemTransform, [ | |
PredefinedEmote.ROBOT, | |
]) |
@ThianHooi I made a new project and it was working with the same exact code, so unfortunately I didn’t find out what was the issue exactly.
Thanks for the reply!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@effektsvk Were you able to solve the issues of
triggerEmote
?