Skip to content

Added Scriptable Events to TitleState #550

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions source/funkin/backend/scripting/events/EnterPressedEvent.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package funkin.backend.scripting.events;

import funkin.backend.utils.CoolUtil.CoolSfx;
import flixel.util.FlxColor;

final class EnterPressedEvent extends CancellableEvent {
/**
* Whether the screen should flash on PRESS ENTER TO START
*/
public var flash:Bool;

/**
* The color that the screen flashes
*/
public var flashColor:FlxColor;

/**
* Whether PRESS ENTER TO START should play a menu sfx
*/
public var playSfx:Bool;

/**
* The menu sound to play
*/
public var menuSound:CoolSfx;

/**
* The time between enter being pressed and the transition into MainMenuState
*/
public var timerLength:Float;
}
30 changes: 30 additions & 0 deletions source/funkin/backend/scripting/events/StartIntroEvent.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package funkin.backend.scripting.events;



final class StartIntroEvent extends CancellableEvent {
/**
* Whether StoryMenuState should play the menu song
*/
public var playMenuSong:Bool;

/**
* Whether StoryMenuState should use the title screen's XML file
*/
public var titleScreenXML:Bool;

/**
* The path for the title sprite (PRESS ENTER TO PLAY)
*/
public var titleSprite:String;

/**
* The path for the Newgrounds sprite
*/
public var ngSprite:String;

/**
* Whether the mouse should be visible
*/
public var showMouse:Bool;
}
4 changes: 3 additions & 1 deletion source/funkin/game/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1480,7 +1480,9 @@ class PlayState extends MusicBeatState
*/
public function endSong():Void
{
scripts.call("onSongEnd");
var event = scripts.event("onSongEnd", new CancellableEvent());
if (event.cancelled) return;

canPause = false;
inst.volume = 0;
vocals.volume = 0;
Expand Down
18 changes: 13 additions & 5 deletions source/funkin/menus/MainMenuState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import flixel.effects.FlxFlicker;
import flixel.text.FlxText;
import flixel.tweens.FlxTween;
import flixel.util.FlxColor;
import flixel.util.FlxTimer;
import lime.app.Application;
import funkin.backend.scripting.events.*;

Expand Down Expand Up @@ -150,17 +151,23 @@ class MainMenuState extends MusicBeatState
}

