2023-09-08 21:45:47 +00:00
|
|
|
package funkin.data.event;
|
|
|
|
|
|
|
|
import funkin.play.event.SongEvent;
|
2024-01-04 02:10:14 +00:00
|
|
|
import funkin.data.event.SongEventSchema;
|
2023-09-08 21:45:47 +00:00
|
|
|
import funkin.data.song.SongData.SongEventData;
|
|
|
|
import funkin.util.macro.ClassMacro;
|
|
|
|
import funkin.play.event.ScriptedSongEvent;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This class statically handles the parsing of internal and scripted song event handlers.
|
|
|
|
*/
|
2024-01-04 02:10:14 +00:00
|
|
|
class SongEventRegistry
|
2023-09-08 21:45:47 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Every built-in event class must be added to this list.
|
|
|
|
* Thankfully, with the power of `SongEventMacro`, this is done automatically.
|
|
|
|
*/
|
|
|
|
static final BUILTIN_EVENTS:List<Class<SongEvent>> = ClassMacro.listSubclassesOf(SongEvent);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Map of internal handlers for song events.
|
|
|
|
* These may be either `ScriptedSongEvents` or built-in classes extending `SongEvent`.
|
|
|
|
*/
|
|
|
|
static final eventCache:Map<String, SongEvent> = new Map<String, SongEvent>();
|
|
|
|
|
|
|
|
public static function loadEventCache():Void
|
|
|
|
{
|
|
|
|
clearEventCache();
|
|
|
|
|
|
|
|
//
|
|
|
|
// BASE GAME EVENTS
|
|
|
|
//
|
|
|
|
registerBaseEvents();
|
|
|
|
registerScriptedEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
static function registerBaseEvents()
|
|
|
|
{
|
|
|
|
trace('Instantiating ${BUILTIN_EVENTS.length} built-in song events...');
|
|
|
|
for (eventCls in BUILTIN_EVENTS)
|
|
|
|
{
|
|
|
|
var eventClsName:String = Type.getClassName(eventCls);
|
|
|
|
if (eventClsName == 'funkin.play.event.SongEvent' || eventClsName == 'funkin.play.event.ScriptedSongEvent') continue;
|
|
|
|
|
|
|
|
var event:SongEvent = Type.createInstance(eventCls, ["UNKNOWN"]);
|
|
|
|
|
|
|
|
if (event != null)
|
|
|
|
{
|
|
|
|
trace(' Loaded built-in song event: (${event.id})');
|
|
|
|
eventCache.set(event.id, event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trace(' Failed to load built-in song event: ${Type.getClassName(eventCls)}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static function registerScriptedEvents()
|
|
|
|
{
|
|
|
|
var scriptedEventClassNames:Array<String> = ScriptedSongEvent.listScriptClasses();
|
|
|
|
if (scriptedEventClassNames == null || scriptedEventClassNames.length == 0) return;
|
|
|
|
|
|
|
|
trace('Instantiating ${scriptedEventClassNames.length} scripted song events...');
|
|
|
|
for (eventCls in scriptedEventClassNames)
|
|
|
|
{
|
|
|
|
var event:SongEvent = ScriptedSongEvent.init(eventCls, "UKNOWN");
|
|
|
|
|
|
|
|
if (event != null)
|
|
|
|
{
|
|
|
|
trace(' Loaded scripted song event: ${event.id}');
|
|
|
|
eventCache.set(event.id, event);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
trace(' Failed to instantiate scripted song event class: ${eventCls}');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function listEventIds():Array<String>
|
|
|
|
{
|
|
|
|
return eventCache.keys().array();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function listEvents():Array<SongEvent>
|
|
|
|
{
|
|
|
|
return eventCache.values();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getEvent(id:String):SongEvent
|
|
|
|
{
|
|
|
|
return eventCache.get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getEventSchema(id:String):SongEventSchema
|
|
|
|
{
|
|
|
|
var event:SongEvent = getEvent(id);
|
|
|
|
if (event == null) return null;
|
|
|
|
|
|
|
|
return event.getEventSchema();
|
|
|
|
}
|
|
|
|
|
|
|
|
static function clearEventCache()
|
|
|
|
{
|
|
|
|
eventCache.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function handleEvent(data:SongEventData):Void
|
|
|
|
{
|
2024-02-18 08:02:36 +00:00
|
|
|
var eventKind:String = data.eventKind;
|
|
|
|
var eventHandler:SongEvent = eventCache.get(eventKind);
|
2023-09-08 21:45:47 +00:00
|
|
|
|
|
|
|
if (eventHandler != null)
|
|
|
|
{
|
|
|
|
eventHandler.handleEvent(data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-02-18 08:02:36 +00:00
|
|
|
trace('WARNING: No event handler for event with kind: ${eventKind}');
|
2023-09-08 21:45:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data.activated = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static inline function handleEvents(events:Array<SongEventData>):Void
|
|
|
|
{
|
|
|
|
for (event in events)
|
|
|
|
{
|
|
|
|
handleEvent(event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Given a list of song events and the current timestamp,
|
|
|
|
* return a list of events that should be handled.
|
|
|
|
*/
|
|
|
|
public static function queryEvents(events:Array<SongEventData>, currentTime:Float):Array<SongEventData>
|
|
|
|
{
|
|
|
|
return events.filter(function(event:SongEventData):Bool {
|
|
|
|
// If the event is already activated, don't activate it again.
|
|
|
|
if (event.activated) return false;
|
|
|
|
|
|
|
|
// If the event is in the future, don't activate it.
|
|
|
|
if (event.time > currentTime) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-27 00:03:04 +00:00
|
|
|
/**
|
|
|
|
* The currentTime has jumped far ahead or back.
|
|
|
|
* If we moved back in time, we need to reset all the events in that space.
|
|
|
|
* If we moved forward in time, we need to skip all the events in that space.
|
|
|
|
*/
|
|
|
|
public static function handleSkippedEvents(events:Array<SongEventData>, currentTime:Float):Void
|
|
|
|
{
|
|
|
|
for (event in events)
|
|
|
|
{
|
|
|
|
// Deactivate future events.
|
|
|
|
if (event.time > currentTime)
|
|
|
|
{
|
|
|
|
event.activated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip past events.
|
|
|
|
if (event.time < currentTime)
|
|
|
|
{
|
|
|
|
event.activated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-08 21:45:47 +00:00
|
|
|
/**
|
|
|
|
* Reset activation of all the provided events.
|
|
|
|
*/
|
|
|
|
public static function resetEvents(events:Array<SongEventData>):Void
|
|
|
|
{
|
|
|
|
for (event in events)
|
|
|
|
{
|
|
|
|
event.activated = false;
|
|
|
|
// TODO: Add an onReset() method to SongEvent?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|