hapticsDirector.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 === 'approaching') {
  57. this.trigger('guidance:approaching')
  58. continue
  59. }
  60. this.trigger('guidance:ready')
  61. continue
  62. }
  63. if (effect.type === 'control_completed') {
  64. if (effect.controlKind === 'start') {
  65. this.trigger('control_completed:start')
  66. continue
  67. }
  68. if (effect.controlKind === 'finish') {
  69. this.trigger('control_completed:finish')
  70. continue
  71. }
  72. this.trigger('control_completed:control')
  73. }
  74. }
  75. }
  76. }