Skip to content

Commit

Permalink
fix block score update when game-over
Browse files Browse the repository at this point in the history
  • Loading branch information
da-in committed Apr 6, 2024
1 parent 9cb329d commit 14a9a96
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/hooks/use-player.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import {Body, Engine, Events, Render, Runner, World} from 'matter-js'
import {blocks} from '../pages/play/setting.ts'
import {createBlock, getBlockIndex, getDynamicCanvasHeight, setField} from '../utils'
import {onMounted, Ref, ref} from 'vue'
import {useTimer} from './use-timer.ts'
import { Body, Engine, Events, Render, Runner, World } from 'matter-js'
import { blocks } from '../pages/play/setting.ts'
import { createBlock, getBlockIndex, getDynamicCanvasHeight, setField } from '../utils'
import { onMounted, Ref, ref } from 'vue'
import { useTimer } from './use-timer.ts'

export const usePlayer = (
element: Ref<HTMLCanvasElement | undefined>,
) => {
const engine = Engine.create(
{
gravity: {x: 0, y: 0.4},
timing: {timeScale: 1.5}
gravity: { x: 0, y: 0.4 },
timing: { timeScale: 1.5 },
})
const runner = Runner.create()

Expand Down Expand Up @@ -50,8 +50,8 @@ export const usePlayer = (
background: 'transparent',
width,
height,
pixelRatio: window.devicePixelRatio
}
pixelRatio: window.devicePixelRatio,
},
})

setField(engine.world, width, height)
Expand All @@ -71,14 +71,17 @@ export const usePlayer = (
if (element?.value?.clientWidth && x > 0 && x < element.value.clientWidth) {
onDrag(x)
}
}, {passive: true})
}, { passive: true })

element.value.addEventListener('touchend', () => {
drop()
})
})

Events.on(engine, 'collisionStart', (event) => {
if (gameOverRef.value) {
return
}
event.pairs.forEach((collision) => {
if (collision.bodyA.label === 'line' || collision.bodyB.label === 'line') {
const circle = collision.bodyA.label === 'line' ? collision.bodyB.id : collision.bodyA.id
Expand Down Expand Up @@ -106,7 +109,7 @@ export const usePlayer = (
const newBlock = createBlock(index + 1, collision.collision.supports[0].x, collision.collision.supports[0].y, ratio)
World.remove(engine.world, [collision.bodyA, collision.bodyB])
World.add(engine.world, newBlock)
}
},
)
})

Expand All @@ -119,7 +122,7 @@ export const usePlayer = (
})
})

const {start, reset} = useTimer(3, () => {
const { start, reset } = useTimer(3, () => {
endGame()
})

Expand Down Expand Up @@ -175,7 +178,7 @@ export const usePlayer = (
if (!currentBlock || isSetBlock) {
return
}
Body.setPosition(currentBlock, {x, y: 60})
Body.setPosition(currentBlock, { x, y: 60 })
}

const endGame = () => {
Expand All @@ -199,5 +202,5 @@ export const usePlayer = (
addBlock()
}

return {replay, nextBlockRef, groundHeightRef, gameOverRef, scoreRef}
return { replay, nextBlockRef, groundHeightRef, gameOverRef, scoreRef }
}

0 comments on commit 14a9a96

Please sign in to comment.