| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- import { MapEngine, type MapEngineStageRect, type MapEngineViewState } from '../../engine/map/mapEngine'
- import { loadRemoteMapConfig } from '../../utils/remoteMapConfig'
- type MapPageData = MapEngineViewState & {
- showDebugPanel: boolean
- }
- const INTERNAL_BUILD_VERSION = 'map-build-88'
- const REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/wxmini/test/qyds-001/game.json'
- let mapEngine: MapEngine | null = null
- function getFallbackStageRect(): MapEngineStageRect {
- const systemInfo = wx.getSystemInfoSync()
- const width = Math.max(320, systemInfo.windowWidth - 20)
- const height = Math.max(280, Math.floor(systemInfo.windowHeight * 0.66))
- return {
- width,
- height,
- left: 10,
- top: 0,
- }
- }
- Page({
- data: { showDebugPanel: false } as MapPageData,
- onLoad() {
- mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
- onData: (patch) => {
- this.setData(patch)
- },
- })
- this.setData({ ...mapEngine.getInitialData(), showDebugPanel: false })
- },
- onReady() {
- this.measureStageAndCanvas()
- this.loadMapConfigFromRemote()
- },
- onUnload() {
- if (mapEngine) {
- mapEngine.destroy()
- mapEngine = null
- }
- },
- loadMapConfigFromRemote() {
- const currentEngine = mapEngine
- if (!currentEngine) {
- return
- }
- loadRemoteMapConfig(REMOTE_GAME_CONFIG_URL)
- .then((config) => {
- if (mapEngine !== currentEngine) {
- return
- }
- currentEngine.applyRemoteMapConfig(config)
- })
- .catch((error) => {
- if (mapEngine !== currentEngine) {
- return
- }
- const errorMessage = error && error.message ? error.message : '未知错误'
- this.setData({
- configStatusText: `载入失败: ${errorMessage}`,
- statusText: `远程地图配置载入失败: ${errorMessage} (${INTERNAL_BUILD_VERSION})`,
- })
- })
- },
- measureStageAndCanvas() {
- const page = this
- const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
- const fallbackRect = getFallbackStageRect()
- const rect: MapEngineStageRect = {
- width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
- height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
- left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
- top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
- }
- const currentEngine = mapEngine
- if (!currentEngine) {
- return
- }
- currentEngine.setStage(rect)
- const canvasQuery = wx.createSelectorQuery().in(page)
- canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
- canvasQuery.exec((canvasRes) => {
- const canvasRef = canvasRes[0] as any
- if (!canvasRef || !canvasRef.node) {
- page.setData({
- statusText: `WebGL 引擎初始化失败 (${INTERNAL_BUILD_VERSION})`,
- })
- return
- }
- const dpr = wx.getSystemInfoSync().pixelRatio || 1
- try {
- currentEngine.attachCanvas(canvasRef.node, rect.width, rect.height, dpr)
- } catch (error) {
- page.setData({
- statusText: `WebGL 初始化失败 (${INTERNAL_BUILD_VERSION})`,
- })
- }
- })
- }
- const query = wx.createSelectorQuery().in(page)
- query.select('.map-stage').boundingClientRect()
- query.exec((res) => {
- const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
- applyStage(rect)
- })
- },
- handleTouchStart(event: WechatMiniprogram.TouchEvent) {
- if (mapEngine) {
- mapEngine.handleTouchStart(event)
- }
- },
- handleTouchMove(event: WechatMiniprogram.TouchEvent) {
- if (mapEngine) {
- mapEngine.handleTouchMove(event)
- }
- },
- handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
- if (mapEngine) {
- mapEngine.handleTouchEnd(event)
- }
- },
- handleTouchCancel() {
- if (mapEngine) {
- mapEngine.handleTouchCancel()
- }
- },
- handleRecenter() {
- if (mapEngine) {
- mapEngine.handleRecenter()
- }
- },
- handleRotateStep() {
- if (mapEngine) {
- mapEngine.handleRotateStep()
- }
- },
- handleRotationReset() {
- if (mapEngine) {
- mapEngine.handleRotationReset()
- }
- },
- handleSetManualMode() {
- if (mapEngine) {
- mapEngine.handleSetManualMode()
- }
- },
- handleSetNorthUpMode() {
- if (mapEngine) {
- mapEngine.handleSetNorthUpMode()
- }
- },
- handleSetHeadingUpMode() {
- if (mapEngine) {
- mapEngine.handleSetHeadingUpMode()
- }
- },
- handleCycleNorthReferenceMode() {
- if (mapEngine) {
- mapEngine.handleCycleNorthReferenceMode()
- }
- },
- handleAutoRotateCalibrate() {
- if (mapEngine) {
- mapEngine.handleAutoRotateCalibrate()
- }
- },
- handleToggleGpsTracking() {
- if (mapEngine) {
- mapEngine.handleToggleGpsTracking()
- }
- },
- handleToggleDebugPanel() {
- this.setData({
- showDebugPanel: !this.data.showDebugPanel,
- })
- },
- })
|