Fix for object depths and proper depths for rotated objects

This commit is contained in:
Andrei Bornstein 2025-03-20 15:17:34 -05:00
parent 3f28d85c30
commit 2881d5f251
4 changed files with 29 additions and 9 deletions

View File

@ -12,7 +12,7 @@ import { computed, defineProps, onMounted, onUnmounted } from 'vue'
const mapEditor = useMapEditorComposable() const mapEditor = useMapEditorComposable()
const partitionPoints = [0] const partitionPoints = [0]
const baseDepth = computed(() => calculateIsometricDepth(props.x!, props.y!)) let baseDepth = 0
const props = defineProps<{ const props = defineProps<{
obj?: PlacedMapObject obj?: PlacedMapObject
@ -31,7 +31,7 @@ const createImagePartition = (startX: number, endX: number, depthOffset: number)
img.setOrigin(props.mapObj.originX, props.mapObj.originY) img.setOrigin(props.mapObj.originX, props.mapObj.originY)
img.setCrop(startX, 0, endX, props.mapObj?.frameHeight) img.setCrop(startX, 0, endX, props.mapObj?.frameHeight)
img.setDepth(baseDepth.value + depthOffset) img.setDepth(baseDepth + depthOffset)
group.add(img) group.add(img)
} }
@ -45,8 +45,11 @@ onPreUpdate(() => {
const isSelected = mapEditor.selectedMapObject.value?.id === props.obj.id const isSelected = mapEditor.selectedMapObject.value?.id === props.obj.id
const isPlacedSelected = mapEditor.selectedPlacedObject.value?.id === props.obj.id const isPlacedSelected = mapEditor.selectedPlacedObject.value?.id === props.obj.id
baseDepth = calculateIsometricDepth(props.obj!.positionX, props.obj!.positionY)
group.setAlpha(isMoving || isSelected ? 0.5 : 1) group.setAlpha(isMoving || isSelected ? 0.5 : 1)
group.setTint(isPlacedSelected ? 0x00ff00 : 0xffffff) group.setTint(isPlacedSelected ? 0x00ff00 : 0xffffff)
group.setDepth(baseDepth)
let orderedImages = group.getChildren() let orderedImages = group.getChildren()
@ -54,13 +57,25 @@ onPreUpdate(() => {
const image = child as Phaser.GameObjects.Image const image = child as Phaser.GameObjects.Image
if (image && props.obj) { if (image && props.obj) {
image.flipX = props.obj.isRotated; image.flipX = props.obj.isRotated;
if (props.obj.isRotated) { if (props.obj.isRotated) {
image.setDepth(baseDepth.value + props.mapObj!.depthOffsets.reverse()[index]) var offsetNum = props.mapObj!.depthOffsets.length
//Could break in case of images with odd partion number
//Shifts image over so that it becomes aligned again after flipping
if (index < offsetNum/2) {
image.x = props.x! - (props.mapObj!.frameWidth/offsetNum)
}
else {
image.x = props.x! + (props.mapObj!.frameWidth/offsetNum)
}
image.setDepth(baseDepth - props.mapObj!.depthOffsets[index])
} }
else { else {
image.setDepth(baseDepth.value + props.mapObj!.depthOffsets[index]) image.x = props.x!
image.setDepth(baseDepth + props.mapObj!.depthOffsets[index])
} }
image.setCrop()
} }
}) })
}) })
@ -68,6 +83,8 @@ onPreUpdate(() => {
// Initial setup // Initial setup
if (props.mapObj && props.x && props.y) { if (props.mapObj && props.x && props.y) {
baseDepth = calculateIsometricDepth(props.obj!.positionX, props.obj!.positionY)
group.setXY(props.x, props.y) group.setXY(props.x, props.y)
group.setOrigin(props.mapObj.originX, props.mapObj.originY) group.setOrigin(props.mapObj.originX, props.mapObj.originY)

View File

@ -114,7 +114,7 @@ if (selectedMapObject.value) {
mapObjectFrameHeight.value = selectedMapObject.value.frameHeight mapObjectFrameHeight.value = selectedMapObject.value.frameHeight
} }
const setPartitionDepth = (event: any, idx: number) => mapObjectDepthOffsets.value[idx] = event.target.value const setPartitionDepth = (event: any, idx: number) => mapObjectDepthOffsets.value[idx] = Number.parseInt(event.target.value)
async function removeObject() { async function removeObject() {
if (!selectedMapObject.value) return if (!selectedMapObject.value) return

View File

@ -179,7 +179,6 @@ function handlePointerMove(pointer: Phaser.Input.Pointer) {
if (mapEditor.inputMode.value === 'hold' && pointer.isDown) { if (mapEditor.inputMode.value === 'hold' && pointer.isDown) {
handlePointerDown(pointer) handlePointerDown(pointer)
} }
scene.children.depthSort()
} }
function handlePointerUp(pointer: Phaser.Input.Pointer) { function handlePointerUp(pointer: Phaser.Input.Pointer) {
@ -236,6 +235,10 @@ onUnmounted(() => {
mapEditor.reset() mapEditor.reset()
}) })
setInterval( () => {
scene.children.queueDepthSort()
}, 0.2)
onBeforeUnmount(() => { onBeforeUnmount(() => {
removeEventListener('keydown', handleKeyDown) removeEventListener('keydown', handleKeyDown)
}) })

View File

@ -62,8 +62,8 @@ export function createTileArray(width: number, height: number, tile: string = 'b
return Array.from({ length: height }, () => Array.from({ length: width }, () => tile)) return Array.from({ length: height }, () => Array.from({ length: width }, () => tile))
} }
export const calculateIsometricDepth = (positionX: number, positionY: number, pivotPoints: { x: number; y: number }[] = []) => { export const calculateIsometricDepth = (positionX: number, positionY: number) => {
return Math.max(positionX + positionY) return positionX + positionY
} }
async function loadTileTextures(tiles: TileT[], scene: Phaser.Scene) { async function loadTileTextures(tiles: TileT[], scene: Phaser.Scene) {