function selectItem() {
selectedSomethin = true;
CoolUtil.playMenuSFX(CONFIRM);
var primevent = event("onInitialSelection", EventManager.get(AmountEvent).recycle(1));
if (!primevent.cancelled) {
selectedSomethin = true;

CoolUtil.playMenuSFX(CONFIRM);

if (Options.flashingMenu) FlxFlicker.flicker(magenta, 1.1, 0.15, false);
if (Options.flashingMenu) FlxFlicker.flicker(magenta, 1.1, 0.15, false);

FlxFlicker.flicker(menuItems.members[curSelected], 1, Options.flashingMenu ? 0.06 : 0.15, false, false, function(flick:FlxFlicker)
{
FlxFlicker.flicker(menuItems.members[curSelected], primevent.amount, Options.flashingMenu ? 0.06 : 0.15, false, false);
}

new FlxTimer().start(primevent.amount, function(timer:FlxTimer) {
var daChoice:String = optionShit[curSelected];

var event = event("onSelectItem", EventManager.get(NameEvent).recycle(daChoice));
if (event.cancelled) return;

switch (event.name)
{
case 'story mode': FlxG.switchState(new StoryMenuState());
Expand All @@ -169,6 +176,7 @@ class MainMenuState extends MusicBeatState
case 'options': FlxG.switchState(new OptionsMenu());
}
});

}
function changeItem(huh:Int = 0)
{
Expand Down
65 changes: 48 additions & 17 deletions source/funkin/menus/TitleState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package funkin.menus;

import funkin.backend.MusicBeatGroup;
import funkin.backend.utils.XMLUtil;
import funkin.backend.scripting.EventManager;
import funkin.backend.scripting.events.StartIntroEvent;
import funkin.backend.scripting.events.NameEvent;
import funkin.backend.scripting.events.EnterPressedEvent;
import funkin.backend.scripting.events.CancellableEvent;
import flixel.util.typeLimit.OneOfTwo;
import flixel.group.FlxGroup;
import flixel.input.gamepad.FlxGamepad;
Expand Down Expand Up @@ -48,7 +53,10 @@ class TitleState extends MusicBeatState

function startIntro()
{
if (!initialized)
var event = event("onStartIntro", EventManager.get(StartIntroEvent).recycle(true, true, 'menus/titlescreen/titleEnter', 'newgrounds_logo', false));
if (event.cancelled) return;

if (!initialized && event.playMenuSong)
CoolUtil.playMenuSong(true);

persistentUpdate = true;
Expand All @@ -57,14 +65,16 @@ class TitleState extends MusicBeatState
add(bg);

#if TITLESCREEN_XML
titleScreenSprites = new MusicBeatGroup();
add(titleScreenSprites);
loadXML();
if (event.titleScreenXML) {
titleScreenSprites = new MusicBeatGroup();
add(titleScreenSprites);
loadXML();
}
#end

if (titleText == null) {
titleText = new FlxSprite(100, FlxG.height * 0.8);
titleText.frames = Paths.getFrames('menus/titlescreen/titleEnter');
titleText.frames = Paths.getFrames(event.titleSprite);
titleText.animation.addByPrefix('idle', "Press Enter to Begin", 24);
titleText.animation.addByPrefix('press', "ENTER PRESSED", 24);
titleText.antialiasing = true;
Expand All @@ -79,7 +89,7 @@ class TitleState extends MusicBeatState
add(blackScreen);

#if !TITLESCREEN_XML
ngSpr = new FlxSprite(0, FlxG.height * 0.52).loadAnimatedGraphic(Paths.image('newgrounds_logo'));
ngSpr = new FlxSprite(0, FlxG.height * 0.52).loadAnimatedGraphic(Paths.image(event.ngSprite));
add(ngSpr);
ngSpr.visible = false;
ngSpr.setGraphicSize(Std.int(ngSpr.width * 0.8));
Expand All @@ -88,7 +98,7 @@ class TitleState extends MusicBeatState
ngSpr.antialiasing = true;
#end

FlxG.mouse.visible = false;
FlxG.mouse.visible = event.showMouse;

if (initialized)
skipIntro();
Expand All @@ -97,10 +107,12 @@ class TitleState extends MusicBeatState

add(textGroup);
}

public function getIntroTextShit():Array<Array<String>>
{
var fullText:String = Assets.getText(Paths.txt('titlescreen/introText'));
{
var event = event("onGetIntroText", EventManager.get(NameEvent).recycle('introText'));

var fullText:String = Assets.getText(Paths.txt(Assets.exists(Paths.txt('titlescreen/${event.name}')) ? 'titlescreen/${event.name}' : 'titlescreen/introText'));

var firstArray:Array<String> = fullText.split('\n');
var swagGoodArray:Array<Array<String>> = [];
Expand All @@ -109,7 +121,7 @@ class TitleState extends MusicBeatState
{
swagGoodArray.push(i.split('--'));
}

return swagGoodArray;
}

Expand Down Expand Up @@ -161,15 +173,18 @@ class TitleState extends MusicBeatState
}

public function pressEnter() {
var event = event("onEnterPressed", EventManager.get(EnterPressedEvent).recycle(true, 0xFFFFFFFF, true, CONFIRM, 2));
if (event.cancelled) return;

titleText.animation.play('press');

FlxG.camera.flash(FlxColor.WHITE, 1);
CoolUtil.playMenuSFX(CONFIRM, 0.7);
if (event.flash) FlxG.camera.flash(event.flashColor, 1);
if (event.playSfx) CoolUtil.playMenuSFX(event.menuSound, 0.7);

transitioning = true;
// FlxG.sound.music.stop();

new FlxTimer().start(2, (_) -> goToMainMenu());
new FlxTimer().start(event.timerLength, (_) -> goToMainMenu());
}

function goToMainMenu() {
Expand All @@ -189,24 +204,37 @@ class TitleState extends MusicBeatState

public function createCoolText(textArray:Array<String>)
{
var money:Alphabet;

var event = event("onCreateText", new CancellableEvent());
if (!event.cancelled) return;

for (i=>text in textArray)
{
if (text == "" || text == null) continue;
var money:Alphabet = new Alphabet(0, (i * 60) + 200, text, true, false);
money = new Alphabet(0, (i * 60) + 200, text, true, false);
money.screenCenter(X);
textGroup.add(money);
}
}

public function addMoreText(text:String)
{
var coolText:Alphabet = new Alphabet(0, (textGroup.length * 60) + 200, text, true, false);
var coolText:Alphabet;

var event = event("onMoreText", new CancellableEvent());
if (event.cancelled) return;

coolText = new Alphabet(0, (textGroup.length * 60) + 200, text, true, false);
coolText.screenCenter(X);
textGroup.add(coolText);
}

public function deleteCoolText()
{
var event = event("onDeleteText", new CancellableEvent());
if (event.cancelled) return;

while (textGroup.members.length > 0) {
textGroup.members[0].destroy();
textGroup.remove(textGroup.members[0], true);
Expand Down Expand Up @@ -326,6 +354,9 @@ class TitleState extends MusicBeatState

public function skipIntro():Void
{
var event = event("onIntroSkipped", new CancellableEvent());
if (event.cancelled) return;

if (!skippedIntro)
{
#if !TITLESCREEN_XML
Expand Down Expand Up @@ -387,4 +418,4 @@ typedef TitleStateImage = {
@:optional var scale:Null<Float>;
@:optional var flipX:Null<Bool>;
@:optional var flipY:Null<Bool>;
}
}