hapticsDirector.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 === 'punch_feedback' && effect.tone === 'warning') {
  44. this.trigger('punch_feedback:warning')
  45. continue
  46. }
  47. if (effect.type === 'guidance_state_changed') {
  48. if (effect.guidanceState === 'searching') {
  49. this.trigger('guidance:searching')
  50. continue
  51. }
  52. if (effect.guidanceState === 'approaching') {
  53. this.trigger('guidance:approaching')
  54. continue
  55. }
  56. this.trigger('guidance:ready')
  57. continue
  58. }
  59. if (effect.type === 'control_completed') {
  60. if (effect.controlKind === 'start') {
  61. this.trigger('control_completed:start')
  62. continue
  63. }
  64. if (effect.controlKind === 'finish') {
  65. this.trigger('control_completed:finish')
  66. continue
  67. }
  68. this.trigger('control_completed:control')
  69. }
  70. }
  71. }
  72. }