hapticsDirector.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { type GameEffect } from '../core/gameResult'
  2. import { DEFAULT_GAME_HAPTICS_CONFIG, type FeedbackCueKey, type GameHapticsConfig } from './feedbackConfig'
  3. export class HapticsDirector {
  4. enabled: boolean
  5. config: GameHapticsConfig
  6. constructor(config: GameHapticsConfig = DEFAULT_GAME_HAPTICS_CONFIG) {
  7. this.enabled = true
  8. this.config = config
  9. }
  10. configure(config: GameHapticsConfig): void {
  11. this.config = config
  12. }
  13. setEnabled(enabled: boolean): void {
  14. this.enabled = enabled
  15. }
  16. destroy(): void {}
  17. trigger(key: FeedbackCueKey): void {
  18. if (!this.enabled || !this.config.enabled) {
  19. return
  20. }
  21. const cue = this.config.cues[key]
  22. if (!cue || !cue.enabled) {
  23. return
  24. }
  25. try {
  26. if (cue.pattern === 'long') {
  27. wx.vibrateLong()
  28. } else {
  29. wx.vibrateShort({ type: 'medium' })
  30. }
  31. } catch {}
  32. }
  33. handleEffects(effects: GameEffect[]): void {
  34. for (const effect of effects) {
  35. if (effect.type === 'session_started') {
  36. this.trigger('session_started')
  37. continue
  38. }
  39. if (effect.type === 'session_finished') {
  40. this.trigger('session_finished')
  41. continue
  42. }
  43. if (effect.type === 'session_cancelled') {
  44. this.trigger('session_finished')
  45. continue
  46. }
  47. if (effect.type === 'punch_feedback' && effect.tone === 'warning') {
  48. this.trigger('punch_feedback:warning')
  49. continue
  50. }
  51. if (effect.type === 'guidance_state_changed') {
  52. if (effect.guidanceState === 'searching') {
  53. this.trigger('guidance:searching')
  54. continue
  55. }
  56. if (effect.guidanceState === 'distant') {
  57. this.trigger('guidance:distant')
  58. continue
  59. }
  60. if (effect.guidanceState === 'approaching') {
  61. this.trigger('guidance:approaching')
  62. continue
  63. }
  64. this.trigger('guidance:ready')
  65. continue
  66. }
  67. if (effect.type === 'control_completed') {
  68. if (effect.controlKind === 'start') {
  69. this.trigger('control_completed:start')
  70. continue
  71. }
  72. if (effect.controlKind === 'finish') {
  73. this.trigger('control_completed:finish')
  74. continue
  75. }
  76. this.trigger('control_completed:control')
  77. }
  78. }
  79. }
  80. }