Psuedo code for movement

This commit is contained in:
2024-07-27 03:10:33 +02:00
parent 8a75b7ae70
commit 925bdb741e
2 changed files with 84 additions and 115 deletions

View File

@ -35,6 +35,7 @@ let pointerUpTimer: number | null = null
const DRAG_DELAY = 150
const MOVE_RESET_DELAY = 100
const CAMERA_SPEED = 4 // Adjust this value to change arrow key movement speed
function updateWaypoint(pointer: Phaser.Input.Pointer) {
const { x: px, y: py } = scene.cameras.main.getWorldPoint(pointer.x, pointer.y)
@ -94,11 +95,37 @@ function onPointerUp() {
}, MOVE_RESET_DELAY)
}
function handleKeyboardInput() {
const cursors = scene.input.keyboard?.createCursorKeys()
if (!cursors) return
if (cursors.left.isDown) {
cam.value.scrollX -= CAMERA_SPEED
}
if (cursors.right.isDown) {
cam.value.scrollX += CAMERA_SPEED
}
if (cursors.up.isDown) {
cam.value.scrollY -= CAMERA_SPEED
}
if (cursors.down.isDown) {
cam.value.scrollY += CAMERA_SPEED
}
if (cursors.left.isDown || cursors.right.isDown || cursors.up.isDown || cursors.down.isDown) {
gameStore.setMovingCamera(true)
} else {
gameStore.setMovingCamera(false)
}
}
function setupEventListeners() {
scene.input.on(Phaser.Input.Events.POINTER_MOVE, updateWaypoint)
scene.input.on(Phaser.Input.Events.POINTER_WHEEL, handleZoom)
scene.input.on(Phaser.Input.Events.POINTER_DOWN, onPointerDown)
scene.input.on(Phaser.Input.Events.POINTER_UP, onPointerUp)
scene.events.on(Phaser.Scenes.Events.UPDATE, handleKeyboardInput)
}
function cleanupEventListeners() {
@ -107,6 +134,7 @@ function cleanupEventListeners() {
scene.input.off(Phaser.Input.Events.POINTER_DOWN, onPointerDown)
scene.input.off(Phaser.Input.Events.POINTER_UP, onPointerUp)
scene.input.off(Phaser.Input.Events.POINTER_WHEEL, handleZoom)
scene.events.off(Phaser.Scenes.Events.UPDATE, handleKeyboardInput)
if (pointerDownTimer) {
clearTimeout(pointerDownTimer)
@ -118,8 +146,6 @@ function cleanupEventListeners() {
}
}
setupEventListeners()
watch(tool, (newTool) => {
if (newTool === 'move') {
scene.input.on(Phaser.Input.Events.POINTER_MOVE, dragZone)
@ -128,5 +154,6 @@ watch(tool, (newTool) => {
}
})
setupEventListeners()
onBeforeUnmount(cleanupEventListeners)
</script>
</script>