1
0
forked from noxious/client

Refractor character component (WIP)

This commit is contained in:
Dennis Postma 2024-07-22 00:40:47 +02:00
parent 016b8e082f
commit 43f0b73309
4 changed files with 8 additions and 51 deletions

View File

@ -21,61 +21,20 @@
</template>
<script lang="ts" setup>
import { Container, Rectangle, Sprite, Text, useScene } from 'phavuer'
import { onBeforeMount, onBeforeUnmount, onMounted, ref } from 'vue'
import { useGameStore } from '@/stores/game'
import { Container, Rectangle, Sprite, Text } from 'phavuer'
import { type Character as CharacterT } from '@/types'
import { getTile, tileToWorldX, tileToWorldXY, tileToWorldY } from '@/services/zone'
import { tileToWorldX, tileToWorldY } from '@/services/zone'
const gameStore = useGameStore()
interface Props {
layer: Phaser.Tilemaps.TilemapLayer
character?: CharacterT
}
const props = defineProps({
layer: Phaser.Tilemaps.TilemapLayer,
character: Object as () => CharacterT
const props = withDefaults(defineProps<Props>(), {
character: undefined
})
const scene = useScene()
const isSelf = props.character?.id === gameStore.character?.id
const createText = (text: Phaser.GameObjects.Text) => {
text.setLetterSpacing(1.5)
}
onBeforeMount(() => {
if (isSelf) setupSelf()
})
function setupSelf() {
scene.input.on(Phaser.Input.Events.POINTER_UP, onPointerClick)
}
function onPointerClick(pointer: Phaser.Input.Pointer) {
if (!isSelf || gameStore.isMovingCamera) return
const px = scene.cameras.main.worldView.x + pointer.x
const py = scene.cameras.main.worldView.y + pointer.y
const pointer_tile = getTile(px, py, props.layer) as Phaser.Tilemaps.Tile
if (!pointer_tile) {
return
}
gameStore.connection?.emit('character:move', { position_x: pointer_tile.x, position_y: pointer_tile.y })
// Directions for player sprite + animations
if (px < 0 && py > 0) {
console.log('down left')
} else if (px < 0 && py < 0) {
console.log('top left')
} else if (px > 0 && py > 0) {
console.log('down right')
} else if (px > 0 && py < 0) {
console.log('top right')
}
}
onBeforeUnmount(() => {
if (!isSelf) return
scene.input.off(Phaser.Input.Events.POINTER_UP, onPointerClick)
})
</script>

View File

@ -1 +0,0 @@
<template></template>

View File

@ -1 +0,0 @@
<template></template>