Added sound files. Listens to collision events -> play sound.

This commit is contained in:
Stiffly
2015-09-16 16:54:02 +02:00
parent 450a73f498
commit 13de4bd1cb
34 changed files with 71 additions and 50 deletions
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,23 +1,23 @@
/*
This file is part of Daydream Engine.
Copyright 2014 Adam Byléhn, Tobias Dahl, Simon Holmberg, Viktor Ljung
Daydream Engine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Daydream Engine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef DAYDREAM_EPLAYSOUND_H
#define DAYDREAM_EPLAYSOUND_H
#ifndef Events_PlaySFX_h__
#define Events_PlaySFX_h__
#include "Core/EventBroker.h"
@@ -27,14 +27,13 @@ namespace dd
namespace Events
{
struct PlaySound : Event
struct PlaySFX : Event
{
float volume;
std::string path;
std::string path;
};
}
}
#endif //DAYDREAM_EPLAYSOUND_H
#endif // Events_PlaySFX_h__
-27
View File
@@ -1,27 +0,0 @@
//
// Created by Adam on 2015-09-09.
//
#ifndef DAYDREAM_EPLAYSOUND_H
#define DAYDREAM_EPLAYSOUND_H
#include <string>
#include "EventBroker.h"
namespace dd
{
namespace Events
{
struct PlaySound
{
std::string path;
};
}
}
#endif //DAYDREAM_EPLAYSOUND_H
+7 -4
View File
@@ -25,7 +25,8 @@
#include "Core/System.h"
#include "Core/World.h"
#include "Core/EKeyDown.h"
#include "Core/EPlaySound.h"
#include "Core/EPlaySFX.h"
#include "Physics/EContact.h"
#include "Core/EventBroker.h"
namespace dd
@@ -53,10 +54,12 @@ public:
private:
//Events
dd::EventRelay<Sound, dd::Events::KeyDown> m_EKeyDown;
bool OnKeyDown(const dd::Events::KeyDown &event);
dd::EventRelay<Sound, dd::Events::PlaySFX> m_EPlaySFX;
dd::EventRelay<Sound, dd::Events::Contact> m_EContact;
//dd::EventRelay<Sound, dd::Events::PlaySound> m_EPlaySound;
//bool PlayASound(const dd::Events::PlaySound &event);
bool OnKeyDown(const dd::Events::KeyDown &event);
bool OnPlaySFX(const dd::Events::PlaySFX &event);
bool OnContact(const dd::Events::Contact &event);
ALuint LoadFile(std::string fileName);
ALuint CreateSource();
+56 -10
View File
@@ -16,6 +16,9 @@
along with Daydream Engine. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Game/CBall.h>
#include <Game/CBrick.h>
#include <Game/CPad.h>
#include "PrecompiledHeader.h"
#include "Core/Sound.h"
@@ -42,8 +45,9 @@ void dd::Systems::Sound::Initialize()
alDistanceModel(AL_INVERSE_DISTANCE_CLAMPED);
//Subscribe to events
EVENT_SUBSCRIBE_MEMBER(m_EContact, &Sound::OnContact);
EVENT_SUBSCRIBE_MEMBER(m_EKeyDown, &Sound::OnKeyDown);
//EVENT_SUBSCRIBE_MEMBER(m_EPlaySound, &Sound::PlayASound);
EVENT_SUBSCRIBE_MEMBER(m_EPlaySFX, &Sound::OnPlaySFX);
}
void dd::Systems::Sound::Update(double dt)
@@ -54,25 +58,67 @@ void dd::Systems::Sound::Update(double dt)
alSourcefv(m_Source, AL_POSITION, pos);
}
/*bool dd::Systems::Sound::PlayASound(const dd::Events::PlaySound &event)
bool dd::Systems::Sound::OnPlaySFX(const dd::Events::PlaySFX &event)
{
m_Source = CreateSource();
ALuint buffer = LoadFile(event.path);
}*/
alSourcei(m_Source, AL_BUFFER, buffer);
alSourcePlay(m_Source);
}
bool dd::Systems::Sound::OnKeyDown(const dd::Events::KeyDown &event)
{
// #define GLFW_KEY_S 83
/*if (event.KeyCode == 83) {
Events::PlaySound e;
e.path = "Sounds/screwed.wav";
EventBroker->Publish(e);
}*/
if (event.KeyCode == 83) {
m_Source = CreateSource();
ALuint buffer = LoadFile("Sounds/screwed.wav");
alSourcei(m_Source, AL_BUFFER, buffer);
alSourcePlay(m_Source);
}
}
bool dd::Systems::Sound::OnContact(const dd::Events::Contact &event)
{
int localBallEntID = 0;
//Make sure a ball is colliding
auto ball = m_World->GetComponent<Components::Ball>(event.Entity1);
if (ball == NULL) {
ball = m_World->GetComponent<Components::Ball>(event.Entity2);
if (ball == NULL) {
return false;
}
else {
localBallEntID = 2;
}
}
else {
localBallEntID = 1;
}
//Determine which event.Entity is not t
EntityID collObject;
if (localBallEntID == 1) {
collObject = event.Entity2;
}
else if (localBallEntID == 2) {
collObject = event.Entity1;
}
/*auto collisionBrick = m_World->GetComponent<Components::Brick>(collObject);
if (collisionBrick != NULL) {
dd::Events::PlaySFX e;
e.path = "Sounds/Brick/brickBreak.wav";
EventBroker->Publish(e);
return true;
}*/
auto collisionPad = m_World->GetComponent<Components::Pad>(collObject);
if (collisionPad != NULL) {
dd::Events::PlaySFX e;
e.path = "Sounds/Metal/impact.wav";
EventBroker->Publish(e);
return true;
}
}