map.ts 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224
  1. import {
  2. MapEngine,
  3. type MapEngineGameInfoRow,
  4. type MapEngineGameInfoSnapshot,
  5. type MapEngineResultSnapshot,
  6. type MapEngineStageRect,
  7. type MapEngineViewState,
  8. } from '../../engine/map/mapEngine'
  9. import { loadRemoteMapConfig } from '../../utils/remoteMapConfig'
  10. import { type AnimationLevel } from '../../utils/animationLevel'
  11. import { type H5ExperienceFallbackPayload, type H5ExperienceRequest } from '../../game/experience/h5Experience'
  12. type CompassTickData = {
  13. angle: number
  14. long: boolean
  15. major: boolean
  16. }
  17. type CompassLabelData = {
  18. text: string
  19. angle: number
  20. rotateBack: number
  21. radius: number
  22. className: string
  23. }
  24. type ScaleRulerMinorTickData = {
  25. key: string
  26. topPx: number
  27. long: boolean
  28. }
  29. type ScaleRulerMajorMarkData = {
  30. key: string
  31. topPx: number
  32. label: string
  33. }
  34. type SideButtonMode = 'shown' | 'hidden'
  35. type SideActionButtonState = 'muted' | 'default' | 'active'
  36. type SideButtonPlacement = 'left' | 'right'
  37. type CenterScaleRulerAnchorMode = 'screen-center' | 'compass-center'
  38. type UserNorthReferenceMode = 'magnetic' | 'true'
  39. type CompassTuningProfile = 'smooth' | 'balanced' | 'responsive'
  40. type SettingLockKey =
  41. | 'lockAnimationLevel'
  42. | 'lockSideButtonPlacement'
  43. | 'lockAutoRotate'
  44. | 'lockCompassTuning'
  45. | 'lockScaleRulerVisible'
  46. | 'lockScaleRulerAnchor'
  47. | 'lockNorthReference'
  48. | 'lockHeartRateDevice'
  49. type StoredUserSettings = {
  50. animationLevel?: AnimationLevel
  51. autoRotateEnabled?: boolean
  52. compassTuningProfile?: CompassTuningProfile
  53. northReferenceMode?: UserNorthReferenceMode
  54. sideButtonPlacement?: SideButtonPlacement
  55. showCenterScaleRuler?: boolean
  56. centerScaleRulerAnchorMode?: CenterScaleRulerAnchorMode
  57. lockAnimationLevel?: boolean
  58. lockSideButtonPlacement?: boolean
  59. lockAutoRotate?: boolean
  60. lockCompassTuning?: boolean
  61. lockScaleRulerVisible?: boolean
  62. lockScaleRulerAnchor?: boolean
  63. lockNorthReference?: boolean
  64. lockHeartRateDevice?: boolean
  65. }
  66. type MapPageData = MapEngineViewState & {
  67. showDebugPanel: boolean
  68. showGameInfoPanel: boolean
  69. showResultScene: boolean
  70. showSystemSettingsPanel: boolean
  71. showCenterScaleRuler: boolean
  72. showPunchHintBanner: boolean
  73. centerScaleRulerAnchorMode: CenterScaleRulerAnchorMode
  74. statusBarHeight: number
  75. topInsetHeight: number
  76. hudPanelIndex: number
  77. configSourceText: string
  78. mockBridgeUrlDraft: string
  79. mockHeartRateBridgeUrlDraft: string
  80. gameInfoTitle: string
  81. gameInfoSubtitle: string
  82. gameInfoLocalRows: MapEngineGameInfoRow[]
  83. gameInfoGlobalRows: MapEngineGameInfoRow[]
  84. resultSceneTitle: string
  85. resultSceneSubtitle: string
  86. resultSceneHeroLabel: string
  87. resultSceneHeroValue: string
  88. resultSceneRows: MapEngineGameInfoRow[]
  89. panelTimerText: string
  90. panelMileageText: string
  91. panelDistanceValueText: string
  92. panelProgressText: string
  93. panelSpeedValueText: string
  94. panelTimerFxClass: string
  95. panelMileageFxClass: string
  96. panelSpeedFxClass: string
  97. panelHeartRateFxClass: string
  98. compassTicks: CompassTickData[]
  99. compassLabels: CompassLabelData[]
  100. sideButtonMode: SideButtonMode
  101. sideButtonPlacement: SideButtonPlacement
  102. autoRotateEnabled: boolean
  103. lockAnimationLevel: boolean
  104. lockSideButtonPlacement: boolean
  105. lockAutoRotate: boolean
  106. lockCompassTuning: boolean
  107. lockScaleRulerVisible: boolean
  108. lockScaleRulerAnchor: boolean
  109. lockNorthReference: boolean
  110. lockHeartRateDevice: boolean
  111. sideToggleIconSrc: string
  112. sideButton2Class: string
  113. sideButton4Class: string
  114. sideButton11Class: string
  115. sideButton12Class: string
  116. sideButton13Class: string
  117. sideButton14Class: string
  118. sideButton16Class: string
  119. centerScaleRulerVisible: boolean
  120. centerScaleRulerCenterXPx: number
  121. centerScaleRulerZeroYPx: number
  122. centerScaleRulerHeightPx: number
  123. centerScaleRulerAxisBottomPx: number
  124. centerScaleRulerZeroVisible: boolean
  125. centerScaleRulerZeroLabel: string
  126. centerScaleRulerMinorTicks: ScaleRulerMinorTickData[]
  127. centerScaleRulerMajorMarks: ScaleRulerMajorMarkData[]
  128. showLeftButtonGroup: boolean
  129. showRightButtonGroups: boolean
  130. showBottomDebugButton: boolean
  131. }
  132. const INTERNAL_BUILD_VERSION = 'map-build-291'
  133. const USER_SETTINGS_STORAGE_KEY = 'cmr_user_settings_v1'
  134. const CLASSIC_REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/gotomars/event/classic-sequential.json'
  135. const SCORE_O_REMOTE_GAME_CONFIG_URL = 'https://oss-mbh5.colormaprun.com/gotomars/event/score-o.json'
  136. const PUNCH_HINT_AUTO_HIDE_MS = 30000
  137. let mapEngine: MapEngine | null = null
  138. let stageCanvasAttached = false
  139. let gameInfoPanelSyncTimer = 0
  140. let centerScaleRulerSyncTimer = 0
  141. let centerScaleRulerUpdateTimer = 0
  142. let punchHintDismissTimer = 0
  143. let panelTimerFxTimer = 0
  144. let panelMileageFxTimer = 0
  145. let panelSpeedFxTimer = 0
  146. let panelHeartRateFxTimer = 0
  147. let lastCenterScaleRulerStablePatch: Pick<
  148. MapPageData,
  149. | 'centerScaleRulerVisible'
  150. | 'centerScaleRulerCenterXPx'
  151. | 'centerScaleRulerZeroYPx'
  152. | 'centerScaleRulerHeightPx'
  153. | 'centerScaleRulerAxisBottomPx'
  154. | 'centerScaleRulerZeroVisible'
  155. | 'centerScaleRulerZeroLabel'
  156. | 'centerScaleRulerMinorTicks'
  157. | 'centerScaleRulerMajorMarks'
  158. > = {
  159. centerScaleRulerVisible: false,
  160. centerScaleRulerCenterXPx: 0,
  161. centerScaleRulerZeroYPx: 0,
  162. centerScaleRulerHeightPx: 0,
  163. centerScaleRulerAxisBottomPx: 0,
  164. centerScaleRulerZeroVisible: false,
  165. centerScaleRulerZeroLabel: '0 m',
  166. centerScaleRulerMinorTicks: [],
  167. centerScaleRulerMajorMarks: [],
  168. }
  169. let centerScaleRulerInputCache: Partial<Pick<
  170. MapPageData,
  171. 'stageWidth'
  172. | 'stageHeight'
  173. | 'zoom'
  174. | 'centerTileY'
  175. | 'tileSizePx'
  176. | 'previewScale'
  177. >> = {}
  178. const DEBUG_ONLY_VIEW_KEYS = new Set<string>([
  179. 'buildVersion',
  180. 'renderMode',
  181. 'projectionMode',
  182. 'mapReady',
  183. 'mapReadyText',
  184. 'mapName',
  185. 'configStatusText',
  186. 'deviceHeadingText',
  187. 'devicePoseText',
  188. 'headingConfidenceText',
  189. 'accelerometerText',
  190. 'gyroscopeText',
  191. 'deviceMotionText',
  192. 'compassSourceText',
  193. 'compassTuningProfile',
  194. 'compassTuningProfileText',
  195. 'northReferenceButtonText',
  196. 'autoRotateSourceText',
  197. 'autoRotateCalibrationText',
  198. 'northReferenceText',
  199. 'centerText',
  200. 'tileSource',
  201. 'visibleTileCount',
  202. 'readyTileCount',
  203. 'memoryTileCount',
  204. 'diskTileCount',
  205. 'memoryHitCount',
  206. 'diskHitCount',
  207. 'networkFetchCount',
  208. 'cacheHitRateText',
  209. 'locationSourceMode',
  210. 'locationSourceText',
  211. 'mockBridgeConnected',
  212. 'mockBridgeStatusText',
  213. 'mockBridgeUrlText',
  214. 'mockCoordText',
  215. 'mockSpeedText',
  216. 'gpsCoordText',
  217. 'heartRateSourceMode',
  218. 'heartRateSourceText',
  219. 'heartRateConnected',
  220. 'heartRateStatusText',
  221. 'heartRateDeviceText',
  222. 'heartRateScanText',
  223. 'heartRateDiscoveredDevices',
  224. 'mockHeartRateBridgeConnected',
  225. 'mockHeartRateBridgeStatusText',
  226. 'mockHeartRateBridgeUrlText',
  227. 'mockHeartRateText',
  228. ])
  229. const CENTER_SCALE_RULER_DEP_KEYS = new Set<string>([
  230. 'showCenterScaleRuler',
  231. 'centerScaleRulerAnchorMode',
  232. 'stageWidth',
  233. 'stageHeight',
  234. 'topInsetHeight',
  235. 'zoom',
  236. 'centerTileY',
  237. 'tileSizePx',
  238. 'previewScale',
  239. ])
  240. const CENTER_SCALE_RULER_CACHE_KEYS: Array<keyof typeof centerScaleRulerInputCache> = [
  241. 'stageWidth',
  242. 'stageHeight',
  243. 'zoom',
  244. 'centerTileY',
  245. 'tileSizePx',
  246. 'previewScale',
  247. ]
  248. const RULER_ONLY_VIEW_KEYS = new Set<string>([
  249. 'zoom',
  250. 'centerTileX',
  251. 'centerTileY',
  252. 'tileSizePx',
  253. 'previewScale',
  254. 'stageWidth',
  255. 'stageHeight',
  256. 'stageLeft',
  257. 'stageTop',
  258. ])
  259. const SIDE_BUTTON_DEP_KEYS = new Set<string>([
  260. 'sideButtonMode',
  261. 'showGameInfoPanel',
  262. 'showCenterScaleRuler',
  263. 'centerScaleRulerAnchorMode',
  264. 'skipButtonEnabled',
  265. 'gameSessionStatus',
  266. 'gpsLockEnabled',
  267. 'gpsLockAvailable',
  268. ])
  269. function hasAnyPatchKey(patch: Record<string, unknown>, keys: Set<string>): boolean {
  270. return Object.keys(patch).some((key) => keys.has(key))
  271. }
  272. function filterDebugOnlyPatch(
  273. patch: Partial<MapPageData>,
  274. includeDebugFields: boolean,
  275. includeRulerFields: boolean,
  276. ): Partial<MapPageData> {
  277. if (includeDebugFields && includeRulerFields) {
  278. return patch
  279. }
  280. const filteredPatch: Partial<MapPageData> = {}
  281. for (const [key, value] of Object.entries(patch)) {
  282. if (!includeDebugFields && DEBUG_ONLY_VIEW_KEYS.has(key)) {
  283. continue
  284. }
  285. if (!includeRulerFields && RULER_ONLY_VIEW_KEYS.has(key)) {
  286. continue
  287. }
  288. {
  289. ;(filteredPatch as Record<string, unknown>)[key] = value
  290. }
  291. }
  292. return filteredPatch
  293. }
  294. function clearGameInfoPanelSyncTimer() {
  295. if (gameInfoPanelSyncTimer) {
  296. clearTimeout(gameInfoPanelSyncTimer)
  297. gameInfoPanelSyncTimer = 0
  298. }
  299. }
  300. function clearCenterScaleRulerSyncTimer() {
  301. if (centerScaleRulerSyncTimer) {
  302. clearTimeout(centerScaleRulerSyncTimer)
  303. centerScaleRulerSyncTimer = 0
  304. }
  305. }
  306. function clearCenterScaleRulerUpdateTimer() {
  307. if (centerScaleRulerUpdateTimer) {
  308. clearTimeout(centerScaleRulerUpdateTimer)
  309. centerScaleRulerUpdateTimer = 0
  310. }
  311. }
  312. function clearPunchHintDismissTimer() {
  313. if (punchHintDismissTimer) {
  314. clearTimeout(punchHintDismissTimer)
  315. punchHintDismissTimer = 0
  316. }
  317. }
  318. function clearHudFxTimer(key: 'timer' | 'mileage' | 'speed' | 'heartRate') {
  319. const timerMap = {
  320. timer: panelTimerFxTimer,
  321. mileage: panelMileageFxTimer,
  322. speed: panelSpeedFxTimer,
  323. heartRate: panelHeartRateFxTimer,
  324. }
  325. const timer = timerMap[key]
  326. if (timer) {
  327. clearTimeout(timer)
  328. }
  329. if (key === 'timer') {
  330. panelTimerFxTimer = 0
  331. } else if (key === 'mileage') {
  332. panelMileageFxTimer = 0
  333. } else if (key === 'speed') {
  334. panelSpeedFxTimer = 0
  335. } else {
  336. panelHeartRateFxTimer = 0
  337. }
  338. }
  339. function updateCenterScaleRulerInputCache(patch: Partial<MapPageData>) {
  340. for (const key of CENTER_SCALE_RULER_CACHE_KEYS) {
  341. if (Object.prototype.hasOwnProperty.call(patch, key)) {
  342. ;(centerScaleRulerInputCache as Record<string, unknown>)[key] =
  343. (patch as Record<string, unknown>)[key]
  344. }
  345. }
  346. }
  347. function loadStoredUserSettings(): StoredUserSettings {
  348. try {
  349. const stored = wx.getStorageSync(USER_SETTINGS_STORAGE_KEY)
  350. if (!stored || typeof stored !== 'object') {
  351. return {}
  352. }
  353. const normalized = stored as Record<string, unknown>
  354. const settings: StoredUserSettings = {}
  355. if (normalized.animationLevel === 'standard' || normalized.animationLevel === 'lite') {
  356. settings.animationLevel = normalized.animationLevel
  357. }
  358. if (normalized.northReferenceMode === 'magnetic' || normalized.northReferenceMode === 'true') {
  359. settings.northReferenceMode = normalized.northReferenceMode
  360. }
  361. if (typeof normalized.autoRotateEnabled === 'boolean') {
  362. settings.autoRotateEnabled = normalized.autoRotateEnabled
  363. }
  364. if (normalized.compassTuningProfile === 'smooth' || normalized.compassTuningProfile === 'balanced' || normalized.compassTuningProfile === 'responsive') {
  365. settings.compassTuningProfile = normalized.compassTuningProfile
  366. }
  367. if (normalized.sideButtonPlacement === 'left' || normalized.sideButtonPlacement === 'right') {
  368. settings.sideButtonPlacement = normalized.sideButtonPlacement
  369. }
  370. if (typeof normalized.showCenterScaleRuler === 'boolean') {
  371. settings.showCenterScaleRuler = normalized.showCenterScaleRuler
  372. }
  373. if (normalized.centerScaleRulerAnchorMode === 'screen-center' || normalized.centerScaleRulerAnchorMode === 'compass-center') {
  374. settings.centerScaleRulerAnchorMode = normalized.centerScaleRulerAnchorMode
  375. }
  376. if (typeof normalized.lockAnimationLevel === 'boolean') {
  377. settings.lockAnimationLevel = normalized.lockAnimationLevel
  378. }
  379. if (typeof normalized.lockSideButtonPlacement === 'boolean') {
  380. settings.lockSideButtonPlacement = normalized.lockSideButtonPlacement
  381. }
  382. if (typeof normalized.lockAutoRotate === 'boolean') {
  383. settings.lockAutoRotate = normalized.lockAutoRotate
  384. }
  385. if (typeof normalized.lockCompassTuning === 'boolean') {
  386. settings.lockCompassTuning = normalized.lockCompassTuning
  387. }
  388. if (typeof normalized.lockScaleRulerVisible === 'boolean') {
  389. settings.lockScaleRulerVisible = normalized.lockScaleRulerVisible
  390. }
  391. if (typeof normalized.lockScaleRulerAnchor === 'boolean') {
  392. settings.lockScaleRulerAnchor = normalized.lockScaleRulerAnchor
  393. }
  394. if (typeof normalized.lockNorthReference === 'boolean') {
  395. settings.lockNorthReference = normalized.lockNorthReference
  396. }
  397. if (typeof normalized.lockHeartRateDevice === 'boolean') {
  398. settings.lockHeartRateDevice = normalized.lockHeartRateDevice
  399. }
  400. return settings
  401. } catch {
  402. return {}
  403. }
  404. }
  405. function persistStoredUserSettings(settings: StoredUserSettings) {
  406. try {
  407. wx.setStorageSync(USER_SETTINGS_STORAGE_KEY, settings)
  408. } catch {}
  409. }
  410. function toggleStoredSettingLock(settings: StoredUserSettings, key: SettingLockKey): StoredUserSettings {
  411. return {
  412. ...settings,
  413. [key]: !settings[key],
  414. }
  415. }
  416. function buildSideButtonVisibility(mode: SideButtonMode) {
  417. return {
  418. sideButtonMode: mode,
  419. showLeftButtonGroup: mode === 'shown',
  420. showRightButtonGroups: false,
  421. showBottomDebugButton: true,
  422. }
  423. }
  424. function getNextSideButtonMode(currentMode: SideButtonMode): SideButtonMode {
  425. return currentMode === 'shown' ? 'hidden' : 'shown'
  426. }
  427. function buildCompassTicks(): CompassTickData[] {
  428. const ticks: CompassTickData[] = []
  429. for (let angle = 0; angle < 360; angle += 5) {
  430. ticks.push({
  431. angle,
  432. long: angle % 15 === 0,
  433. major: angle % 45 === 0,
  434. })
  435. }
  436. return ticks
  437. }
  438. function buildCompassLabels(): CompassLabelData[] {
  439. return [
  440. { text: '\u5317', angle: 0, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal compass-widget__mark--north' },
  441. { text: '\u4e1c\u5317', angle: 45, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northeast' },
  442. { text: '\u4e1c', angle: 90, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  443. { text: '\u4e1c\u5357', angle: 135, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  444. { text: '\u5357', angle: 180, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  445. { text: '\u897f\u5357', angle: 225, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate' },
  446. { text: '\u897f', angle: 270, rotateBack: 0, radius: 68, className: 'compass-widget__mark--cardinal' },
  447. { text: '\u897f\u5317', angle: 315, rotateBack: 0, radius: 58, className: 'compass-widget__mark--intermediate compass-widget__mark--northwest' },
  448. ]
  449. }
  450. function getFallbackStageRect(): MapEngineStageRect {
  451. const systemInfo = wx.getSystemInfoSync()
  452. const width = Math.max(320, systemInfo.windowWidth)
  453. const height = Math.max(280, systemInfo.windowHeight)
  454. return {
  455. width,
  456. height,
  457. left: 0,
  458. top: 0,
  459. }
  460. }
  461. function getSideToggleIconSrc(mode: SideButtonMode): string {
  462. if (mode === 'hidden') {
  463. return '../../assets/btn_more1.png'
  464. }
  465. return '../../assets/btn_more3.png'
  466. }
  467. function getSideActionButtonClass(state: SideActionButtonState): string {
  468. if (state === 'muted') {
  469. return 'map-side-button map-side-button--muted'
  470. }
  471. if (state === 'active') {
  472. return 'map-side-button map-side-button--active'
  473. }
  474. return 'map-side-button map-side-button--default'
  475. }
  476. function buildSideButtonState(data: Pick<MapPageData, 'sideButtonMode' | 'showGameInfoPanel' | 'showSystemSettingsPanel' | 'showCenterScaleRuler' | 'centerScaleRulerAnchorMode' | 'skipButtonEnabled' | 'gameSessionStatus' | 'gpsLockEnabled' | 'gpsLockAvailable'>) {
  477. const sideButton2State: SideActionButtonState = !data.gpsLockAvailable
  478. ? 'muted'
  479. : data.gpsLockEnabled
  480. ? 'active'
  481. : 'default'
  482. const sideButton4State: SideActionButtonState = data.gameSessionStatus === 'running' ? 'active' : 'muted'
  483. const sideButton11State: SideActionButtonState = data.showGameInfoPanel ? 'active' : 'default'
  484. const sideButton12State: SideActionButtonState = data.showSystemSettingsPanel ? 'active' : 'default'
  485. const sideButton13State: SideActionButtonState = data.showCenterScaleRuler ? 'active' : 'default'
  486. const sideButton14State: SideActionButtonState = !data.showCenterScaleRuler
  487. ? 'muted'
  488. : data.centerScaleRulerAnchorMode === 'compass-center'
  489. ? 'active'
  490. : 'default'
  491. const sideButton16State: SideActionButtonState = data.skipButtonEnabled ? 'default' : 'muted'
  492. return {
  493. sideToggleIconSrc: getSideToggleIconSrc(data.sideButtonMode),
  494. sideButton2Class: getSideActionButtonClass(sideButton2State),
  495. sideButton4Class: getSideActionButtonClass(sideButton4State),
  496. sideButton11Class: getSideActionButtonClass(sideButton11State),
  497. sideButton12Class: getSideActionButtonClass(sideButton12State),
  498. sideButton13Class: getSideActionButtonClass(sideButton13State),
  499. sideButton14Class: getSideActionButtonClass(sideButton14State),
  500. sideButton16Class: getSideActionButtonClass(sideButton16State),
  501. }
  502. }
  503. function getRpxUnitInPx(): number {
  504. const systemInfo = wx.getSystemInfoSync()
  505. return systemInfo.windowWidth / 750
  506. }
  507. function worldTileYToLat(worldTileY: number, zoom: number): number {
  508. const scale = Math.pow(2, zoom)
  509. const n = Math.PI - (2 * Math.PI * worldTileY) / scale
  510. return (180 / Math.PI) * Math.atan(Math.sinh(n))
  511. }
  512. function getNiceDistanceMeters(rawDistanceMeters: number): number {
  513. if (!Number.isFinite(rawDistanceMeters) || rawDistanceMeters <= 0) {
  514. return 50
  515. }
  516. const exponent = Math.floor(Math.log10(rawDistanceMeters))
  517. const base = Math.pow(10, exponent)
  518. const normalized = rawDistanceMeters / base
  519. if (normalized <= 1) {
  520. return base
  521. }
  522. if (normalized <= 2) {
  523. return 2 * base
  524. }
  525. if (normalized <= 5) {
  526. return 5 * base
  527. }
  528. return 10 * base
  529. }
  530. function formatScaleDistanceLabel(distanceMeters: number): string {
  531. if (distanceMeters >= 1000) {
  532. const distanceKm = distanceMeters / 1000
  533. const formatted = distanceKm >= 10 ? distanceKm.toFixed(0) : distanceKm.toFixed(1)
  534. return `${formatted.replace(/\.0$/, '')} km`
  535. }
  536. return `${Math.round(distanceMeters)} m`
  537. }
  538. function buildCenterScaleRulerPatch(data: Pick<MapPageData, 'showCenterScaleRuler' | 'centerScaleRulerAnchorMode' | 'stageWidth' | 'stageHeight' | 'topInsetHeight' | 'zoom' | 'centerTileY' | 'tileSizePx' | 'previewScale'>) {
  539. if (!data.showCenterScaleRuler) {
  540. lastCenterScaleRulerStablePatch = {
  541. centerScaleRulerVisible: false,
  542. centerScaleRulerCenterXPx: 0,
  543. centerScaleRulerZeroYPx: 0,
  544. centerScaleRulerHeightPx: 0,
  545. centerScaleRulerAxisBottomPx: 0,
  546. centerScaleRulerZeroVisible: false,
  547. centerScaleRulerZeroLabel: '0 m',
  548. centerScaleRulerMinorTicks: [] as ScaleRulerMinorTickData[],
  549. centerScaleRulerMajorMarks: [] as ScaleRulerMajorMarkData[],
  550. }
  551. return { ...lastCenterScaleRulerStablePatch }
  552. }
  553. if (!data.stageWidth || !data.stageHeight) {
  554. return { ...lastCenterScaleRulerStablePatch }
  555. }
  556. const topPadding = 12
  557. const rpxUnitPx = getRpxUnitInPx()
  558. const compassBottomPaddingPx = 248 * rpxUnitPx
  559. const compassDialRadiusPx = (196 * rpxUnitPx) / 2
  560. const compassHeadingOverlayHeightPx = 40 * rpxUnitPx
  561. const compassOcclusionPaddingPx = 10 * rpxUnitPx
  562. const zeroYPx = data.centerScaleRulerAnchorMode === 'compass-center'
  563. ? Math.round(data.stageHeight - compassBottomPaddingPx - compassDialRadiusPx)
  564. : Math.round(data.stageHeight / 2)
  565. const fallbackHeight = Math.max(zeroYPx - topPadding, 160)
  566. const coveredBottomPx = data.centerScaleRulerAnchorMode === 'compass-center'
  567. ? Math.round(compassDialRadiusPx + compassHeadingOverlayHeightPx + compassOcclusionPaddingPx)
  568. : 0
  569. if (
  570. !data.tileSizePx
  571. || !Number.isFinite(data.zoom)
  572. || !Number.isFinite(data.centerTileY)
  573. ) {
  574. return {
  575. ...lastCenterScaleRulerStablePatch,
  576. centerScaleRulerVisible: true,
  577. centerScaleRulerCenterXPx: Math.round(data.stageWidth / 2),
  578. centerScaleRulerZeroYPx: zeroYPx,
  579. centerScaleRulerHeightPx: lastCenterScaleRulerStablePatch.centerScaleRulerHeightPx || fallbackHeight,
  580. centerScaleRulerAxisBottomPx: coveredBottomPx,
  581. centerScaleRulerZeroVisible: data.centerScaleRulerAnchorMode !== 'compass-center',
  582. }
  583. }
  584. const centerLat = worldTileYToLat(data.centerTileY + 0.5, data.zoom)
  585. const metersPerTile = Math.cos(centerLat * Math.PI / 180) * 40075016.686 / Math.pow(2, data.zoom)
  586. const metersPerPixel = metersPerTile / data.tileSizePx
  587. const effectivePreviewScale = Number.isFinite(data.previewScale) && data.previewScale > 0 ? data.previewScale : 1
  588. const effectiveMetersPerPixel = metersPerPixel / effectivePreviewScale
  589. const rulerHeight = Math.floor(zeroYPx - topPadding)
  590. if (!Number.isFinite(effectiveMetersPerPixel) || effectiveMetersPerPixel <= 0 || rulerHeight < 120) {
  591. return {
  592. ...lastCenterScaleRulerStablePatch,
  593. centerScaleRulerVisible: true,
  594. centerScaleRulerCenterXPx: Math.round(data.stageWidth / 2),
  595. centerScaleRulerZeroYPx: zeroYPx,
  596. centerScaleRulerHeightPx: lastCenterScaleRulerStablePatch.centerScaleRulerHeightPx || fallbackHeight,
  597. centerScaleRulerAxisBottomPx: coveredBottomPx,
  598. centerScaleRulerZeroVisible: data.centerScaleRulerAnchorMode !== 'compass-center',
  599. }
  600. }
  601. const labelDistanceMeters = getNiceDistanceMeters(effectiveMetersPerPixel * 80)
  602. const minorDistanceMeters = labelDistanceMeters / 8
  603. const minorStepPx = minorDistanceMeters / effectiveMetersPerPixel
  604. const visibleTopLimitPx = rulerHeight - coveredBottomPx
  605. const minorTicks: ScaleRulerMinorTickData[] = []
  606. const majorMarks: ScaleRulerMajorMarkData[] = []
  607. for (let index = 1; index <= 200; index += 1) {
  608. const topPx = Math.round(rulerHeight - index * minorStepPx)
  609. if (topPx < 0) {
  610. break
  611. }
  612. if (topPx >= visibleTopLimitPx) {
  613. continue
  614. }
  615. const isHalfMajor = index % 4 === 0
  616. const isLabelMajor = index % 8 === 0
  617. minorTicks.push({
  618. key: `minor-${index}`,
  619. topPx,
  620. long: isHalfMajor,
  621. })
  622. if (isLabelMajor) {
  623. majorMarks.push({
  624. key: `major-${index}`,
  625. topPx,
  626. label: formatScaleDistanceLabel((index / 8) * labelDistanceMeters),
  627. })
  628. }
  629. }
  630. lastCenterScaleRulerStablePatch = {
  631. centerScaleRulerVisible: true,
  632. centerScaleRulerCenterXPx: Math.round(data.stageWidth / 2),
  633. centerScaleRulerZeroYPx: zeroYPx,
  634. centerScaleRulerHeightPx: rulerHeight,
  635. centerScaleRulerAxisBottomPx: coveredBottomPx,
  636. centerScaleRulerZeroVisible: data.centerScaleRulerAnchorMode !== 'compass-center',
  637. centerScaleRulerZeroLabel: '0 m',
  638. centerScaleRulerMinorTicks: minorTicks,
  639. centerScaleRulerMajorMarks: majorMarks,
  640. }
  641. return { ...lastCenterScaleRulerStablePatch }
  642. }
  643. function buildEmptyGameInfoSnapshot(): MapEngineGameInfoSnapshot {
  644. return {
  645. title: '当前游戏',
  646. subtitle: '未开始',
  647. localRows: [],
  648. globalRows: [
  649. { label: '全球积分', value: '未接入' },
  650. { label: '全球排名', value: '未接入' },
  651. { label: '在线人数', value: '未接入' },
  652. { label: '队伍状态', value: '未接入' },
  653. { label: '实时广播', value: '未接入' },
  654. ],
  655. }
  656. }
  657. function buildEmptyResultSceneSnapshot(): MapEngineResultSnapshot {
  658. return {
  659. title: '本局结果',
  660. subtitle: '未开始',
  661. heroLabel: '本局用时',
  662. heroValue: '--',
  663. rows: [],
  664. }
  665. }
  666. Page({
  667. data: {
  668. showDebugPanel: false,
  669. showGameInfoPanel: false,
  670. showResultScene: false,
  671. showSystemSettingsPanel: false,
  672. showCenterScaleRuler: false,
  673. statusBarHeight: 0,
  674. topInsetHeight: 12,
  675. hudPanelIndex: 0,
  676. configSourceText: '顺序赛配置',
  677. centerScaleRulerAnchorMode: 'screen-center',
  678. autoRotateEnabled: false,
  679. lockAnimationLevel: false,
  680. lockSideButtonPlacement: false,
  681. lockAutoRotate: false,
  682. lockCompassTuning: false,
  683. lockScaleRulerVisible: false,
  684. lockScaleRulerAnchor: false,
  685. lockNorthReference: false,
  686. lockHeartRateDevice: false,
  687. gameInfoTitle: '当前游戏',
  688. gameInfoSubtitle: '未开始',
  689. gameInfoLocalRows: [],
  690. gameInfoGlobalRows: buildEmptyGameInfoSnapshot().globalRows,
  691. resultSceneTitle: '本局结果',
  692. resultSceneSubtitle: '未开始',
  693. resultSceneHeroLabel: '本局用时',
  694. resultSceneHeroValue: '--',
  695. resultSceneRows: buildEmptyResultSceneSnapshot().rows,
  696. panelTimerText: '00:00:00',
  697. panelMileageText: '0m',
  698. panelActionTagText: '目标',
  699. panelDistanceTagText: '点距',
  700. panelDistanceValueText: '--',
  701. panelDistanceUnitText: '',
  702. panelProgressText: '0/0',
  703. showPunchHintBanner: true,
  704. sideButtonPlacement: 'left',
  705. gameSessionStatus: 'idle',
  706. gameModeText: '顺序赛',
  707. gpsLockEnabled: false,
  708. gpsLockAvailable: false,
  709. locationSourceMode: 'real',
  710. locationSourceText: '真实定位',
  711. mockBridgeConnected: false,
  712. mockBridgeStatusText: '未连接',
  713. mockBridgeUrlText: 'wss://gs.gotomars.xyz/mock-gps',
  714. mockBridgeUrlDraft: 'wss://gs.gotomars.xyz/mock-gps',
  715. mockCoordText: '--',
  716. mockSpeedText: '--',
  717. heartRateSourceMode: 'real',
  718. heartRateSourceText: '真实心率',
  719. mockHeartRateBridgeConnected: false,
  720. mockHeartRateBridgeStatusText: '未连接',
  721. mockHeartRateBridgeUrlText: 'wss://gs.gotomars.xyz/mock-gps',
  722. mockHeartRateBridgeUrlDraft: 'wss://gs.gotomars.xyz/mock-gps',
  723. mockHeartRateText: '--',
  724. heartRateScanText: '未扫描',
  725. heartRateDiscoveredDevices: [],
  726. panelSpeedValueText: '0',
  727. panelTelemetryTone: 'blue',
  728. panelHeartRateZoneNameText: '--',
  729. panelHeartRateZoneRangeText: '',
  730. heartRateConnected: false,
  731. heartRateStatusText: '心率带未连接',
  732. heartRateDeviceText: '--',
  733. panelHeartRateValueText: '--',
  734. panelHeartRateUnitText: '',
  735. panelCaloriesValueText: '0',
  736. panelCaloriesUnitText: 'kcal',
  737. panelAverageSpeedValueText: '0',
  738. panelAverageSpeedUnitText: 'km/h',
  739. panelAccuracyValueText: '--',
  740. panelAccuracyUnitText: '',
  741. deviceHeadingText: '--',
  742. devicePoseText: '竖持',
  743. headingConfidenceText: '低',
  744. accelerometerText: '--',
  745. gyroscopeText: '--',
  746. deviceMotionText: '--',
  747. compassSourceText: '无数据',
  748. compassTuningProfile: 'balanced',
  749. compassTuningProfileText: '平衡',
  750. punchButtonText: '打点',
  751. punchButtonEnabled: false,
  752. skipButtonEnabled: false,
  753. punchHintText: '等待进入检查点范围',
  754. punchFeedbackVisible: false,
  755. punchFeedbackText: '',
  756. punchFeedbackTone: 'neutral',
  757. contentCardVisible: false,
  758. contentCardTitle: '',
  759. contentCardBody: '',
  760. punchButtonFxClass: '',
  761. panelProgressFxClass: '',
  762. panelDistanceFxClass: '',
  763. punchFeedbackFxClass: '',
  764. contentCardFxClass: '',
  765. mapPulseVisible: false,
  766. mapPulseLeftPx: 0,
  767. mapPulseTopPx: 0,
  768. mapPulseFxClass: '',
  769. stageFxVisible: false,
  770. stageFxClass: '',
  771. centerScaleRulerVisible: false,
  772. centerScaleRulerCenterXPx: 0,
  773. centerScaleRulerZeroYPx: 0,
  774. centerScaleRulerHeightPx: 0,
  775. centerScaleRulerAxisBottomPx: 0,
  776. centerScaleRulerZeroVisible: false,
  777. centerScaleRulerZeroLabel: '0 m',
  778. centerScaleRulerMinorTicks: [],
  779. centerScaleRulerMajorMarks: [],
  780. compassTicks: buildCompassTicks(),
  781. compassLabels: buildCompassLabels(),
  782. ...buildSideButtonVisibility('shown'),
  783. ...buildSideButtonState({
  784. sideButtonMode: 'shown',
  785. showGameInfoPanel: false,
  786. showSystemSettingsPanel: false,
  787. showCenterScaleRuler: false,
  788. centerScaleRulerAnchorMode: 'screen-center',
  789. skipButtonEnabled: false,
  790. gameSessionStatus: 'idle',
  791. gpsLockEnabled: false,
  792. gpsLockAvailable: false,
  793. }),
  794. } as unknown as MapPageData,
  795. onLoad() {
  796. const systemInfo = wx.getSystemInfoSync()
  797. const statusBarHeight = systemInfo.statusBarHeight || 0
  798. const menuButtonRect = wx.getMenuButtonBoundingClientRect()
  799. const menuButtonBottom = menuButtonRect && typeof menuButtonRect.bottom === 'number' ? menuButtonRect.bottom : statusBarHeight
  800. if (mapEngine) {
  801. mapEngine.destroy()
  802. mapEngine = null
  803. }
  804. mapEngine = new MapEngine(INTERNAL_BUILD_VERSION, {
  805. onData: (patch) => {
  806. const nextPatch = patch as Partial<MapPageData>
  807. const includeDebugFields = this.data.showDebugPanel
  808. const includeRulerFields = this.data.showCenterScaleRuler
  809. const nextData: Partial<MapPageData> = filterDebugOnlyPatch({
  810. ...nextPatch,
  811. }, includeDebugFields, includeRulerFields)
  812. if (
  813. typeof nextPatch.mockBridgeUrlText === 'string'
  814. && this.data.mockBridgeUrlDraft === this.data.mockBridgeUrlText
  815. ) {
  816. nextData.mockBridgeUrlDraft = nextPatch.mockBridgeUrlText
  817. }
  818. if (
  819. typeof nextPatch.mockHeartRateBridgeUrlText === 'string'
  820. && this.data.mockHeartRateBridgeUrlDraft === this.data.mockHeartRateBridgeUrlText
  821. ) {
  822. nextData.mockHeartRateBridgeUrlDraft = nextPatch.mockHeartRateBridgeUrlText
  823. }
  824. updateCenterScaleRulerInputCache(nextPatch)
  825. const mergedData = {
  826. ...centerScaleRulerInputCache,
  827. ...this.data,
  828. ...nextData,
  829. } as MapPageData
  830. const derivedPatch: Partial<MapPageData> = {}
  831. if (typeof nextPatch.orientationMode === 'string') {
  832. nextData.autoRotateEnabled = nextPatch.orientationMode === 'heading-up'
  833. }
  834. if (
  835. this.data.showCenterScaleRuler
  836. && hasAnyPatchKey(nextPatch as Record<string, unknown>, CENTER_SCALE_RULER_DEP_KEYS)
  837. ) {
  838. clearCenterScaleRulerUpdateTimer()
  839. Object.assign(derivedPatch, buildCenterScaleRulerPatch(mergedData))
  840. }
  841. if (hasAnyPatchKey(nextPatch as Record<string, unknown>, SIDE_BUTTON_DEP_KEYS)) {
  842. Object.assign(derivedPatch, buildSideButtonState(mergedData))
  843. }
  844. if (typeof nextPatch.punchHintText === 'string') {
  845. const nextHintText = nextPatch.punchHintText.trim()
  846. if (nextHintText !== this.data.punchHintText) {
  847. clearPunchHintDismissTimer()
  848. nextData.showPunchHintBanner = nextHintText.length > 0
  849. if (nextHintText.length > 0) {
  850. punchHintDismissTimer = setTimeout(() => {
  851. punchHintDismissTimer = 0
  852. this.setData({
  853. showPunchHintBanner: false,
  854. })
  855. }, PUNCH_HINT_AUTO_HIDE_MS) as unknown as number
  856. }
  857. } else if (!nextHintText) {
  858. clearPunchHintDismissTimer()
  859. nextData.showPunchHintBanner = false
  860. }
  861. }
  862. const nextAnimationLevel = typeof nextPatch.animationLevel === 'string'
  863. ? nextPatch.animationLevel
  864. : this.data.animationLevel
  865. if (nextAnimationLevel === 'lite') {
  866. clearHudFxTimer('timer')
  867. clearHudFxTimer('mileage')
  868. clearHudFxTimer('speed')
  869. clearHudFxTimer('heartRate')
  870. nextData.panelTimerFxClass = ''
  871. nextData.panelMileageFxClass = ''
  872. nextData.panelSpeedFxClass = ''
  873. nextData.panelHeartRateFxClass = ''
  874. } else {
  875. if (typeof nextPatch.panelTimerText === 'string' && nextPatch.panelTimerText !== this.data.panelTimerText && this.data.panelTimerText !== '00:00:00') {
  876. clearHudFxTimer('timer')
  877. nextData.panelTimerFxClass = 'race-panel__timer--fx-tick'
  878. panelTimerFxTimer = setTimeout(() => {
  879. panelTimerFxTimer = 0
  880. this.setData({ panelTimerFxClass: '' })
  881. }, 320) as unknown as number
  882. }
  883. if (typeof nextPatch.panelMileageText === 'string' && nextPatch.panelMileageText !== this.data.panelMileageText && this.data.panelMileageText !== '0m') {
  884. clearHudFxTimer('mileage')
  885. nextData.panelMileageFxClass = 'race-panel__mileage-wrap--fx-update'
  886. panelMileageFxTimer = setTimeout(() => {
  887. panelMileageFxTimer = 0
  888. this.setData({ panelMileageFxClass: '' })
  889. }, 360) as unknown as number
  890. }
  891. if (typeof nextPatch.panelSpeedValueText === 'string' && nextPatch.panelSpeedValueText !== this.data.panelSpeedValueText && this.data.panelSpeedValueText !== '0') {
  892. clearHudFxTimer('speed')
  893. nextData.panelSpeedFxClass = 'race-panel__metric-group--fx-speed-update'
  894. panelSpeedFxTimer = setTimeout(() => {
  895. panelSpeedFxTimer = 0
  896. this.setData({ panelSpeedFxClass: '' })
  897. }, 360) as unknown as number
  898. }
  899. if (typeof nextPatch.panelHeartRateValueText === 'string' && nextPatch.panelHeartRateValueText !== this.data.panelHeartRateValueText && this.data.panelHeartRateValueText !== '--') {
  900. clearHudFxTimer('heartRate')
  901. nextData.panelHeartRateFxClass = 'race-panel__metric-group--fx-heart-rate-update'
  902. panelHeartRateFxTimer = setTimeout(() => {
  903. panelHeartRateFxTimer = 0
  904. this.setData({ panelHeartRateFxClass: '' })
  905. }, 400) as unknown as number
  906. }
  907. }
  908. if (typeof nextPatch.gameSessionStatus === 'string') {
  909. if (
  910. nextPatch.gameSessionStatus !== this.data.gameSessionStatus
  911. && (nextPatch.gameSessionStatus === 'finished' || nextPatch.gameSessionStatus === 'failed')
  912. ) {
  913. this.syncResultSceneSnapshot()
  914. nextData.showResultScene = true
  915. nextData.showDebugPanel = false
  916. nextData.showGameInfoPanel = false
  917. nextData.showSystemSettingsPanel = false
  918. clearGameInfoPanelSyncTimer()
  919. } else if (nextPatch.gameSessionStatus === 'running' || nextPatch.gameSessionStatus === 'idle') {
  920. nextData.showResultScene = false
  921. }
  922. }
  923. if (Object.keys(nextData).length || Object.keys(derivedPatch).length) {
  924. this.setData({
  925. ...nextData,
  926. ...derivedPatch,
  927. })
  928. }
  929. if (this.data.showGameInfoPanel) {
  930. this.scheduleGameInfoPanelSnapshotSync()
  931. }
  932. },
  933. onOpenH5Experience: (request) => {
  934. this.openH5Experience(request)
  935. },
  936. })
  937. const storedUserSettings = loadStoredUserSettings()
  938. if (storedUserSettings.animationLevel) {
  939. mapEngine.handleSetAnimationLevel(storedUserSettings.animationLevel)
  940. }
  941. const initialAutoRotateEnabled = storedUserSettings.autoRotateEnabled !== false
  942. if (initialAutoRotateEnabled) {
  943. mapEngine.handleSetHeadingUpMode()
  944. } else {
  945. mapEngine.handleSetManualMode()
  946. }
  947. if (storedUserSettings.compassTuningProfile) {
  948. mapEngine.handleSetCompassTuningProfile(storedUserSettings.compassTuningProfile)
  949. }
  950. if (storedUserSettings.northReferenceMode) {
  951. mapEngine.handleSetNorthReferenceMode(storedUserSettings.northReferenceMode)
  952. }
  953. const initialSideButtonPlacement = storedUserSettings.sideButtonPlacement || 'left'
  954. mapEngine.setDiagnosticUiEnabled(false)
  955. centerScaleRulerInputCache = {
  956. stageWidth: 0,
  957. stageHeight: 0,
  958. zoom: 0,
  959. centerTileY: 0,
  960. tileSizePx: 0,
  961. previewScale: 1,
  962. }
  963. const initialShowCenterScaleRuler = !!storedUserSettings.showCenterScaleRuler
  964. const initialCenterScaleRulerAnchorMode = storedUserSettings.centerScaleRulerAnchorMode || 'screen-center'
  965. this.setData({
  966. ...mapEngine.getInitialData(),
  967. showDebugPanel: false,
  968. showGameInfoPanel: false,
  969. showSystemSettingsPanel: false,
  970. showCenterScaleRuler: initialShowCenterScaleRuler,
  971. statusBarHeight,
  972. topInsetHeight: Math.max(statusBarHeight + 12, menuButtonBottom + 20),
  973. hudPanelIndex: 0,
  974. configSourceText: '顺序赛配置',
  975. centerScaleRulerAnchorMode: initialCenterScaleRulerAnchorMode,
  976. autoRotateEnabled: initialAutoRotateEnabled,
  977. lockAnimationLevel: !!storedUserSettings.lockAnimationLevel,
  978. lockSideButtonPlacement: !!storedUserSettings.lockSideButtonPlacement,
  979. lockAutoRotate: !!storedUserSettings.lockAutoRotate,
  980. lockCompassTuning: !!storedUserSettings.lockCompassTuning,
  981. lockScaleRulerVisible: !!storedUserSettings.lockScaleRulerVisible,
  982. lockScaleRulerAnchor: !!storedUserSettings.lockScaleRulerAnchor,
  983. lockNorthReference: !!storedUserSettings.lockNorthReference,
  984. lockHeartRateDevice: !!storedUserSettings.lockHeartRateDevice,
  985. sideButtonPlacement: initialSideButtonPlacement,
  986. gameInfoTitle: '当前游戏',
  987. gameInfoSubtitle: '未开始',
  988. gameInfoLocalRows: [],
  989. gameInfoGlobalRows: buildEmptyGameInfoSnapshot().globalRows,
  990. panelTimerText: '00:00:00',
  991. panelTimerFxClass: '',
  992. panelMileageText: '0m',
  993. panelMileageFxClass: '',
  994. panelActionTagText: '目标',
  995. panelDistanceTagText: '点距',
  996. panelDistanceValueText: '--',
  997. panelDistanceUnitText: '',
  998. panelProgressText: '0/0',
  999. showPunchHintBanner: true,
  1000. gameSessionStatus: 'idle',
  1001. gameModeText: '顺序赛',
  1002. gpsLockEnabled: false,
  1003. gpsLockAvailable: false,
  1004. locationSourceMode: 'real',
  1005. locationSourceText: '真实定位',
  1006. mockBridgeConnected: false,
  1007. mockBridgeStatusText: '未连接',
  1008. mockBridgeUrlText: 'wss://gs.gotomars.xyz/mock-gps',
  1009. mockBridgeUrlDraft: 'wss://gs.gotomars.xyz/mock-gps',
  1010. mockCoordText: '--',
  1011. mockSpeedText: '--',
  1012. heartRateSourceMode: 'real',
  1013. heartRateSourceText: '真实心率',
  1014. mockHeartRateBridgeConnected: false,
  1015. mockHeartRateBridgeStatusText: '未连接',
  1016. mockHeartRateBridgeUrlText: 'wss://gs.gotomars.xyz/mock-gps',
  1017. mockHeartRateBridgeUrlDraft: 'wss://gs.gotomars.xyz/mock-gps',
  1018. mockHeartRateText: '--',
  1019. panelSpeedValueText: '0',
  1020. panelSpeedFxClass: '',
  1021. panelTelemetryTone: 'blue',
  1022. panelHeartRateZoneNameText: '--',
  1023. panelHeartRateZoneRangeText: '',
  1024. heartRateConnected: false,
  1025. heartRateStatusText: '心率带未连接',
  1026. heartRateDeviceText: '--',
  1027. panelHeartRateValueText: '--',
  1028. panelHeartRateFxClass: '',
  1029. panelHeartRateUnitText: '',
  1030. panelCaloriesValueText: '0',
  1031. panelCaloriesUnitText: 'kcal',
  1032. panelAverageSpeedValueText: '0',
  1033. panelAverageSpeedUnitText: 'km/h',
  1034. panelAccuracyValueText: '--',
  1035. panelAccuracyUnitText: '',
  1036. deviceHeadingText: '--',
  1037. devicePoseText: '竖持',
  1038. headingConfidenceText: '低',
  1039. accelerometerText: '--',
  1040. gyroscopeText: '--',
  1041. deviceMotionText: '--',
  1042. compassSourceText: '无数据',
  1043. compassTuningProfile: 'balanced',
  1044. compassTuningProfileText: '平衡',
  1045. punchButtonText: '打点',
  1046. punchButtonEnabled: false,
  1047. skipButtonEnabled: false,
  1048. punchHintText: '等待进入检查点范围',
  1049. punchFeedbackVisible: false,
  1050. punchFeedbackText: '',
  1051. punchFeedbackTone: 'neutral',
  1052. contentCardVisible: false,
  1053. contentCardTitle: '',
  1054. contentCardBody: '',
  1055. punchButtonFxClass: '',
  1056. panelProgressFxClass: '',
  1057. panelDistanceFxClass: '',
  1058. punchFeedbackFxClass: '',
  1059. contentCardFxClass: '',
  1060. mapPulseVisible: false,
  1061. mapPulseLeftPx: 0,
  1062. mapPulseTopPx: 0,
  1063. mapPulseFxClass: '',
  1064. stageFxVisible: false,
  1065. stageFxClass: '',
  1066. compassTicks: buildCompassTicks(),
  1067. compassLabels: buildCompassLabels(),
  1068. ...buildSideButtonVisibility('shown'),
  1069. ...buildSideButtonState({
  1070. sideButtonMode: 'shown',
  1071. showGameInfoPanel: false,
  1072. showSystemSettingsPanel: false,
  1073. showCenterScaleRuler: initialShowCenterScaleRuler,
  1074. centerScaleRulerAnchorMode: initialCenterScaleRulerAnchorMode,
  1075. skipButtonEnabled: false,
  1076. gameSessionStatus: 'idle',
  1077. gpsLockEnabled: false,
  1078. gpsLockAvailable: false,
  1079. }),
  1080. ...buildCenterScaleRulerPatch({
  1081. ...(mapEngine.getInitialData() as MapPageData),
  1082. showCenterScaleRuler: initialShowCenterScaleRuler,
  1083. centerScaleRulerAnchorMode: initialCenterScaleRulerAnchorMode,
  1084. stageWidth: 0,
  1085. stageHeight: 0,
  1086. topInsetHeight: Math.max(statusBarHeight + 12, menuButtonBottom + 20),
  1087. zoom: 0,
  1088. centerTileY: 0,
  1089. tileSizePx: 0,
  1090. }),
  1091. })
  1092. },
  1093. onReady() {
  1094. stageCanvasAttached = false
  1095. this.measureStageAndCanvas()
  1096. this.loadMapConfigFromRemote(CLASSIC_REMOTE_GAME_CONFIG_URL, '顺序赛配置')
  1097. },
  1098. onShow() {
  1099. if (mapEngine) {
  1100. mapEngine.handleAppShow()
  1101. }
  1102. },
  1103. onHide() {
  1104. if (mapEngine) {
  1105. mapEngine.handleAppHide()
  1106. }
  1107. },
  1108. onUnload() {
  1109. clearGameInfoPanelSyncTimer()
  1110. clearCenterScaleRulerSyncTimer()
  1111. clearCenterScaleRulerUpdateTimer()
  1112. clearPunchHintDismissTimer()
  1113. clearHudFxTimer('timer')
  1114. clearHudFxTimer('mileage')
  1115. clearHudFxTimer('speed')
  1116. clearHudFxTimer('heartRate')
  1117. if (mapEngine) {
  1118. mapEngine.destroy()
  1119. mapEngine = null
  1120. }
  1121. stageCanvasAttached = false
  1122. },
  1123. loadMapConfigFromRemote(configUrl: string, configLabel: string) {
  1124. const currentEngine = mapEngine
  1125. if (!currentEngine) {
  1126. return
  1127. }
  1128. this.setData({
  1129. configSourceText: configLabel,
  1130. configStatusText: `加载中: ${configLabel}`,
  1131. })
  1132. loadRemoteMapConfig(configUrl)
  1133. .then((config) => {
  1134. if (mapEngine !== currentEngine) {
  1135. return
  1136. }
  1137. currentEngine.applyRemoteMapConfig(config)
  1138. })
  1139. .catch((error) => {
  1140. if (mapEngine !== currentEngine) {
  1141. return
  1142. }
  1143. const errorMessage = error && error.message ? error.message : '未知错误'
  1144. this.setData({
  1145. configStatusText: `载入失败: ${errorMessage}`,
  1146. statusText: `远程地图配置载入失败: ${errorMessage} (${INTERNAL_BUILD_VERSION})`,
  1147. })
  1148. })
  1149. },
  1150. measureStageAndCanvas(onApplied?: () => void) {
  1151. const page = this
  1152. const applyStage = (rawRect?: Partial<WechatMiniprogram.BoundingClientRectCallbackResult>) => {
  1153. const fallbackRect = getFallbackStageRect()
  1154. const rect: MapEngineStageRect = {
  1155. width: rawRect && typeof rawRect.width === 'number' ? rawRect.width : fallbackRect.width,
  1156. height: rawRect && typeof rawRect.height === 'number' ? rawRect.height : fallbackRect.height,
  1157. left: rawRect && typeof rawRect.left === 'number' ? rawRect.left : fallbackRect.left,
  1158. top: rawRect && typeof rawRect.top === 'number' ? rawRect.top : fallbackRect.top,
  1159. }
  1160. const currentEngine = mapEngine
  1161. if (!currentEngine) {
  1162. return
  1163. }
  1164. currentEngine.setStage(rect)
  1165. if (onApplied) {
  1166. onApplied()
  1167. }
  1168. if (stageCanvasAttached) {
  1169. return
  1170. }
  1171. const canvasQuery = wx.createSelectorQuery().in(page)
  1172. canvasQuery.select('#mapCanvas').fields({ node: true, size: true })
  1173. canvasQuery.select('#routeLabelCanvas').fields({ node: true, size: true })
  1174. canvasQuery.exec((canvasRes) => {
  1175. const canvasRef = canvasRes[0] as any
  1176. const labelCanvasRef = canvasRes[1] as any
  1177. if (!canvasRef || !canvasRef.node) {
  1178. page.setData({
  1179. statusText: `WebGL 引擎初始化失败 (${INTERNAL_BUILD_VERSION})`,
  1180. })
  1181. return
  1182. }
  1183. const dpr = wx.getSystemInfoSync().pixelRatio || 1
  1184. try {
  1185. currentEngine.attachCanvas(
  1186. canvasRef.node,
  1187. rect.width,
  1188. rect.height,
  1189. dpr,
  1190. labelCanvasRef && labelCanvasRef.node ? labelCanvasRef.node : undefined,
  1191. )
  1192. stageCanvasAttached = true
  1193. } catch (error) {
  1194. page.setData({
  1195. statusText: `WebGL 鍒濆鍖栧け璐?(${INTERNAL_BUILD_VERSION})`,
  1196. })
  1197. }
  1198. })
  1199. }
  1200. const query = wx.createSelectorQuery().in(page)
  1201. query.select('.map-stage').boundingClientRect()
  1202. query.exec((res) => {
  1203. const rect = res[0] as WechatMiniprogram.BoundingClientRectCallbackResult | undefined
  1204. applyStage(rect)
  1205. })
  1206. },
  1207. handleTouchStart(event: WechatMiniprogram.TouchEvent) {
  1208. if (mapEngine) {
  1209. mapEngine.handleTouchStart(event)
  1210. }
  1211. },
  1212. handleTouchMove(event: WechatMiniprogram.TouchEvent) {
  1213. if (mapEngine) {
  1214. mapEngine.handleTouchMove(event)
  1215. }
  1216. },
  1217. handleTouchEnd(event: WechatMiniprogram.TouchEvent) {
  1218. if (mapEngine) {
  1219. mapEngine.handleTouchEnd(event)
  1220. }
  1221. },
  1222. handleTouchCancel() {
  1223. if (mapEngine) {
  1224. mapEngine.handleTouchCancel()
  1225. }
  1226. },
  1227. handleRecenter() {
  1228. if (mapEngine) {
  1229. mapEngine.handleRecenter()
  1230. }
  1231. },
  1232. handleRotateStep() {
  1233. if (mapEngine) {
  1234. mapEngine.handleRotateStep()
  1235. }
  1236. },
  1237. handleRotationReset() {
  1238. if (mapEngine) {
  1239. mapEngine.handleRotationReset()
  1240. }
  1241. },
  1242. handleSetManualMode() {
  1243. if (mapEngine) {
  1244. mapEngine.handleSetManualMode()
  1245. }
  1246. },
  1247. handleSetNorthUpMode() {
  1248. if (mapEngine) {
  1249. mapEngine.handleSetNorthUpMode()
  1250. }
  1251. },
  1252. handleSetHeadingUpMode() {
  1253. if (mapEngine) {
  1254. mapEngine.handleSetHeadingUpMode()
  1255. }
  1256. },
  1257. handleCycleNorthReferenceMode() {
  1258. if (mapEngine) {
  1259. mapEngine.handleCycleNorthReferenceMode()
  1260. }
  1261. },
  1262. handleAutoRotateCalibrate() {
  1263. if (mapEngine) {
  1264. mapEngine.handleAutoRotateCalibrate()
  1265. }
  1266. },
  1267. handleToggleGpsTracking() {
  1268. if (mapEngine) {
  1269. mapEngine.handleToggleGpsTracking()
  1270. }
  1271. },
  1272. handleSetRealLocationMode() {
  1273. if (mapEngine) {
  1274. mapEngine.handleSetRealLocationMode()
  1275. }
  1276. },
  1277. handleSetMockLocationMode() {
  1278. if (mapEngine) {
  1279. mapEngine.handleSetMockLocationMode()
  1280. }
  1281. },
  1282. handleConnectMockLocationBridge() {
  1283. if (mapEngine) {
  1284. mapEngine.handleConnectMockLocationBridge()
  1285. }
  1286. },
  1287. handleConnectAllMockSources() {
  1288. if (!mapEngine) {
  1289. return
  1290. }
  1291. mapEngine.handleConnectMockLocationBridge()
  1292. mapEngine.handleSetMockLocationMode()
  1293. mapEngine.handleSetMockHeartRateMode()
  1294. mapEngine.handleConnectMockHeartRateBridge()
  1295. },
  1296. handleOpenWebViewTest() {
  1297. wx.navigateTo({
  1298. url: '/pages/webview-test/webview-test',
  1299. })
  1300. },
  1301. handleMockBridgeUrlInput(event: WechatMiniprogram.Input) {
  1302. this.setData({
  1303. mockBridgeUrlDraft: event.detail.value,
  1304. })
  1305. },
  1306. handleSaveMockBridgeUrl() {
  1307. if (mapEngine) {
  1308. mapEngine.handleSetMockLocationBridgeUrl(this.data.mockBridgeUrlDraft)
  1309. }
  1310. },
  1311. handleDisconnectMockLocationBridge() {
  1312. if (mapEngine) {
  1313. mapEngine.handleDisconnectMockLocationBridge()
  1314. }
  1315. },
  1316. handleSetRealHeartRateMode() {
  1317. if (mapEngine) {
  1318. mapEngine.handleSetRealHeartRateMode()
  1319. }
  1320. },
  1321. handleSetMockHeartRateMode() {
  1322. if (mapEngine) {
  1323. mapEngine.handleSetMockHeartRateMode()
  1324. }
  1325. },
  1326. handleMockHeartRateBridgeUrlInput(event: WechatMiniprogram.Input) {
  1327. this.setData({
  1328. mockHeartRateBridgeUrlDraft: event.detail.value,
  1329. })
  1330. },
  1331. handleSaveMockHeartRateBridgeUrl() {
  1332. if (mapEngine) {
  1333. mapEngine.handleSetMockHeartRateBridgeUrl(this.data.mockHeartRateBridgeUrlDraft)
  1334. }
  1335. },
  1336. handleConnectMockHeartRateBridge() {
  1337. if (mapEngine) {
  1338. mapEngine.handleConnectMockHeartRateBridge()
  1339. }
  1340. },
  1341. handleDisconnectMockHeartRateBridge() {
  1342. if (mapEngine) {
  1343. mapEngine.handleDisconnectMockHeartRateBridge()
  1344. }
  1345. },
  1346. handleConnectHeartRate() {
  1347. if (mapEngine) {
  1348. mapEngine.handleConnectHeartRate()
  1349. }
  1350. },
  1351. handleDisconnectHeartRate() {
  1352. if (mapEngine) {
  1353. mapEngine.handleDisconnectHeartRate()
  1354. }
  1355. },
  1356. handleConnectHeartRateDevice(event: WechatMiniprogram.BaseEvent<{ deviceId?: string }>) {
  1357. if (mapEngine && event.currentTarget && event.currentTarget.dataset && event.currentTarget.dataset.deviceId) {
  1358. mapEngine.handleConnectHeartRateDevice(event.currentTarget.dataset.deviceId)
  1359. }
  1360. },
  1361. handleClearPreferredHeartRateDevice() {
  1362. if (this.data.lockHeartRateDevice) {
  1363. return
  1364. }
  1365. if (mapEngine) {
  1366. mapEngine.handleClearPreferredHeartRateDevice()
  1367. }
  1368. },
  1369. handleDebugHeartRateBlue() {
  1370. if (mapEngine) {
  1371. mapEngine.handleDebugHeartRateTone('blue')
  1372. }
  1373. },
  1374. handleDebugHeartRatePurple() {
  1375. if (mapEngine) {
  1376. mapEngine.handleDebugHeartRateTone('purple')
  1377. }
  1378. },
  1379. handleDebugHeartRateGreen() {
  1380. if (mapEngine) {
  1381. mapEngine.handleDebugHeartRateTone('green')
  1382. }
  1383. },
  1384. handleDebugHeartRateYellow() {
  1385. if (mapEngine) {
  1386. mapEngine.handleDebugHeartRateTone('yellow')
  1387. }
  1388. },
  1389. handleDebugHeartRateOrange() {
  1390. if (mapEngine) {
  1391. mapEngine.handleDebugHeartRateTone('orange')
  1392. }
  1393. },
  1394. handleDebugHeartRateRed() {
  1395. if (mapEngine) {
  1396. mapEngine.handleDebugHeartRateTone('red')
  1397. }
  1398. },
  1399. handleClearDebugHeartRate() {
  1400. if (mapEngine) {
  1401. mapEngine.handleClearDebugHeartRate()
  1402. }
  1403. },
  1404. handleToggleOsmReference() {
  1405. if (mapEngine) {
  1406. mapEngine.handleToggleOsmReference()
  1407. }
  1408. },
  1409. handleStartGame() {
  1410. if (mapEngine) {
  1411. mapEngine.handleStartGame()
  1412. }
  1413. },
  1414. handleLoadClassicConfig() {
  1415. this.loadMapConfigFromRemote(CLASSIC_REMOTE_GAME_CONFIG_URL, '顺序赛配置')
  1416. },
  1417. handleLoadScoreOConfig() {
  1418. this.loadMapConfigFromRemote(SCORE_O_REMOTE_GAME_CONFIG_URL, '积分赛配置')
  1419. },
  1420. handleForceExitGame() {
  1421. if (!mapEngine || this.data.gameSessionStatus !== 'running') {
  1422. return
  1423. }
  1424. wx.showModal({
  1425. title: '确认退出',
  1426. content: '确认强制结束当前对局并返回开始前状态?',
  1427. confirmText: '确认退出',
  1428. cancelText: '取消',
  1429. success: (result) => {
  1430. if (result.confirm && mapEngine) {
  1431. mapEngine.handleForceExitGame()
  1432. }
  1433. },
  1434. })
  1435. },
  1436. handleSkipAction() {
  1437. if (!mapEngine || !this.data.skipButtonEnabled) {
  1438. return
  1439. }
  1440. if (!mapEngine.shouldConfirmSkipAction()) {
  1441. mapEngine.handleSkipAction()
  1442. return
  1443. }
  1444. wx.showModal({
  1445. title: '确认跳点',
  1446. content: '确认跳过当前检查点并切换到下一个目标点?',
  1447. confirmText: '确认跳过',
  1448. cancelText: '取消',
  1449. success: (result) => {
  1450. if (result.confirm && mapEngine) {
  1451. mapEngine.handleSkipAction()
  1452. }
  1453. },
  1454. })
  1455. },
  1456. handleClearMapTestArtifacts() {
  1457. if (mapEngine) {
  1458. mapEngine.handleClearMapTestArtifacts()
  1459. }
  1460. },
  1461. syncGameInfoPanelSnapshot() {
  1462. if (!mapEngine) {
  1463. return
  1464. }
  1465. const snapshot = mapEngine.getGameInfoSnapshot()
  1466. const localRows = snapshot.localRows.concat([
  1467. { label: '比例尺开关', value: this.data.showCenterScaleRuler ? '开启' : '关闭' },
  1468. { label: '比例尺锚点', value: this.data.centerScaleRulerAnchorMode === 'compass-center' ? '指北针圆心' : '屏幕中心' },
  1469. { label: '按钮习惯', value: this.data.sideButtonPlacement === 'right' ? '右手' : '左手' },
  1470. { label: '比例尺可见', value: this.data.centerScaleRulerVisible ? 'true' : 'false' },
  1471. { label: '比例尺中心X', value: `${this.data.centerScaleRulerCenterXPx}px` },
  1472. { label: '比例尺零点Y', value: `${this.data.centerScaleRulerZeroYPx}px` },
  1473. { label: '比例尺高度', value: `${this.data.centerScaleRulerHeightPx}px` },
  1474. { label: '比例尺主刻度数', value: String(this.data.centerScaleRulerMajorMarks.length) },
  1475. ])
  1476. this.setData({
  1477. gameInfoTitle: snapshot.title,
  1478. gameInfoSubtitle: snapshot.subtitle,
  1479. gameInfoLocalRows: localRows,
  1480. gameInfoGlobalRows: snapshot.globalRows,
  1481. })
  1482. },
  1483. syncResultSceneSnapshot() {
  1484. if (!mapEngine) {
  1485. return
  1486. }
  1487. const snapshot = mapEngine.getResultSceneSnapshot()
  1488. this.setData({
  1489. resultSceneTitle: snapshot.title,
  1490. resultSceneSubtitle: snapshot.subtitle,
  1491. resultSceneHeroLabel: snapshot.heroLabel,
  1492. resultSceneHeroValue: snapshot.heroValue,
  1493. resultSceneRows: snapshot.rows,
  1494. })
  1495. },
  1496. scheduleGameInfoPanelSnapshotSync() {
  1497. if (!this.data.showGameInfoPanel) {
  1498. clearGameInfoPanelSyncTimer()
  1499. return
  1500. }
  1501. if (gameInfoPanelSyncTimer) {
  1502. return
  1503. }
  1504. gameInfoPanelSyncTimer = setTimeout(() => {
  1505. gameInfoPanelSyncTimer = 0
  1506. if (this.data.showGameInfoPanel) {
  1507. this.syncGameInfoPanelSnapshot()
  1508. }
  1509. }, 400) as unknown as number
  1510. },
  1511. handleOpenGameInfoPanel() {
  1512. clearGameInfoPanelSyncTimer()
  1513. this.syncGameInfoPanelSnapshot()
  1514. this.setData({
  1515. showDebugPanel: false,
  1516. showSystemSettingsPanel: false,
  1517. showGameInfoPanel: true,
  1518. ...buildSideButtonState({
  1519. sideButtonMode: this.data.sideButtonMode,
  1520. showGameInfoPanel: true,
  1521. showSystemSettingsPanel: false,
  1522. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1523. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1524. skipButtonEnabled: this.data.skipButtonEnabled,
  1525. gameSessionStatus: this.data.gameSessionStatus,
  1526. gpsLockEnabled: this.data.gpsLockEnabled,
  1527. gpsLockAvailable: this.data.gpsLockAvailable,
  1528. }),
  1529. })
  1530. },
  1531. handleCloseGameInfoPanel() {
  1532. clearGameInfoPanelSyncTimer()
  1533. this.setData({
  1534. showGameInfoPanel: false,
  1535. ...buildSideButtonState({
  1536. sideButtonMode: this.data.sideButtonMode,
  1537. showGameInfoPanel: false,
  1538. showSystemSettingsPanel: this.data.showSystemSettingsPanel,
  1539. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1540. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1541. skipButtonEnabled: this.data.skipButtonEnabled,
  1542. gameSessionStatus: this.data.gameSessionStatus,
  1543. gpsLockEnabled: this.data.gpsLockEnabled,
  1544. gpsLockAvailable: this.data.gpsLockAvailable,
  1545. }),
  1546. })
  1547. },
  1548. handleGameInfoPanelTap() {},
  1549. handleResultSceneTap() {},
  1550. handleCloseResultScene() {
  1551. this.setData({
  1552. showResultScene: false,
  1553. })
  1554. },
  1555. handleRestartFromResult() {
  1556. if (!mapEngine) {
  1557. return
  1558. }
  1559. this.setData({
  1560. showResultScene: false,
  1561. }, () => {
  1562. if (mapEngine) {
  1563. mapEngine.handleStartGame()
  1564. }
  1565. })
  1566. },
  1567. handleOpenSystemSettingsPanel() {
  1568. clearGameInfoPanelSyncTimer()
  1569. this.setData({
  1570. showDebugPanel: false,
  1571. showGameInfoPanel: false,
  1572. showSystemSettingsPanel: true,
  1573. ...buildSideButtonState({
  1574. sideButtonMode: this.data.sideButtonMode,
  1575. showGameInfoPanel: false,
  1576. showSystemSettingsPanel: true,
  1577. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1578. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1579. skipButtonEnabled: this.data.skipButtonEnabled,
  1580. gameSessionStatus: this.data.gameSessionStatus,
  1581. gpsLockEnabled: this.data.gpsLockEnabled,
  1582. gpsLockAvailable: this.data.gpsLockAvailable,
  1583. }),
  1584. })
  1585. },
  1586. handleCloseSystemSettingsPanel() {
  1587. this.setData({
  1588. showSystemSettingsPanel: false,
  1589. ...buildSideButtonState({
  1590. sideButtonMode: this.data.sideButtonMode,
  1591. showGameInfoPanel: this.data.showGameInfoPanel,
  1592. showSystemSettingsPanel: false,
  1593. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1594. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1595. skipButtonEnabled: this.data.skipButtonEnabled,
  1596. gameSessionStatus: this.data.gameSessionStatus,
  1597. gpsLockEnabled: this.data.gpsLockEnabled,
  1598. gpsLockAvailable: this.data.gpsLockAvailable,
  1599. }),
  1600. })
  1601. },
  1602. handleSystemSettingsPanelTap() {},
  1603. handleSetAnimationLevelStandard() {
  1604. if (this.data.lockAnimationLevel || !mapEngine) {
  1605. return
  1606. }
  1607. mapEngine.handleSetAnimationLevel('standard')
  1608. persistStoredUserSettings({
  1609. ...loadStoredUserSettings(),
  1610. animationLevel: 'standard',
  1611. })
  1612. },
  1613. handleSetAnimationLevelLite() {
  1614. if (this.data.lockAnimationLevel || !mapEngine) {
  1615. return
  1616. }
  1617. mapEngine.handleSetAnimationLevel('lite')
  1618. persistStoredUserSettings({
  1619. ...loadStoredUserSettings(),
  1620. animationLevel: 'lite',
  1621. })
  1622. },
  1623. handleSetSideButtonPlacementLeft() {
  1624. if (this.data.lockSideButtonPlacement) {
  1625. return
  1626. }
  1627. this.setData({
  1628. sideButtonPlacement: 'left',
  1629. })
  1630. persistStoredUserSettings({
  1631. ...loadStoredUserSettings(),
  1632. sideButtonPlacement: 'left',
  1633. })
  1634. },
  1635. handleSetSideButtonPlacementRight() {
  1636. if (this.data.lockSideButtonPlacement) {
  1637. return
  1638. }
  1639. this.setData({
  1640. sideButtonPlacement: 'right',
  1641. })
  1642. persistStoredUserSettings({
  1643. ...loadStoredUserSettings(),
  1644. sideButtonPlacement: 'right',
  1645. })
  1646. },
  1647. handleSetAutoRotateEnabledOn() {
  1648. if (this.data.lockAutoRotate || !mapEngine) {
  1649. return
  1650. }
  1651. mapEngine.handleSetHeadingUpMode()
  1652. persistStoredUserSettings({
  1653. ...loadStoredUserSettings(),
  1654. autoRotateEnabled: true,
  1655. })
  1656. },
  1657. handleSetAutoRotateEnabledOff() {
  1658. if (this.data.lockAutoRotate || !mapEngine) {
  1659. return
  1660. }
  1661. mapEngine.handleSetManualMode()
  1662. persistStoredUserSettings({
  1663. ...loadStoredUserSettings(),
  1664. autoRotateEnabled: false,
  1665. })
  1666. },
  1667. handleSetCompassTuningSmooth() {
  1668. if (this.data.lockCompassTuning || !mapEngine) {
  1669. return
  1670. }
  1671. mapEngine.handleSetCompassTuningProfile('smooth')
  1672. persistStoredUserSettings({
  1673. ...loadStoredUserSettings(),
  1674. compassTuningProfile: 'smooth',
  1675. })
  1676. },
  1677. handleSetCompassTuningBalanced() {
  1678. if (this.data.lockCompassTuning || !mapEngine) {
  1679. return
  1680. }
  1681. mapEngine.handleSetCompassTuningProfile('balanced')
  1682. persistStoredUserSettings({
  1683. ...loadStoredUserSettings(),
  1684. compassTuningProfile: 'balanced',
  1685. })
  1686. },
  1687. handleSetCompassTuningResponsive() {
  1688. if (this.data.lockCompassTuning || !mapEngine) {
  1689. return
  1690. }
  1691. mapEngine.handleSetCompassTuningProfile('responsive')
  1692. persistStoredUserSettings({
  1693. ...loadStoredUserSettings(),
  1694. compassTuningProfile: 'responsive',
  1695. })
  1696. },
  1697. handleSetNorthReferenceMagnetic() {
  1698. if (this.data.lockNorthReference || !mapEngine) {
  1699. return
  1700. }
  1701. mapEngine.handleSetNorthReferenceMode('magnetic')
  1702. persistStoredUserSettings({
  1703. ...loadStoredUserSettings(),
  1704. northReferenceMode: 'magnetic',
  1705. })
  1706. },
  1707. handleSetNorthReferenceTrue() {
  1708. if (this.data.lockNorthReference || !mapEngine) {
  1709. return
  1710. }
  1711. mapEngine.handleSetNorthReferenceMode('true')
  1712. persistStoredUserSettings({
  1713. ...loadStoredUserSettings(),
  1714. northReferenceMode: 'true',
  1715. })
  1716. },
  1717. handleToggleSettingLock(event: WechatMiniprogram.TouchEvent) {
  1718. const key = event.currentTarget.dataset.key as SettingLockKey | undefined
  1719. if (!key) {
  1720. return
  1721. }
  1722. const nextValue = !this.data[key]
  1723. this.setData({
  1724. [key]: nextValue,
  1725. } as Record<string, boolean>)
  1726. persistStoredUserSettings(toggleStoredSettingLock(loadStoredUserSettings(), key))
  1727. },
  1728. handleOverlayTouch() {},
  1729. handlePunchAction() {
  1730. if (!this.data.punchButtonEnabled) {
  1731. return
  1732. }
  1733. if (mapEngine) {
  1734. mapEngine.handlePunchAction()
  1735. }
  1736. },
  1737. handleOpenPendingContentCard() {
  1738. if (mapEngine) {
  1739. mapEngine.openPendingContentCard()
  1740. }
  1741. },
  1742. openH5Experience(request: H5ExperienceRequest) {
  1743. wx.navigateTo({
  1744. url: '/pages/experience-webview/experience-webview',
  1745. success: (result) => {
  1746. const eventChannel = result.eventChannel
  1747. eventChannel.on('fallback', (payload: H5ExperienceFallbackPayload) => {
  1748. if (mapEngine) {
  1749. mapEngine.handleH5ExperienceFallback(payload)
  1750. }
  1751. })
  1752. eventChannel.on('close', () => {
  1753. if (mapEngine) {
  1754. mapEngine.handleH5ExperienceClosed()
  1755. }
  1756. })
  1757. eventChannel.on('submitResult', () => {
  1758. if (mapEngine) {
  1759. mapEngine.handleH5ExperienceClosed()
  1760. }
  1761. })
  1762. eventChannel.emit('init', request)
  1763. },
  1764. fail: () => {
  1765. if (mapEngine) {
  1766. mapEngine.handleH5ExperienceFallback(request.fallback)
  1767. }
  1768. },
  1769. })
  1770. },
  1771. handleCloseContentCard() {
  1772. if (mapEngine) {
  1773. mapEngine.closeContentCard()
  1774. }
  1775. },
  1776. handleClosePunchHint() {
  1777. clearPunchHintDismissTimer()
  1778. this.setData({
  1779. showPunchHintBanner: false,
  1780. })
  1781. },
  1782. handlePunchHintTap() {},
  1783. handleHudPanelChange(event: WechatMiniprogram.CustomEvent<{ current: number }>) {
  1784. this.setData({
  1785. hudPanelIndex: event.detail.current || 0,
  1786. })
  1787. },
  1788. handleCycleSideButtons() {
  1789. const nextMode = getNextSideButtonMode(this.data.sideButtonMode)
  1790. this.setData({
  1791. ...buildSideButtonVisibility(nextMode),
  1792. ...buildSideButtonState({
  1793. sideButtonMode: nextMode,
  1794. showGameInfoPanel: this.data.showGameInfoPanel,
  1795. showSystemSettingsPanel: this.data.showSystemSettingsPanel,
  1796. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1797. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1798. skipButtonEnabled: this.data.skipButtonEnabled,
  1799. gameSessionStatus: this.data.gameSessionStatus,
  1800. gpsLockEnabled: this.data.gpsLockEnabled,
  1801. gpsLockAvailable: this.data.gpsLockAvailable,
  1802. }),
  1803. })
  1804. },
  1805. handleToggleGpsLock() {
  1806. if (mapEngine) {
  1807. mapEngine.handleToggleGpsLock()
  1808. }
  1809. },
  1810. handleToggleMapRotateMode() {
  1811. if (!mapEngine || this.data.lockAutoRotate) {
  1812. return
  1813. }
  1814. if (this.data.orientationMode === 'heading-up') {
  1815. mapEngine.handleSetManualMode()
  1816. persistStoredUserSettings({
  1817. ...loadStoredUserSettings(),
  1818. autoRotateEnabled: false,
  1819. })
  1820. return
  1821. }
  1822. mapEngine.handleSetHeadingUpMode()
  1823. persistStoredUserSettings({
  1824. ...loadStoredUserSettings(),
  1825. autoRotateEnabled: true,
  1826. })
  1827. },
  1828. handleToggleDebugPanel() {
  1829. const nextShowDebugPanel = !this.data.showDebugPanel
  1830. if (!nextShowDebugPanel) {
  1831. clearGameInfoPanelSyncTimer()
  1832. }
  1833. if (mapEngine) {
  1834. mapEngine.setDiagnosticUiEnabled(nextShowDebugPanel)
  1835. }
  1836. this.setData({
  1837. showDebugPanel: nextShowDebugPanel,
  1838. showGameInfoPanel: false,
  1839. showSystemSettingsPanel: false,
  1840. ...buildSideButtonState({
  1841. sideButtonMode: this.data.sideButtonMode,
  1842. showGameInfoPanel: false,
  1843. showSystemSettingsPanel: false,
  1844. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1845. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1846. skipButtonEnabled: this.data.skipButtonEnabled,
  1847. gameSessionStatus: this.data.gameSessionStatus,
  1848. gpsLockEnabled: this.data.gpsLockEnabled,
  1849. gpsLockAvailable: this.data.gpsLockAvailable,
  1850. }),
  1851. })
  1852. },
  1853. handleCloseDebugPanel() {
  1854. if (mapEngine) {
  1855. mapEngine.setDiagnosticUiEnabled(false)
  1856. }
  1857. this.setData({
  1858. showDebugPanel: false,
  1859. ...buildSideButtonState({
  1860. sideButtonMode: this.data.sideButtonMode,
  1861. showGameInfoPanel: this.data.showGameInfoPanel,
  1862. showSystemSettingsPanel: this.data.showSystemSettingsPanel,
  1863. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1864. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1865. skipButtonEnabled: this.data.skipButtonEnabled,
  1866. gameSessionStatus: this.data.gameSessionStatus,
  1867. gpsLockEnabled: this.data.gpsLockEnabled,
  1868. gpsLockAvailable: this.data.gpsLockAvailable,
  1869. }),
  1870. })
  1871. },
  1872. applyCenterScaleRulerSettings(nextEnabled: boolean, nextAnchorMode: CenterScaleRulerAnchorMode) {
  1873. this.data.showCenterScaleRuler = nextEnabled
  1874. this.data.centerScaleRulerAnchorMode = nextAnchorMode
  1875. clearCenterScaleRulerSyncTimer()
  1876. clearCenterScaleRulerUpdateTimer()
  1877. const syncRulerFromEngine = () => {
  1878. if (!mapEngine) {
  1879. return
  1880. }
  1881. const engineSnapshot = mapEngine.getInitialData() as Partial<MapPageData>
  1882. updateCenterScaleRulerInputCache(engineSnapshot)
  1883. const mergedData = {
  1884. ...centerScaleRulerInputCache,
  1885. ...this.data,
  1886. showCenterScaleRuler: nextEnabled,
  1887. centerScaleRulerAnchorMode: nextAnchorMode,
  1888. } as MapPageData
  1889. this.setData({
  1890. ...filterDebugOnlyPatch(engineSnapshot, this.data.showDebugPanel, nextEnabled),
  1891. showCenterScaleRuler: nextEnabled,
  1892. centerScaleRulerAnchorMode: nextAnchorMode,
  1893. ...buildCenterScaleRulerPatch(mergedData),
  1894. ...buildSideButtonState(mergedData),
  1895. })
  1896. }
  1897. if (!nextEnabled) {
  1898. syncRulerFromEngine()
  1899. return
  1900. }
  1901. this.setData({
  1902. showCenterScaleRuler: true,
  1903. centerScaleRulerAnchorMode: nextAnchorMode,
  1904. ...buildSideButtonState({
  1905. ...this.data,
  1906. showCenterScaleRuler: true,
  1907. centerScaleRulerAnchorMode: nextAnchorMode,
  1908. } as MapPageData),
  1909. })
  1910. this.measureStageAndCanvas(() => {
  1911. syncRulerFromEngine()
  1912. })
  1913. centerScaleRulerSyncTimer = setTimeout(() => {
  1914. centerScaleRulerSyncTimer = 0
  1915. if (!this.data.showCenterScaleRuler) {
  1916. return
  1917. }
  1918. syncRulerFromEngine()
  1919. }, 96) as unknown as number
  1920. },
  1921. handleSetCenterScaleRulerVisibleOn() {
  1922. if (this.data.lockScaleRulerVisible) {
  1923. return
  1924. }
  1925. this.applyCenterScaleRulerSettings(true, this.data.centerScaleRulerAnchorMode)
  1926. persistStoredUserSettings({
  1927. ...loadStoredUserSettings(),
  1928. showCenterScaleRuler: true,
  1929. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1930. })
  1931. },
  1932. handleSetCenterScaleRulerVisibleOff() {
  1933. if (this.data.lockScaleRulerVisible) {
  1934. return
  1935. }
  1936. this.applyCenterScaleRulerSettings(false, this.data.centerScaleRulerAnchorMode)
  1937. persistStoredUserSettings({
  1938. ...loadStoredUserSettings(),
  1939. showCenterScaleRuler: false,
  1940. centerScaleRulerAnchorMode: this.data.centerScaleRulerAnchorMode,
  1941. })
  1942. },
  1943. handleSetCenterScaleRulerAnchorScreenCenter() {
  1944. if (this.data.lockScaleRulerAnchor) {
  1945. return
  1946. }
  1947. this.applyCenterScaleRulerSettings(this.data.showCenterScaleRuler, 'screen-center')
  1948. persistStoredUserSettings({
  1949. ...loadStoredUserSettings(),
  1950. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1951. centerScaleRulerAnchorMode: 'screen-center',
  1952. })
  1953. },
  1954. handleSetCenterScaleRulerAnchorCompassCenter() {
  1955. if (this.data.lockScaleRulerAnchor) {
  1956. return
  1957. }
  1958. this.applyCenterScaleRulerSettings(this.data.showCenterScaleRuler, 'compass-center')
  1959. persistStoredUserSettings({
  1960. ...loadStoredUserSettings(),
  1961. showCenterScaleRuler: this.data.showCenterScaleRuler,
  1962. centerScaleRulerAnchorMode: 'compass-center',
  1963. })
  1964. },
  1965. handleToggleCenterScaleRulerAnchor() {
  1966. if (!this.data.showCenterScaleRuler) {
  1967. return
  1968. }
  1969. const nextAnchorMode: CenterScaleRulerAnchorMode = this.data.centerScaleRulerAnchorMode === 'screen-center'
  1970. ? 'compass-center'
  1971. : 'screen-center'
  1972. const engineSnapshot = mapEngine ? (mapEngine.getInitialData() as Partial<MapPageData>) : {}
  1973. updateCenterScaleRulerInputCache(engineSnapshot)
  1974. this.data.centerScaleRulerAnchorMode = nextAnchorMode
  1975. const mergedData = {
  1976. ...centerScaleRulerInputCache,
  1977. ...this.data,
  1978. centerScaleRulerAnchorMode: nextAnchorMode,
  1979. } as MapPageData
  1980. this.setData({
  1981. ...filterDebugOnlyPatch(engineSnapshot, this.data.showDebugPanel, true),
  1982. centerScaleRulerAnchorMode: nextAnchorMode,
  1983. ...buildCenterScaleRulerPatch(mergedData),
  1984. ...buildSideButtonState(mergedData),
  1985. })
  1986. if (this.data.showGameInfoPanel) {
  1987. this.syncGameInfoPanelSnapshot()
  1988. }
  1989. },
  1990. handleDebugPanelTap() {},
  1991. })