From 791830fd6faa4c0b9a0db9eed2267baf300b7419 Mon Sep 17 00:00:00 2001 From: Andrei Date: Sat, 25 Jan 2025 23:27:15 -0600 Subject: [PATCH] Refactoring of modalShown booleans --- src/App.vue | 12 +- src/application/types.ts | 4 +- src/components/game/character/Character.vue | 8 +- src/components/game/map/MapTiles.vue | 15 +- src/components/gameMaster/GmPanel.vue | 3 +- src/components/gameMaster/mapEditor/Map.vue | 52 ++++- .../mapEditor/mapPartials/MapEventTiles.vue | 98 ++++------ .../mapEditor/mapPartials/MapTiles.vue | 175 ++++++----------- .../mapEditor/mapPartials/PlacedMapObject.vue | 6 +- .../mapPartials/PlacedMapObjects.vue | 185 +++++++----------- .../mapEditor/partials/CreateMap.vue | 5 +- .../gameMaster/mapEditor/partials/MapList.vue | 18 +- .../mapEditor/partials/MapObjectList.vue | 9 +- .../mapEditor/partials/MapSettings.vue | 7 +- .../mapEditor/partials/TeleportModal.vue | 20 +- .../mapEditor/partials/TileList.vue | 9 +- .../gameMaster/mapEditor/partials/Toolbar.vue | 24 ++- src/components/screens/MapEditor.vue | 56 +++--- src/components/utilities/Modal.vue | 12 +- src/composables/mapComposable.ts | 60 ++---- .../pointerHandlers/useGamePointerHandlers.ts | 11 +- .../useMapEditorPointerHandlers.ts | 5 +- src/stores/mapEditorStore.ts | 31 +-- 23 files changed, 377 insertions(+), 448 deletions(-) diff --git a/src/App.vue b/src/App.vue index 971ca84..b2a2de4 100644 --- a/src/App.vue +++ b/src/App.vue @@ -2,7 +2,7 @@ - + @@ -17,24 +17,26 @@ import BackgroundImageLoader from '@/components/utilities/BackgroundImageLoader. import Debug from '@/components/utilities/Debug.vue' import Notifications from '@/components/utilities/Notifications.vue' import { useGameStore } from '@/stores/gameStore' +import { useMapEditorStore } from '@/stores/mapEditorStore' +import { computed, ref, useTemplateRef, watch } from 'vue' import { useMapEditorComposable } from '@/composables/useMapEditorComposable' -import { computed, watch } from 'vue' const gameStore = useGameStore() -const mapEditor = useMapEditorComposable() + +const isEditorShown = ref(false) const currentScreen = computed(() => { if (!gameStore.game.isLoaded) return Loading if (!gameStore.connection) return Login if (!gameStore.token) return Login if (!gameStore.character) return Characters - if (mapEditor.active.value) return MapEditor + if (isEditorShown.value) return MapEditor return Game }) // Watch mapEditor.active and empty gameStore.game.loadedAssets watch( - () => mapEditor.active.value, + () => isEditorShown.value, () => { gameStore.game.loadedTextures = [] } diff --git a/src/application/types.ts b/src/application/types.ts index deefce5..3aeb993 100644 --- a/src/application/types.ts +++ b/src/application/types.ts @@ -68,7 +68,7 @@ export type Map = { name: string width: number height: number - tiles: any | null + tiles: string[][] pvp: boolean mapEffects: MapEffect[] mapEventTiles: MapEventTile[] @@ -105,7 +105,7 @@ export enum MapEventTileType { export type MapEventTile = { id: UUID - map: Map + mapId: UUID type: MapEventTileType positionX: number positionY: number diff --git a/src/components/game/character/Character.vue b/src/components/game/character/Character.vue index a70d14c..8ce8892 100644 --- a/src/components/game/character/Character.vue +++ b/src/components/game/character/Character.vue @@ -147,9 +147,11 @@ watch( ) onMounted(async () => { + let character = props.mapCharacter.character + const characterTypeStorage = new CharacterTypeStorage() - const spriteId = await characterTypeStorage.getSpriteId(props.mapCharacter.character.characterType!) + const spriteId = await characterTypeStorage.getSpriteId(character.characterType!) if (!spriteId) return charSpriteId.value = spriteId @@ -161,14 +163,14 @@ onMounted(async () => { charContainer.value!.setName(props.mapCharacter.character!.name) - if (props.mapCharacter.character.id === gameStore.character!.id) { + if (character.id === gameStore.character!.id) { mapStore.setCharacterLoaded(true) // #146 : Set camera position to character, need to be improved still scene.cameras.main.startFollow(charContainer.value as Phaser.GameObjects.Container) } - updatePosition(props.mapCharacter.character.positionX, props.mapCharacter.character.positionY, props.mapCharacter.character.rotation) + updatePosition(character.positionX, character.positionY, character.rotation) }) onUnmounted(() => { diff --git a/src/components/game/map/MapTiles.vue b/src/components/game/map/MapTiles.vue index d93fe3e..7b9517f 100644 --- a/src/components/game/map/MapTiles.vue +++ b/src/components/game/map/MapTiles.vue @@ -4,10 +4,10 @@ diff --git a/src/components/gameMaster/mapEditor/mapPartials/MapTiles.vue b/src/components/gameMaster/mapEditor/mapPartials/MapTiles.vue index 4ea592c..6ac5493 100644 --- a/src/components/gameMaster/mapEditor/mapPartials/MapTiles.vue +++ b/src/components/gameMaster/mapEditor/mapPartials/MapTiles.vue @@ -22,6 +22,8 @@ const tileStorage = new TileStorage() const tileMap = shallowRef() const tileLayer = shallowRef() +defineExpose({handlePointer}) + function createTileMap() { const mapData = new Phaser.Tilemaps.MapData({ width: mapEditor.currentMap.value?.width, @@ -32,9 +34,7 @@ function createTileMap() { format: Phaser.Tilemaps.Formats.ARRAY_2D }) - const newTileMap = new Phaser.Tilemaps.Tilemap(scene, mapData) - emit('tileMap:create', newTileMap) - return newTileMap + return new Phaser.Tilemaps.Tilemap(scene, mapData) } async function createTileLayer(currentTileMap: Phaser.Tilemaps.Tilemap) { @@ -55,111 +55,81 @@ async function createTileLayer(currentTileMap: Phaser.Tilemaps.Tilemap) { return layer } -function pencil(pointer: Phaser.Input.Pointer) { - if (!tileMap.value || !tileLayer.value) return - - // Check if map is set - if (!mapEditor.currentMap.value) return - console.log(mapEditor.tool.value) - - // Check if tool is pencil - if (mapEditor.tool.value !== 'pencil') return - - // Check if draw mode is tile - if (mapEditor.drawMode.value !== 'tile') return - - // Check if there is a selected tile - if (!mapEditor.selectedTile.value) return // Changed this line to access .value - - // Check if left mouse button is pressed - if (!pointer.isDown) return - - // Check if shift is not pressed, this means we are moving the camera - if (pointer.event.shiftKey) return +function pencil(pointer: Phaser.Input.Pointer, tileMap: Phaser.Tilemaps.Tilemap, tileLayer: Phaser.Tilemaps.TilemapLayer) { + var map = mapEditor.currentMap.value + if (!map) return // Check if there is a tile - const tile = getTile(tileLayer.value, pointer.worldX, pointer.worldY) + const tile = tileLayer.getTileAtWorldXY(pointer.worldX, pointer.worldY) if (!tile) return // Place tile - placeTile(tileMap.value, tileLayer.value, tile.x, tile.y, mapEditor.selectedTile.value) + placeTile(tileMap, tileLayer, tile.x, tile.y, mapEditorStore.selectedTile) - // Adjust mapEditor tiles - mapEditor.currentMap.value.tiles[tile.y][tile.x] = mapEditor.selectedTile.value + // Adjust mapEditorStore.map.tiles + tileMap.tiles[tile.y][tile.x] = map.tiles[tile.y][tile.x] } -function eraser(pointer: Phaser.Input.Pointer) { - if (!tileMap.value || !tileLayer.value) return - - // Check if map is set - if (!mapEditor.currentMap.value) return - - // Check if tool is pencil - if (mapEditor.tool.value !== 'eraser') return - - // Check if draw mode is tile - if (mapEditor.eraserMode.value !== 'tile') return - - // Check if left mouse button is pressed - if (!pointer.isDown) return - - // Check if shift is not pressed, this means we are moving the camera - if (pointer.event.shiftKey) return - - // Check if alt is pressed - if (pointer.event.altKey) return +function eraser(pointer: Phaser.Input.Pointer, tileMap: Phaser.Tilemaps.Tilemap, tileLayer: Phaser.Tilemaps.TilemapLayer) { + let map = mapEditor.currentMap.value + if (!map) return // Check if there is a tile - const tile = getTile(tileLayer.value, pointer.worldX, pointer.worldY) + const tile = getTile(tileLayer, pointer.worldX, pointer.worldY) if (!tile) return // Place tile - placeTile(tileMap.value, tileLayer.value, tile.x, tile.y, 'blank_tile') + placeTile(tileMap, tileLayer, tile.x, tile.y, 'blank_tile') - // Adjust mapEditor.map.tiles - mapEditor.currentMap.value.tiles[tile.y][tile.x] = 'blank_tile' + // Adjust mapEditorStore.map.tiles + map.tiles[tile.y][tile.x] = 'blank_tile' } -function paint(pointer: Phaser.Input.Pointer) { - if (!tileMap.value || !tileLayer.value) return - - // Check if map is set - if (!mapEditor.currentMap.value) return - - // Check if tool is pencil - if (mapEditor.tool.value !== 'paint') return - - // Check if there is a selected tile - if (!mapEditor.selectedTile.value) return - - // Check if left mouse button is pressed - if (!pointer.isDown) return - - // Check if shift is not pressed, this means we are moving the camera - if (pointer.event.shiftKey) return - - // Check if alt is pressed - if (pointer.event.altKey) return - +function paint(pointer: Phaser.Input.Pointer, tileMap: Phaser.Tilemaps.Tilemap, tileLayer: Phaser.Tilemaps.TilemapLayer) { // Set new tileArray with selected tile - setLayerTiles(tileMap.value, tileLayer.value, createTileArray(tileMap.value.width, tileMap.value.height, mapEditor.selectedTile.value)) + setLayerTiles(tileMap, tileLayer, createTileArray(tileMap.width, tileMap.height, mapEditorStore.selectedTile)) - // Adjust mapEditor.map.tiles - mapEditor.currentMap.value.tiles = createTileArray(tileMap.value.width, tileMap.value.height, mapEditor.selectedTile.value) + // Adjust mapEditorStore.map.tiles + if (mapEditor.currentMap.value) { + mapEditor.currentMap.value.tiles = createTileArray(tileMap.width, tileMap.height, mapEditorStore.selectedTile) + } } // When alt is pressed, and the pointer is down, select the tile that the pointer is over -function tilePicker(pointer: Phaser.Input.Pointer) { - if (!tileMap.value || !tileLayer.value) return +function tilePicker(pointer: Phaser.Input.Pointer, tileLayer: Phaser.Tilemaps.TilemapLayer) { + let map = mapEditor.currentMap.value + if (!map) return - // Check if map is set + // Check if there is a tile + const tile = getTile(tileLayer, pointer.worldX, pointer.worldY) + if (!tile) return + + // Select the tile + mapEditorStore.setSelectedTile(map.tiles[tile.y][tile.x]) +} + +watch( + () => mapEditorStore.shouldClearTiles, + (shouldClear) => { + if (shouldClear && mapEditor.currentMap.value && tileMap.value && tileLayer.value) { + const blankTiles = createTileArray(tileLayer.value.width, tileLayer.value.height, 'blank_tile') + setLayerTiles(tileMap.value, tileLayer.value, blankTiles) + mapEditor.currentMap.value.tiles = blankTiles + mapEditorStore.resetClearTilesFlag() + } + } +) + +function handlePointer(pointer: Phaser.Input.Pointer) { if (!mapEditor.currentMap.value) return - // Check if tool is pencil - if (mapEditor.tool.value !== 'pencil') return + if (!tileMap.value || !tileLayer.value) return - // Check if draw mode is tile - if (mapEditor.drawMode.value !== 'tile') return + // Check if tool is pencil + if (mapEditorStore.drawMode !== 'tile') return + + // Check if there is a selected tile + if (!mapEditorStore.selectedTile) return // Check if left mouse button is pressed if (!pointer.isDown) return @@ -170,25 +140,18 @@ function tilePicker(pointer: Phaser.Input.Pointer) { // Check if alt is pressed if (!pointer.event.altKey) return - // Check if there is a tile - const tile = getTile(tileLayer.value, pointer.worldX, pointer.worldY) - if (!tile) return - - // Select the tile - mapEditor.setSelectedTile(mapEditor.currentMap.value.tiles[tile.y][tile.x]) -} - -watch( - () => mapEditor.shouldClearTiles.value, - (shouldClear) => { - if (shouldClear && mapEditor.currentMap.value && tileMap.value && tileLayer.value) { - const blankTiles = createTileArray(tileMap.value.width, tileMap.value.height, 'blank_tile') - setLayerTiles(tileMap.value, tileLayer.value, blankTiles) - mapEditor.currentMap.value.tiles = blankTiles - mapEditor.resetClearTilesFlag() - } + // Check if draw mode is tile + switch (mapEditorStore.tool) { + case 'pencil': + pencil(pointer, tileMap.value, tileLayer.value) + case 'eraser': + eraser(pointer, tileMap.value, tileLayer.value) + case 'paint': + paint(pointer, tileMap.value, tileLayer.value) + case 'tile picker': + tilePicker(pointer, tileLayer.value) } -) +} onMounted(async () => { if (!mapEditor.currentMap.value?.tiles) return @@ -211,19 +174,9 @@ onMounted(async () => { } setLayerTiles(tileMap.value, tileLayer.value, blankTiles) - - scene.input.on(Phaser.Input.Events.POINTER_MOVE, pencil) - scene.input.on(Phaser.Input.Events.POINTER_MOVE, eraser) - scene.input.on(Phaser.Input.Events.POINTER_DOWN, paint) - scene.input.on(Phaser.Input.Events.POINTER_DOWN, tilePicker) }) onUnmounted(() => { - scene.input.off(Phaser.Input.Events.POINTER_MOVE, pencil) - scene.input.off(Phaser.Input.Events.POINTER_MOVE, eraser) - scene.input.off(Phaser.Input.Events.POINTER_DOWN, paint) - scene.input.off(Phaser.Input.Events.POINTER_DOWN, tilePicker) - if (tileMap.value) { tileMap.value.destroyLayer('tiles') tileMap.value.removeAllLayers() diff --git a/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObject.vue b/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObject.vue index 688c546..0f3bff8 100644 --- a/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObject.vue +++ b/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObject.vue @@ -11,7 +11,7 @@ import { Image, useScene } from 'phavuer' import { computed } from 'vue' const props = defineProps<{ - tilemap: Phaser.Tilemaps.Tilemap + tileMap: Phaser.Tilemaps.Tilemap placedMapObject: PlacedMapObject selectedPlacedMapObject: PlacedMapObject | null movingPlacedMapObject: PlacedMapObject | null @@ -24,8 +24,8 @@ const imageProps = computed(() => ({ alpha: props.movingPlacedMapObject?.id === props.placedMapObject.id ? 0.5 : 1, tint: props.selectedPlacedMapObject?.id === props.placedMapObject.id ? 0x00ff00 : 0xffffff, depth: calculateIsometricDepth(props.placedMapObject.positionX, props.placedMapObject.positionY, props.placedMapObject.mapObject.frameWidth, props.placedMapObject.mapObject.frameHeight), - x: tileToWorldX(props.tilemap, props.placedMapObject.positionX, props.placedMapObject.positionY), - y: tileToWorldY(props.tilemap, props.placedMapObject.positionX, props.placedMapObject.positionY), + x: tileToWorldX(props.tileMap, props.placedMapObject.positionX, props.placedMapObject.positionY), + y: tileToWorldY(props.tileMap, props.placedMapObject.positionX, props.placedMapObject.positionY), flipX: props.placedMapObject.isRotated, texture: props.placedMapObject.mapObject.id, originY: Number(props.placedMapObject.mapObject.originX), diff --git a/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObjects.vue b/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObjects.vue index b7142a3..fb30685 100644 --- a/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObjects.vue +++ b/src/components/gameMaster/mapEditor/mapPartials/PlacedMapObjects.vue @@ -1,142 +1,87 @@ diff --git a/src/components/gameMaster/mapEditor/partials/MapList.vue b/src/components/gameMaster/mapEditor/partials/MapList.vue index f3dfcdf..c060667 100644 --- a/src/components/gameMaster/mapEditor/partials/MapList.vue +++ b/src/components/gameMaster/mapEditor/partials/MapList.vue @@ -1,5 +1,5 @@ - + diff --git a/src/components/gameMaster/mapEditor/partials/TileList.vue b/src/components/gameMaster/mapEditor/partials/TileList.vue index d0e4516..01e17bc 100644 --- a/src/components/gameMaster/mapEditor/partials/TileList.vue +++ b/src/components/gameMaster/mapEditor/partials/TileList.vue @@ -1,5 +1,5 @@