From de7ee5ef022a8ccaece0ea5f5402adedeafe36b4 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 12 Mar 2022 09:05:21 -0500 Subject: added back bump sound (and assets for fuller music) --- src/game.h | 11 +++++++++++ src/main.cpp | 16 ++++++++++++++-- src/timer.h | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/timer.h (limited to 'src') diff --git a/src/game.h b/src/game.h index f0385ee..c489afc 100644 --- a/src/game.h +++ b/src/game.h @@ -7,6 +7,7 @@ #include #include "map.h" #include "muxer.h" +#include "timer.h" const int GAME_WIDTH = 640*2; const int GAME_HEIGHT = 480*2; @@ -43,6 +44,14 @@ struct Input { bool right = false; bool up = false; bool down = false; + + bool operator==(const Input& rhs) const { + return std::tie(left, right, up, down) == std::tie(rhs.left, rhs.right, rhs.up, rhs.down); + } + + bool operator!=(const Input& rhs) const { + return !(*this == rhs); + } }; using coord = std::tuple; @@ -110,6 +119,8 @@ public: bool firstInput = false; Input lastInput; + bool alreadyBumped = false; + Timer bumpCooldown = {500}; }; diff --git a/src/main.cpp b/src/main.cpp index 25da94f..cd1a8c2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -106,7 +106,11 @@ bool movePlayer(Game& game, int x, int y) return true; } else { - //game.muxer.playSoundAtPosition("bump", game.player_x, game.player_y); + if (!game.alreadyBumped) { + game.muxer.playSoundAtPosition("bump", x, y); + game.alreadyBumped = true; + game.bumpCooldown.reset(); + } return false; } @@ -409,7 +413,7 @@ void setZoom(Game& game, size_t zoom) { game.curBoundY = game.map.getBottom() - zoom * ZOOM_Y_FACTOR; } - + int zoomLevel = getZoomLevel(game); if (zoomLevel == 0) { game.muxer.setMusicLevel(0); @@ -538,6 +542,7 @@ int main(int, char**) game.numLamps++; game.dirtyLighting = true; kickUpDust(game, game.player_x, game.player_y, 0); + game.muxer.playSoundAtPosition("drop", game.player_x, game.player_y); if (game.firstInput) { @@ -576,6 +581,8 @@ int main(int, char**) std::get<0>(moveTo), std::get<1>(moveTo)); } + + //game.muxer.playSoundAtPosition("dash", game.player_x, game.player_y); } } } @@ -592,6 +599,11 @@ int main(int, char**) keystate.up = state[SDL_SCANCODE_UP]; keystate.down = state[SDL_SCANCODE_DOWN]; + game.bumpCooldown.accumulate(frameTime); + if (game.alreadyBumped && keystate != game.lastInput && game.bumpCooldown.step()) { + game.alreadyBumped = false; + } + if (keystate.left || keystate.right || keystate.up || keystate.down) { game.firstInput = true; diff --git a/src/timer.h b/src/timer.h new file mode 100644 index 0000000..ec34f3e --- /dev/null +++ b/src/timer.h @@ -0,0 +1,32 @@ +#ifndef TIMER_H_45E2F1F9 +#define TIMER_H_45E2F1F9 + +class Timer { +public: + + Timer(int dt) : dt_(dt) {} + + void accumulate(int t) { + acc_ += t; + } + + bool step() { + if (acc_ > dt_) { + acc_ -= dt_; + return true; + } else { + return false; + } + } + + void reset() { + acc_ = 0; + } + +private: + + int dt_; + int acc_ = 0; +}; + +#endif /* end of include guard: TIMER_H_45E2F1F9 */ -- cgit 1.4.1