2022-03-11 06:30:01 +00:00
|
|
|
package funkin.modding.events;
|
|
|
|
|
|
|
|
import funkin.modding.IScriptedClass.IPlayStateScriptedClass;
|
2022-04-18 23:36:09 +00:00
|
|
|
import funkin.modding.IScriptedClass;
|
2022-03-11 06:30:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility functions to assist with handling scripted classes.
|
|
|
|
*/
|
|
|
|
class ScriptEventDispatcher
|
|
|
|
{
|
2023-01-23 03:25:45 +00:00
|
|
|
public static function callEvent(target:IScriptedClass, event:ScriptEvent):Void
|
|
|
|
{
|
|
|
|
if (target == null || event == null) return;
|
2022-03-13 18:36:03 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
target.onScriptEvent(event);
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
// If one target says to stop propagation, stop.
|
|
|
|
if (!event.shouldPropagate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
// IScriptedClass
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case ScriptEvent.CREATE:
|
|
|
|
target.onCreate(event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.DESTROY:
|
|
|
|
target.onDestroy(event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.UPDATE:
|
|
|
|
target.onUpdate(cast event);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-05-26 21:10:08 +00:00
|
|
|
if (Std.isOfType(target, IStateStageProp))
|
|
|
|
{
|
|
|
|
var t:IStateStageProp = cast(target, IStateStageProp);
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case ScriptEvent.ADDED:
|
|
|
|
t.onAdd(cast event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
if (Std.isOfType(target, IPlayStateScriptedClass))
|
|
|
|
{
|
|
|
|
var t = cast(target, IPlayStateScriptedClass);
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case ScriptEvent.NOTE_HIT:
|
|
|
|
t.onNoteHit(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.NOTE_MISS:
|
|
|
|
t.onNoteMiss(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.NOTE_GHOST_MISS:
|
|
|
|
t.onNoteGhostMiss(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.SONG_BEAT_HIT:
|
|
|
|
t.onBeatHit(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.SONG_STEP_HIT:
|
|
|
|
t.onStepHit(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.SONG_START:
|
|
|
|
t.onSongStart(event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.SONG_END:
|
|
|
|
t.onSongEnd(event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.SONG_RETRY:
|
|
|
|
t.onSongRetry(event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.GAME_OVER:
|
|
|
|
t.onGameOver(event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.PAUSE:
|
|
|
|
t.onPause(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.RESUME:
|
|
|
|
t.onResume(event);
|
|
|
|
return;
|
2023-04-20 17:48:15 +00:00
|
|
|
case ScriptEvent.SONG_EVENT:
|
|
|
|
t.onSongEvent(cast event);
|
|
|
|
return;
|
2023-01-23 03:25:45 +00:00
|
|
|
case ScriptEvent.COUNTDOWN_START:
|
|
|
|
t.onCountdownStart(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.COUNTDOWN_STEP:
|
|
|
|
t.onCountdownStep(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.COUNTDOWN_END:
|
|
|
|
t.onCountdownEnd(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.SONG_LOADED:
|
|
|
|
t.onSongLoaded(cast event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
if (Std.isOfType(target, IStateChangingScriptedClass))
|
|
|
|
{
|
|
|
|
var t = cast(target, IStateChangingScriptedClass);
|
|
|
|
switch (event.type)
|
|
|
|
{
|
|
|
|
case ScriptEvent.STATE_CHANGE_BEGIN:
|
|
|
|
t.onStateChangeBegin(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.STATE_CHANGE_END:
|
|
|
|
t.onStateChangeEnd(cast event);
|
|
|
|
return;
|
|
|
|
case ScriptEvent.SUBSTATE_OPEN_BEGIN:
|
2023-06-02 18:34:47 +00:00
|
|
|
t.onSubStateOpenBegin(cast event);
|
2023-01-23 03:25:45 +00:00
|
|
|
return;
|
|
|
|
case ScriptEvent.SUBSTATE_OPEN_END:
|
2023-06-02 18:34:47 +00:00
|
|
|
t.onSubStateOpenEnd(cast event);
|
2023-01-23 03:25:45 +00:00
|
|
|
return;
|
|
|
|
case ScriptEvent.SUBSTATE_CLOSE_BEGIN:
|
2023-06-02 18:34:47 +00:00
|
|
|
t.onSubStateCloseBegin(cast event);
|
2023-01-23 03:25:45 +00:00
|
|
|
return;
|
|
|
|
case ScriptEvent.SUBSTATE_CLOSE_END:
|
2023-06-02 18:34:47 +00:00
|
|
|
t.onSubStateCloseEnd(cast event);
|
2023-01-23 03:25:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Prevent "NO HELPER error."
|
|
|
|
return;
|
|
|
|
}
|
2022-04-18 23:36:09 +00:00
|
|
|
|
2023-04-20 17:48:15 +00:00
|
|
|
// If you get a crash on this line, that means ERIC FUCKED UP!
|
|
|
|
throw 'No function called for event type: ${event.type}';
|
2023-01-23 03:25:45 +00:00
|
|
|
}
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
public static function callEventOnAllTargets(targets:Iterator<IScriptedClass>, event:ScriptEvent):Void
|
|
|
|
{
|
|
|
|
if (targets == null || event == null) return;
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
if (Std.isOfType(targets, Array))
|
|
|
|
{
|
|
|
|
var t = cast(targets, Array<Dynamic>);
|
|
|
|
if (t.length == 0) return;
|
|
|
|
}
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
for (target in targets)
|
|
|
|
{
|
|
|
|
var t:IScriptedClass = cast target;
|
|
|
|
if (t == null) continue;
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
callEvent(t, event);
|
2022-03-11 06:30:01 +00:00
|
|
|
|
2023-01-23 03:25:45 +00:00
|
|
|
// If one target says to stop propagation, stop.
|
|
|
|
if (!event.shouldPropagate)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-11 06:30:01 +00:00
|
|
|
}
|