map.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. import { MapEngine, type MapEngineStageRect, type MapEngineViewState } from '../../engine/map/mapEngine'
  2. import { loadRemoteMapConfig } from '../../utils/remoteMapConfig'
  3. type CompassTickData = {
  4. angle: number
  5. long: boolean
  6. major: boolean
  7. }
  8. type CompassLabelData = {
  9. text: string
  10. angle: number
  11. rotateBack: number
  12. radius: number
  13. className: string
  14. }
  15. type SideButtonMode = 'all' | 'left' | 'right' | 'hidden'
  16. type MapPageData = MapEngineViewState & {
  17. showDebugPanel: boolean
  18. statusBarHeight: number
  19. topInsetHeight: number
  20. hudPanelIndex: number
  21. panelTimerText: string
  22. panelMileageText: string
  23. panelDistanceValueText: string
  24. panelProgressText: string
  25. panelSpeedValueText: string
  26. compassTicks: CompassTickData[]
  27. compassLabels: CompassLabelData[]
  28. sideButtonMode: SideButtonMode
  29. showLeftButtonGroup: boolean
  30. showRightButtonGroups: boolean
  31. showBottomDebugButton: boolean
  32. }
  33. const INTERNAL_BUILD_VERSION = 'map-build-166'
  34. const REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/wxmini/test/game.json'
  35. let mapEngine: MapEngine | null = null
  36. function buildSideButtonVisibility(mode: SideButtonMode) {
  37. return {
  38. sideButtonMode: mode,
  39. showLeftButtonGroup: mode === 'all' || mode === 'left' || mode === 'right',
  40. showRightButtonGroups: mode === 'all' || mode === 'right',
  41. showBottomDebugButton: mode !== 'hidden',
  42. }
  43. }
  44. function getNextSideButtonMode(currentMode: SideButtonMode): SideButtonMode {
  45. if (currentMode === 'all') {
  46. return 'left'
  47. }
  48. if (currentMode === 'left') {
  49. return 'right'
  50. }
  51. if (currentMode === 'right') {
  52. return 'hidden'
  53. }
  54. return 'left'
  55. }
  56. function buildCompassTicks(): CompassTickData[] {
  57. const ticks: CompassTickData[] = []
  58. for (let angle = 0; angle < 360; angle += 5) {
  59. ticks.push({
  60. angle,
  61. long: angle % 15 === 0,
  62. major: angle % 45 === 0,
  63. })
  64. }
  65. return ticks
  66. }
  67. function buildCompassLabels(): CompassLabelData[] {
  68. return [
  69. { text: '\u5317', angle: 0, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal compass-widget__mark--north' },
  70. { text: '\u4e1c\u5317', angle: 45, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northeast' },
  71. { text: '\u4e1c', angle: 90, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  72. { text: '\u4e1c\u5357', angle: 135, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  73. { text: '\u5357', angle: 180, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  74. { text: '\u897f\u5357', angle: 225, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  75. { text: '\u897f', angle: 270, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  76. { text: '\u897f\u5317', angle: 315, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northwest' },
  77. ]
  78. }
  79. function getFallbackStageRect(): MapEngineStageRect {
  80. const systemInfo = wx.getSystemInfoSync()
  81. const width = Math.max(320, systemInfo.windowWidth)
  82. const height = Math.max(280, systemInfo.windowHeight)
  83. return {
  84. width,
  85. height,
  86. left: 0,
  87. top: 0,
  88. }
  89. }
  90. Page({
  91. data: {
  92. showDebugPanel: false,
  93. statusBarHeight: 0,
  94. topInsetHeight: 12,
  95. hudPanelIndex: 0,
  96. panelTimerText: '00:00:00',
  97. panelMileageText: '0m',
  98. panelActionTagText: '目标',
  99. panelDistanceTagText: '点距',
  100. panelDistanceValueText: '--',
  101. panelDistanceUnitText: '',
  102. panelProgressText: '0/0',
  103. gameSessionStatus: 'idle',
  104. gameModeText: '顺序赛',
  105. panelSpeedValueText: '0',
  106. panelTelemetryTone: 'blue',
  107. panelHeartRateZoneNameText: '--',
  108. panelHeartRateZoneRangeText: '',
  109. heartRateConnected: false,
  110. heartRateStatusText: '心率带未连接',
  111. heartRateDeviceText: '--',
  112. panelHeartRateValueText: '--',
  113. panelHeartRateUnitText: '',
  114. panelCaloriesValueText: '0',
  115. panelCaloriesUnitText: 'kcal',
  116. panelAverageSpeedValueText: '0',
  117. panelAverageSpeedUnitText: 'km/h',
  118. panelAccuracyValueText: '--',
  119. panelAccuracyUnitText: '',
  120. punchButtonText: '打点',
  121. punchButtonEnabled: false,
  122. punchHintText: '等待进入检查点范围',
  123. punchFeedbackVisible: false,
  124. punchFeedbackText: '',
  125. punchFeedbackTone: 'neutral',
  126. contentCardVisible: false,
  127. contentCardTitle: '',
  128. contentCardBody: '',
  129. punchButtonFxClass: '',
  130. punchFeedbackFxClass: '',
  131. contentCardFxClass: '',
  132. mapPulseVisible: false,
  133. mapPulseLeftPx: 0,
  134. mapPulseTopPx: 0,
  135. mapPulseFxClass: '',
  136. stageFxVisible: false,
  137. stageFxClass: '',
  138. compassTicks: buildCompassTicks(),
  139. compassLabels: buildCompassLabels(),
  140. ...buildSideButtonVisibility('left'),
  141. } as MapPageData,
  142. onLoad() {
  143. const systemInfo = wx.getSystemInfoSync()
  144. const statusBarHeight = systemInfo.statusBarHeight || 0
  145. const menuButtonRect = wx.getMenuButtonBoundingClientRect()
  146. const menuButtonBottom = menuButtonRect && typeof menuButtonRect.bottom === 'number' ? menuButtonRect.bottom : statusBarHeight
  147. mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
  148. onData: (patch) => {
  149. this.setData(patch)
  150. },
  151. })
  152. this.setData({
  153. ...mapEngine.getInitialData(),
  154. showDebugPanel: false,
  155. statusBarHeight,
  156. topInsetHeight: Math.max(statusBarHeight + 12, menuButtonBottom + 20),
  157. hudPanelIndex: 0,
  158. panelTimerText: '00:00:00',
  159. panelMileageText: '0m',
  160. panelActionTagText: '目标',
  161. panelDistanceTagText: '点距',
  162. panelDistanceValueText: '--',
  163. panelDistanceUnitText: '',
  164. panelProgressText: '0/0',
  165. gameSessionStatus: 'idle',
  166. gameModeText: '顺序赛',
  167. panelSpeedValueText: '0',
  168. panelTelemetryTone: 'blue',
  169. panelHeartRateZoneNameText: '--',
  170. panelHeartRateZoneRangeText: '',
  171. heartRateConnected: false,
  172. heartRateStatusText: '心率带未连接',
  173. heartRateDeviceText: '--',
  174. panelHeartRateValueText: '--',
  175. panelHeartRateUnitText: '',
  176. panelCaloriesValueText: '0',
  177. panelCaloriesUnitText: 'kcal',
  178. panelAverageSpeedValueText: '0',
  179. panelAverageSpeedUnitText: 'km/h',
  180. panelAccuracyValueText: '--',
  181. panelAccuracyUnitText: '',
  182. punchButtonText: '打点',
  183. punchButtonEnabled: false,
  184. punchHintText: '等待进入检查点范围',
  185. punchFeedbackVisible: false,
  186. punchFeedbackText: '',
  187. punchFeedbackTone: 'neutral',
  188. contentCardVisible: false,
  189. contentCardTitle: '',
  190. contentCardBody: '',
  191. punchButtonFxClass: '',
  192. punchFeedbackFxClass: '',
  193. contentCardFxClass: '',
  194. mapPulseVisible: false,
  195. mapPulseLeftPx: 0,
  196. mapPulseTopPx: 0,
  197. mapPulseFxClass: '',
  198. stageFxVisible: false,
  199. stageFxClass: '',
  200. compassTicks: buildCompassTicks(),
  201. compassLabels: buildCompassLabels(),
  202. ...buildSideButtonVisibility('left'),
  203. })
  204. },
  205. onReady() {
  206. this.measureStageAndCanvas()
  207. this.loadMapConfigFromRemote()
  208. },
  209. onUnload() {
  210. if (mapEngine) {
  211. mapEngine.destroy()
  212. mapEngine = null
  213. }
  214. },
  215. loadMapConfigFromRemote() {
  216. const currentEngine = mapEngine
  217. if (!currentEngine) {
  218. return
  219. }
  220. loadRemoteMapConfig(REMOTE_GAME_CONFIG_URL)
  221. .then((config) => {
  222. if (mapEngine !== currentEngine) {
  223. return
  224. }
  225. currentEngine.applyRemoteMapConfig(config)
  226. })
  227. .catch((error) => {
  228. if (mapEngine !== currentEngine) {
  229. return
  230. }
  231. const errorMessage = error && error.message ? error.message : '未知错误'
  232. this.setData({
  233. configStatusText: `载入失败: ${errorMessage}`,
  234. statusText: `远程地图配置载入失败: ${errorMessage} (${INTERNAL_BUILD_VERSION})`,
  235. })
  236. })
  237. },
  238. measureStageAndCanvas() {
  239. const page = this
  240. const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
  241. const fallbackRect = getFallbackStageRect()
  242. const rect: MapEngineStageRect = {
  243. width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
  244. height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
  245. left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
  246. top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
  247. }
  248. const currentEngine = mapEngine
  249. if (!currentEngine) {
  250. return
  251. }
  252. currentEngine.setStage(rect)
  253. const canvasQuery = wx.createSelectorQuery().in(page)
  254. canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
  255. canvasQuery.select('#routeLabelCanvas').fields({ node: true, size: true })
  256. canvasQuery.exec((canvasRes) => {
  257. const canvasRef = canvasRes[0] as any
  258. const labelCanvasRef = canvasRes[1] as any
  259. if (!canvasRef || !canvasRef.node) {
  260. page.setData({
  261. statusText: `WebGL 引擎初始化失败 (${INTERNAL_BUILD_VERSION})`,
  262. })
  263. return
  264. }
  265. const dpr = wx.getSystemInfoSync().pixelRatio || 1
  266. try {
  267. currentEngine.attachCanvas(
  268. canvasRef.node,
  269. rect.width,
  270. rect.height,
  271. dpr,
  272. labelCanvasRef && labelCanvasRef.node ? labelCanvasRef.node : undefined,
  273. )
  274. } catch (error) {
  275. page.setData({
  276. statusText: `WebGL 鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  277. })
  278. }
  279. })
  280. }
  281. const query = wx.createSelectorQuery().in(page)
  282. query.select('.map-stage').boundingClientRect()
  283. query.exec((res) => {
  284. const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
  285. applyStage(rect)
  286. })
  287. },
  288. handleTouchStart(event: WechatMiniprogram.TouchEvent) {
  289. if (mapEngine) {
  290. mapEngine.handleTouchStart(event)
  291. }
  292. },
  293. handleTouchMove(event: WechatMiniprogram.TouchEvent) {
  294. if (mapEngine) {
  295. mapEngine.handleTouchMove(event)
  296. }
  297. },
  298. handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
  299. if (mapEngine) {
  300. mapEngine.handleTouchEnd(event)
  301. }
  302. },
  303. handleTouchCancel() {
  304. if (mapEngine) {
  305. mapEngine.handleTouchCancel()
  306. }
  307. },
  308. handleRecenter() {
  309. if (mapEngine) {
  310. mapEngine.handleRecenter()
  311. }
  312. },
  313. handleRotateStep() {
  314. if (mapEngine) {
  315. mapEngine.handleRotateStep()
  316. }
  317. },
  318. handleRotationReset() {
  319. if (mapEngine) {
  320. mapEngine.handleRotationReset()
  321. }
  322. },
  323. handleSetManualMode() {
  324. if (mapEngine) {
  325. mapEngine.handleSetManualMode()
  326. }
  327. },
  328. handleSetNorthUpMode() {
  329. if (mapEngine) {
  330. mapEngine.handleSetNorthUpMode()
  331. }
  332. },
  333. handleSetHeadingUpMode() {
  334. if (mapEngine) {
  335. mapEngine.handleSetHeadingUpMode()
  336. }
  337. },
  338. handleCycleNorthReferenceMode() {
  339. if (mapEngine) {
  340. mapEngine.handleCycleNorthReferenceMode()
  341. }
  342. },
  343. handleAutoRotateCalibrate() {
  344. if (mapEngine) {
  345. mapEngine.handleAutoRotateCalibrate()
  346. }
  347. },
  348. handleToggleGpsTracking() {
  349. if (mapEngine) {
  350. mapEngine.handleToggleGpsTracking()
  351. }
  352. },
  353. handleConnectHeartRate() {
  354. if (mapEngine) {
  355. mapEngine.handleConnectHeartRate()
  356. }
  357. },
  358. handleDisconnectHeartRate() {
  359. if (mapEngine) {
  360. mapEngine.handleDisconnectHeartRate()
  361. }
  362. },
  363. handleDebugHeartRateBlue() {
  364. if (mapEngine) {
  365. mapEngine.handleDebugHeartRateTone('blue')
  366. }
  367. },
  368. handleDebugHeartRatePurple() {
  369. if (mapEngine) {
  370. mapEngine.handleDebugHeartRateTone('purple')
  371. }
  372. },
  373. handleDebugHeartRateGreen() {
  374. if (mapEngine) {
  375. mapEngine.handleDebugHeartRateTone('green')
  376. }
  377. },
  378. handleDebugHeartRateYellow() {
  379. if (mapEngine) {
  380. mapEngine.handleDebugHeartRateTone('yellow')
  381. }
  382. },
  383. handleDebugHeartRateOrange() {
  384. if (mapEngine) {
  385. mapEngine.handleDebugHeartRateTone('orange')
  386. }
  387. },
  388. handleDebugHeartRateRed() {
  389. if (mapEngine) {
  390. mapEngine.handleDebugHeartRateTone('red')
  391. }
  392. },
  393. handleClearDebugHeartRate() {
  394. if (mapEngine) {
  395. mapEngine.handleClearDebugHeartRate()
  396. }
  397. },
  398. handleToggleOsmReference() {
  399. if (mapEngine) {
  400. mapEngine.handleToggleOsmReference()
  401. }
  402. },
  403. handleStartGame() {
  404. if (mapEngine) {
  405. mapEngine.handleStartGame()
  406. }
  407. },
  408. handleSetClassicMode() {
  409. if (mapEngine) {
  410. mapEngine.handleSetGameMode('classic-sequential')
  411. }
  412. },
  413. handleSetScoreOMode() {
  414. if (mapEngine) {
  415. mapEngine.handleSetGameMode('score-o')
  416. }
  417. },
  418. handleOverlayTouch() {},
  419. handlePunchAction() {
  420. if (!this.data.punchButtonEnabled) {
  421. return
  422. }
  423. if (mapEngine) {
  424. mapEngine.handlePunchAction()
  425. }
  426. },
  427. handleCloseContentCard() {
  428. if (mapEngine) {
  429. mapEngine.closeContentCard()
  430. }
  431. },
  432. handleHudPanelChange(event: WechatMiniprogram.CustomEvent<{ current: number }>) {
  433. this.setData({
  434. hudPanelIndex: event.detail.current || 0,
  435. })
  436. },
  437. handleCycleSideButtons() {
  438. this.setData(buildSideButtonVisibility(getNextSideButtonMode(this.data.sideButtonMode)))
  439. },
  440. handleToggleMapRotateMode() {
  441. if (!mapEngine) {
  442. return
  443. }
  444. if (this.data.orientationMode === 'heading-up') {
  445. mapEngine.handleSetManualMode()
  446. return
  447. }
  448. mapEngine.handleSetHeadingUpMode()
  449. },
  450. handleToggleDebugPanel() {
  451. this.setData({
  452. showDebugPanel: !this.data.showDebugPanel,
  453. })
  454. },
  455. handleCloseDebugPanel() {
  456. this.setData({
  457. showDebugPanel: false,
  458. })
  459. },
  460. handleDebugPanelTap() {},
  461. })