Use sfx in game

This commit is contained in:
Stephen Seo 2023-01-07 17:25:51 +09:00
parent 9235282962
commit 280a969d6a
3 changed files with 48 additions and 2 deletions

View file

@ -12,7 +12,7 @@ Game::Game()
: re(std::random_device{}()), dist(0, FOOD_COUNT - 1), score(0), : re(std::random_device{}()), dist(0, FOOD_COUNT - 1), score(0),
highScore(0), areaSizeRatio(1.0F), currentFood(dist(re)), highScore(0), areaSizeRatio(1.0F), currentFood(dist(re)),
blinkTimer(10.0F), cutTimer(0.0F), cutTimerRateInc(1.0F), blinkTimer(10.0F), cutTimer(0.0F), cutTimerRateInc(1.0F),
postCutTimer(0.0F) { postCutTimer(0.0F), audioNoticeTimer(7.0F) {
flags.set(0); flags.set(0);
flags.set(3); flags.set(3);
@ -24,6 +24,8 @@ void Game::do_update() {
draw_impl(); draw_impl();
} }
void Game::screen_resized() { flags.set(3); }
void Game::update_impl() { void Game::update_impl() {
const float dt = GetFrameTime(); const float dt = GetFrameTime();
@ -111,13 +113,19 @@ void Game::update_impl() {
flags.set(2); flags.set(2);
++score; ++score;
flags.set(0); flags.set(0);
PlaySound(nicecut.at(std::uniform_int_distribution<unsigned int>(
0, nicecut.size() - 1)(re)));
} else if (cutPos > offsetY + height / 3.0F) { } else if (cutPos > offsetY + height / 3.0F) {
// past range // past range
flags.set(5); flags.set(5);
postCutTimer = POST_CUT_TIME; postCutTimer = POST_CUT_TIME;
PlaySound(ohno.at(
std::uniform_int_distribution<unsigned int>(0, ohno.size() - 1)(re)));
} else { } else {
// before range // before range
postCutTimer = POST_CUT_TIME; postCutTimer = POST_CUT_TIME;
PlaySound(ohno.at(
std::uniform_int_distribution<unsigned int>(0, ohno.size() - 1)(re)));
} }
if (flags.test(4) && (flags.test(2) || flags.test(5)) && !flags.test(6)) { if (flags.test(4) && (flags.test(2) || flags.test(5)) && !flags.test(6)) {
@ -146,6 +154,33 @@ void Game::update_impl() {
reset(!flags.test(2)); reset(!flags.test(2));
} }
} }
if (!flags.test(7)) {
InitAudioDevice();
if (IsAudioDeviceReady()) {
nicecut.at(0) = LoadSound("resources/nicecut0.ogg");
nicecut.at(1) = LoadSound("resources/nicecut1.ogg");
nicecut.at(2) = LoadSound("resources/nicecut2.ogg");
nicecut.at(3) = LoadSound("resources/nicecut3.ogg");
nicecut.at(4) = LoadSound("resources/nicecut4.ogg");
nicecut.at(5) = LoadSound("resources/nicecut5.ogg");
nicecut.at(6) = LoadSound("resources/nicecut6.ogg");
ohno.at(0) = LoadSound("resources/ohno0.ogg");
ohno.at(1) = LoadSound("resources/ohno1.ogg");
ohno.at(2) = LoadSound("resources/ohno2.ogg");
ohno.at(3) = LoadSound("resources/ohno3.ogg");
ohno.at(4) = LoadSound("resources/ohno4.ogg");
ohno.at(5) = LoadSound("resources/ohno5.ogg");
ohno.at(6) = LoadSound("resources/ohno6.ogg");
flags.set(7);
}
}
if (audioNoticeTimer > 0.0F) {
audioNoticeTimer -= dt;
}
} }
void Game::draw_impl() { void Game::draw_impl() {
@ -205,6 +240,9 @@ void Game::draw_impl() {
DrawText(scoreString.c_str(), 2, 2, 32, BLACK); DrawText(scoreString.c_str(), 2, 2, 32, BLACK);
DrawText(highScoreString.c_str(), 2, 34, 32, BLACK); DrawText(highScoreString.c_str(), 2, 34, 32, BLACK);
if (audioNoticeTimer > 0.0F) {
DrawText("Try refreshing if there is no audio", 2, 70, 32, BLACK);
}
EndDrawing(); EndDrawing();
} }

View file

@ -2,6 +2,7 @@
#define LD52_HARVEST_FOOD_CUTS_GAME_H_ #define LD52_HARVEST_FOOD_CUTS_GAME_H_
// standard library includes // standard library includes
#include <array>
#include <bitset> #include <bitset>
#include <random> #include <random>
@ -14,6 +15,8 @@ public:
void do_update(); void do_update();
void screen_resized();
private: private:
void update_impl(); void update_impl();
void draw_impl(); void draw_impl();
@ -25,6 +28,8 @@ private:
std::string scoreString; std::string scoreString;
std::string highScoreString; std::string highScoreString;
Texture2D spriteSheet; Texture2D spriteSheet;
std::array<Sound, 7> nicecut;
std::array<Sound, 7> ohno;
unsigned long long score; unsigned long long score;
unsigned long long highScore; unsigned long long highScore;
/* /*
@ -35,6 +40,7 @@ private:
* 4 - cut has happened * 4 - cut has happened
* 5 - sad * 5 - sad
* 6 - relativeCutPos is set * 6 - relativeCutPos is set
* 7 - audio loaded
*/ */
std::bitset<32> flags; std::bitset<32> flags;
float areaSizeRatio; float areaSizeRatio;
@ -57,6 +63,7 @@ private:
float splitDY; float splitDY;
float splitDAngle; float splitDAngle;
float postCutTimer; float postCutTimer;
float audioNoticeTimer;
}; };
#endif #endif

View file

@ -22,6 +22,7 @@ EM_BOOL resize_event_callback(int event_type, const EmscriptenUiEvent *event,
void *ud) { void *ud) {
if (event_type == EMSCRIPTEN_EVENT_RESIZE) { if (event_type == EMSCRIPTEN_EVENT_RESIZE) {
SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height()); SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height());
((Game *)ud)->screen_resized();
} }
return false; return false;
} }
@ -45,7 +46,7 @@ int main() {
SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height()); SetWindowSize(call_js_get_canvas_width(), call_js_get_canvas_height());
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, nullptr, false, emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, &game, false,
resize_event_callback); resize_event_callback);
emscripten_set_main_loop_arg(game_update, &game, 0, 1); emscripten_set_main_loop_arg(game_update, &game, 0, 1);