mirror of
https://git.rezel.net/LudoTech/traque.git
synced 2026-02-09 10:20:16 +01:00
added front end for showing zone to player
This commit is contained in:
@@ -5,6 +5,7 @@ import 'leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility
|
|||||||
import "leaflet-defaulticon-compatibility";
|
import "leaflet-defaulticon-compatibility";
|
||||||
import "leaflet/dist/leaflet.css";
|
import "leaflet/dist/leaflet.css";
|
||||||
import useGame from '@/hook/useGame';
|
import useGame from '@/hook/useGame';
|
||||||
|
import { useTeamContext } from '@/context/teamContext';
|
||||||
|
|
||||||
const DEFAULT_ZOOM = 17;
|
const DEFAULT_ZOOM = 17;
|
||||||
|
|
||||||
@@ -15,22 +16,38 @@ function MapPan(props) {
|
|||||||
const [initialized, setInitialized] = useState(false);
|
const [initialized, setInitialized] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if(!initialized && props.center) {
|
if (!initialized && props.center) {
|
||||||
map.flyTo(props.center, DEFAULT_ZOOM, {animate: false});
|
map.flyTo(props.center, DEFAULT_ZOOM, { animate: false });
|
||||||
setInitialized(true)
|
setInitialized(true)
|
||||||
}
|
}
|
||||||
},[props.center]);
|
}, [props.center]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LiveMap({ ...props}) {
|
function LiveZone() {
|
||||||
const {currentPosition, enemyPosition} = useGame();
|
const { zone } = useTeamContext();
|
||||||
|
console.log('Zone', zone);
|
||||||
|
return zone && <Circle center={zone.center} radius={zone.radius} color='blue' fill={false} />
|
||||||
|
}
|
||||||
|
|
||||||
|
function ZoneExtremities() {
|
||||||
|
const { zoneExtremities } = useTeamContext();
|
||||||
|
console.log('Zone extremities', zoneExtremities);
|
||||||
|
return zoneExtremities && zoneExtremities.begin && zoneExtremities.end && <>
|
||||||
|
<Circle center={zoneExtremities.begin.center} radius={zoneExtremities.begin.radius} color='black' fill={false} />
|
||||||
|
<Circle center={zoneExtremities.end.center} radius={zoneExtremities.end.radius} color='red' fill={false} />
|
||||||
|
</>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export function LiveMap({ ...props }) {
|
||||||
|
const { currentPosition, enemyPosition } = useGame();
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log('Current position', currentPosition);
|
console.log('Current position', currentPosition);
|
||||||
}, [currentPosition]);
|
}, [currentPosition]);
|
||||||
return (
|
return (
|
||||||
<MapContainer {...props} className='min-h-full z-0' center={[0,0]} zoom={0} scrollWheelZoom={true}>
|
<MapContainer {...props} className='min-h-full z-0' center={[0, 0]} zoom={0} scrollWheelZoom={true}>
|
||||||
<TileLayer
|
<TileLayer
|
||||||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
|
||||||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png"
|
||||||
@@ -57,13 +74,15 @@ export function LiveMap({ ...props}) {
|
|||||||
Position de l'ennemi
|
Position de l'ennemi
|
||||||
</Popup>
|
</Popup>
|
||||||
</Marker>}
|
</Marker>}
|
||||||
<MapPan center={currentPosition}/>
|
<MapPan center={currentPosition} />
|
||||||
|
<LiveZone />
|
||||||
|
<ZoneExtremities />
|
||||||
</MapContainer>
|
</MapContainer>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function PlacementMap({ ...props}) {
|
export function PlacementMap({ ...props }) {
|
||||||
const {currentPosition, startingArea} = useGame();
|
const { currentPosition, startingArea } = useGame();
|
||||||
return (
|
return (
|
||||||
<MapContainer {...props} className='min-h-full w-full z-0' scrollWheelZoom={true}>
|
<MapContainer {...props} className='min-h-full w-full z-0' scrollWheelZoom={true}>
|
||||||
<TileLayer
|
<TileLayer
|
||||||
@@ -81,7 +100,7 @@ export function PlacementMap({ ...props}) {
|
|||||||
Votre position
|
Votre position
|
||||||
</Popup>
|
</Popup>
|
||||||
</Marker>}
|
</Marker>}
|
||||||
<MapPan center={currentPosition}/>
|
<MapPan center={currentPosition} />
|
||||||
{startingArea && <Circle center={startingArea?.center} radius={startingArea?.radius} color='blue' />}
|
{startingArea && <Circle center={startingArea?.center} radius={startingArea?.radius} color='blue' />}
|
||||||
</MapContainer>
|
</MapContainer>
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { useLocation } from "@/hook/useLocation";
|
import { useLocation } from "@/hook/useLocation";
|
||||||
import { useSocketListener } from "@/hook/useSocketListener";
|
import { useSocketListener } from "@/hook/useSocketListener";
|
||||||
import { createContext, useContext, useEffect, useMemo, useRef, useState } from "react";
|
import { createContext, use, useContext, useEffect, useMemo, useRef, useState } from "react";
|
||||||
import { useSocket } from "./socketContext";
|
import { useSocket } from "./socketContext";
|
||||||
import { useTeamConnexion } from "./teamConnexionContext";
|
import { useTeamConnexion } from "./teamConnexionContext";
|
||||||
import { GameState } from "@/util/gameState";
|
import { GameState } from "@/util/gameState";
|
||||||
@@ -11,6 +11,8 @@ const teamContext = createContext()
|
|||||||
function TeamProvider({children}) {
|
function TeamProvider({children}) {
|
||||||
const [teamInfos, setTeamInfos] = useState({});
|
const [teamInfos, setTeamInfos] = useState({});
|
||||||
const [gameState, setGameState] = useState(GameState.SETUP);
|
const [gameState, setGameState] = useState(GameState.SETUP);
|
||||||
|
const [zone, setZone] = useState(null);
|
||||||
|
const [zoneExtremities, setZoneExtremities] = useState(null);
|
||||||
const measuredLocation = useLocation(10000);
|
const measuredLocation = useLocation(10000);
|
||||||
const {teamSocket} = useSocket();
|
const {teamSocket} = useSocket();
|
||||||
const {loggedIn} = useTeamConnexion();
|
const {loggedIn} = useTeamConnexion();
|
||||||
@@ -23,6 +25,9 @@ function TeamProvider({children}) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
useSocketListener(teamSocket, "game_state", setGameState);
|
useSocketListener(teamSocket, "game_state", setGameState);
|
||||||
|
useSocketListener(teamSocket, "zone", setZone);
|
||||||
|
useSocketListener(teamSocket, "new_zone", setZoneExtremities);
|
||||||
|
|
||||||
|
|
||||||
//Send the current position to the server when the user is logged in
|
//Send the current position to the server when the user is logged in
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -32,7 +37,7 @@ function TeamProvider({children}) {
|
|||||||
}
|
}
|
||||||
}, [loggedIn, measuredLocation]);
|
}, [loggedIn, measuredLocation]);
|
||||||
|
|
||||||
const value = useMemo(() => ({teamInfos, gameState}), [teamInfos, gameState]);
|
const value = useMemo(() => ({teamInfos, gameState, zone, zoneExtremities}), [teamInfos, gameState, zone, zoneExtremities]);
|
||||||
return (
|
return (
|
||||||
<teamContext.Provider value={value}>
|
<teamContext.Provider value={value}>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
Reference in New Issue
Block a user