100 lines
3.2 KiB
Vue
100 lines
3.2 KiB
Vue
<template>
|
|
<TilemapLayer v-if="mapStore.isLoaded" :tilemap="tileMap" :tileset="mapStore.getTiles" ref="tilemapLayer" :layerIndex="0" :cull-padding-x="10" :cull-padding-y="10" />
|
|
<Controls :layer="layer" />
|
|
<Player :layer="layer" />
|
|
<Container v-if="mapStore.isLoaded">
|
|
<Player :layer="layer" v-for="player in mapStore.getPlayers" :key="player.id" :player="player" />
|
|
</Container>
|
|
</template>
|
|
|
|
<script setup lang="ts">
|
|
/**
|
|
* 1 tile is 64x32
|
|
* the map is 10x10
|
|
* so the map is 640x320
|
|
*/
|
|
import { Container, refObj, TilemapLayer, useScene } from 'phavuer'
|
|
import Player from '@/components/sprites/player/Player.vue'
|
|
import config from '@/config'
|
|
import { onBeforeMount, onMounted, reactive, ref, type Ref, toRaw, watch } from 'vue'
|
|
import Tileset = Phaser.Tilemaps.Tileset
|
|
import Controls from '@/components/Controls.vue'
|
|
import { useSocketStore } from '@/stores/socket'
|
|
import { useMapStore } from '@/stores/map'
|
|
|
|
// Phavuer logic
|
|
let scene = useScene()
|
|
let tilemapLayer = ref();
|
|
let mapData = new Phaser.Tilemaps.MapData({
|
|
width: 10, // @TODO : get this from the server
|
|
height: 10, // @TODO : get this from the server
|
|
tileWidth: config.tile_size.x,
|
|
tileHeight: config.tile_size.y,
|
|
orientation: Phaser.Tilemaps.Orientation.ISOMETRIC,
|
|
format: Phaser.Tilemaps.Formats.ARRAY_2D,
|
|
});
|
|
let tileMap = new Phaser.Tilemaps.Tilemap(scene, mapData);
|
|
let tileset: (Tileset|null) = tileMap.addTilesetImage('default', 'tiles');
|
|
let layer = tileMap.createBlankLayer('layer', tileset, 0, config.tile_size.y);
|
|
|
|
// center camera
|
|
const centerY = (tileMap.height * tileMap.tileHeight) / 2
|
|
const centerX = (tileMap.width * tileMap.tileWidth) / 2
|
|
scene.cameras.main.centerOn(centerX, centerY)
|
|
|
|
// Multiplayer / server logics
|
|
const mapStore = useMapStore();
|
|
const socket = useSocketStore();
|
|
|
|
// Watch for changes in the mapStore and update the layer
|
|
watch (() => mapStore.loaded, () => {
|
|
mapStore.getTiles.forEach((row, y) => row.forEach((tile, x) => layer.putTileAt(tile, x, y)));
|
|
}, { deep: true })
|
|
|
|
|
|
// Load the map from the server
|
|
onBeforeMount(() => {
|
|
socket.socket?.emit('get_map');
|
|
})
|
|
|
|
// Listen for the map event from the server and load the map
|
|
socket.socket?.on('map', (data) => {
|
|
mapStore.loadTiles(data.tiles)
|
|
|
|
|
|
/**
|
|
* @TODO
|
|
* bug , when 2nd player joins, the first player is not added to the map
|
|
*/
|
|
|
|
console.log(data.players);
|
|
|
|
console.log(data.players[1]); // key is user id
|
|
|
|
// remove self from the players list
|
|
delete data.players[socket.socket?.id];
|
|
|
|
mapStore.addPlayers(data.players);
|
|
})
|
|
|
|
// Listen for player join events
|
|
socket.socket?.on('player_join', (data) => {
|
|
console.log('player_join', data)
|
|
mapStore.addPlayer(data);
|
|
})
|
|
|
|
socket.socket?.on('ping', (data) => {
|
|
console.log('ping', data)
|
|
})
|
|
|
|
/**
|
|
* Resources
|
|
* https://clintbellanger.net/articles/isometric_math/
|
|
* https://gist.github.com/veleek/3be73dc61d5f5a80abc0f72c3ffe390e
|
|
* https://gamedev.stackexchange.com/questions/116485/how-to-center-a-tilemap-in-phaser
|
|
* https://github.com/Nulligma/phaser-isometric-test/blob/master/src/main.ts
|
|
* https://github.com/neki-dev/isometric-snippets
|
|
* https://github.com/itsezc/CycloneIO/blob/next/packages/client/games/HabboEngine.ts
|
|
* https://github.com/daan93/phaser-isometric-demo/tree/main
|
|
*/
|
|
</script> |