Moved spawner to OnGameUpdate hook

This commit is contained in:
KiritoDv 2024-12-28 01:31:13 -06:00 committed by Lywx
parent 4dd2eb6eb6
commit 7cfa58117d
5 changed files with 58 additions and 18 deletions

View File

@ -4,6 +4,7 @@
#include "assets/ast_logo.h"
#include "mods.h"
#include "port/interpolation/FrameInterpolation.h"
#include "port/hooks/list/EngineEvent.h"
f32 gNextVsViewScale;
f32 gVsViewScale;
@ -346,6 +347,9 @@ void Game_Update(void) {
u8 partialFill;
u8 soundMode;
// @port: @event: Call GAME_UPDATE_EVENT_PRE
EventSystem_CallEvent(GAME_UPDATE_EVENT_PRE, NULL);
Game_SetGameState();
if (gGameStandby) {
Game_InitStandbyDL(&gUnkDisp1);
@ -600,12 +604,9 @@ void Game_Update(void) {
gFillScreenRed, gFillScreenGreen, gFillScreenBlue, gFillScreenAlpha);
}
Audio_dummy_80016A50();
#if MODS_RAM_MOD == 1
RamMod_Update();
#endif
if(CVarGetInteger("gSpawnerMod", 0) == 1){
Spawner();
}
// @port: @event: Call GAME_UPDATE_EVENT_POST
EventSystem_CallEvent(GAME_UPDATE_EVENT_POST, NULL);
}
}

View File

@ -3,12 +3,25 @@
EventSystem* EventSystem::Instance = new EventSystem();
size_t EventSystem::RegisterListener(EventID id, EventPriority priority, EventCallback callback) {
if (std::find(this->mEventListeners[id].begin(), this->mEventListeners[id].end(), callback) != this->mEventListeners[id].end()) {
ListenerID EventSystem::RegisterListener(EventID id, EventCallback callback, EventPriority priority) {
if(std::find_if(this->mEventListeners[id].begin(), this->mEventListeners[id].end(), [callback](EventListener listener) {
return listener.function == callback;
}) != this->mEventListeners[id].end()) {
throw std::runtime_error("Listener already registered");
}
this->mEventListeners[id].push_back(callback);
this->mEventListeners[id].push_back({ priority, callback });
// Sort by priority
std::sort(this->mEventListeners[id].begin(), this->mEventListeners[id].end(), [](EventListener a, EventListener b) {
return a.priority < b.priority;
});
return this->mEventListeners[id].size() - 1;
}
void EventSystem::UnregisterListener(EventID ev, ListenerID id) {
this->mEventListeners[ev].erase(this->mEventListeners[ev].begin() + id);
}
void EventSystem::CallEvent(EventID id, IEvent* event) {
@ -16,13 +29,17 @@ void EventSystem::CallEvent(EventID id, IEvent* event) {
return;
}
for (auto& callback : this->mEventListeners[id]) {
callback(event);
for (auto& listener : this->mEventListeners[id]) {
listener.function(event);
}
}
extern "C" size_t EventSystem_RegisterListener(EventID id, EventPriority priority, EventCallback callback) {
return EventSystem::Instance->RegisterListener(id, priority, callback);
extern "C" size_t EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority) {
return EventSystem::Instance->RegisterListener(id, callback, priority);
}
extern "C" void EventSystem_UnregisterListener(EventID ev, size_t id) {
EventSystem::Instance->UnregisterListener(ev, id);
}
extern "C" void EventSystem_CallEvent(EventID id, void* event) {

View File

@ -5,6 +5,7 @@
#include <stddef.h>
typedef uint16_t EventID;
typedef uint32_t ListenerID;
typedef enum {
EVENT_TYPE_PRE,
@ -23,6 +24,11 @@ typedef struct {
typedef void (*EventCallback)(IEvent*);
typedef struct {
EventPriority priority;
EventCallback function;
} EventListener;
// ID Type
// 000000000000000 0
#define EVENT_ID(id, type) ((id << 1) | type)
@ -34,12 +40,14 @@ typedef void (*EventCallback)(IEvent*);
class EventSystem {
public:
static EventSystem* Instance;
size_t RegisterListener(EventID id, EventPriority priority, EventCallback callback);
ListenerID RegisterListener(EventID id, EventCallback callback, EventPriority priority = EVENT_PRIORITY_NORMAL);
void UnregisterListener(EventID ev, ListenerID id);
void CallEvent(EventID id, IEvent* event);
private:
std::array<std::vector<EventCallback>, 0xFFFF> mEventListeners;
std::array<std::vector<EventListener>, 0xFFFF> mEventListeners;
};
#else
extern size_t EventSystem_RegisterListener(EventID id, EventPriority priority, EventCallback callback);
extern ListenerID EventSystem_RegisterListener(EventID id, EventCallback callback, EventPriority priority);
extern void EventSystem_UnregisterListener(EventID ev, ListenerID id);
extern void EventSystem_CallEvent(EventID id, void* event);
#endif

View File

@ -4,3 +4,6 @@
#define DISPLAY_UPDATE_EVENT_PRE EVENT_ID(1, EVENT_TYPE_PRE)
#define DISPLAY_UPDATE_EVENT_POST EVENT_ID(1, EVENT_TYPE_POST)
#define GAME_UPDATE_EVENT_PRE EVENT_ID(2, EVENT_TYPE_PRE)
#define GAME_UPDATE_EVENT_POST EVENT_ID(2, EVENT_TYPE_POST)

View File

@ -2,6 +2,7 @@
#include "port/hooks/list/EngineEvent.h"
#include "global.h"
#include "hit64.h"
#include "mods.h"
void OnDisplayUpdatePre(IEvent* event) {
#if DEBUG_BOSS_KILLER == 1
@ -145,9 +146,19 @@ void OnDisplayUpdatePre(IEvent* event) {
#endif
}
void OnGameUpdatePost(IEvent* event) {
#if MODS_RAM_MOD == 1
RamMod_Update();
#endif
if(CVarGetInteger("gSpawnerMod", 0) == 1){
Spawner();
}
}
void PortEnhancements_Init() {
// Register event listeners
EventSystem_RegisterListener(DISPLAY_UPDATE_EVENT_PRE, EVENT_PRIORITY_NORMAL, OnDisplayUpdatePre);
EventSystem_RegisterListener(DISPLAY_UPDATE_EVENT_PRE, OnDisplayUpdatePre, EVENT_PRIORITY_NORMAL);
EventSystem_RegisterListener(GAME_UPDATE_EVENT_POST, OnGameUpdatePost, EVENT_PRIORITY_NORMAL);
}
void PortEnhancements_Exit() {