1
0
forked from noxious/client

Updated tiles logics

This commit is contained in:
Dennis Postma 2024-07-11 19:52:33 +02:00
parent 90046613ca
commit e72a3a9f45
9 changed files with 143 additions and 105 deletions

View File

@ -4,7 +4,8 @@
<span>{{ chip }}</span>
<i class="cursor-pointer text-white font-light font-default not-italic hover:text-gray-50" @click="deleteChip(i)">X</i>
</div>
<input class="outline-none border-none max-w-[250px] p-1 m-1 text-white" v-model="currentInput" @keyup.enter="saveChip" @keydown.delete="backspaceDelete" />
<!-- Use keypress event and prevent modifier -->
<input class="outline-none border-none max-w-[250px] p-1 m-1 text-white" v-model="currentInput" @keypress.enter.prevent="saveChip" @keydown.delete="backspaceDelete" />
</div>
</template>
@ -15,7 +16,9 @@ const modelValue = defineModel('modelValue', { type: Array, default: () => [] })
const currentInput = ref('')
const saveChip = () => {
const saveChip = (event) => {
// Prevent form submission explicitly if needed
event.preventDefault()
if (currentInput.value.trim() && !modelValue.value.includes(currentInput.value)) {
modelValue.value = [...modelValue.value, currentInput.value]
currentInput.value = ''

View File

@ -2,66 +2,61 @@
<div class="h-full overflow-auto">
<div class="relative p-2.5 flex flex-col items-center justify-between h-[300px]">
<div class="filler"></div>
<img class="max-h-[280px]" :src="tileImageUrl" :alt="'Tile ' + selectedTile" />
<button class="btn-bordeaux px-[15px] py-1.5 min-w-[100px]" type="button" @click="removeTile">Remove</button>
<img class="max-h-[280px]" :src="`${config.server_endpoint}/assets/tiles/${selectedTile?.id}.png`" :alt="'Tile ' + selectedTile?.id" />
<button class="btn-bordeaux px-[15px] py-1.5 min-w-[100px]" type="button" @click.prevent="removeTile">Remove</button>
<div class="absolute left-0 bottom-0 w-full h-[1px] bg-cyan-200"></div>
</div>
<div class="m-2.5 p-2.5 block">
<form class="flex g-2.5 flex-wrap" @submit.prevent>
<div class="flex flex-col mb-5">
<label class="mb-1.5 font-titles" for="tags">Tags</label>
<ChipsInput v-model="tags" @update:modelValue="handleTagsUpdate" />
<form class="flex gap-2.5 flex-wrap" @submit.prevent="saveTile">
<div class="w-full flex flex-col mb-5">
<label class="mb-1.5 font-titles" for="name">Name</label>
<input v-model="tileName" class="input-cyan" type="text" name="name" placeholder="Tile #1" />
</div>
<div class="w-full flex flex-col mb-5">
<label class="mb-1.5 font-titles" for="origin-x">Tags</label>
<ChipsInput v-model="tileTags" @update:modelValue="tileTags = $event" />
</div>
<button class="btn-cyan px-[15px] py-1.5 min-w-[100px]" type="submit">Save</button>
</form>
</div>
</div>
</template>
<script setup lang="ts">
import { ref, computed, watch, onBeforeUnmount, onMounted } from 'vue'
import type { Tile } from '@/types'
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
import { useAssetManagerStore } from '@/stores/assetManager'
import { useZoneEditorStore } from '@/stores/zoneEditor'
import { useSocketStore } from '@/stores/socket'
import ChipsInput from '@/components/forms/ChipsInput.vue'
import config from '@/config'
import ChipsInput from '@/components/forms/ChipsInput.vue'
const socket = useSocketStore()
const assetManagerStore = useAssetManagerStore()
const tags = ref<string[]>([])
const zoneEditorStore = useZoneEditorStore()
const selectedTile = computed(() => assetManagerStore.selectedTile)
const tileImageUrl = computed(() => `${config.server_endpoint}/assets/tiles/${selectedTile.value}.png`)
const tileName = ref('')
const tileTags = ref()
watch(selectedTile, fetchTileTags)
function fetchTileTags() {
if (!selectedTile.value) return
socket.connection.emit('gm:tile:tags', { selectedTile }, (response: string[]) => {
tags.value = response
})
if (!selectedTile.value) {
console.error('No tile selected')
}
function handleTagsUpdate(newTags: string[]) {
if (config.development) console.log(newTags)
saveTags(newTags)
if (selectedTile.value) {
tileName.value = selectedTile.value.name
tileTags.value = selectedTile.value.tags
}
function saveTags(tagsToSave: string[]) {
socket.connection.emit(
'gm:tile:tags:update',
{
tile: selectedTile.value,
tags: tagsToSave
},
(response: boolean) => {
if (!response) console.error('Failed to save tags')
}
)
}
watch(selectedTile, (tile: Tile | null) => {
if (!tile) return
tileName.value = tile.name
tileTags.value = tile.tags
})
function removeTile() {
socket.connection.emit('gm:tile:remove', { tile: selectedTile.value }, (response: boolean) => {
socket.connection.emit('gm:tile:remove', { tile: selectedTile.value?.id }, (response: boolean) => {
if (!response) {
console.error('Failed to remove tile')
return
@ -71,18 +66,45 @@ function removeTile() {
}
function refreshTileList() {
socket.connection.emit('gm:tile:list', {}, (response: string[]) => {
socket.connection.emit('gm:tile:list', {}, (response: Tile[]) => {
assetManagerStore.setTileList(response)
assetManagerStore.setSelectedTile('')
assetManagerStore.setSelectedTile(null)
if (zoneEditorStore.active) {
console.log('Refreshing tile list for zone store')
zoneEditorStore.setTileList(response)
}
})
}
function saveTile() {
if (!selectedTile.value) {
console.error('No tile selected')
return
}
socket.connection.emit(
'gm:tile:update',
{
id: selectedTile.value.id,
name: tileName.value,
tags: tileTags.value,
},
(response: boolean) => {
if (!response) {
console.error('Failed to save tile')
return
}
refreshTileList()
}
)
}
onMounted(() => {
if (!selectedTile.value) return
fetchTileTags(selectedTile.value)
})
onBeforeUnmount(() => {
assetManagerStore.setSelectedTile('')
assetManagerStore.setSelectedTile(null)
})
</script>

View File

@ -5,17 +5,17 @@
Upload tile(s)
</label>
<input v-model="searchQuery" class="input-cyan w-full" placeholder="Search..." @input="handleSearch" />
<div class="absolute left-0 bottom-0 w-full height-[1px] bg-cyan-200"></div>
<div class="absolute left-0 bottom-0 w-full h-[1px] bg-cyan-200"></div>
</div>
<div>
<a class="relative p-2.5 cursor-pointer flex gap-y-2.5 gap-x-5 flex-wrap w-full" :class="{ 'bg-cyan/80': assetManagerStore.selectedTile === tile }" v-for="(tile, index) in filteredTiles" :key="index" @click="assetManagerStore.setSelectedTile(tile)">
<div class="flex items-center gap-2.5">
<img class="h-[28px]" :src="`${config.server_endpoint}/assets/tiles/${tile}.png`" alt="Tile" />
<span class="">{{ tile }}</span>
<a class="relative p-2.5 cursor-pointer" :class="{ 'bg-cyan/80': assetManagerStore.selectedTile?.id === tile.id }" v-for="(tile, index) in filteredTiles" :key="index" @click="assetManagerStore.setSelectedTile(tile as Tile)">
<div class="flex items-center gap-2.5">
<div class="h-[28px] w-[75px] max-w-[75px] flex justify-center">
<img class="h-[28px]" :src="`${config.server_endpoint}/assets/tiles/${tile.id}.png`" alt="Tile" />
</div>
<div class="absolute left-0 bottom-0 w-full h-[1px] bg-cyan-200"></div>
</a>
</div>
<span>{{ tile.name }}</span>
</div>
<div class="absolute left-0 bottom-0 w-full h-[1px] bg-cyan-200"></div>
</a>
</template>
<script setup lang="ts">
@ -24,6 +24,7 @@ import { useSocketStore } from '@/stores/socket'
import { onMounted, ref, computed } from 'vue'
import { useAssetManagerStore } from '@/stores/assetManager'
import { useAssetStore } from '@/stores/assets'
import type { Tile } from '@/types'
const socket = useSocketStore()
const tileUploadField = ref(null)
@ -43,7 +44,7 @@ const handleFileUpload = (e: Event) => {
assetStore.fetchAssets()
socket.connection.emit('gm:tile:list', {}, (response: string[]) => {
socket.connection.emit('gm:tile:list', {}, (response: Tile[]) => {
assetManagerStore.setTileList(response)
})
})
@ -58,11 +59,11 @@ const filteredTiles = computed(() => {
if (!searchQuery.value) {
return assetManagerStore.tileList
}
return assetManagerStore.tileList.filter((tile) => tile.toLowerCase().includes(searchQuery.value.toLowerCase()))
return assetManagerStore.tileList.filter((tile) => tile.name.toLowerCase().includes(searchQuery.value.toLowerCase()))
})
onMounted(() => {
socket.connection.emit('gm:tile:list', {}, (response: string[]) => {
socket.connection.emit('gm:tile:list', {}, (response: Tile[]) => {
assetManagerStore.setTileList(response)
})
})

View File

@ -2,41 +2,39 @@
<Teleport to="body">
<Modal v-if="isModalOpen" @modal:close="() => zoneEditorStore.setTool('move')" :isModalOpen="true" :modal-width="645" :modal-height="260">
<template #modalHeader>
<h3 class="m-0 font-medium shrink-0">Tiles</h3>
<h3 class="text-lg">Tiles</h3>
<div class="flex">
<div class="w-full flex gap-1.5 flex-row">
<div>
<label class="mb-1.5 font-titles hidden" for="search">Search...</label>
<input @mousedown.stop class="input-cyan" type="text" name="search" placeholder="Search" />
<input
@mousedown.stop
class="input-cyan"
type="text"
name="search"
placeholder="Search"
v-model="searchQuery"
/>
</div>
</div>
</div>
</template>
<template #modalBody>
<div class="m-[15px]">
<div class="flex flex-wrap gap-2.5">
<img
class="w-[64px] h-[32px] cursor-pointer border-2 border-solid transition-all ease duration-300"
src="/assets/zone/blank_tile.png"
alt="Blank tile"
@click="zoneEditorStore.setSelectedTile('blank_tile')"
:class="{
'border-cyan shadow-lg scale-105': zoneEditorStore.selectedTile === 'blank_tile',
'border-transparent hover:border-gray-300': zoneEditorStore.selectedTile !== 'blank_tile'
}"
/>
<img
class="w-[64px] h-[32px] cursor-pointer border-2 border-solid transition-all ease duration-300"
v-for="(tile, index) in zoneEditorStore.tileList"
:key="index"
:src="`${config.server_endpoint}/assets/tiles/${tile}.png`"
alt="Tile"
@click="zoneEditorStore.setSelectedTile(tile)"
:class="{
'border-cyan shadow-lg scale-105': zoneEditorStore.selectedTile === tile,
'border-transparent hover:border-gray-300': zoneEditorStore.selectedTile !== tile
}"
/>
<div class="flex justify-between flex-wrap gap-2.5 items-center">
<div v-for="(tile, index) in filteredTiles" :key="index" class="max-w-[25%] inline-block">
<img
class="border-2 border-solid max-w-full"
:src="`${config.server_endpoint}/assets/tiles/${tile.id}.png`"
alt="Tile"
@click="zoneEditorStore.setSelectedTile(tile)"
:class="{
'cursor-pointer transition-all duration-300': true,
'border-cyan shadow-lg scale-105': zoneEditorStore.selectedTile?.id === tile.id,
'border-transparent hover:border-gray-300': zoneEditorStore.selectedTile?.id !== tile.id
}"
/>
</div>
</div>
</div>
</template>
@ -46,19 +44,30 @@
<script setup lang="ts">
import config from '@/config'
import { ref, onMounted } from 'vue'
import { ref, onMounted, computed, watch } from 'vue'
import { useZoneEditorStore } from '@/stores/zoneEditor'
import { useSocketStore } from '@/stores/socket'
import Modal from '@/components/utilities/Modal.vue'
import type { Tile } from '@/types'
const socket = useSocketStore()
const isModalOpen = ref(false)
const zoneEditorStore = useZoneEditorStore()
const searchQuery = ref('')
const filteredTiles = computed(() => {
if (!searchQuery.value) {
return zoneEditorStore.tileList
}
return zoneEditorStore.tileList.filter(tile =>
tile.name.toLowerCase().includes(searchQuery.value.toLowerCase())
)
})
onMounted(async () => {
isModalOpen.value = true
socket.connection.emit('gm:tile:list', {}, (response: string[]) => {
zoneEditorStore.tileList = response
socket.connection.emit('gm:tile:list', {}, (response: Tile[]) => {
zoneEditorStore.setTileList(response)
})
})
</script>

View File

@ -119,9 +119,9 @@ function eraser(tile: Phaser.Tilemaps.Tile) {
function pencil(tile: Phaser.Tilemaps.Tile) {
if (zoneEditorStore.drawMode === 'tile') {
if (!zoneEditorStore.selectedTile) return
placeTile(zoneTilemap, tiles, tile.x, tile.y, zoneEditorStore.selectedTile)
zoneTiles[tile.y][tile.x] = zoneEditorStore.selectedTile
if (zoneEditorStore.selectedTile === null) return
placeTile(zoneTilemap, tiles, tile.x, tile.y, zoneEditorStore.selectedTile.id)
zoneTiles[tile.y][tile.x] = zoneEditorStore.selectedTile.id
}
if (zoneEditorStore.drawMode === 'object') {
@ -161,9 +161,9 @@ function pencil(tile: Phaser.Tilemaps.Tile) {
}
function paint(tile: Phaser.Tilemaps.Tile) {
if (!zoneEditorStore.selectedTile) return
exampleTilesArray.forEach((row, y) => row.forEach((tile, x) => placeTile(zoneTilemap, tiles, x, y, zoneEditorStore.selectedTile)))
zoneTiles.forEach((row, y) => row.forEach((tile, x) => zoneTiles[y][x] = zoneEditorStore.selectedTile))
if (zoneEditorStore.selectedTile === null) return
exampleTilesArray.forEach((row, y) => row.forEach((tile, x) => placeTile(zoneTilemap, tiles, x, y, zoneEditorStore.selectedTile.id)))
zoneTiles.forEach((row, y) => row.forEach((tile, x) => zoneTiles[y][x] = zoneEditorStore.selectedTile.id))
}
function save() {

View File

@ -1,7 +1,7 @@
export default {
name: 'New Quest',
server_endpoint: 'https://nq-server.cr-a.directonline.io',
development: true,
server_endpoint: 'http://localhost:4000',
width: 960,
height: 540,
tile_size: { x: 64, y: 32, z: 1 },

View File

@ -1,15 +1,15 @@
import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { Object } from '@/types'
import type { Tile, Object } from '@/types'
export const useAssetManagerStore = defineStore('assetManager', () => {
const tileList = ref<string[]>([])
const selectedTile = ref<string | null>(null)
const tileList = ref<Tile[]>([])
const selectedTile = ref<Tile | null>(null)
const objectList = ref<Object[]>([])
const selectedObject = ref<Object | null>(null)
function setTileList(tiles: string[]) {
function setTileList(tiles: Tile[]) {
tileList.value = tiles
}
@ -17,7 +17,7 @@ export const useAssetManagerStore = defineStore('assetManager', () => {
objectList.value = objects
}
function setSelectedTile(tile: string) {
function setSelectedTile(tile: Tile | null) {
selectedTile.value = tile
}

View File

@ -1,5 +1,5 @@
import { defineStore } from 'pinia'
import type { Object, ZoneObject, Zone } from '@/types'
import type { Zone, Object, Tile } from '@/types'
export const useZoneEditorStore = defineStore('zoneEditor', {
state: () => ({
@ -7,9 +7,9 @@ export const useZoneEditorStore = defineStore('zoneEditor', {
zone: null as Zone | null,
tool: 'move',
drawMode: 'tile',
tileList: [] as string[],
tileList: [] as Tile[],
objectList: [] as Object[],
selectedTile: '',
selectedTile: null as Tile | null,
selectedObject: null as Object | null,
objectDepth: 0,
isZoneListModalShown: false,
@ -44,13 +44,13 @@ export const useZoneEditorStore = defineStore('zoneEditor', {
setDrawMode(mode: string) {
this.drawMode = mode
},
setTileList(tiles: string[]) {
setTileList(tiles: Tile[]) {
this.tileList = tiles
},
setObjectList(objects: Object[]) {
this.objectList = objects
},
setSelectedTile(tile: string) {
setSelectedTile(tile: Tile) {
this.selectedTile = tile
},
setSelectedObject(object: any) {
@ -74,7 +74,7 @@ export const useZoneEditorStore = defineStore('zoneEditor', {
this.objectList = []
this.tool = 'move'
this.drawMode = 'tile'
this.selectedTile = ''
this.selectedTile = null
this.selectedObject = null
this.objectDepth = 0
this.isSettingsModalShown = false

View File

@ -10,6 +10,14 @@ export type Asset = {
type: 'base64' | 'link'
}
export type Tile = {
id: string
name: string
tags: string[]
createdAt: Date
updatedAt: Date
}
export type Object = {
id: string
name: string
@ -65,11 +73,6 @@ export type CharacterItem = {
quantity: number
}
export type TileTag = {
tile: string
tags: any // Using 'any' for Json type, consider using a more specific type if possible
}
export type Zone = {
id: number
name: string