// React import { useState, useEffect, useRef, Fragment } from 'react'; import { View, Image, Alert, StyleSheet, TouchableOpacity } from 'react-native'; import MapView, { Marker, Circle, Polygon } from 'react-native-maps'; import LinearGradient from 'react-native-linear-gradient'; // Contexts import { useTeamContext } from '../context/teamContext'; // Hooks import { useGame } from '../hook/useGame'; // Util import { GameState } from '../util/gameState'; import { ZoneTypes, InitialRegions } from '../util/constants'; export const CustomMap = () => { const {zoneType, zoneExtremities, location, gameState} = useTeamContext(); const {sendCurrentPosition, enemyLocation, startingArea, captured, lastSentLocation, hasHandicap} = useGame(); const mapRef = useRef(null); const [centerMap, setCenterMap] = useState(true); // Center the map on user position useEffect(() => { if (centerMap && mapRef.current && location) { mapRef.current.animateToRegion({latitude: location[0], longitude: location[1], latitudeDelta: 0, longitudeDelta: 0.02}, 1000); } }, [centerMap, mapRef, location]); const latToLatitude = (pos) => ({latitude: pos.lat, longitude: pos.lng}); return ( setCenterMap(false)} toolbarEnabled={false}> { gameState == GameState.PLACEMENT && startingArea && } { gameState == GameState.PLAYING && zoneExtremities && { zoneType == ZoneTypes.circle && zoneExtremities.begin && } { zoneType == ZoneTypes.circle && zoneExtremities.end && } { zoneType == ZoneTypes.polygon && zoneExtremities.begin && latToLatitude(pos))} strokeColor="red" fillColor="rgba(255,0,0,0.1)" strokeWidth={2} /> } { zoneType == ZoneTypes.polygon && zoneExtremities.end && latToLatitude(pos))} strokeColor="green" fillColor="rgba(0,255,0,0.1)" strokeWidth={2} /> } } { location && Alert.alert("Position actuelle", "Ceci est votre position")}> } { gameState == GameState.PLAYING && lastSentLocation && !hasHandicap && Alert.alert("Position envoyée", "Ceci est votre dernière position connue par le serveur")}> } { gameState == GameState.PLAYING && enemyLocation && !hasHandicap && Alert.alert("Position ennemie", "Ceci est la dernière position de vos ennemis connue")}/> } { !centerMap && setCenterMap(true)}> } { gameState == GameState.PLAYING && !captured && { !hasHandicap && } } ); }; const styles = StyleSheet.create({ mapContainer: { flex: 1, width: '100%', borderTopLeftRadius: 30, borderTopRightRadius: 30, overflow: 'hidden', }, centerMapContainer: { position: 'absolute', right: 20, top: 20, width: 40, height: 40, borderRadius: 20, backgroundColor: 'white', borderWidth: 2, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }, toolBarRight: { position: 'absolute', right: 30, bottom: 80 }, updatePositionContainer: { width: 60, height: 60, borderRadius: 30, backgroundColor: 'white', borderWidth: 4, borderColor: 'black', alignItems: 'center', justifyContent: 'center', }, });