map.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. panelTimerText: string
  21. panelMileageText: string
  22. panelDistanceValueText: string
  23. panelProgressText: string
  24. panelSpeedValueText: string
  25. compassTicks: CompassTickData[]
  26. compassLabels: CompassLabelData[]
  27. sideButtonMode: SideButtonMode
  28. showLeftButtonGroup: boolean
  29. showRightButtonGroups: boolean
  30. showBottomDebugButton: boolean
  31. }
  32. const INTERNAL_BUILD_VERSION = 'map-build-134'
  33. const REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/wxmini/test/game.json'
  34. let mapEngine: MapEngine | null = null
  35. function buildSideButtonVisibility(mode: SideButtonMode) {
  36. return {
  37. sideButtonMode: mode,
  38. showLeftButtonGroup: mode === 'all' || mode === 'left' || mode === 'right',
  39. showRightButtonGroups: mode === 'all' || mode === 'right',
  40. showBottomDebugButton: mode !== 'hidden',
  41. }
  42. }
  43. function getNextSideButtonMode(currentMode: SideButtonMode): SideButtonMode {
  44. if (currentMode === 'all') {
  45. return 'left'
  46. }
  47. if (currentMode === 'left') {
  48. return 'right'
  49. }
  50. if (currentMode === 'right') {
  51. return 'hidden'
  52. }
  53. return 'left'
  54. }
  55. function buildCompassTicks(): CompassTickData[] {
  56. const ticks: CompassTickData[] = []
  57. for (let angle = 0; angle < 360; angle += 5) {
  58. ticks.push({
  59. angle,
  60. long: angle % 15 === 0,
  61. major: angle % 45 === 0,
  62. })
  63. }
  64. return ticks
  65. }
  66. function buildCompassLabels(): CompassLabelData[] {
  67. return [
  68. { text: '\u5317', angle: 0, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal compass-widget__mark--north' },
  69. { text: '\u4e1c\u5317', angle: 45, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northeast' },
  70. { text: '\u4e1c', angle: 90, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  71. { text: '\u4e1c\u5357', angle: 135, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  72. { text: '\u5357', angle: 180, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  73. { text: '\u897f\u5357', angle: 225, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  74. { text: '\u897f', angle: 270, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  75. { text: '\u897f\u5317', angle: 315, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northwest' },
  76. ]
  77. }
  78. function getFallbackStageRect(): MapEngineStageRect {
  79. const systemInfo = wx.getSystemInfoSync()
  80. const width = Math.max(320, systemInfo.windowWidth)
  81. const height = Math.max(280, systemInfo.windowHeight)
  82. return {
  83. width,
  84. height,
  85. left: 0,
  86. top: 0,
  87. }
  88. }
  89. Page({
  90. data: {
  91. showDebugPanel: false,
  92. statusBarHeight: 0,
  93. topInsetHeight: 12,
  94. panelTimerText: '00:00:00',
  95. panelMileageText: '0m',
  96. panelDistanceValueText: '108',
  97. panelProgressText: '0/0',
  98. gameSessionStatus: 'idle',
  99. panelSpeedValueText: '0',
  100. punchButtonText: '打点',
  101. punchButtonEnabled: false,
  102. punchHintText: '等待进入检查点范围',
  103. punchFeedbackVisible: false,
  104. punchFeedbackText: '',
  105. punchFeedbackTone: 'neutral',
  106. contentCardVisible: false,
  107. contentCardTitle: '',
  108. contentCardBody: '',
  109. compassTicks: buildCompassTicks(),
  110. compassLabels: buildCompassLabels(),
  111. ...buildSideButtonVisibility('left'),
  112. } as MapPageData,
  113. onLoad() {
  114. const systemInfo = wx.getSystemInfoSync()
  115. const statusBarHeight = systemInfo.statusBarHeight || 0
  116. const menuButtonRect = wx.getMenuButtonBoundingClientRect()
  117. const menuButtonBottom = menuButtonRect && typeof menuButtonRect.bottom === 'number' ? menuButtonRect.bottom : statusBarHeight
  118. mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
  119. onData: (patch) => {
  120. this.setData(patch)
  121. },
  122. })
  123. this.setData({
  124. ...mapEngine.getInitialData(),
  125. showDebugPanel: false,
  126. statusBarHeight,
  127. topInsetHeight: Math.max(statusBarHeight + 12, menuButtonBottom + 20),
  128. panelTimerText: '00:00:00',
  129. panelMileageText: '0m',
  130. panelDistanceValueText: '108',
  131. panelProgressText: '0/0',
  132. gameSessionStatus: 'idle',
  133. panelSpeedValueText: '0',
  134. punchButtonText: '打点',
  135. punchButtonEnabled: false,
  136. punchHintText: '等待进入检查点范围',
  137. punchFeedbackVisible: false,
  138. punchFeedbackText: '',
  139. punchFeedbackTone: 'neutral',
  140. contentCardVisible: false,
  141. contentCardTitle: '',
  142. contentCardBody: '',
  143. compassTicks: buildCompassTicks(),
  144. compassLabels: buildCompassLabels(),
  145. ...buildSideButtonVisibility('left'),
  146. })
  147. },
  148. onReady() {
  149. this.measureStageAndCanvas()
  150. this.loadMapConfigFromRemote()
  151. },
  152. onUnload() {
  153. if (mapEngine) {
  154. mapEngine.destroy()
  155. mapEngine = null
  156. }
  157. },
  158. loadMapConfigFromRemote() {
  159. const currentEngine = mapEngine
  160. if (!currentEngine) {
  161. return
  162. }
  163. loadRemoteMapConfig(REMOTE_GAME_CONFIG_URL)
  164. .then((config) => {
  165. if (mapEngine !== currentEngine) {
  166. return
  167. }
  168. currentEngine.applyRemoteMapConfig(config)
  169. })
  170. .catch((error) => {
  171. if (mapEngine !== currentEngine) {
  172. return
  173. }
  174. const errorMessage = error && error.message ? error.message : '鏈煡閿欒'
  175. this.setData({
  176. configStatusText: `杞藉叆澶辫触: ${errorMessage}`,
  177. statusText: `杩滅▼鍦板浘閰嶇疆杞藉叆澶辫触: ${errorMessage} (${INTERNAL_BUILD_VERSION})`,
  178. })
  179. })
  180. },
  181. measureStageAndCanvas() {
  182. const page = this
  183. const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
  184. const fallbackRect = getFallbackStageRect()
  185. const rect: MapEngineStageRect = {
  186. width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
  187. height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
  188. left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
  189. top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
  190. }
  191. const currentEngine = mapEngine
  192. if (!currentEngine) {
  193. return
  194. }
  195. currentEngine.setStage(rect)
  196. const canvasQuery = wx.createSelectorQuery().in(page)
  197. canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
  198. canvasQuery.select('#routeLabelCanvas').fields({ node: true, size: true })
  199. canvasQuery.exec((canvasRes) => {
  200. const canvasRef = canvasRes[0] as any
  201. const labelCanvasRef = canvasRes[1] as any
  202. if (!canvasRef || !canvasRef.node) {
  203. page.setData({
  204. statusText: `WebGL 寮曟搸鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  205. })
  206. return
  207. }
  208. const dpr = wx.getSystemInfoSync().pixelRatio || 1
  209. try {
  210. currentEngine.attachCanvas(
  211. canvasRef.node,
  212. rect.width,
  213. rect.height,
  214. dpr,
  215. labelCanvasRef && labelCanvasRef.node ? labelCanvasRef.node : undefined,
  216. )
  217. } catch (error) {
  218. page.setData({
  219. statusText: `WebGL 鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  220. })
  221. }
  222. })
  223. }
  224. const query = wx.createSelectorQuery().in(page)
  225. query.select('.map-stage').boundingClientRect()
  226. query.exec((res) => {
  227. const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
  228. applyStage(rect)
  229. })
  230. },
  231. handleTouchStart(event: WechatMiniprogram.TouchEvent) {
  232. if (mapEngine) {
  233. mapEngine.handleTouchStart(event)
  234. }
  235. },
  236. handleTouchMove(event: WechatMiniprogram.TouchEvent) {
  237. if (mapEngine) {
  238. mapEngine.handleTouchMove(event)
  239. }
  240. },
  241. handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
  242. if (mapEngine) {
  243. mapEngine.handleTouchEnd(event)
  244. }
  245. },
  246. handleTouchCancel() {
  247. if (mapEngine) {
  248. mapEngine.handleTouchCancel()
  249. }
  250. },
  251. handleRecenter() {
  252. if (mapEngine) {
  253. mapEngine.handleRecenter()
  254. }
  255. },
  256. handleRotateStep() {
  257. if (mapEngine) {
  258. mapEngine.handleRotateStep()
  259. }
  260. },
  261. handleRotationReset() {
  262. if (mapEngine) {
  263. mapEngine.handleRotationReset()
  264. }
  265. },
  266. handleSetManualMode() {
  267. if (mapEngine) {
  268. mapEngine.handleSetManualMode()
  269. }
  270. },
  271. handleSetNorthUpMode() {
  272. if (mapEngine) {
  273. mapEngine.handleSetNorthUpMode()
  274. }
  275. },
  276. handleSetHeadingUpMode() {
  277. if (mapEngine) {
  278. mapEngine.handleSetHeadingUpMode()
  279. }
  280. },
  281. handleCycleNorthReferenceMode() {
  282. if (mapEngine) {
  283. mapEngine.handleCycleNorthReferenceMode()
  284. }
  285. },
  286. handleAutoRotateCalibrate() {
  287. if (mapEngine) {
  288. mapEngine.handleAutoRotateCalibrate()
  289. }
  290. },
  291. handleToggleGpsTracking() {
  292. if (mapEngine) {
  293. mapEngine.handleToggleGpsTracking()
  294. }
  295. },
  296. handleToggleOsmReference() {
  297. if (mapEngine) {
  298. mapEngine.handleToggleOsmReference()
  299. }
  300. },
  301. handleStartGame() {
  302. if (mapEngine) {
  303. mapEngine.handleStartGame()
  304. }
  305. },
  306. handleOverlayTouch() {},
  307. handlePunchAction() {
  308. if (!this.data.punchButtonEnabled) {
  309. return
  310. }
  311. if (mapEngine) {
  312. mapEngine.handlePunchAction()
  313. }
  314. },
  315. handleCloseContentCard() {
  316. if (mapEngine) {
  317. mapEngine.closeContentCard()
  318. }
  319. },
  320. handleCycleSideButtons() {
  321. this.setData(buildSideButtonVisibility(getNextSideButtonMode(this.data.sideButtonMode)))
  322. },
  323. handleToggleMapRotateMode() {
  324. if (!mapEngine) {
  325. return
  326. }
  327. if (this.data.orientationMode === 'heading-up') {
  328. mapEngine.handleSetManualMode()
  329. return
  330. }
  331. mapEngine.handleSetHeadingUpMode()
  332. },
  333. handleToggleDebugPanel() {
  334. this.setData({
  335. showDebugPanel: !this.data.showDebugPanel,
  336. })
  337. },
  338. handleCloseDebugPanel() {
  339. this.setData({
  340. showDebugPanel: false,
  341. })
  342. },
  343. handleDebugPanelTap() {},
  344. })