Repository navigation
Expand file tree
/
Copy pathcomponent_sound.go
More file actions
174 lines (142 loc) · 5.52 KB
/
Copy pathcomponent_sound.go
File metadata and controls
174 lines (142 loc) · 5.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Copyright (c) 2021 The XGo Authors (xgo.dev). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package spx
import (
coreproject "github.com/goplus/spx/v3/internal/core/project"
engine "github.com/goplus/spx/v3/pkg/spx/pkg/engine"
)
// ============================================================================
// Sound Component
// ============================================================================
// This component encapsulates all sound-related functionality.
type soundComponent struct {
componentBase
// Sound object
soundObj engine.Object
// Pending audios
pendingAudios []string
}
// ============================================================================
// Lifecycle
// ============================================================================
// initialize initializes the sound component from config.
func (s *soundComponent) initialize(sprite *SpriteImpl, spriteCfg *coreproject.SpriteConfig) {
s.componentBase.initialize(sprite, spriteCfg)
// Always initialize with default sound values
s.soundObj = 0
s.pendingAudios = make([]string, 0)
}
// cloneFrom creates a new sound component by cloning from source.
func (s *soundComponent) cloneFrom(src component, newSprite *SpriteImpl) component {
// srcSound := src.(*soundComponent) // Not used since we don't clone sound state
return &soundComponent{
componentBase: componentBase{sprite: newSprite},
soundObj: 0, // Don't share sound object, will be allocated if needed
pendingAudios: make([]string, 0),
}
}
// onDestroy cleans up when the component is destroyed.
func (s *soundComponent) onDestroy() {
if s.soundObj != 0 {
s.sprite.g.soundMgr.ReleaseSound(s.soundObj)
s.soundObj = 0
}
}
// ============================================================================
// Sound Playback Control
// ============================================================================
func (s *soundComponent) Play(name SoundName, loop bool) {
s.checkSoundObj()
s.sprite.g.playSound(s.sprite.runtimeState.SyncSprite, s.soundObj, name, loop, s.sprite.g.audioState.AudioAttenuation, s.sprite.g.audioState.AudioMaxDistance)
}
func (s *soundComponent) PlayAndWait(name SoundName) {
s.checkSoundObj()
s.sprite.g.playSoundAndWait(s.sprite.runtimeState.SyncSprite, s.soundObj, name, s.sprite.g.audioState.AudioAttenuation, s.sprite.g.audioState.AudioMaxDistance)
}
func (s *soundComponent) PausePlaying(name SoundName) {
s.sprite.g.pauseSound(name)
}
func (s *soundComponent) ResumePlaying(name SoundName) {
s.sprite.g.resumeSound(name)
}
func (s *soundComponent) StopPlaying(name SoundName) {
s.sprite.g.stopSound(name)
}
// ============================================================================
// Sound Volume Control
// ============================================================================
func (s *soundComponent) GetVolume() float64 {
s.checkSoundObj()
return s.sprite.g.soundMgr.GetVolume(s.soundObj)
}
func (s *soundComponent) SetVolume(volume float64) {
s.checkSoundObj()
s.sprite.g.soundMgr.SetVolume(s.soundObj, volume)
}
func (s *soundComponent) ChangeVolume(delta float64) {
s.checkSoundObj()
s.sprite.g.soundMgr.ChangeVolume(s.soundObj, delta)
}
// ============================================================================
// Sound Effects Control
// ============================================================================
func (s *soundComponent) GetSoundEffect(kind SoundEffectKind) float64 {
s.checkSoundObj()
return s.sprite.g.getSoundEffect(s.soundObj, kind)
}
func (s *soundComponent) SetSoundEffect(kind SoundEffectKind, value float64) {
s.checkSoundObj()
s.sprite.g.setSoundEffect(s.soundObj, kind, value)
}
func (s *soundComponent) ChangeSoundEffect(kind SoundEffectKind, delta float64) {
s.checkSoundObj()
s.sprite.g.changeSoundEffect(s.soundObj, kind, delta)
}
// ============================================================================
// Internal Audio Management
// ============================================================================
func (s *soundComponent) playAudio(name SoundName, loop bool) int64 {
s.checkSoundObj()
return s.sprite.g.playSound(s.sprite.runtimeState.SyncSprite, s.soundObj, name, loop, s.sprite.g.audioState.AudioAttenuation, s.sprite.g.audioState.AudioMaxDistance)
}
func (s *soundComponent) stopAudioPlayback(id int64) {
s.sprite.g.stopSoundPlayback(id)
}
func (s *soundComponent) restartAudioPlayback(id int64) bool {
return s.sprite.g.restartSoundPlayback(id)
}
func (s *soundComponent) restartOrPlayLoopedAudio(name SoundName, id int64) int64 {
if name == "" {
return 0
}
if id != 0 && s.restartAudioPlayback(id) {
return id
}
return s.playAudio(name, true)
}
func (s *soundComponent) checkSoundObj() {
if s.soundObj == 0 {
s.soundObj = s.sprite.g.soundMgr.AllocSound()
}
}
func (s *soundComponent) addPendingAudio(audioName string) {
s.pendingAudios = append(s.pendingAudios, audioName)
}
func (s *soundComponent) takePendingAudios(buffer []string) []string {
buffer = append(buffer, s.pendingAudios...)
s.pendingAudios = s.pendingAudios[:0]
return buffer
}