Skip to content

Commit 6f4cc8f

Browse files
committed
feat: integrate multiplayer functionality in World component
- Added multiplayer support in the World component by utilizing the useMultiplayer composable. - Updated player position handling to send messages in multiplayer mode, while maintaining local updates for single-player. - Adjusted test-level-a.json to reposition a chest and modify its rotation for better gameplay experience. - Introduced Vector3 type import in useGameStore for improved type safety.
1 parent 1bf6737 commit 6f4cc8f

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

components/environments/World.vue

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import type { ThreeEvent, TresEvent } from '@tresjs/core'
33
import type { Group } from 'three'
44
import { MeshBasicMaterial } from 'three'
5+
import { useMultiplayer } from '~/composables/game/useMultiplayer'
56
const { getResource } = useResourcePreloader()
67
const gameStore = useGameStore()
78
@@ -11,6 +12,7 @@ const { scene: modelChests } = getResource('models', 'chests')
1112
const { scene: modelDoors } = getResource('models', 'doors')
1213
const { scene: modelSpikes } = getResource('models', 'spikes')
1314
const { scene: modelVisualBlocker, nodes: modelVisualBlockerNodes } = getResource('models', 'visual_blocker')
15+
const lobbyStore = useLobbyStore()
1416
1517
const floor = modelVisualBlockerNodes['room1001'].clone()
1618
@@ -63,10 +65,20 @@ const cylinderShader = {
6365
const showIndicator = ref(false)
6466
const hoverIndicatorRef = shallowRef()
6567
68+
const { sendMsg } = useMultiplayer(gameStore.isMultiplayer)
6669
6770
const handleFloorClick = (e: ThreeEvent<PointerEvent>) => {
6871
const newPosition = { x: e.point.x, y: 0, z: e.point.z }
69-
gameStore.setPlayerPosition(gameStore.players[0], newPosition)
72+
if (gameStore.isMultiplayer) {
73+
sendMsg({
74+
type: 'UPDATE_PLAYER_POSITION',
75+
lobbyId: lobbyStore.currentLobbyId,
76+
position: [newPosition.x, newPosition.y, newPosition.z],
77+
})
78+
}
79+
else {
80+
gameStore.setPlayerPosition(gameStore.players[0], newPosition)
81+
}
7082
7183
}
7284

content/levels/test-level-a.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
{
1414
"id": "test-chest",
1515
"type": "chest",
16-
"position": [2, 0, -5],
17-
"rotation": [0, 0, 0],
16+
"position": [2, 0, 10],
17+
"rotation": [0, 90, 0],
1818
"state": {
1919
"isLocked": true,
2020
"isOpen": false

stores/useGameStore.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { defineStore } from 'pinia'
2+
import type { Vector3 } from 'three'
23
import type { Character, CharacterTemplate, GameItem, Level, Player } from '~/types'
34

45
/**

0 commit comments

Comments
 (0)