Skip to content

Commit

Permalink
feat: add ability to update same ui from diff code
Browse files Browse the repository at this point in the history
add id based sticky message so same ui element
can be changed from different parts of code
  • Loading branch information
Stan-Stani committed Nov 19, 2023
1 parent 5e9618f commit 470ec28
Showing 1 changed file with 18 additions and 18 deletions.
36 changes: 18 additions & 18 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,28 +143,32 @@ class GameScene extends Scene {
spaceBar.on('down', () => {
if (this.#playerOne?.body?.blocked.down) {
this.#playerOne?.setVelocityY(-100)
toastMessage('space')
}
})

right
.on('down', () => {
this.#playerOne?.setAccelerationX(30)
this.isRunning = true

})
.on('up', () => {
this.#playerOne?.setAccelerationX(0)
this.isRunning = false
})

left
.on('down', () => {
this.#playerOne?.setAccelerationX(-30)
this.isRunning = true

stickyMessage({_id: 'left'}, 'left: down')
})
.on('up', () => {
this.#playerOne?.setAccelerationX(0)
this.isRunning = false

stickyMessage({_id: 'left'}, 'left: up')
})
} catch (e: any) {
toastMessage(e.message)
Expand Down Expand Up @@ -279,15 +283,26 @@ class GameScene extends Scene {
function stickyMessage(...messages: any) {
// console.log(message)
const prettyMessages: string[] = []
let identifier;


identifier = getStackIdentifier()
for (const message of messages) {
if (message.hasOwnProperty('_id')) {
identifier = message._id
continue;
}

prettyMessages.push(
typeof message === 'string' || typeof message === 'number'
? String(message)
: JSON.stringify(message)
)

}

let identifier = getStackIdentifier()


if (!stackToDivMap[identifier]) {
console.log('s')
// Create a new div for this identifier
Expand Down Expand Up @@ -343,21 +358,6 @@ function getStackIdentifier() {
return ''
}

function createOrUpdateDivByStack(message: string) {
let identifier = getStackIdentifier()

if (!stackToDivMap[identifier]) {
// Create a new div for this identifier
let newDiv = document.createElement('div')
newDiv.textContent = message
document.body.appendChild(newDiv)
stackToDivMap[identifier] = newDiv
} else {
// Update the existing div
stackToDivMap[identifier].textContent = message
}
}

const config: Phaser.Types.Core.GameConfig = {
type: WEBGL,
width: WIDTH,
Expand Down

0 comments on commit 470ec28

Please sign in to comment.