Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

step 2: handle players queue #20

Merged
merged 1 commit into from
Oct 2, 2024
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
4 changes: 2 additions & 2 deletions example-scene/src/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Color4 } from '@dcl/sdk/math'
import { getRandomHexColor } from './utils'

// Cube factory
export function createCube(x: number, y: number, z: number, spawner = true): Entity {
export function createCube(x: number, y: number, z: number, color?: Color4): Entity {
const entity = engine.addEntity()

// Used to track the cubes
Expand All @@ -25,7 +25,7 @@ export function createCube(x: number, y: number, z: number, spawner = true): Ent
// set how the cube looks and collides
MeshRenderer.setBox(entity)
MeshCollider.setBox(entity)
Material.setPbrMaterial(entity, { albedoColor: Color4.fromHexString(getRandomHexColor()) })
Material.setPbrMaterial(entity, { albedoColor: color ?? Color4.fromHexString(getRandomHexColor()) })

// Make the cube spin, with the circularSystem
Spinner.create(entity, { speed: 100 * Math.random() })
Expand Down
32 changes: 26 additions & 6 deletions example-scene/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
// We define the empty imports so the auto-complete feature works as expected.
import {} from '@dcl/sdk/math'
import { engine } from '@dcl/sdk/ecs'
import { Color4 } from '@dcl/sdk/math'
import { engine, pointerEventsSystem } from '@dcl/sdk/ecs'

import { changeColorSystem, circularSystem } from './systems'
import { setupUi } from './ui'

import { initLibrary } from '@dcl-sdk/mini-games/src'
import { initLibrary, queue } from '@dcl-sdk/mini-games/src'
import { syncEntity } from '@dcl/sdk/network'
import players from '@dcl/sdk/players'
import { createCube } from './factory'

// make sure to put this line outside the main function.
initLibrary(engine, syncEntity, players, {
Expand All @@ -20,6 +20,26 @@ export function main() {
engine.addSystem(circularSystem)
engine.addSystem(changeColorSystem)

// draw UI. Here is the logic to spawn cubes.
setupUi()
const playGameCube = createCube(2, 1, 2, Color4.Green())
const finishCube = createCube(2, 4, 2, Color4.Red())

// Add player to the qeue.
pointerEventsSystem.onPointerDown({ entity: playGameCube, opts: { hoverText: 'Start game' } }, () => {
queue.addPlayer()
console.log('Current queue', queue.getQueue())
})

// End Game.
pointerEventsSystem.onPointerDown({ entity: finishCube, opts: { hoverText: 'Finish game' } }, () => {
queue.setNextPlayer()
console.log('Current queue', queue.getQueue())
})

queue.listeners.onActivePlayerChange = (player) => {
console.log('active player changed', player)
console.log('Current queue', queue.getQueue())
// here you can set the logic to start the new game
// such as reset the old state, move the player inside the area, set a coutner to start the game, etc.
// game.startNewGame()
}
}
79 changes: 0 additions & 79 deletions example-scene/src/ui.tsx

This file was deleted.

Loading