Removed overuse of polymorphisim
This commit is contained in:
parent
1b6f5cff5b
commit
dccd19ac80
19 changed files with 5766 additions and 154 deletions
BIN
Resources/soundeffects/big_boom.ogg
Normal file
BIN
Resources/soundeffects/big_boom.ogg
Normal file
Binary file not shown.
BIN
Resources/soundeffects/small_pew.ogg
Normal file
BIN
Resources/soundeffects/small_pew.ogg
Normal file
Binary file not shown.
|
|
@ -35,6 +35,7 @@ add_executable (YuppleMayham
|
|||
"src/thirdparty/glad.c"
|
||||
"src/sound/engine.cpp"
|
||||
"src/sound/audiostream.cpp"
|
||||
"src/sound/soundeffect.cpp"
|
||||
"include/thirdparty/stb_vorbis.c"
|
||||
"src/utility/data/font_data.c"
|
||||
"src/utility/ftfont.cpp"
|
||||
|
|
@ -102,6 +103,6 @@ endif()
|
|||
|
||||
target_include_directories(YuppleMayham PRIVATE "${PROJECT_SOURCE_DIR}/YuppleMayham/include" ${LuaJIT_INCLUDE_DIRS} ${FREETYPE_INCLUDE_DIR_ft2build})
|
||||
|
||||
target_link_libraries(YuppleMayham SDL2::SDL2main SDL2::SDL2 SDL2_image::SDL2_image openal glm::glm-header-only sol2 tinyxml2 freetype ${LuaJIT_LINK_LIBRARIES})
|
||||
target_link_libraries(YuppleMayham SDL2::SDL2main SDL2::SDL2 SDL2_image::SDL2_image openal glm::glm-header-only tinyxml2 freetype ${LuaJIT_LINK_LIBRARIES})
|
||||
|
||||
# TODO: Add tests and install targets if needed.
|
||||
|
|
|
|||
|
|
@ -42,6 +42,8 @@ public:
|
|||
void update(double deltaTime);
|
||||
void draw();
|
||||
|
||||
const std::string getType() const { return weaponType; }
|
||||
|
||||
struct BulletData {
|
||||
glm::vec3 origin;
|
||||
glm::vec2 direction;
|
||||
|
|
@ -70,6 +72,8 @@ public:
|
|||
private:
|
||||
void adjustWeapon();
|
||||
|
||||
std::string weaponType;
|
||||
|
||||
glm::vec2 weaponSize;
|
||||
glm::vec2 weaponOffset;
|
||||
// TODO: Add reloading
|
||||
|
|
@ -104,4 +108,4 @@ private:
|
|||
|
||||
};
|
||||
|
||||
#endif // _H_WEAPON_H
|
||||
#endif // _H_WEAPON_H
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@
|
|||
#include "thirdparty/stb_vorbis.c"
|
||||
|
||||
#include "utility/logger.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <AL/al.h>
|
||||
#include <AL/alc.h>
|
||||
|
|
@ -13,11 +14,6 @@
|
|||
#include <mutex>
|
||||
#include <thread>
|
||||
|
||||
namespace AUDIO {
|
||||
constexpr size_t CHUNK_SIZE = 4096;
|
||||
constexpr int SAMPLE_RATE = 44100;
|
||||
}
|
||||
|
||||
class AudioStream
|
||||
{
|
||||
public:
|
||||
|
|
|
|||
38
YuppleMayham/include/sound/soundeffect.h
Normal file
38
YuppleMayham/include/sound/soundeffect.h
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
/* Resource manager is going to hold a list of soundeffects for each item and the state that item is in.
|
||||
* ie. A shotgun is reloading, so the shotgun is the item, and the state (reloading) has a list of different reloading sounds
|
||||
* this is so we don't have annoying repeats.
|
||||
*
|
||||
* The sound engine is going to handle global events that are passed from these objects which will contain their entity ID
|
||||
* and event specific information, such as event location and in special cases the type of item that is triggering the event
|
||||
* such as my shotgun has fired so I will have my audio engine look through a list of shotgun firing sound effects to play,
|
||||
* Once the engine knows what sound to play, it will look for the next available source, load the buffer onto that source and play.
|
||||
* Every frame the engine is responsible for checking every source and pull off buffers that are no longer in use
|
||||
*/
|
||||
|
||||
#ifndef _H_SOUNDEFFECT_H
|
||||
#define _H_SOUNDEFFECT_H
|
||||
|
||||
#define STB_VORBIS_HEADER_ONLY
|
||||
#include "thirdparty/stb_vorbis.c"
|
||||
|
||||
#include "utility/logger.h"
|
||||
#include "util.h"
|
||||
|
||||
#include <AL/al.h>
|
||||
#include <string>
|
||||
|
||||
class SoundEffect
|
||||
{
|
||||
public:
|
||||
SoundEffect(const std::string& filename);
|
||||
|
||||
|
||||
|
||||
~SoundEffect();
|
||||
private:
|
||||
bool loadFile(const std::string &filename);
|
||||
|
||||
ALuint buffer;
|
||||
};
|
||||
|
||||
#endif // _H_SOUNDEFFECT_H
|
||||
5584
YuppleMayham/include/thirdparty/stb_vorbis.c
vendored
Normal file
5584
YuppleMayham/include/thirdparty/stb_vorbis.c
vendored
Normal file
File diff suppressed because it is too large
Load diff
|
|
@ -5,7 +5,12 @@
|
|||
#include <random>
|
||||
|
||||
namespace UTIL
|
||||
{
|
||||
{
|
||||
namespace AUDIO {
|
||||
constexpr size_t CHUNK_SIZE = 4096;
|
||||
constexpr int SAMPLE_RATE = 44100;
|
||||
}
|
||||
|
||||
constexpr float INF_TIME = -99.6875f;
|
||||
|
||||
void flip_surface(SDL_Surface* surface);
|
||||
|
|
@ -25,4 +30,4 @@ namespace UTIL
|
|||
};
|
||||
}
|
||||
|
||||
#endif // _H_UTIL_H
|
||||
#endif // _H_UTIL_H
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#define _H_EVENTS_H
|
||||
|
||||
#include <string>
|
||||
#include <typeindex>
|
||||
#include <memory>
|
||||
#include <functional>
|
||||
#include <unordered_map>
|
||||
|
|
@ -12,124 +13,91 @@
|
|||
class Bullet;
|
||||
struct PhysicsComponent;
|
||||
|
||||
class Event {
|
||||
public:
|
||||
virtual ~Event() { };
|
||||
virtual std::string getType() const = 0;
|
||||
struct BulletFiredEvent {
|
||||
std::weak_ptr<Bullet> bullet;
|
||||
};
|
||||
|
||||
class BulletFiredEvent : public Event {
|
||||
public:
|
||||
BulletFiredEvent(const std::shared_ptr<Bullet>& bullet) : bullet(bullet) {};
|
||||
std::string getType() const override { return "OnBulletFired"; }
|
||||
std::shared_ptr<Bullet> bullet;
|
||||
};
|
||||
|
||||
class BulletDiedEvent : public Event {
|
||||
public:
|
||||
BulletDiedEvent(const std::shared_ptr<PhysicsComponent>& physObj) : physObj(physObj) {};
|
||||
std::string getType() const override { return "OnBulletDied"; }
|
||||
struct BulletDiedEvent {
|
||||
std::shared_ptr<PhysicsComponent> physObj;
|
||||
};
|
||||
|
||||
class BulletCollideEvent : public Event {
|
||||
public:
|
||||
BulletCollideEvent(const unsigned int ownerID, const unsigned int victimID, const std::shared_ptr<PhysicsComponent>& bullet, const glm::vec2& normal)
|
||||
: ownerID(ownerID), victimID(victimID), bullet(bullet), normal(normal){}
|
||||
std::string getType() const override { return "OnBulletCollide"; }
|
||||
struct BulletCollideEvent {
|
||||
unsigned int ownerID;
|
||||
unsigned int victimID;
|
||||
std::shared_ptr<PhysicsComponent> bullet;
|
||||
glm::vec2 normal;
|
||||
};
|
||||
|
||||
class DirectionChangeEvent : public Event {
|
||||
public:
|
||||
DirectionChangeEvent(const int entityid, Direction direction) : direction(direction), entityid(entityid) {}
|
||||
std::string getType() const override { return "OnDirectionChange"; }
|
||||
struct DirectionChangeEvent {
|
||||
int entityid;
|
||||
Direction direction;
|
||||
};
|
||||
|
||||
struct EntityMoveEvent {
|
||||
int entityid;
|
||||
};
|
||||
|
||||
class EntityMoveEvent : public Event {
|
||||
public:
|
||||
EntityMoveEvent(const int entityid) : entityid(entityid) {}
|
||||
std::string getType() const override { return "OnEntityMove"; }
|
||||
struct EntityStopEvent {
|
||||
int entityid;
|
||||
};
|
||||
|
||||
class EntityStopEvent : public Event {
|
||||
public:
|
||||
EntityStopEvent(const int entityid) : entityid(entityid) {}
|
||||
std::string getType() const override { return "OnEntityStop"; }
|
||||
struct EntityReloadEvent {
|
||||
int entityid;
|
||||
glm::vec3 position;
|
||||
std::string weaponType;
|
||||
};
|
||||
|
||||
struct EntityFinishReloadEvent {
|
||||
int entityid;
|
||||
};
|
||||
|
||||
class EntityReloadEvent : public Event {
|
||||
public:
|
||||
EntityReloadEvent(const int entityid) : entityid(entityid) {}
|
||||
std::string getType() const override { return "OnEntityReload"; }
|
||||
int entityid;
|
||||
};
|
||||
|
||||
class EntityFinishReloadEvent : public Event {
|
||||
public:
|
||||
EntityFinishReloadEvent(const int entityid) : entityid(entityid) {}
|
||||
std::string getType() const override { return "OnEntityFinishReload"; }
|
||||
int entityid;
|
||||
};
|
||||
|
||||
class EntityFireEvent : public Event {
|
||||
public:
|
||||
EntityFireEvent(const int entityid, const float fireDelay) : entityid(entityid), fireDelay(fireDelay) {}
|
||||
std::string getType() const override { return "OnEntityFire"; }
|
||||
struct EntityFireEvent {
|
||||
int entityid;
|
||||
float fireDelay;
|
||||
glm::vec3 firePosition;
|
||||
std::string weaponType;
|
||||
};
|
||||
|
||||
class AnimationFinishedEvent : public Event {
|
||||
public:
|
||||
AnimationFinishedEvent(const int entityid, const std::string animType) : animType(animType), entityid(entityid) {}
|
||||
std::string getType() const override { return "OnAnimationFinished"; }
|
||||
std::string animType;
|
||||
struct AnimationFinishedEvent {
|
||||
int entityid;
|
||||
std::string animType;
|
||||
};
|
||||
|
||||
class ScreenShakeEvent : public Event {
|
||||
public:
|
||||
ScreenShakeEvent(float intensity, float duration) : intensity(intensity), duration(duration) {}
|
||||
std::string getType() const override { return "OnScreenShake"; }
|
||||
struct ScreenShakeEvent {
|
||||
float intensity;
|
||||
float duration;
|
||||
};
|
||||
|
||||
class ScreenBlurEvent : public Event {
|
||||
public:
|
||||
ScreenBlurEvent(float intensity, float duration) : intensity(intensity), duration(duration) {}
|
||||
std::string getType() const override { return "OnScreenBlur"; }
|
||||
struct ScreenBlurEvent {
|
||||
float intensity;
|
||||
float duration;
|
||||
};
|
||||
|
||||
class EventManager {
|
||||
public:
|
||||
using EventCallback = std::function<void(std::shared_ptr<Event>)>;
|
||||
template <typename T>
|
||||
using EventCallback = std::function<void(const T&)>;
|
||||
|
||||
void subscribe(std::string eventType, EventCallback callback) {
|
||||
listeners[eventType].push_back(callback);
|
||||
template <typename T>
|
||||
void subscribe(EventCallback<T> cb) {
|
||||
auto wrapper = [cb](void *ev) {
|
||||
cb(*static_cast<T*>(ev));
|
||||
};
|
||||
callbacks[typeid(T)].push_back(wrapper);
|
||||
}
|
||||
void notify(const std::shared_ptr<Event>& event) {
|
||||
auto iterator = listeners.find(event->getType());
|
||||
if (iterator != listeners.end())
|
||||
template <typename T>
|
||||
void notify(const T& event) {
|
||||
auto iterator = callbacks.find(typeid(T));
|
||||
if (iterator != callbacks.end())
|
||||
{
|
||||
for (auto& callback : iterator->second)
|
||||
for (auto& cb : iterator->second)
|
||||
{
|
||||
callback(event);
|
||||
cb((void*)&event);
|
||||
}
|
||||
}
|
||||
}
|
||||
private:
|
||||
std::unordered_map <std::string, std::vector <EventCallback>> listeners;
|
||||
std::unordered_map <std::type_index, std::vector <std::function<void(void*)>>> callbacks;
|
||||
};
|
||||
|
||||
#endif //_H_EVENTS_H
|
||||
#endif //_H_EVENTS_H
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ void GameActor::setRotation(const float& rotation)
|
|||
if (auto eventManager = sceneContext->getEventManager().lock()) {
|
||||
Direction newDir = getDirectionFromRotation(rotation);
|
||||
if (getDirectionFromRotation(this->rotation) != newDir)
|
||||
eventManager->notify(std::make_shared<DirectionChangeEvent>(entityid, newDir));
|
||||
eventManager->notify<DirectionChangeEvent>((DirectionChangeEvent){ entityid, newDir });
|
||||
}
|
||||
}
|
||||
this->rotation = rotation;
|
||||
|
|
@ -59,14 +59,14 @@ void GameActor::update(double deltaTime)
|
|||
if (isMoving && !wasMoving)
|
||||
{
|
||||
if (auto event = sceneContext->getEventManager().lock()) {
|
||||
event->notify(std::make_shared<EntityMoveEvent>(entityid));
|
||||
event->notify<EntityMoveEvent>((EntityMoveEvent){ entityid });
|
||||
}
|
||||
wasMoving = true;
|
||||
}
|
||||
else if (!isMoving && wasMoving)
|
||||
{
|
||||
if (auto event = sceneContext->getEventManager().lock()) {
|
||||
event->notify(std::make_shared<EntityStopEvent>(entityid));
|
||||
event->notify<EntityStopEvent>((EntityStopEvent){ entityid });
|
||||
}
|
||||
wasMoving = false;
|
||||
}
|
||||
|
|
@ -99,8 +99,8 @@ void GameActor::fireWeapon()const {
|
|||
if (weapon->shoot()) {
|
||||
if (sceneContext->getPlayerID() == entityid) {
|
||||
if (auto gEvent = sceneContext->getGlobalEventManager().lock()) {
|
||||
gEvent->notify(std::make_shared<ScreenShakeEvent>(0.01f, 0.8f));
|
||||
gEvent->notify(std::make_shared<ScreenBlurEvent>(1.f, 0.8f));
|
||||
gEvent->notify<ScreenShakeEvent>((ScreenShakeEvent){0.01f, 0.8f});
|
||||
gEvent->notify<ScreenBlurEvent>((ScreenBlurEvent){1.f, 0.8f});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -134,7 +134,7 @@ void GameActor::followMouse(const MouseState& mouse_state)
|
|||
float newRotation = glm::degrees(glm::atan(direction.y, direction.x));
|
||||
if (getDirectionFromRotation(rotation) != getDirectionFromRotation(newRotation)) {
|
||||
if (auto event = sceneContext->getEventManager().lock()) {
|
||||
event->notify(std::make_shared<DirectionChangeEvent>(entityid, getDirectionFromRotation(newRotation)));
|
||||
event->notify<DirectionChangeEvent>((DirectionChangeEvent){ entityid, getDirectionFromRotation(newRotation) });
|
||||
}
|
||||
}
|
||||
//setRotation(glm::degrees(glm::atan(direction.y, direction.x)));
|
||||
|
|
|
|||
|
|
@ -4,18 +4,16 @@
|
|||
|
||||
#include "utility/logger.h"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
void PhysicsEngine::hookEventManager(const std::shared_ptr<EventManager>& eventManager)
|
||||
{
|
||||
this->eventManager = eventManager;
|
||||
this->eventManager->subscribe("OnBulletFired", [this](std::shared_ptr<Event> e) {
|
||||
auto bulletEvent = std::static_pointer_cast<BulletFiredEvent>(e);
|
||||
this->addObject(bulletEvent->bullet->getPhysicsComponent());
|
||||
this->eventManager->subscribe<BulletFiredEvent>([this](const BulletFiredEvent& event) {
|
||||
if (auto bullet = event.bullet.lock()) {
|
||||
this->addObject(bullet->getPhysicsComponent());
|
||||
}
|
||||
});
|
||||
this->eventManager->subscribe("OnBulletDied", [this](std::shared_ptr<Event> e) {
|
||||
auto bulletEvent = std::static_pointer_cast<BulletDiedEvent>(e);
|
||||
this->removeObject(bulletEvent->physObj);
|
||||
this->eventManager->subscribe<BulletDiedEvent>([this](const BulletDiedEvent& event) {
|
||||
this->removeObject(event.physObj);
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -214,14 +214,13 @@ void Scene::unloadScene()
|
|||
void Scene::hookSceneEvents()
|
||||
{
|
||||
std::weak_ptr<Scene> weakSelf = shared_from_this();
|
||||
eventManager->subscribe("OnBulletCollide", [weakSelf](std::shared_ptr<Event> e) {
|
||||
eventManager->subscribe<BulletCollideEvent>([weakSelf](const BulletCollideEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto collideEvent = std::static_pointer_cast<BulletCollideEvent>(e);
|
||||
GameActor* shooter = self->getGameActorByID(collideEvent->ownerID);
|
||||
GameActor* target = self->getGameActorByID(collideEvent->victimID);
|
||||
GameActor* shooter = self->getGameActorByID(e.ownerID);
|
||||
GameActor* target = self->getGameActorByID(e.victimID);
|
||||
if (shooter && target)
|
||||
if (auto& weapon = shooter->getHeldWeapon())
|
||||
weapon->onHitCallback(target, collideEvent->bullet.get(), collideEvent->normal);
|
||||
weapon->onHitCallback(target, e.bullet.get(), e.normal);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,11 +14,10 @@ void BulletManager::hookEventManager(std::weak_ptr<EventManager> eventManager)
|
|||
this->eventManager = eventManager;
|
||||
if (auto event = this->eventManager.lock()) {
|
||||
std::weak_ptr<BulletManager> weakSelf = shared_from_this();
|
||||
event->subscribe("OnBulletDied", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<BulletDiedEvent>([weakSelf](const BulletDiedEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto bulletEvent = std::static_pointer_cast<BulletDiedEvent>(e);
|
||||
auto iterator = std::find_if(self->bullets.begin(), self->bullets.end(), [&](std::shared_ptr<Bullet> bullet) {
|
||||
return bullet->getPhysicsComponent() == bulletEvent->physObj;
|
||||
return bullet->getPhysicsComponent() == e.physObj;
|
||||
});
|
||||
if (iterator != self->bullets.end())
|
||||
self->bullets.erase(iterator);
|
||||
|
|
@ -57,7 +56,7 @@ void BulletManager::update(double deltaTime)
|
|||
if (distance > bullet->getBulletDrop() || glm::length(bullet->getPhysicsComponent()->rigidBody.velocity) < 100.0f)
|
||||
{
|
||||
if (auto event = eventManager.lock())
|
||||
event->notify(std::make_shared<BulletDiedEvent>(bullet->getPhysicsComponent()));
|
||||
event->notify<BulletDiedEvent>((BulletDiedEvent){ bullet->getPhysicsComponent() });
|
||||
//bullets.erase(std::remove(bullets.begin(), bullets.end(), bullet));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@
|
|||
Weapon::Weapon(std::shared_ptr<WeaponData> data, const unsigned weaponShaderID, const unsigned bulletShaderID, ResourceManager* resourceManager)
|
||||
:
|
||||
Entity (weaponShaderID),
|
||||
weaponType (data->name),
|
||||
weaponSize (glm::vec2(data->sizeX, data->sizeY)),
|
||||
weaponOffset (glm::vec2(data->offsetX, data->offsetY)),
|
||||
weaponMag (data->clipSize),
|
||||
|
|
@ -51,10 +52,9 @@ void Weapon::addComponent(std::unique_ptr<Component> comp) {
|
|||
|
||||
void Weapon::reload()
|
||||
{
|
||||
// TODO: Create reload event that will be captured by the gun animation set, to start the reloading animation
|
||||
if (auto event = eventManager.lock())
|
||||
{
|
||||
event->notify(std::make_shared<EntityReloadEvent>(entityid));
|
||||
event->notify<EntityReloadEvent>((EntityReloadEvent){ entityid, wielder->getPosition(), weaponType });
|
||||
reloading = true;
|
||||
if (weaponAmmo < weaponMagSize) {
|
||||
weaponMag = weaponAmmo;
|
||||
|
|
@ -79,7 +79,7 @@ bool Weapon::shoot()
|
|||
{
|
||||
shotsFired = true;
|
||||
if (auto event = eventManager.lock())
|
||||
event->notify(std::make_shared<EntityFireEvent>(entityid, fireSpeed));
|
||||
event->notify<EntityFireEvent>((EntityFireEvent){entityid, fireSpeed, wielder->getPosition(), weaponType});
|
||||
if (!weaponScript || !weaponScript->lua["onShoot"].valid())
|
||||
{
|
||||
// create bullet using this generated data
|
||||
|
|
@ -120,10 +120,9 @@ void Weapon::hookEventManager(std::weak_ptr<EventManager> eventManager)
|
|||
|
||||
if (auto event = this->eventManager.lock()) {
|
||||
std::weak_ptr<Weapon> selfWeak = shared_from_this();
|
||||
event->subscribe("OnAnimationFinished", [selfWeak](std::shared_ptr<Event> e) {
|
||||
event->subscribe<AnimationFinishedEvent>([selfWeak](const AnimationFinishedEvent& e) {
|
||||
if (auto self = selfWeak.lock()) {
|
||||
auto animFinished = std::static_pointer_cast<AnimationFinishedEvent>(e);
|
||||
if (animFinished->entityid == self->entityid && animFinished->animType == "reload")
|
||||
if (e.entityid == self->entityid && e.animType == "reload")
|
||||
{
|
||||
if (self->reloading)
|
||||
{
|
||||
|
|
@ -173,7 +172,7 @@ void Weapon::update(double deltaTime)
|
|||
{
|
||||
wasReloading = false;
|
||||
if (auto event = eventManager.lock()) {
|
||||
event->notify(std::make_shared<EntityFinishReloadEvent>(entityid));
|
||||
event->notify<EntityFinishReloadEvent>((EntityFinishReloadEvent){entityid});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -259,7 +258,7 @@ void Weapon::createBullet(const Weapon::BulletData& data)
|
|||
bullet->getPhysicsComponent()->rigidBody.velocity += bulletSpeed * data.direction / data.mass;
|
||||
|
||||
if (auto event = eventManager.lock())
|
||||
event->notify(std::make_shared<BulletFiredEvent>(bullet));
|
||||
event->notify<BulletFiredEvent>((BulletFiredEvent){bullet});
|
||||
bulletManager->addBullet(bullet);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -109,18 +109,18 @@ void AnimationSet::attachEventManager(std::weak_ptr<EventManager> e)
|
|||
if (auto event = eventManager.lock()) {
|
||||
std::weak_ptr<AnimationSet> weakSelf = shared_from_this();
|
||||
|
||||
event->subscribe("OnDirectionChange", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<DirectionChangeEvent>([weakSelf](const DirectionChangeEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto directionEvent = std::static_pointer_cast<DirectionChangeEvent>(e);
|
||||
if (directionEvent->entityid == self->entityid)
|
||||
self->setFacingDir(directionEvent->direction);
|
||||
if (e.entityid == self->entityid) {
|
||||
Direction d = e.direction;
|
||||
self->setFacingDir(d);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
event->subscribe("OnEntityMove", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<EntityMoveEvent>([weakSelf](const EntityMoveEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto moveEvent = std::static_pointer_cast<EntityMoveEvent>(e);
|
||||
if (moveEvent->entityid == self->entityid)
|
||||
if (e.entityid == self->entityid)
|
||||
{
|
||||
if (self->isDirectional)
|
||||
{
|
||||
|
|
@ -133,10 +133,9 @@ void AnimationSet::attachEventManager(std::weak_ptr<EventManager> e)
|
|||
}
|
||||
});
|
||||
|
||||
event->subscribe("OnEntityStop", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<EntityStopEvent>([weakSelf](const EntityStopEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto stopEvent = std::static_pointer_cast<EntityStopEvent>(e);
|
||||
if (stopEvent->entityid == self->entityid)
|
||||
if (e.entityid == self->entityid)
|
||||
{
|
||||
if (self->isDirectional)
|
||||
{
|
||||
|
|
@ -149,24 +148,22 @@ void AnimationSet::attachEventManager(std::weak_ptr<EventManager> e)
|
|||
}
|
||||
});
|
||||
|
||||
event->subscribe("OnEntityReload", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<EntityReloadEvent>([weakSelf](const EntityReloadEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto reloadEvent = std::static_pointer_cast<EntityReloadEvent>(e);
|
||||
if (reloadEvent->entityid == self->entityid)
|
||||
if (e.entityid == self->entityid)
|
||||
{
|
||||
if (self->anims["reload"] != NULL)
|
||||
{
|
||||
self->curAnim = self->anims["reload"].get();
|
||||
self->curAnim->reset();
|
||||
self->curAnim = self->anims["reload"].get();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
event->subscribe("OnEntityFinishReload", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<EntityFinishReloadEvent>([weakSelf](const EntityFinishReloadEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto reloadEvent = std::static_pointer_cast<EntityFinishReloadEvent>(e);
|
||||
if (reloadEvent->entityid == self->entityid)
|
||||
if (e.entityid == self->entityid)
|
||||
{
|
||||
if (self->anims["idle"] != NULL)
|
||||
self->curAnim = self->anims["idle"].get();
|
||||
|
|
@ -174,26 +171,24 @@ void AnimationSet::attachEventManager(std::weak_ptr<EventManager> e)
|
|||
}
|
||||
});
|
||||
|
||||
event->subscribe("OnEntityFire", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<EntityFireEvent>([weakSelf](const EntityFireEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto fireEvent = std::static_pointer_cast<EntityFireEvent>(e);
|
||||
if (fireEvent->entityid == self->entityid)
|
||||
if (e.entityid == self->entityid)
|
||||
{
|
||||
if (self->anims["fire"] != NULL)
|
||||
{
|
||||
self->curAnim = self->anims["fire"].get();
|
||||
self->curAnim->reset();
|
||||
float newFPS = (1000.f / fireEvent->fireDelay) * 15.f;
|
||||
float newFPS = (1000.f / e.fireDelay) * 15.f;
|
||||
self->curAnim->setFPS(newFPS);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
event->subscribe("OnAnimationFinished", [weakSelf](std::shared_ptr<Event> e) {
|
||||
event->subscribe<AnimationFinishedEvent>([weakSelf](const AnimationFinishedEvent& e) {
|
||||
if (auto self = weakSelf.lock()) {
|
||||
auto animEvent = std::static_pointer_cast<AnimationFinishedEvent>(e);
|
||||
if (animEvent->entityid == self->entityid && animEvent->animType == "fire")
|
||||
if (e.entityid == self->entityid && e.animType == "fire")
|
||||
{
|
||||
if (self->anims["idle"] != NULL)
|
||||
self->curAnim = self->anims["idle"].get();
|
||||
|
|
@ -220,7 +215,7 @@ void AnimationSet::draw()
|
|||
// If the animation has cycled, we send this event after every cycle
|
||||
if ((curAnim->getCycles() - lastCycle) > 0) {
|
||||
if (auto event = eventManager.lock()) {
|
||||
event->notify(std::make_shared<AnimationFinishedEvent>(entityid, curAnim->getType()));
|
||||
event->notify((AnimationFinishedEvent){entityid, curAnim->getType()});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,18 +21,16 @@ Renderer::Renderer(const std::shared_ptr<ResourceManager>& r)
|
|||
void Renderer::hookEventManager(std::weak_ptr<EventManager> eventManager)
|
||||
{
|
||||
if (auto e = eventManager.lock()) {
|
||||
e->subscribe("OnScreenShake", [this](std::shared_ptr<Event> event) {
|
||||
auto shakeEvent = std::dynamic_pointer_cast<ScreenShakeEvent>(event);
|
||||
e->subscribe<ScreenShakeEvent>([this](const ScreenShakeEvent& event) {
|
||||
postProcessor->ApplyEffect(Postprocessor::SHAKE,
|
||||
shakeEvent->intensity,
|
||||
shakeEvent->duration
|
||||
event.intensity,
|
||||
event.duration
|
||||
);
|
||||
});
|
||||
e->subscribe("OnScreenBlur", [this](std::shared_ptr<Event> event) {
|
||||
auto blurEvent = std::dynamic_pointer_cast<ScreenBlurEvent>(event);
|
||||
e->subscribe<ScreenBlurEvent>([this](const ScreenBlurEvent& event) {
|
||||
postProcessor->ApplyEffect(Postprocessor::BLUR,
|
||||
blurEvent->intensity,
|
||||
blurEvent->duration
|
||||
event.intensity,
|
||||
event.duration
|
||||
);
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
#include "graphics/sprite.h"
|
||||
#include "graphics/texture.h"
|
||||
#include "graphics/quad.h"
|
||||
#include "util.h"
|
||||
#include "utility/events.h"
|
||||
|
||||
bool Sprite::loaded() const { return (texture != nullptr); }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
#include "sound/audiostream.h"
|
||||
#include <AL/al.h>
|
||||
#include <thread>
|
||||
|
||||
AudioStream::AudioStream()
|
||||
{
|
||||
alGenBuffers(4, buffers);
|
||||
alGenSources(1, &source);
|
||||
data = (short *)std::malloc(AUDIO::CHUNK_SIZE * sizeof(short) * 2); // Make space for stereo even if we are playing mono
|
||||
data = (short *)std::malloc(UTIL::AUDIO::CHUNK_SIZE * sizeof(short) * 2); // Make space for stereo even if we are playing mono
|
||||
stopThread = false;
|
||||
eof = true;
|
||||
paused = true;
|
||||
|
|
@ -46,14 +44,14 @@ bool AudioStream::openFile(std::string fileName)
|
|||
return false;
|
||||
}
|
||||
info = stb_vorbis_get_info(file);
|
||||
if (info.sample_rate != AUDIO::SAMPLE_RATE) {
|
||||
if (info.sample_rate != UTIL::AUDIO::SAMPLE_RATE) {
|
||||
LOG(ERROR, "Failed to open file: '{}', make sure to convert sample rate to 44100!", CurStreamName());
|
||||
stb_vorbis_close(file);
|
||||
return false;
|
||||
}
|
||||
stb_vorbis_seek_start(file);
|
||||
std::free(data);
|
||||
data = (short *)std::malloc(AUDIO::CHUNK_SIZE * sizeof(short) * info.channels);
|
||||
data = (short *)std::malloc(UTIL::AUDIO::CHUNK_SIZE * sizeof(short) * info.channels);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
@ -66,7 +64,7 @@ int AudioStream::loadChunk(ALuint buffer)
|
|||
if (info.channels == 0) {
|
||||
return sample;
|
||||
}
|
||||
sample = stb_vorbis_get_samples_short_interleaved(file, info.channels, data, AUDIO::CHUNK_SIZE);
|
||||
sample = stb_vorbis_get_samples_short_interleaved(file, info.channels, data, UTIL::AUDIO::CHUNK_SIZE);
|
||||
if (sample == 0)
|
||||
return sample;
|
||||
format = info.channels == 1 ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
|
||||
|
|
|
|||
32
YuppleMayham/src/sound/soundeffect.cpp
Normal file
32
YuppleMayham/src/sound/soundeffect.cpp
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
#include "sound/soundeffect.h"
|
||||
#include "util.h"
|
||||
#include <AL/al.h>
|
||||
|
||||
SoundEffect::SoundEffect(const std::string& filename)
|
||||
{
|
||||
if (!loadFile(filename)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// load the whole file into a buffer, this should only be used for small sound effects. Never with music or sounds exceeding 10MB!
|
||||
bool SoundEffect::loadFile(const std::string& filename)
|
||||
{
|
||||
short *data = (short *)std::malloc(UTIL::AUDIO::CHUNK_SIZE * sizeof(short) * 2);
|
||||
int channels, sample_rate, samples;
|
||||
|
||||
samples = stb_vorbis_decode_filename(filename.c_str(), &channels, &sample_rate, &data);
|
||||
if (samples == 0) {
|
||||
LOG(ERROR, "Failed to load sound effect '{}'", filename);
|
||||
return false;
|
||||
}
|
||||
alGenBuffers(1, &buffer);
|
||||
alBufferData(buffer, channels == 2 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, data, samples * channels * sizeof(short), sample_rate);
|
||||
std::free(data);
|
||||
return true;
|
||||
}
|
||||
|
||||
SoundEffect::~SoundEffect()
|
||||
{
|
||||
alDeleteBuffers(1, &buffer);
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue