I am running into what I think is a bug right now. I am adding in the UI for starting the game and mobile phone controls. I have a custom component on multiple entities that I am using to move the terrain and Items on Tick. When I start the game I send an event out that should change the state of the component on both entities so that movement starts but it is currently only working on one at a time. Here are recordings from my phone, Desktop and the simulator. None of them have both working. Here is also the code I am using. Essentially on click i am using this:
world.events.addListener(schema.startButton,
ecs.input.SCREEN_TOUCH_START, () => {
console.log('click')
world.events.dispatch(eid, 'Start Game')
})
and then this is the Movement component that is on the object container and the terrain container. both of those are parents to the entities I want to move.
// This is a component file. You can use this file to define a custom component for your project.
// This component will appear as a custom component in the editor.
import * as ecs from '@8thwall/ecs' // This is how you access the ecs library.
ecs.registerComponent({
name: 'MoveEntities',
schema: {
// Add data that can be configured on the component.
objectMoveSpeed: ecs.f32, // this is the move speed of the player going forward
endingSpot: ecs.f32, // this is the z position when the object gets to the end move spot
},
schemaDefaults: {
// Add defaults for the schema fields.
objectMoveSpeed: 10,
endingSpot: -80,
},
data: {
// Add data that cannot be configured outside of the component.
gameActive: ecs.boolean,
actualForwardMoveSpeed: ecs.f32, // So users dont have to input a decimal for move speed
},
stateMachine: ({world, eid, schemaAttribute, dataAttribute}) => {
const schema = schemaAttribute.cursor(eid)
const data = dataAttribute.cursor(eid)
ecs.defineState('preGame').initial()
.onEvent('Start Game', 'inGame', {target: world.events.globalId})
ecs.defineState('inGame')
.onEvent('Game Over', 'gameOver', {target: world.events.globalId})
.onEnter(() => {
data.gameActive = true
})
.onExit(() => {
})
ecs.defineState('gameOver')
.onEvent('Restart Game', 'preGame', {target: world.events.globalId})
.onEnter(() => {
data.gameActive = false
})
},
add: (world, component) => {
// Runs when the component is added to the world.
const {eid, dataAttribute, schemaAttribute} = component
component.dataAttribute.cursor(component.eid).gameActive = false
dataAttribute.cursor(eid).actualForwardMoveSpeed =
schemaAttribute.cursor(eid).objectMoveSpeed / 1000
},
tick: (world, component) => {
// Runs every frame.
const {eid, dataAttribute, schemaAttribute} = component
const data = dataAttribute.cursor(eid)
const schema = schemaAttribute.cursor(eid)
const {delta} = world.time
if (data.gameActive) {
[...world.getChildren(eid)].forEach((element) => {
const objectPosition = ecs.Position.get(world, element)
const newX = objectPosition.x
const newY = objectPosition.y
let newZ = objectPosition.z
newZ = objectPosition.z - (data.actualForwardMoveSpeed * delta)
world.setPosition(element, newX, newY, newZ)
if (newZ < schema.endingSpot) {
world.events.dispatch(element, 'end')
}
})
}
},
})
Here are also video of the code running on my mobile device, desktop, and in the simulator: