Skip to content

Commit

Permalink
Sound and music
Browse files Browse the repository at this point in the history
  • Loading branch information
feresr committed May 20, 2020
1 parent 6d2eba1 commit 84fd729
Show file tree
Hide file tree
Showing 19 changed files with 82 additions and 9 deletions.
Binary file added assets/music/blockbreak.wav
Binary file not shown.
Binary file added assets/music/blockhit.wav
Binary file not shown.
Binary file added assets/music/coin.wav
Binary file not shown.
Binary file added assets/music/death.wav
Binary file not shown.
Binary file added assets/music/jump.wav
Binary file not shown.
Binary file added assets/music/mushroomappear.wav
Binary file not shown.
Binary file added assets/music/mushroomeat.wav
Binary file not shown.
Binary file added assets/music/shrink.wav
Binary file not shown.
Binary file added assets/music/stomp.wav
Binary file not shown.
11 changes: 11 additions & 0 deletions include/Components.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "SDL.h"
#include "TextureManager.h"
#include "TileType.h"
#include "SoundManger.h"

struct TransformComponent : public Component {

Expand Down Expand Up @@ -225,6 +226,16 @@ struct BreakableComponent : public Component {
int height[13] = {-2, 1, 3, 2, 1, 1, 1, 0, 0, -1, -1, -4, -1};
};

struct MusicComponent : public Component {
explicit MusicComponent() = default;
};

struct SoundComponent : public Component {
explicit SoundComponent(Sound::Id sound) : sound{sound} {}

Sound::Id sound;
};

struct TileComponent : public Component {
};

Expand Down
17 changes: 15 additions & 2 deletions include/SoundManger.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
#pragma once
#include "SDL_mixer.h"
#include <unordered_map>

namespace Sound {
enum Id {
BACKGROUND_MUSIC,
JUMP,
MUSHROOM
BLOCK_HIT,
BLOCK_BREAK,
STOMP,
MUSHROOM_GROW,
MUSHROOM_EAT,
COIN,
DEATH,
SHRINK,
};
}

Expand All @@ -14,7 +21,13 @@ class SoundManager {
public:
SoundManager();

void playSound(Sound::Id sound);
void playMusic();
void stopMusic();

~SoundManager();

std::unordered_map<Sound::Id, Mix_Chunk*> sounds;
};


2 changes: 1 addition & 1 deletion include/systems/PlayerSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ class PlayerSystem : public System {

private:
void setAnimation(ANIMATION_STATE animationState);
void eatMushroom();
void eatMushroom(World* world);
};
1 change: 1 addition & 0 deletions include/systems/SoundSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "ecs/ecs.h"
#include <SDL_mixer.h>
#include "SoundManger.h"
#include "Components.h"

class SoundSystem : public System {

Expand Down
2 changes: 1 addition & 1 deletion src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ void Game::init(const char* title, int width, int height, bool fullscreen) {
}

isRunning = true;
world.registerSystem(new SoundSystem());
world.registerSystem(new RenderSystem(window, SNES_RESOLUTION_WIDTH, SNES_RESOLUTION_HEIGHT));
world.registerSystem(new PlayerSystem());
world.registerSystem(new MapSystem());
world.registerSystem(new SoundSystem());
world.registerSystem(new EnemySystem());
world.registerSystem(new CallbackSystem());
world.registerSystem(new AnimationSystem());
Expand Down
28 changes: 26 additions & 2 deletions src/SoundManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,34 @@ Mix_Music* music;

SoundManager::SoundManager() {
//Initialize SDL_mixer
Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096);
Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 1024);
music = Mix_LoadMUS("assets/music/overworld.wav");

Mix_PlayMusic(music, -1 );
sounds.insert_or_assign(Sound::Id::JUMP, Mix_LoadWAV("assets/music/jump.wav"));
sounds.insert_or_assign(Sound::Id::BLOCK_HIT, Mix_LoadWAV("assets/music/blockhit.wav"));
sounds.insert_or_assign(Sound::Id::BLOCK_BREAK, Mix_LoadWAV("assets/music/blockbreak.wav"));
sounds.insert_or_assign(Sound::Id::STOMP, Mix_LoadWAV("assets/music/stomp.wav"));
sounds.insert_or_assign(Sound::Id::MUSHROOM_EAT, Mix_LoadWAV("assets/music/mushroomeat.wav"));
sounds.insert_or_assign(Sound::Id::MUSHROOM_GROW, Mix_LoadWAV("assets/music/mushroomappear.wav"));
sounds.insert_or_assign(Sound::Id::COIN, Mix_LoadWAV("assets/music/coin.wav"));
sounds.insert_or_assign(Sound::Id::DEATH, Mix_LoadWAV("assets/music/death.wav"));
sounds.insert_or_assign(Sound::Id::SHRINK, Mix_LoadWAV("assets/music/shrink.wav"));


}

void SoundManager::playSound(Sound::Id sound) {
auto chunk = sounds.at(sound);
Mix_VolumeChunk(chunk, 100);
Mix_PlayChannel(-1, chunk, 0);
}

void SoundManager::playMusic() {
if (!Mix_PlayingMusic()) Mix_PlayMusic(music, -1);
}

void SoundManager::stopMusic() {
Mix_HaltMusic();
}

