Funkin/source/funkin/modding/events/ScriptEvent.hx

667 lines
18 KiB
Haxe
Raw Normal View History

package funkin.modding.events;
2023-06-22 01:41:01 -04:00
import funkin.play.song.SongData.SongNoteData;
import flixel.FlxState;
import flixel.FlxSubState;
2023-06-22 01:41:01 -04:00
import funkin.play.notes.NoteSprite;
2023-06-16 17:37:56 -04:00
import funkin.play.cutscene.dialogue.Conversation;
import funkin.play.Countdown.CountdownStep;
2023-06-22 01:41:01 -04:00
import funkin.play.notes.NoteDirection;
import openfl.events.EventType;
import openfl.events.KeyboardEvent;
typedef ScriptEventType = EventType<ScriptEvent>;
/**
* This is a base class for all events that are issued to scripted classes.
* It can be used to identify the type of event called, store data, and cancel event propagation.
*/
class ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* Called when the relevant object is created.
* Keep in mind that the constructor may be called before the object is needed,
* for the purposes of caching data or otherwise.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final CREATE:ScriptEventType = 'CREATE';
2023-01-22 19:55:30 -05:00
/**
* Called when the relevant object is destroyed.
* This should perform relevant cleanup to ensure good performance.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final DESTROY:ScriptEventType = 'DESTROY';
2023-01-22 19:55:30 -05:00
/**
* Called when the relevent object is added to the game state.
* This assumes all data is loaded and ready to go.
2023-06-08 16:30:45 -04:00
*
* This event is not cancelable.
*/
public static inline final ADDED:ScriptEventType = 'ADDED';
2023-01-22 19:55:30 -05:00
/**
* Called during the update function.
* This is called every frame, so be careful!
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final UPDATE:ScriptEventType = 'UPDATE';
2023-01-22 19:55:30 -05:00
/**
* Called when the player moves to pause the game.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event IS cancelable! Canceling the event will prevent the game from pausing.
*/
2023-06-02 14:35:28 -04:00
public static inline final PAUSE:ScriptEventType = 'PAUSE';
2023-01-22 19:55:30 -05:00
/**
* Called when the player moves to unpause the game while paused.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event IS cancelable! Canceling the event will prevent the game from resuming.
*/
2023-06-02 14:35:28 -04:00
public static inline final RESUME:ScriptEventType = 'RESUME';
2023-01-22 19:55:30 -05:00
/**
* Called once per step in the song. This happens 4 times per measure.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SONG_BEAT_HIT:ScriptEventType = 'BEAT_HIT';
2023-01-22 19:55:30 -05:00
/**
* Called once per step in the song. This happens 16 times per measure.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SONG_STEP_HIT:ScriptEventType = 'STEP_HIT';
2023-01-22 19:55:30 -05:00
/**
* Called when a character hits a note.
* Important information such as judgement/timing, note data, player/opponent, etc. are all provided.
*
* This event IS cancelable! Canceling this event prevents the note from being hit,
* and will likely result in a miss later.
*/
2023-06-02 14:35:28 -04:00
public static inline final NOTE_HIT:ScriptEventType = 'NOTE_HIT';
2023-01-22 19:55:30 -05:00
/**
* Called when a character misses a note.
* Important information such as note data, player/opponent, etc. are all provided.
*
* This event IS cancelable! Canceling this event prevents the note from being considered missed,
* avoiding a combo break and lost health.
*/
2023-06-02 14:35:28 -04:00
public static inline final NOTE_MISS:ScriptEventType = 'NOTE_MISS';
2023-01-22 19:55:30 -05:00
/**
* Called when a character presses a note when there was none there, causing them to lose health.
* Important information such as direction pressed, etc. are all provided.
*
* This event IS cancelable! Canceling this event prevents the note from being considered missed,
* avoiding lost health/score and preventing the miss animation.
*/
2023-06-02 14:35:28 -04:00
public static inline final NOTE_GHOST_MISS:ScriptEventType = 'NOTE_GHOST_MISS';
2023-01-22 19:55:30 -05:00
/**
* Called when a song event is reached in the chart.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event IS cancelable! Cancelling this event prevents the event from being triggered,
* thus blocking its normal functionality.
*/
2023-06-02 14:35:28 -04:00
public static inline final SONG_EVENT:ScriptEventType = 'SONG_EVENT';
2023-01-22 19:55:30 -05:00
/**
* Called when the song starts. This occurs as the countdown ends and the instrumental and vocals begin.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SONG_START:ScriptEventType = 'SONG_START';
2023-01-22 19:55:30 -05:00
/**
* Called when the song ends. This happens as the instrumental and vocals end.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SONG_END:ScriptEventType = 'SONG_END';
2023-01-22 19:55:30 -05:00
/**
* Called when the countdown begins. This occurs before the song starts.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event IS cancelable! Canceling this event will prevent the countdown from starting.
* - The song will not start until you call Countdown.performCountdown() later.
* - Note that calling performCountdown() will trigger this event again, so be sure to add logic to ignore it.
*/
2023-06-02 14:35:28 -04:00
public static inline final COUNTDOWN_START:ScriptEventType = 'COUNTDOWN_START';
2023-01-22 19:55:30 -05:00
/**
* Called when a step of the countdown happens.
* Includes information about what step of the countdown was hit.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event IS cancelable! Canceling this event will pause the countdown.
* - The countdown will not resume until you call PlayState.resumeCountdown().
*/
2023-06-02 14:35:28 -04:00
public static inline final COUNTDOWN_STEP:ScriptEventType = 'COUNTDOWN_STEP';
2023-01-22 19:55:30 -05:00
/**
* Called when the countdown is done but just before the song starts.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final COUNTDOWN_END:ScriptEventType = 'COUNTDOWN_END';
2023-01-22 19:55:30 -05:00
/**
* Called before the game over screen triggers and the death animation plays.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final GAME_OVER:ScriptEventType = 'GAME_OVER';
2023-01-22 19:55:30 -05:00
/**
* Called after the player presses a key to restart the game.
* This can happen from the pause menu or the game over screen.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event IS cancelable! Canceling this event will prevent the game from restarting.
*/
2023-06-02 14:35:28 -04:00
public static inline final SONG_RETRY:ScriptEventType = 'SONG_RETRY';
2023-01-22 19:55:30 -05:00
/**
* Called when the player pushes down any key on the keyboard.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final KEY_DOWN:ScriptEventType = 'KEY_DOWN';
2023-01-22 19:55:30 -05:00
/**
* Called when the player releases a key on the keyboard.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final KEY_UP:ScriptEventType = 'KEY_UP';
2023-01-22 19:55:30 -05:00
/**
* Called when the game has finished loading the notes from JSON.
* This allows modders to mutate the notes before they are used in the song.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SONG_LOADED:ScriptEventType = 'SONG_LOADED';
2023-01-22 19:55:30 -05:00
/**
* Called when the game is about to switch the current FlxState.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final STATE_CHANGE_BEGIN:ScriptEventType = 'STATE_CHANGE_BEGIN';
2023-01-22 19:55:30 -05:00
/**
* Called when the game has finished switching the current FlxState.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final STATE_CHANGE_END:ScriptEventType = 'STATE_CHANGE_END';
2023-01-22 19:55:30 -05:00
/**
* Called when the game is about to open a new FlxSubState.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SUBSTATE_OPEN_BEGIN:ScriptEventType = 'SUBSTATE_OPEN_BEGIN';
2023-01-22 19:55:30 -05:00
/**
* Called when the game has finished opening a new FlxSubState.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SUBSTATE_OPEN_END:ScriptEventType = 'SUBSTATE_OPEN_END';
2023-01-22 19:55:30 -05:00
/**
* Called when the game is about to close the current FlxSubState.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SUBSTATE_CLOSE_BEGIN:ScriptEventType = 'SUBSTATE_CLOSE_BEGIN';
2023-01-22 19:55:30 -05:00
/**
* Called when the game has finished closing the current FlxSubState.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-02 14:35:28 -04:00
public static inline final SUBSTATE_CLOSE_END:ScriptEventType = 'SUBSTATE_CLOSE_END';
2023-01-22 19:55:30 -05:00
/**
2023-06-16 17:37:56 -04:00
* Called when the game starts a conversation.
2023-06-08 16:30:45 -04:00
*
2023-01-22 19:55:30 -05:00
* This event is not cancelable.
*/
2023-06-16 17:37:56 -04:00
public static inline final DIALOGUE_START:ScriptEventType = 'DIALOGUE_START';
/**
* Called to display the next line of conversation.
*
* This event IS cancelable! Canceling this event will prevent the conversation from moving to the next line.
* - This event is called when the conversation starts, or when the user presses ACCEPT to advance the conversation.
*/
public static inline final DIALOGUE_LINE:ScriptEventType = 'DIALOGUE_LINE';
/**
* Called to skip scrolling the current line of conversation.
*
* This event IS cancelable! Canceling this event will prevent the conversation from skipping to the next line.
* - This event is called when the user presses ACCEPT to advance the conversation while it is already advancing.
*/
public static inline final DIALOGUE_COMPLETE_LINE:ScriptEventType = 'DIALOGUE_COMPLETE_LINE';
/**
* Called to skip the conversation.
*
* This event IS cancelable! Canceling this event will prevent the conversation from skipping.
*/
public static inline final DIALOGUE_SKIP:ScriptEventType = 'DIALOGUE_SKIP';
/**
* Called when the game ends a conversation.
*
* This event is not cancelable.
*/
public static inline final DIALOGUE_END:ScriptEventType = 'DIALOGUE_END';
2023-01-22 19:55:30 -05:00
/**
* If true, the behavior associated with this event can be prevented.
* For example, cancelling COUNTDOWN_START should prevent the countdown from starting,
* until another script restarts it, or cancelling NOTE_HIT should cause the note to be missed.
*/
public var cancelable(default, null):Bool;
/**
* The type associated with the event.
*/
public var type(default, null):ScriptEventType;
/**
* Whether the event should continue to be triggered on additional targets.
*/
public var shouldPropagate(default, null):Bool;
/**
* Whether the event has been canceled by one of the scripts that received it.
*/
public var eventCanceled(default, null):Bool;
public function new(type:ScriptEventType, cancelable:Bool = false):Void
{
this.type = type;
this.cancelable = cancelable;
this.eventCanceled = false;
this.shouldPropagate = true;
}
/**
* Call this function on a cancelable event to cancel the associated behavior.
* For example, cancelling COUNTDOWN_START will prevent the countdown from starting.
*/
public function cancelEvent():Void
{
if (cancelable)
{
eventCanceled = true;
}
}
2023-06-02 14:35:28 -04:00
/**
* Cancel this event.
* This is an alias for cancelEvent() but I make this typo all the time.
*/
2023-01-22 19:55:30 -05:00
public function cancel():Void
{
cancelEvent();
}
/**
* Call this function to stop any other Scripteds from receiving the event.
*/
public function stopPropagation():Void
{
shouldPropagate = false;
}
public function toString():String
{
return 'ScriptEvent(type=$type, cancelable=$cancelable)';
}
}
/**
* SPECIFIC EVENTS
*/
/**
* An event that is fired associated with a specific note.
*/
class NoteScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The note associated with this event.
* You cannot replace it, but you can edit it.
*/
2023-06-22 01:41:01 -04:00
public var note(default, null):NoteSprite;
2023-01-22 19:55:30 -05:00
/**
* The combo count as it is with this event.
* Will be (combo) on miss events and (combo + 1) on hit events (the stored combo count won't update if the event is cancelled).
*/
public var comboCount(default, null):Int;
2023-06-02 14:35:28 -04:00
/**
* Whether to play the record scratch sound (if this eventn type is `NOTE_MISS`).
*/
public var playSound(default, default):Bool;
2023-06-22 01:41:01 -04:00
public function new(type:ScriptEventType, note:NoteSprite, comboCount:Int = 0, cancelable:Bool = false):Void
2023-01-22 19:55:30 -05:00
{
super(type, cancelable);
this.note = note;
this.comboCount = comboCount;
2023-06-02 14:35:28 -04:00
this.playSound = true;
2023-01-22 19:55:30 -05:00
}
public override function toString():String
{
return 'NoteScriptEvent(type=' + type + ', cancelable=' + cancelable + ', note=' + note + ', comboCount=' + comboCount + ')';
}
}
/**
* An event that is fired when you press a key with no note present.
*/
class GhostMissNoteScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The direction that was mistakenly pressed.
*/
2023-06-22 01:41:01 -04:00
public var dir(default, null):NoteDirection;
2023-01-22 19:55:30 -05:00
/**
* Whether there was a note within judgement range when this ghost note was pressed.
*/
public var hasPossibleNotes(default, null):Bool;
/**
* How much health should be lost when this ghost note is pressed.
* Remember that max health is 2.00.
*/
public var healthChange(default, default):Float;
/**
* How much score should be lost when this ghost note is pressed.
*/
public var scoreChange(default, default):Int;
/**
* Whether to play the record scratch sound.
*/
public var playSound(default, default):Bool;
/**
* Whether to play the miss animation on the player.
*/
public var playAnim(default, default):Bool;
2023-06-22 01:41:01 -04:00
public function new(dir:NoteDirection, hasPossibleNotes:Bool, healthChange:Float, scoreChange:Int):Void
2023-01-22 19:55:30 -05:00
{
super(ScriptEvent.NOTE_GHOST_MISS, true);
this.dir = dir;
this.hasPossibleNotes = hasPossibleNotes;
this.healthChange = healthChange;
this.scoreChange = scoreChange;
this.playSound = true;
this.playAnim = true;
}
public override function toString():String
{
return 'GhostMissNoteScriptEvent(dir=' + dir + ', hasPossibleNotes=' + hasPossibleNotes + ')';
}
}
/**
* An event that is fired when the song reaches an event.
*/
class SongEventScriptEvent extends ScriptEvent
{
/**
* The note associated with this event.
* You cannot replace it, but you can edit it.
*/
public var event(default, null):funkin.play.song.SongData.SongEventData;
public function new(event:funkin.play.song.SongData.SongEventData):Void
{
super(ScriptEvent.SONG_EVENT, true);
this.event = event;
}
public override function toString():String
{
return 'SongEventScriptEvent(event=' + event + ')';
}
}
/**
* An event that is fired during the update loop.
*/
class UpdateScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The note associated with this event.
* You cannot replace it, but you can edit it.
*/
public var elapsed(default, null):Float;
public function new(elapsed:Float):Void
{
super(ScriptEvent.UPDATE, false);
this.elapsed = elapsed;
}
public override function toString():String
{
return 'UpdateScriptEvent(elapsed=$elapsed)';
}
}
/**
* An event that is fired regularly during the song.
* May be on beat or on step.
*/
class SongTimeScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The current beat of the song.
*/
public var beat(default, null):Int;
/**
* The current step of the song.
*/
public var step(default, null):Int;
public function new(type:ScriptEventType, beat:Int, step:Int):Void
{
super(type, true);
this.beat = beat;
this.step = step;
}
public override function toString():String
{
return 'SongTimeScriptEvent(type=' + type + ', beat=' + beat + ', step=' + step + ')';
}
}
/**
* An event that is fired regularly during the song.
* May be on beat or on step.
*/
class CountdownScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The current step of the countdown.
*/
public var step(default, null):CountdownStep;
2023-06-02 14:35:28 -04:00
public function new(type:ScriptEventType, step:CountdownStep, cancelable:Bool = true):Void
2023-01-22 19:55:30 -05:00
{
super(type, cancelable);
this.step = step;
}
public override function toString():String
{
return 'CountdownScriptEvent(type=' + type + ', step=' + step + ')';
}
}
2023-06-16 17:37:56 -04:00
/**
* An event that is fired during a dialogue.
*/
class DialogueScriptEvent extends ScriptEvent
{
/**
* The dialogue being referenced by the event.
*/
public var conversation(default, null):Conversation;
public function new(type:ScriptEventType, conversation:Conversation, cancelable:Bool = true):Void
{
super(type, cancelable);
this.conversation = conversation;
}
public override function toString():String
{
return 'DialogueScriptEvent(type=$type, conversation=$conversation)';
}
}
/**
* An event that is fired when the player presses a key.
*/
class KeyboardInputScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The associated keyboard event.
*/
public var event(default, null):KeyboardEvent;
public function new(type:ScriptEventType, event:KeyboardEvent):Void
{
super(type, false);
this.event = event;
}
public override function toString():String
{
return 'KeyboardInputScriptEvent(type=' + type + ', event=' + event + ')';
}
}
/**
* An event that is fired once the song's chart has been parsed.
*/
class SongLoadScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The note associated with this event.
* You cannot replace it, but you can edit it.
*/
2023-06-22 01:41:01 -04:00
public var notes(default, set):Array<SongNoteData>;
2023-01-22 19:55:30 -05:00
public var id(default, null):String;
public var difficulty(default, null):String;
2023-06-22 01:41:01 -04:00
function set_notes(notes:Array<SongNoteData>):Array<SongNoteData>
2023-01-22 19:55:30 -05:00
{
this.notes = notes;
return this.notes;
}
2023-06-22 01:41:01 -04:00
public function new(id:String, difficulty:String, notes:Array<SongNoteData>):Void
2023-01-22 19:55:30 -05:00
{
super(ScriptEvent.SONG_LOADED, false);
this.id = id;
this.difficulty = difficulty;
this.notes = notes;
}
public override function toString():String
{
var noteStr = notes == null ? 'null' : 'Array(' + notes.length + ')';
return 'SongLoadScriptEvent(notes=$noteStr, id=$id, difficulty=$difficulty)';
}
}
2022-03-14 20:48:45 -04:00
/**
* An event that is fired when moving out of or into an FlxState.
*/
class StateChangeScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The state the game is moving into.
*/
public var targetState(default, null):FlxState;
public function new(type:ScriptEventType, targetState:FlxState, cancelable:Bool = false):Void
{
super(type, cancelable);
this.targetState = targetState;
}
public override function toString():String
{
return 'StateChangeScriptEvent(type=' + type + ', targetState=' + targetState + ')';
}
}
/**
* An event that is fired when moving out of or into an FlxSubState.
*/
class SubStateScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* The state the game is moving into.
*/
public var targetState(default, null):FlxSubState;
public function new(type:ScriptEventType, targetState:FlxSubState, cancelable:Bool = false):Void
{
super(type, cancelable);
this.targetState = targetState;
}
public override function toString():String
{
return 'SubStateScriptEvent(type=' + type + ', targetState=' + targetState + ')';
}
2022-03-14 20:48:45 -04:00
}
/**
* An event which is called when the player attempts to pause the game.
*/
class PauseScriptEvent extends ScriptEvent
{
2023-01-22 19:55:30 -05:00
/**
* Whether to use the Gitaroo Man pause.
*/
public var gitaroo(default, default):Bool;
public function new(gitaroo:Bool):Void
{
super(ScriptEvent.PAUSE, true);
this.gitaroo = gitaroo;
}
}