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