forked from noxious/client
136 lines
5.2 KiB
Vue
136 lines
5.2 KiB
Vue
<template>
|
|
<TilemapLayerC :tilemap="tileTilemap" :tileset="exampleTilesArray" :layerIndex="0" :cull-padding-x="10" :cull-padding-y="10" />
|
|
|
|
<Controls :layer="exampleTiles" />
|
|
<!-- @TODO: inside asset manager we need to be able to set the originX and originY per individial asset -->
|
|
<Container>
|
|
<Image :texture="'wall1'" :x="pos.position_x" :y="pos.position_y" :originY="1.13" :originX="1" />
|
|
<Image :texture="'wall1'" :x="pos2.position_x" :y="pos2.position_y" :originY="1.13" :originX="1" />
|
|
<Image :texture="'wall2'" :x="pos3.position_x" :y="pos3.position_y" :originY="1.255" :originX="1" />
|
|
</Container>
|
|
|
|
<Toolbar :layer="exampleTiles" @eraser="eraser" @pencil="pencil" @save="save" />
|
|
<Tiles v-if="(zoneEditorStore.tool === 'pencil' || zoneEditorStore.tool === 'eraser') && zoneEditorStore.drawMode === 'tile'" />
|
|
<Decorations v-if="(zoneEditorStore.tool === 'pencil' || zoneEditorStore.tool === 'eraser') && zoneEditorStore.drawMode === 'decoration'" />
|
|
<ZoneSettings v-if="zoneEditorStore.isSettingsModalShown" />
|
|
<GmPanel />
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
import config from '@/config'
|
|
import Tileset = Phaser.Tilemaps.Tileset
|
|
import TilemapLayer = Phaser.Tilemaps.TilemapLayer
|
|
import { Container, TilemapLayer as TilemapLayerC, useScene, Image } from 'phavuer'
|
|
import { onBeforeMount, onBeforeUnmount, onMounted, ref, type Ref, watch } from 'vue'
|
|
import Controls from '@/components/utilities/Controls.vue'
|
|
import { useSocketStore } from '@/stores/socket'
|
|
import Toolbar from '@/components/utilities/zoneEditor/Toolbar.vue'
|
|
import Tiles from '@/components/utilities/zoneEditor/Tiles.vue'
|
|
import { useZoneEditorStore } from '@/stores/zoneEditor'
|
|
import ZoneSettings from '@/components/utilities/zoneEditor/ZoneSettings.vue'
|
|
import Decorations from '@/components/utilities/zoneEditor/Decorations.vue'
|
|
import { generateTilemap, tileToWorldX, tileToWorldXY, tileToWorldY } from '@/services/zone'
|
|
import type { Zone } from '@/types'
|
|
import GmPanel from '@/components/utilities/GmPanel.vue'
|
|
|
|
// Phavuer logic
|
|
let scene = useScene()
|
|
const socket = useSocketStore()
|
|
const zoneEditorStore = useZoneEditorStore()
|
|
|
|
// Tile tilemap
|
|
const tileMapData = new Phaser.Tilemaps.MapData({
|
|
width: zoneEditorStore.width,
|
|
height: zoneEditorStore.height,
|
|
tileWidth: config.tile_size.x,
|
|
tileHeight: config.tile_size.y,
|
|
orientation: Phaser.Tilemaps.Orientation.ISOMETRIC,
|
|
format: Phaser.Tilemaps.Formats.ARRAY_2D
|
|
})
|
|
let tileTilemap = new Phaser.Tilemaps.Tilemap(scene, tileMapData)
|
|
let tilesImg = tileTilemap.addTilesetImage('default', 'tiles')
|
|
let exampleTiles = tileTilemap.createBlankLayer('exampleTiles', tilesImg as Tileset, 0, config.tile_size.y) as TilemapLayer
|
|
|
|
const exampleTilesArray = Array.from({ length: zoneEditorStore.width }, () => Array.from({ length: zoneEditorStore.height }, () => 1))
|
|
|
|
onMounted(() => {})
|
|
const pos = tileToWorldXY(exampleTiles, 1, 1)
|
|
const pos2 = tileToWorldXY(exampleTiles, 1, 2)
|
|
const pos3 = tileToWorldXY(exampleTiles, 2, 1)
|
|
console.log(pos)
|
|
// center camera
|
|
const centerY = (tileTilemap.height * tileTilemap.tileHeight) / 2
|
|
const centerX = (tileTilemap.width * tileTilemap.tileWidth) / 2
|
|
scene.cameras.main.centerOn(centerX, centerY)
|
|
|
|
onBeforeMount(() => {
|
|
exampleTilesArray.forEach((row, y) => row.forEach((tile, x) => exampleTiles.putTileAt(tile, x, y)))
|
|
|
|
socket.connection.emit('gm:zone_editor:zone:request', { zoneId: socket.character.zoneId })
|
|
})
|
|
|
|
// Watch for changes in the zoneStore and update the layer
|
|
// watch(
|
|
// () => zoneEditorStore.tiles,
|
|
// () => {
|
|
// // @TODO : change to zone for when loading other maps
|
|
// zoneEditorStore.tiles.forEach((row, y) => row.forEach((tile, x) => layer.putTileAt(tile, x, y)))
|
|
// },
|
|
// { deep: true }
|
|
// )
|
|
|
|
// socket.connection.on('gm:zone_editor:zone:load', (data: Zone) => {
|
|
// tileTilemap = generateTilemap(scene, data.width, data.height);
|
|
// zoneEditorStore.setName(data.name)
|
|
// zoneEditorStore.setWidth(data.width)
|
|
// zoneEditorStore.setHeight(data.height)
|
|
// })
|
|
|
|
function eraser(tile: Phaser.Tilemaps.Tile) {
|
|
if (zoneEditorStore.drawMode === 'tile') {
|
|
tiles.putTileAt(0, tile.x, tile.y)
|
|
zoneEditorStore.updateTile(tile.x, tile.y, 0)
|
|
}
|
|
|
|
if (zoneEditorStore.drawMode === 'wall') {
|
|
walls.putTileAt(0, tile.x, tile.y)
|
|
zoneEditorStore.updateWall(tile.x, tile.y, 0)
|
|
}
|
|
}
|
|
|
|
function pencil(tile: Phaser.Tilemaps.Tile) {
|
|
if (zoneEditorStore.drawMode === 'tile') {
|
|
if (zoneEditorStore.selectedTile === null) return
|
|
tiles.putTileAt(zoneEditorStore.selectedTile, tile.x, tile.y)
|
|
zoneEditorStore.setTiles(tile.x, tile.y, zoneEditorStore.selectedTile)
|
|
}
|
|
|
|
if (zoneEditorStore.drawMode === 'wall') {
|
|
// @TODO fix position
|
|
if (zoneEditorStore.selectedWall === null) return
|
|
walls.putTileAt(zoneEditorStore.selectedWall, tile.x, tile.y)
|
|
zoneEditorStore.updateWall(tile.x, tile.y, zoneEditorStore.selectedWall)
|
|
}
|
|
}
|
|
|
|
function save() {
|
|
socket.connection.emit('gm:zone_editor:zone:save', {
|
|
zoneId: socket.character.zoneId,
|
|
name: zoneEditorStore.name,
|
|
width: zoneEditorStore.width,
|
|
height: zoneEditorStore.height,
|
|
tiles: zoneEditorStore.tiles,
|
|
walls: zoneEditorStore.walls
|
|
})
|
|
}
|
|
|
|
onBeforeUnmount(() => {
|
|
zoneEditorStore.reset()
|
|
})
|
|
|
|
/**
|
|
* Resources:
|
|
* https://stackoverflow.com/questions/14488989/drawing-isometric-walls
|
|
*/
|
|
</script>
|