map.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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/14',
  98. panelSpeedValueText: '0',
  99. compassTicks: buildCompassTicks(),
  100. compassLabels: buildCompassLabels(),
  101. ...buildSideButtonVisibility('left'),
  102. } as MapPageData,
  103. onLoad() {
  104. const systemInfo = wx.getSystemInfoSync()
  105. const statusBarHeight = systemInfo.statusBarHeight || 0
  106. const menuButtonRect = wx.getMenuButtonBoundingClientRect()
  107. const menuButtonBottom = menuButtonRect && typeof menuButtonRect.bottom === 'number' ? menuButtonRect.bottom : statusBarHeight
  108. mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
  109. onData: (patch) => {
  110. this.setData(patch)
  111. },
  112. })
  113. this.setData({
  114. ...mapEngine.getInitialData(),
  115. showDebugPanel: false,
  116. statusBarHeight,
  117. topInsetHeight: Math.max(statusBarHeight + 12, menuButtonBottom + 20),
  118. panelTimerText: '00:00:00',
  119. panelMileageText: '0m',
  120. panelDistanceValueText: '108',
  121. panelProgressText: '0/14',
  122. panelSpeedValueText: '0',
  123. compassTicks: buildCompassTicks(),
  124. compassLabels: buildCompassLabels(),
  125. ...buildSideButtonVisibility('left'),
  126. })
  127. },
  128. onReady() {
  129. this.measureStageAndCanvas()
  130. this.loadMapConfigFromRemote()
  131. },
  132. onUnload() {
  133. if (mapEngine) {
  134. mapEngine.destroy()
  135. mapEngine = null
  136. }
  137. },
  138. loadMapConfigFromRemote() {
  139. const currentEngine = mapEngine
  140. if (!currentEngine) {
  141. return
  142. }
  143. loadRemoteMapConfig(REMOTE_GAME_CONFIG_URL)
  144. .then((config) => {
  145. if (mapEngine !== currentEngine) {
  146. return
  147. }
  148. currentEngine.applyRemoteMapConfig(config)
  149. })
  150. .catch((error) => {
  151. if (mapEngine !== currentEngine) {
  152. return
  153. }
  154. const errorMessage = error && error.message ? error.message : '鏈煡閿欒'
  155. this.setData({
  156. configStatusText: `杞藉叆澶辫触: ${errorMessage}`,
  157. statusText: `杩滅▼鍦板浘閰嶇疆杞藉叆澶辫触: ${errorMessage} (${INTERNAL_BUILD_VERSION})`,
  158. })
  159. })
  160. },
  161. measureStageAndCanvas() {
  162. const page = this
  163. const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
  164. const fallbackRect = getFallbackStageRect()
  165. const rect: MapEngineStageRect = {
  166. width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
  167. height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
  168. left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
  169. top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
  170. }
  171. const currentEngine = mapEngine
  172. if (!currentEngine) {
  173. return
  174. }
  175. currentEngine.setStage(rect)
  176. const canvasQuery = wx.createSelectorQuery().in(page)
  177. canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
  178. canvasQuery.exec((canvasRes) => {
  179. const canvasRef = canvasRes[0] as any
  180. if (!canvasRef || !canvasRef.node) {
  181. page.setData({
  182. statusText: `WebGL 寮曟搸鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  183. })
  184. return
  185. }
  186. const dpr = wx.getSystemInfoSync().pixelRatio || 1
  187. try {
  188. currentEngine.attachCanvas(canvasRef.node, rect.width, rect.height, dpr)
  189. } catch (error) {
  190. page.setData({
  191. statusText: `WebGL 鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  192. })
  193. }
  194. })
  195. }
  196. const query = wx.createSelectorQuery().in(page)
  197. query.select('.map-stage').boundingClientRect()
  198. query.exec((res) => {
  199. const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
  200. applyStage(rect)
  201. })
  202. },
  203. handleTouchStart(event: WechatMiniprogram.TouchEvent) {
  204. if (mapEngine) {
  205. mapEngine.handleTouchStart(event)
  206. }
  207. },
  208. handleTouchMove(event: WechatMiniprogram.TouchEvent) {
  209. if (mapEngine) {
  210. mapEngine.handleTouchMove(event)
  211. }
  212. },
  213. handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
  214. if (mapEngine) {
  215. mapEngine.handleTouchEnd(event)
  216. }
  217. },
  218. handleTouchCancel() {
  219. if (mapEngine) {
  220. mapEngine.handleTouchCancel()
  221. }
  222. },
  223. handleRecenter() {
  224. if (mapEngine) {
  225. mapEngine.handleRecenter()
  226. }
  227. },
  228. handleRotateStep() {
  229. if (mapEngine) {
  230. mapEngine.handleRotateStep()
  231. }
  232. },
  233. handleRotationReset() {
  234. if (mapEngine) {
  235. mapEngine.handleRotationReset()
  236. }
  237. },
  238. handleSetManualMode() {
  239. if (mapEngine) {
  240. mapEngine.handleSetManualMode()
  241. }
  242. },
  243. handleSetNorthUpMode() {
  244. if (mapEngine) {
  245. mapEngine.handleSetNorthUpMode()
  246. }
  247. },
  248. handleSetHeadingUpMode() {
  249. if (mapEngine) {
  250. mapEngine.handleSetHeadingUpMode()
  251. }
  252. },
  253. handleCycleNorthReferenceMode() {
  254. if (mapEngine) {
  255. mapEngine.handleCycleNorthReferenceMode()
  256. }
  257. },
  258. handleAutoRotateCalibrate() {
  259. if (mapEngine) {
  260. mapEngine.handleAutoRotateCalibrate()
  261. }
  262. },
  263. handleToggleGpsTracking() {
  264. if (mapEngine) {
  265. mapEngine.handleToggleGpsTracking()
  266. }
  267. },
  268. handleToggleOsmReference() {
  269. if (mapEngine) {
  270. mapEngine.handleToggleOsmReference()
  271. }
  272. },
  273. handleCycleSideButtons() {
  274. this.setData(buildSideButtonVisibility(getNextSideButtonMode(this.data.sideButtonMode)))
  275. },
  276. handleToggleMapRotateMode() {
  277. if (!mapEngine) {
  278. return
  279. }
  280. if (this.data.orientationMode === 'heading-up') {
  281. mapEngine.handleSetManualMode()
  282. return
  283. }
  284. mapEngine.handleSetHeadingUpMode()
  285. },
  286. handleToggleDebugPanel() {
  287. this.setData({
  288. showDebugPanel: !this.data.showDebugPanel,
  289. })
  290. },
  291. handleCloseDebugPanel() {
  292. this.setData({
  293. showDebugPanel: false,
  294. })
  295. },
  296. handleDebugPanelTap() {},
  297. })