SoundManager::~SoundManager() {
Expand Down
1 change: 1 addition & 0 deletions src/systems/EnemySystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ void EnemySystem::tick(World* world) {

for (auto enemy : world->find<EnemyComponent, TransformComponent, CrushedComponent>()) {
auto enemyTransform = enemy->get<TransformComponent>();
world->create()->assign<SoundComponent>(Sound::Id::STOMP);
switch (enemy->get<EnemyComponent>()->type) {
case Enemy::Type::GOOMBA: {
enemy->clearComponents();
Expand Down
18 changes: 15 additions & 3 deletions src/systems/PlayerSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ void createDebris(World* world, TransformComponent* brickTransform) {
debris4->assign<TileComponent>();
debris4->assign<TextureComponent>(TextureId::BRICK_DEBRIS_4);
debris4->assign<TransformComponent>(brickTransform->x + 8, brickTransform->y + 8, 8, 8);

world->create()->assign<SoundComponent>(Sound::Id::BLOCK_BREAK);
}

void onGameOver(World* world, Entity* player) {
world->create()->assign<SoundComponent>(Sound::Id::DEATH);
auto music = world->findFirst<MusicComponent>();
if (music) world->destroy(music);

auto kinetic = player->get<KineticComponent>();
player->get<TextureComponent>()->id = TextureId::MARIO_DEAD;
player->assign<DeadComponent>();
Expand Down Expand Up @@ -169,7 +175,11 @@ void PlayerSystem::tick(World* world) {
} else {
if (player->has<BottomCollisionComponent>()) {
kinetic->accX = (float) dirX * MARIO_ACCELERATION_X * 1.7f;
if (jump) player->get<KineticComponent>()->accY = -MARIO_JUMP_ACCELERATION;
if (jump) {
player->get<KineticComponent>()->accY = -MARIO_JUMP_ACCELERATION;
auto jumpsound = world->create();
jumpsound->assign<SoundComponent>(Sound::Id::JUMP);
}
if ((bool) std::abs(kinetic->speedX) || (bool) std::abs(kinetic->accX)) {
if ((kinetic->speedX > 0 && kinetic->accX < 0) ||
(kinetic->speedX < 0 && kinetic->accX > 0)) {
Expand All @@ -196,6 +206,7 @@ void PlayerSystem::tick(World* world) {
kinetic->speedY = -MARIO_BOUNCE;
} else {
if (player->has<SuperMarioComponent>()) {
world->create()->assign<SoundComponent>(Sound::Id::SHRINK);
player->remove<SuperMarioComponent>();
transform->h = SMALL_MARIO_COLLIDER_HEIGHT;
} else {
Expand All @@ -219,7 +230,7 @@ void PlayerSystem::tick(World* world) {
// Eat mushrooms
for (auto collectible : world->find<CollectibleComponent, TransformComponent>()) {
if (AABBCollision(collectible->get<TransformComponent>(), player->get<TransformComponent>())) {
eatMushroom();
eatMushroom(world);
world->destroy(collectible);
}
}
Expand All @@ -233,8 +244,9 @@ void PlayerSystem::tick(World* world) {
player->remove<TopCollisionComponent>();
}

void PlayerSystem::eatMushroom() {
void PlayerSystem::eatMushroom(World* world) {
if (player->has<SuperMarioComponent>()) return;
world->create()->assign<SoundComponent>(Sound::Id::MUSHROOM_EAT);
player->assign<SuperMarioComponent>();
player->assign<AnimationComponent>(
std::vector<TextureId>{
Expand Down
7 changes: 7 additions & 0 deletions src/systems/SoundSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,17 @@
void SoundSystem::onAddedToWorld(World* world) {
System::onAddedToWorld(world);
soundManager = new SoundManager();
world->create()->assign<MusicComponent>();
}

void SoundSystem::tick(World* world) {
auto music = world->findFirst<MusicComponent>();
if (music) soundManager->playMusic(); else soundManager->stopMusic();

for (auto sound : world->find<SoundComponent>()) {
soundManager->playSound(sound->get<SoundComponent>()->sound);
world->destroy(sound);
}
}

void SoundSystem::handleEvent(SDL_Event& event) {
Expand Down
4 changes: 4 additions & 0 deletions src/systems/TileSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ void createMushroom(World* world, Entity* block) {
TILE_SIZE - 8,
TILE_SIZE
);
world->create()->assign<SoundComponent>(Sound::Id::MUSHROOM_GROW);
mushroom->assign<GrowComponent>();
}

Expand All @@ -49,6 +50,8 @@ void createCoin(World* world, Entity* block) {
coin->assign<TileComponent>();
coin->assign<KineticComponent>(0.0f, -20.0f);
coin->assign<CallbackComponent>([=]() { coin->clearComponents(); }, 20);

world->create()->assign<SoundComponent>(Sound::Id::COIN);
}

void TileSystem::tick(World* world) {
Expand Down Expand Up @@ -85,6 +88,7 @@ void TileSystem::tick(World* world) {
}

for (auto entity : world->find<BreakableComponent, BottomCollisionComponent>()) {
world->create()->assign<SoundComponent>(Sound::Id::BLOCK_HIT);
entity->get<BreakableComponent>()->hit = true;
}

Expand Down

0 comments on commit 84fd729

Please sign in to comment.