#159 : Camera control fixes

This commit is contained in:
Dennis Postma 2024-09-28 00:21:24 +02:00
parent 71042881dc
commit 0f231e10fa
4 changed files with 26 additions and 43 deletions

View File

@ -11,9 +11,7 @@ export function useGamePointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilema
const pointerTile = getTile(px, py, layer) const pointerTile = getTile(px, py, layer)
waypoint.value.visible = !!pointerTile waypoint.value.visible = !!pointerTile
if (!pointerTile) { if (!pointerTile) return
return
}
const worldPoint = tileToWorldXY(layer, pointerTile.x, pointerTile.y) const worldPoint = tileToWorldXY(layer, pointerTile.x, pointerTile.y)
waypoint.value.x = worldPoint.positionX waypoint.value.x = worldPoint.positionX
@ -21,9 +19,7 @@ export function useGamePointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilema
} }
function dragZone(pointer: Phaser.Input.Pointer) { function dragZone(pointer: Phaser.Input.Pointer) {
if (!gameStore.isPlayerDraggingCamera) { if (!gameStore.isPlayerDraggingCamera) return
return
}
const { x, y, prevPosition } = pointer const { x, y, prevPosition } = pointer
const { scrollX, scrollY, zoom } = camera const { scrollX, scrollY, zoom } = camera
@ -39,9 +35,7 @@ export function useGamePointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilema
const { x: px, y: py } = camera.getWorldPoint(pointer.x, pointer.y) const { x: px, y: py } = camera.getWorldPoint(pointer.x, pointer.y)
const pointerTile = getTile(px, py, layer) const pointerTile = getTile(px, py, layer)
if (!pointerTile) { if (!pointerTile) return
return
}
gameStore.connection?.emit('character:initMove', { gameStore.connection?.emit('character:initMove', {
positionX: pointerTile.x, positionX: pointerTile.x,
@ -49,13 +43,11 @@ export function useGamePointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilema
}) })
} }
function handleZoom({ event, deltaY }: Phaser.Input.Pointer) { function handleZoom(pointer: Phaser.Input.Pointer) {
if (event instanceof WheelEvent && event.shiftKey) { if (!(pointer.event instanceof WheelEvent) || !pointer.event.shiftKey) return
return
}
scene.scale.setZoom(scene.scale.zoom - deltaY * 0.01) const deltaY = pointer.event.deltaY
camera = scene.cameras.main scene.scale.setZoom(scene.scale.zoom - deltaY * 0.005)
} }
const setupPointerHandlers = () => { const setupPointerHandlers = () => {

View File

@ -1,4 +1,4 @@
import { computed, type Ref, ref } from 'vue' import { computed, type Ref } from 'vue'
import { getTile, tileToWorldXY } from '@/composables/zoneComposable' import { getTile, tileToWorldXY } from '@/composables/zoneComposable'
import { useZoneEditorStore } from '@/stores/zoneEditorStore' import { useZoneEditorStore } from '@/stores/zoneEditorStore'
import config from '@/config' import config from '@/config'
@ -14,9 +14,7 @@ export function useZoneEditorPointerHandlers(scene: Phaser.Scene, layer: Phaser.
const pointerTile = getTile(px, py, layer) const pointerTile = getTile(px, py, layer)
waypoint.value.visible = !!pointerTile waypoint.value.visible = !!pointerTile
if (!pointerTile) { if (!pointerTile) return
return
}
const worldPoint = tileToWorldXY(layer, pointerTile.x, pointerTile.y) const worldPoint = tileToWorldXY(layer, pointerTile.x, pointerTile.y)
waypoint.value.x = worldPoint.positionX waypoint.value.x = worldPoint.positionX
@ -24,11 +22,11 @@ export function useZoneEditorPointerHandlers(scene: Phaser.Scene, layer: Phaser.
} }
function dragZone(pointer: Phaser.Input.Pointer) { function dragZone(pointer: Phaser.Input.Pointer) {
if (gameStore.isPlayerDraggingCamera) { if (!gameStore.isPlayerDraggingCamera) return
const { x, y, prevPosition } = pointer
const { scrollX, scrollY, zoom } = camera const { x, y, prevPosition } = pointer
camera.setScroll(scrollX - (x - prevPosition.x) / zoom, scrollY - (y - prevPosition.y) / zoom) const { scrollX, scrollY, zoom } = camera
} camera.setScroll(scrollX - (x - prevPosition.x) / zoom, scrollY - (y - prevPosition.y) / zoom)
} }
function handlePointerMove(pointer: Phaser.Input.Pointer) { function handlePointerMove(pointer: Phaser.Input.Pointer) {
@ -38,13 +36,11 @@ export function useZoneEditorPointerHandlers(scene: Phaser.Scene, layer: Phaser.
updateWaypoint(pointer) updateWaypoint(pointer)
} }
function handleZoom({ event, deltaY }: Phaser.Input.Pointer) { function handleZoom(pointer: Phaser.Input.Pointer) {
if (event! instanceof WheelEvent && !event.shiftKey) { if (!(pointer.event instanceof WheelEvent) || pointer.event.shiftKey) return
return
}
scene.scale.setZoom(scene.scale.zoom - deltaY * 0.01) const deltaY = pointer.event.deltaY
camera = scene.cameras.main scene.scale.setZoom(scene.scale.zoom - deltaY * 0.005)
} }
const setupPointerHandlers = () => { const setupPointerHandlers = () => {

View File

@ -1,5 +1,4 @@
import { useGameStore } from '@/stores/gameStore' import { useGameStore } from '@/stores/gameStore'
import { useScene } from 'phavuer'
import { watch } from 'vue' import { watch } from 'vue'
import { useZoneStore } from '@/stores/zoneStore' import { useZoneStore } from '@/stores/zoneStore'
@ -9,9 +8,7 @@ export function useCameraControls(scene: Phaser.Scene): any {
const camera = scene.cameras.main const camera = scene.cameras.main
function onPointerDown(pointer: Phaser.Input.Pointer) { function onPointerDown(pointer: Phaser.Input.Pointer) {
if (pointer.event instanceof MouseEvent || pointer.event.shiftKey) { gameStore.setPlayerDraggingCamera(true)
gameStore.setPlayerDraggingCamera(true)
}
} }
function onPointerUp() { function onPointerUp() {
@ -21,9 +18,8 @@ export function useCameraControls(scene: Phaser.Scene): any {
watch( watch(
() => zoneStore.characterLoaded, () => zoneStore.characterLoaded,
(characterLoaded) => { (characterLoaded) => {
if(characterLoaded) { if(!characterLoaded) return;
scene.cameras.main.startFollow(scene.children.getByName(gameStore.character!.name) as Phaser.GameObjects.Container); // scene.cameras.main.startFollow(scene.children.getByName(gameStore.character!.name) as Phaser.GameObjects.Container);
}
} }
) )

View File

@ -4,12 +4,11 @@ import { useGamePointerHandlers } from '@/composables/pointerHandlers/useGamePoi
import { useZoneEditorPointerHandlers } from '@/composables/pointerHandlers/useZoneEditorPointerHandlers' import { useZoneEditorPointerHandlers } from '@/composables/pointerHandlers/useZoneEditorPointerHandlers'
import { useGameStore } from '@/stores/gameStore' import { useGameStore } from '@/stores/gameStore'
export function usePointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilemaps.TilemapLayer, waypoint: Ref<{ visible: boolean; x: number; y: number }>, camera: Ref<Phaser.Cameras.Scene2D.Camera>, isDragging: Ref<boolean>) { export function usePointerHandlers(scene: Phaser.Scene, layer: Phaser.Tilemaps.TilemapLayer, waypoint: Ref<{ visible: boolean; x: number; y: number }>, camera: Phaser.Cameras.Scene2D.Camera) {
const gameStore = useGameStore()
const zoneEditorStore = useZoneEditorStore() const zoneEditorStore = useZoneEditorStore()
const gameHandlers = useGamePointerHandlers(scene, layer, waypoint, camera, isDragging) const gameHandlers = useGamePointerHandlers(scene, layer, waypoint, camera)
const zoneEditorHandlers = useZoneEditorPointerHandlers(scene, layer, waypoint, camera, isDragging) const zoneEditorHandlers = useZoneEditorPointerHandlers(scene, layer, waypoint, camera)
const currentHandlers = computed(() => (zoneEditorStore.active ? zoneEditorHandlers : gameHandlers)) const currentHandlers = computed(() => (zoneEditorStore.active ? zoneEditorHandlers : gameHandlers))