1
0
forked from noxious/client

Typescript improvements, added move zone object logic.

This commit is contained in:
Dennis Postma 2024-10-18 02:33:51 +02:00
parent 390b9517e0
commit c6869f47b1
3 changed files with 77 additions and 40 deletions

View File

@ -1,5 +1,5 @@
<template> <template>
<SelectedZoneObject v-if="selectedZoneObject" :zoneObject="selectedZoneObject" /> <SelectedZoneObject v-if="selectedZoneObject" :zoneObject="selectedZoneObject" @move="moveZoneObject" @rotate="rotateZoneObject" @delete="deleteZoneObject" />
<Image <Image
v-for="object in zoneEditorStore.zone?.zoneObjects" v-for="object in zoneEditorStore.zone?.zoneObjects"
v-bind="getObjectImageProps(object)" v-bind="getObjectImageProps(object)"
@ -19,6 +19,7 @@ import { onBeforeMount, onBeforeUnmount, ref, watch } from 'vue'
const scene = useScene() const scene = useScene()
const zoneEditorStore = useZoneEditorStore() const zoneEditorStore = useZoneEditorStore()
const selectedZoneObject = ref<ZoneObject | null>(null) const selectedZoneObject = ref<ZoneObject | null>(null)
const movingZoneObject = ref<ZoneObject | null>(null)
const props = defineProps<{ const props = defineProps<{
tilemap: Phaser.Tilemaps.Tilemap tilemap: Phaser.Tilemaps.Tilemap
@ -26,11 +27,11 @@ const props = defineProps<{
function getObjectImageProps(object: ZoneObject) { function getObjectImageProps(object: ZoneObject) {
return { return {
// alpha: object.id === movingZoneObject.value?.id ? .5 : 1, alpha: object.id === movingZoneObject.value?.id ? .5 : 1,
depth: calculateIsometricDepth(object.positionX, object.positionY, object.object.frameWidth, object.object.frameHeight), depth: calculateIsometricDepth(object.positionX, object.positionY, object.object.frameWidth, object.object.frameHeight),
tint: selectedZoneObject.value?.id === object.id ? 0x00ff00 : 0xffffff, tint: selectedZoneObject.value?.id === object.id ? 0x00ff00 : 0xffffff,
x: tileToWorldX(props.tilemap as any, object.positionX, object.positionY), x: tileToWorldX(props.tilemap, object.positionX, object.positionY),
y: tileToWorldY(props.tilemap as any, object.positionX, object.positionY), y: tileToWorldY(props.tilemap, object.positionX, object.positionY),
flipX: object.isRotated, flipX: object.isRotated,
texture: object.object.id, texture: object.object.id,
originY: Number(object.object.originX), originY: Number(object.object.originX),
@ -76,6 +77,49 @@ function addZoneObject(pointer: Phaser.Input.Pointer) {
zoneEditorStore.zone.zoneObjects = zoneEditorStore.zone.zoneObjects.concat(newObject as ZoneObject) zoneEditorStore.zone.zoneObjects = zoneEditorStore.zone.zoneObjects.concat(newObject as ZoneObject)
} }
function moveZoneObject(id: string) {
if (!zoneEditorStore.zone) return
movingZoneObject.value = zoneEditorStore.zone.zoneObjects.find((object) => object.id === id) as ZoneObject
function handlePointerMove(pointer: Phaser.Input.Pointer) {
if (!movingZoneObject.value) return
const tile = getTile(props.tilemap, pointer.worldX, pointer.worldY)
if (!tile) return
movingZoneObject.value.positionX = tile.x
movingZoneObject.value.positionY = tile.y
}
scene.input.on(Phaser.Input.Events.POINTER_MOVE, handlePointerMove)
function handlePointerUp() {
scene.input.off(Phaser.Input.Events.POINTER_MOVE, handlePointerMove)
movingZoneObject.value = null
}
scene.input.on(Phaser.Input.Events.POINTER_UP, handlePointerUp)
}
function rotateZoneObject(id: string) {
if (!zoneEditorStore.zone) return
zoneEditorStore.zone.zoneObjects = zoneEditorStore.zone.zoneObjects.map((object) => {
if (object.id === id) {
return {
...object,
isRotated: !object.isRotated
}
}
return object
})
}
function deleteZoneObject(id: string) {
if (!zoneEditorStore.zone) return
zoneEditorStore.zone.zoneObjects = zoneEditorStore.zone.zoneObjects.filter((object) => object.id !== id)
selectedZoneObject.value = null
}
onBeforeMount(() => { onBeforeMount(() => {
scene.input.on(Phaser.Input.Events.POINTER_DOWN, addZoneObject) scene.input.on(Phaser.Input.Events.POINTER_DOWN, addZoneObject)
scene.input.on(Phaser.Input.Events.POINTER_MOVE, addZoneObject) scene.input.on(Phaser.Input.Events.POINTER_MOVE, addZoneObject)
@ -88,14 +132,14 @@ onBeforeUnmount(() => {
// watch zoneEditorStore.objectList and update originX and originY of objects in zoneObjects // watch zoneEditorStore.objectList and update originX and originY of objects in zoneObjects
watch( watch(
zoneEditorStore.objectList, () => zoneEditorStore.objectList,
(newObjects) => { (newObjects) => {
// Check if zoneEditorStore.zone is set
if (!zoneEditorStore.zone) return if (!zoneEditorStore.zone) return
// Update zoneObjects console.log('Updating zone objects')
zoneEditorStore.zone.zoneObjects = zoneEditorStore.zone.zoneObjects.map((zoneObject) => {
const updatedObject = newObjects.find((obj) => obj.id === zoneObject.objectId) const updatedZoneObjects = zoneEditorStore.zone.zoneObjects.map((zoneObject) => {
const updatedObject = newObjects.find((obj) => obj.id === zoneObject.object.id)
if (updatedObject) { if (updatedObject) {
return { return {
...zoneObject, ...zoneObject,
@ -109,6 +153,12 @@ watch(
return zoneObject return zoneObject
}) })
// Update the zone with the new zoneObjects
zoneEditorStore.setZone({
...zoneEditorStore.zone,
zoneObjects: updatedZoneObjects
})
// Update selectedObject if it's set // Update selectedObject if it's set
if (zoneEditorStore.selectedObject) { if (zoneEditorStore.selectedObject) {
const updatedObject = newObjects.find((obj) => obj.id === zoneEditorStore.selectedObject?.id) const updatedObject = newObjects.find((obj) => obj.id === zoneEditorStore.selectedObject?.id)

View File

@ -1,10 +1,6 @@
<template> <template>
<div class="flex flex-col items-center py-5 px-3 fixed bottom-14 right-0"> <div class="flex flex-col items-center py-5 px-3 fixed bottom-14 right-0">
<div class="self-end mt-2 flex gap-2"> <div class="self-end mt-2 flex gap-2">
<div>
<label class="mb-1.5 font-titles block text-sm text-gray-700 hidden" for="depth">Depth</label>
<input v-model="objectDepth" @mousedown.stop @input="handleDepthInput" class="input-field max-w-24 px-2 py-1 border rounded" type="number" name="depth" placeholder="Depth" />
</div>
<button @mousedown.stop @click="handleDelete" class="btn-red py-1.5 px-4"> <button @mousedown.stop @click="handleDelete" class="btn-red py-1.5 px-4">
<img src="/assets/icons/trashcan.svg" class="w-4 h-4" alt="Delete" /> <img src="/assets/icons/trashcan.svg" class="w-4 h-4" alt="Delete" />
</button> </button>
@ -15,38 +11,23 @@
</template> </template>
<script setup lang="ts"> <script setup lang="ts">
import { useZoneEditorStore } from '@/stores/zoneEditorStore' import type { ZoneObject } from '@/types'
import { ref, computed, watch } from 'vue'
const emit = defineEmits(['update_depth', 'move', 'delete', 'rotate']) const props = defineProps<{
const zoneEditorStore = useZoneEditorStore() zoneObject: ZoneObject
}>()
const objectDepth = ref(zoneEditorStore.objectDepth) const emit = defineEmits(['move', 'rotate', 'delete'])
watch( const handleMove = () => {
() => zoneEditorStore.selectedZoneObject, emit('move', props.zoneObject.id)
(selectedZoneObject) => {
objectDepth.value = selectedZoneObject?.depth ?? 0
}
)
const handleDepthInput = () => {
const depth = parseFloat(objectDepth.value.toString())
if (!isNaN(depth)) {
emit('update_depth', depth)
}
} }
const handleRotate = () => { const handleRotate = () => {
emit('rotate', zoneEditorStore.selectedZoneObject?.id) emit('rotate', props.zoneObject.id)
}
const handleMove = () => {
emit('move', zoneEditorStore.selectedZoneObject?.id)
} }
const handleDelete = () => { const handleDelete = () => {
emit('delete', zoneEditorStore.selectedZoneObject?.id) emit('delete', props.zoneObject.id)
zoneEditorStore.setSelectedZoneObject(null)
} }
</script> </script>

View File

@ -11,21 +11,27 @@ export function getTile(layer: TilemapLayer | Tilemap, x: number, y: number): Ti
return tile return tile
} }
export function tileToWorldXY(layer: TilemapLayer, pos_x: number, pos_y: number) { export function tileToWorldXY(layer: TilemapLayer | Tilemap, pos_x: number, pos_y: number) {
const worldPoint = layer.tileToWorldXY(pos_x, pos_y) const worldPoint = layer.tileToWorldXY(pos_x, pos_y)
if (!worldPoint) return { positionX: 0, positionY: 0 }
const positionX = worldPoint.x + config.tile_size.y const positionX = worldPoint.x + config.tile_size.y
const positionY = worldPoint.y const positionY = worldPoint.y
return { positionX, positionY } return { positionX, positionY }
} }
export function tileToWorldX(layer: TilemapLayer, pos_x: number, pos_y: number): number { export function tileToWorldX(layer: TilemapLayer | Tilemap, pos_x: number, pos_y: number): number {
const worldPoint = layer.tileToWorldXY(pos_x, pos_y) const worldPoint = layer.tileToWorldXY(pos_x, pos_y)
if (!worldPoint) return 0
return worldPoint.x + config.tile_size.x / 2 return worldPoint.x + config.tile_size.x / 2
} }
export function tileToWorldY(layer: TilemapLayer, pos_x: number, pos_y: number): number { export function tileToWorldY(layer: TilemapLayer | Tilemap, pos_x: number, pos_y: number): number {
const worldPoint = layer.tileToWorldXY(pos_x, pos_y) const worldPoint = layer.tileToWorldXY(pos_x, pos_y)
if (!worldPoint) return 0
return worldPoint.y + config.tile_size.y * 1.5 return worldPoint.y + config.tile_size.y * 1.5
} }