Skip to content

Commit 6859d8c

Browse files
committed
fix sound effect command updating
1 parent dcb20c6 commit 6859d8c

2 files changed

Lines changed: 39 additions & 75 deletions

File tree

LuaSTG/LuaSTG/GameResource/Implement/ResourceSoundEffectImpl.cpp

Lines changed: 30 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,96 +2,60 @@
22

33
namespace luastg {
44
void ResourceSoundEffectImpl::FlushCommand() {
5-
// 根据最后的命令对音效进行操作
5+
// apply
66
switch (m_last_command.type) {
7-
case CommandType::None:
7+
case CommandType::none:
88
break;
9-
case CommandType::Play:
10-
m_player->resume();
11-
break;
12-
case CommandType::Stop:
13-
m_player->pause();
14-
break;
15-
case CommandType::Reset:
9+
case CommandType::play:
1610
m_player->setVolume(m_last_command.vol);
1711
m_player->setBalance(m_last_command.pan);
1812
m_player->play(0.0);
1913
break;
20-
case CommandType::ResetAndStop:
14+
case CommandType::pause:
15+
m_player->pause();
16+
break;
17+
case CommandType::resume:
18+
m_player->resume();
19+
break;
20+
case CommandType::stop:
2121
m_player->stop();
2222
break;
2323
}
24-
// 重置为无命令
25-
m_last_command.type = CommandType::None;
26-
m_last_command.vol = 0.0f;
24+
// clear
25+
m_last_command.type = CommandType::none;
26+
m_last_command.vol = 1.0f;
2727
m_last_command.pan = 0.0f;
28-
// 刷新状态
29-
if (m_status == 2) {
30-
if (m_player->getState() != core::AudioPlayerState::playing) {
31-
m_status = 0; // 已结束播放
28+
// read state
29+
if (m_state != core::AudioPlayerState::stopped) {
30+
if (m_player->getState() == core::AudioPlayerState::stopped) {
31+
m_state = core::AudioPlayerState::stopped;
3232
}
3333
}
3434
}
3535
void ResourceSoundEffectImpl::Play(float const vol, float const pan) {
36-
// 优先级最高的命令,覆盖其他一切命令
37-
m_last_command.type = CommandType::Reset;
38-
m_last_command.vol = std::max(m_last_command.vol, vol); // 取音量最高
36+
m_last_command.type = CommandType::play;
37+
m_last_command.vol = vol;
3938
m_last_command.pan = pan;
40-
m_status = 2; // playing
39+
m_state = core::AudioPlayerState::playing;
4140
}
4241
void ResourceSoundEffectImpl::Resume() {
43-
switch (m_last_command.type) {
44-
case CommandType::None: // 初始化命令
45-
case CommandType::Play: // 就是这个命令
46-
case CommandType::Stop: // 覆盖暂停/停止命令
47-
m_last_command.type = CommandType::Play;
48-
break;
49-
case CommandType::Reset:
50-
// 保持当前命令
51-
break;
52-
case CommandType::ResetAndStop:
53-
// 修正为重新播放命令
54-
m_last_command.type = CommandType::Reset;
55-
break;
42+
if (m_state == core::AudioPlayerState::paused) {
43+
m_last_command.type = CommandType::resume;
44+
m_state = core::AudioPlayerState::playing;
5645
}
57-
m_status = 2; // playing
5846
}
5947
void ResourceSoundEffectImpl::Pause() {
60-
switch (m_last_command.type) {
61-
case CommandType::None: // 初始化命令
62-
case CommandType::Play: // 覆盖播放/恢复命令
63-
case CommandType::Stop: // 就是这个命令
64-
m_last_command.type = CommandType::Stop;
65-
break;
66-
case CommandType::Reset:
67-
// 修正为回到起始命令
68-
m_last_command.type = CommandType::ResetAndStop;
69-
break;
70-
case CommandType::ResetAndStop:
71-
// 保持当前命令
72-
break;
48+
if (m_state == core::AudioPlayerState::playing) {
49+
m_last_command.type = CommandType::pause;
50+
m_state = core::AudioPlayerState::paused;
7351
}
74-
m_status = 1; // pause
7552
}
7653
void ResourceSoundEffectImpl::Stop() {
77-
switch (m_last_command.type) {
78-
case CommandType::None: // 初始化命令
79-
case CommandType::Play: // 覆盖播放/恢复命令
80-
case CommandType::Stop: // 就是这个命令
81-
m_last_command.type = CommandType::Stop;
82-
break;
83-
case CommandType::Reset:
84-
// 修正为回到起始命令
85-
m_last_command.type = CommandType::ResetAndStop;
86-
break;
87-
case CommandType::ResetAndStop:
88-
// 保持当前命令
89-
break;
90-
}
91-
m_status = 0; // stop
54+
m_last_command.type = CommandType::stop;
55+
m_state = core::AudioPlayerState::stopped;
9256
}
93-
bool ResourceSoundEffectImpl::IsPlaying() { return m_player->getState() == core::AudioPlayerState::playing || m_status == 2; }
94-
bool ResourceSoundEffectImpl::IsStopped() { return !IsPlaying() && m_status != 1; }
57+
bool ResourceSoundEffectImpl::IsPlaying() { return m_state == core::AudioPlayerState::playing; }
58+
bool ResourceSoundEffectImpl::IsStopped() { return m_state == core::AudioPlayerState::stopped; }
9559
bool ResourceSoundEffectImpl::SetSpeed(float const speed) { return m_player->setSpeed(speed); }
9660
float ResourceSoundEffectImpl::GetSpeed() { return m_player->getSpeed(); }
9761

LuaSTG/LuaSTG/GameResource/Implement/ResourceSoundEffectImpl.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ namespace luastg {
2020

2121
private:
2222
enum class CommandType : uint8_t {
23-
None,
24-
Play,
25-
Stop,
26-
Reset,
27-
ResetAndStop,
23+
none,
24+
play,
25+
pause,
26+
resume,
27+
stop,
2828
};
2929

3030
struct Command {
31-
CommandType type = CommandType::None;
32-
float vol = 0.0f;
33-
float pan = 0.0f;
31+
CommandType type{ CommandType::none };
32+
float vol{ 1.0f };
33+
float pan{ 0.0f };
3434
};
3535

3636
core::SmartReference<core::IAudioPlayer> m_player;
37-
int m_status = 0; // 0停止 1暂停 2播放
37+
core::AudioPlayerState m_state{ core::AudioPlayerState::stopped };
3838
Command m_last_command;
3939
};
4040
}

0 commit comments

Comments
 (0)