Skip to content

Commit

Permalink
a lot of lua stuff, experimental
Browse files Browse the repository at this point in the history
yes custom states, video functions, camera functions,
  • Loading branch information
charlesisfeline committed Feb 21, 2024
1 parent c436352 commit f22862a
Show file tree
Hide file tree
Showing 22 changed files with 1,467 additions and 761 deletions.
2 changes: 1 addition & 1 deletion source/animateatlas/AtlasFrameMaker.hx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class AtlasFrameMaker extends FlxFramesCollection

if (Paths.fileExists('images/$key/spritemap1.json', TEXT))
{
PlayState.instance.addTextToDebug("Only Spritemaps made with Adobe Animate 2018 are supported", FlxColor.RED);
psychlua.ScriptHandler.addTextToDebug("Only Spritemaps made with Adobe Animate 2018 are supported", FlxColor.RED);
trace("Only Spritemaps made with Adobe Animate 2018 are supported");
return null;
}
Expand Down
3 changes: 2 additions & 1 deletion source/backend/Mods.hx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ class Mods
'weeks',
'fonts',
'scripts',
'achievements'
'achievements',
'states'
];

private static var globalMods:Array<String> = [];
Expand Down
44 changes: 41 additions & 3 deletions source/backend/MusicBeatSubstate.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ class MusicBeatSubstate extends FlxSubState
inline function get_controls():Controls
return Controls.instance;

override function create()
{
// Moves debug group to front of this substate
if (psychlua.ScriptHandler.luaDebugGroup != null)
{
FlxG.state.remove(psychlua.ScriptHandler.luaDebugGroup);
add(psychlua.ScriptHandler.luaDebugGroup);
}

super.create();
}

override function update(elapsed:Float)
{
//everyStep();
Expand Down Expand Up @@ -136,16 +148,30 @@ class MusicBeatSubstate extends FlxSubState
{
if (curStep % 4 == 0)
beatHit();

if (!FlxG.state.persistentUpdate) // Prevents scripts from being called twice
{
psychlua.ScriptHandler.setOnScripts('curStep', curStep);
psychlua.ScriptHandler.callOnScripts('onStepHit');
}
}

public function beatHit():Void
{
//do literally nothing dumbass
if (!FlxG.state.persistentUpdate)
{
psychlua.ScriptHandler.setOnScripts('curBeat', curBeat);
psychlua.ScriptHandler.callOnScripts('onBeatHit');
}
}

public function sectionHit():Void
{
//yep, you guessed it, nothing again, dumbass
if (!FlxG.state.persistentUpdate)
{
psychlua.ScriptHandler.setOnScripts('curSection', curSection);
psychlua.ScriptHandler.callOnScripts('onSectionHit');
}
}

function getBeatsOnSection()
Expand All @@ -154,4 +180,16 @@ class MusicBeatSubstate extends FlxSubState
if(PlayState.SONG != null && PlayState.SONG.notes[curSection] != null) val = PlayState.SONG.notes[curSection].sectionBeats;
return val == null ? 4 : val;
}

override function destroy()
{
// Moves debug group back to original state
if (psychlua.ScriptHandler.luaDebugGroup != null)
{
remove(psychlua.ScriptHandler.luaDebugGroup);
FlxG.state.add(psychlua.ScriptHandler.luaDebugGroup);
}

super.destroy();
}
}
10 changes: 5 additions & 5 deletions source/cutscenes/DialogueBoxPsych.hx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DialogueBoxPsych extends FlxSpriteGroup
{
if (ignoreThisFrame)
{
PlayState.instance.callOnScripts('onDialogueStart'); // It's here, because scripts might want to access PlayState.instance.psychDialogue (aka this)
psychlua.ScriptHandler.callOnScripts('onDialogueStart'); // It's here, because scripts might want to access PlayState.instance.psychDialogue (aka this)
startNextDialog();

ignoreThisFrame = false;
Expand All @@ -83,7 +83,7 @@ class DialogueBoxPsych extends FlxSpriteGroup
if (bgFade.alpha > 0.5)
bgFade.alpha = 0.5;

var ret:Dynamic = PlayState.instance.callOnScripts('onDialogueConfirm', null, false);
var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onDialogueConfirm', null, false);
if ((confirmDialogue || Controls.instance.ACCEPT) && ret != FunkinLua.Function_Stop)
{
if (!typedText.finishedText)
Expand Down Expand Up @@ -205,7 +205,7 @@ class DialogueBoxPsych extends FlxSpriteGroup
var event:DialogueEvent = curDialogue.events[i];

// Call the current event
PlayState.instance.callOnScripts('onDialogueEvent', [event.type, event.arguments]);
psychlua.ScriptHandler.callOnScripts('onDialogueEvent', [event.type, event.arguments]);
}
}

Expand Down Expand Up @@ -552,7 +552,7 @@ class DialogueBoxPsych extends FlxSpriteGroup
currentCharAlpha = char.alpha;

// Call Update
var ret:Dynamic = PlayState.instance.callOnScripts('onCharacterMove', [tag, char.curCharacter, char.startingPos, offsetPos, elapsed], false);
var ret:Dynamic = psychlua.ScriptHandler.callOnScripts('onCharacterMove', [tag, char.curCharacter, char.startingPos, offsetPos, elapsed], false);

if (ret != FunkinLua.Function_Stop)
{
Expand Down Expand Up @@ -657,7 +657,7 @@ class DialogueBoxPsych extends FlxSpriteGroup
typedText.setScale(0.7);
typedText.onUpdate = function(text:String):Void
{
PlayState.instance.callOnScripts('onDialogueTextUpdate', [text]);
psychlua.ScriptHandler.callOnScripts('onDialogueTextUpdate', [text]);
};
add(typedText);
}
Expand Down
9 changes: 9 additions & 0 deletions source/import.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ import backend.Discord;
import llua.*;
import llua.Lua;
import psychlua.*;
#else
import psychlua.FunkinLua;
import psychlua.LuaUtils;
import psychlua.HScript;
import psychlua.ScriptHandler;
#end

#if SScript
import tea.SScript;
#end


// FlxAnimate
#if flxanimate
import flxanimate.*;
Expand Down
2 changes: 1 addition & 1 deletion source/psychlua/CallbackHandler.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CallbackHandler
if(last == null || last.lua != l)
{
//trace('looping thru scripts');
for (script in PlayState.instance.luaArray)
for (script in psychlua.ScriptHandler.luaArray)
if(script != FunkinLua.lastCalledScript && script != null && script.lua == l)
{
//trace('found script');
Expand Down
79 changes: 79 additions & 0 deletions source/psychlua/CameraFunctions.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package psychlua;

class CameraFunctions
{
public static function implement(funk:FunkinLua)
{
var lua = funk.lua;
Lua_helper.add_callback(lua, "makeLuaCamera", function(tag:String, ?x:Float, ?y:Float, ?width:Int, ?height:Int, ?zoom:Float) {
tag = tag.replace('.', '');
LuaUtils.resetTag(tag, psychlua.ScriptHandler.modchartCameras);
var newCam:FlxCamera = new FlxCamera(x, y, width, height, zoom);
newCam.bgColor.alpha = 0;
psychlua.ScriptHandler.modchartCameras.set(tag, newCam);
});


Lua_helper.add_callback(lua, "addLuaCamera", function(tag:String, defaultDraw:Bool = false) {
if(psychlua.ScriptHandler.modchartCameras.exists(tag)) {
var shit:FlxCamera = psychlua.ScriptHandler.modchartCameras.get(tag);
FlxG.cameras.add(shit, defaultDraw);
}
});
Lua_helper.add_callback(lua, "removeLuaCamera", function(tag:String, destroy:Bool = true) {
if(!psychlua.ScriptHandler.modchartCameras.exists(tag)) {
return;
}

var pee:FlxCamera = psychlua.ScriptHandler.modchartCameras.get(tag);
if(destroy) pee.kill();

FlxG.cameras.remove(pee, false);
if(destroy) {
pee.destroy();
psychlua.ScriptHandler.modchartCameras.remove(tag);
}
});

Lua_helper.add_callback(lua, "getCameraOrder", function(camera:String) {
var cam:FlxCamera = LuaUtils.cameraFromString(camera);
if(cam != null) return FlxG.cameras.list.indexOf(cam);
FunkinLua.luaTrace("getCameraOrder: Default camera doesn't exist!", false, false, FlxColor.RED);
return -1;
});
Lua_helper.add_callback(lua, "setCameraOrder", function(camera:String, position:Int) {
var cam:FlxCamera = LuaUtils.cameraFromString(camera);
if(cam != null) {
if(FlxG.cameras.list.contains(cam)){
var list:Array<FlxCamera> = FlxG.cameras.list.copy();
var defaults:Array<FlxCamera> = @:privateAccess FlxG.cameras.defaults.copy();

for(flxCam in list) FlxG.cameras.remove(flxCam, false);

list.remove(cam);
list.insert(position, cam);

for(flxCam in list){
var defaultDraw = defaults.contains(flxCam);
FlxG.cameras.add(flxCam, defaultDraw);
}

return;
}else{
FunkinLua.luaTrace('setCameraOrder: Camera $camera isn\'t in the list yet! Use "addLuaCamera($camera)" first', false, false, FlxColor.RED);
}
}
FunkinLua.luaTrace("setCameraOrder: Default camera doesn't exist!", false, false, FlxColor.RED);
});

Lua_helper.add_callback(lua, "cameraShake", function(camera:String, ?intensity:Float, ?duration:Float) {
LuaUtils.cameraFromString(camera).shake(intensity, duration);
});
Lua_helper.add_callback(lua, "cameraFlash", function(camera:String, ?color:String, ?duration:Float, ?forced:Bool) {
LuaUtils.cameraFromString(camera).flash(CoolUtil.colorFromString(color), duration, null, forced);
});
Lua_helper.add_callback(lua, "cameraFade", function(camera:String, ?color:String, ?duration:Float, ?fadeIn:Bool, ?forced:Bool) {
LuaUtils.cameraFromString(camera).fade(CoolUtil.colorFromString(color), duration, fadeIn, null, forced);
});
}
}
117 changes: 117 additions & 0 deletions source/psychlua/CustomState.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package psychlua;

import flixel.FlxObject;
import flixel.FlxState;
import flixel.addons.transition.FlxTransitionableState;

final packageMap:Map<String, String> = [
'CustomState' => 'psychlua.',

'FlashingState' => 'states.',
'TitleState' => 'states.',
'OutdatedState' => 'states.',
'MainMenuState' => 'states.',
'StoryMenuState' => 'states.',
'PlayState' => 'states.',
'FreeplayState' => 'states.',
'ModsMenuState' => 'states.',
'CreditsState' => 'states.',
'AchievementsMenuState' => 'states.',

'OptionsState' => 'options.',
'NoteOffsetState' => 'options.',

'CharacterEditorState' => 'states.editors.',
'ChartingState' => 'states.editors.',
'DialogueCharacterEditorState' => 'states.editors.',
'MasterEditorMenu' => 'states.editors.',
'MenuCharacterEditorState' => 'states.editors.',
'NoteSplashDebugState' => 'states.editors.',
'WeekEditorState' => 'states.editors.'
];

class CustomState extends MusicBeatState
{
public static var name:String = 'unnamed';
public static var newName:Bool = false;
public static var instance:CustomState;

#if LUA_ALLOWED
public static function implement(funk:FunkinLua)
{
var lua:State = funk.lua;
Lua_helper.add_callback(lua, "switchState", function(name:String, ?args:Array<Dynamic> = null, ?load:Bool = false, ?stopMusic:Bool = false) {
if (args == null) args = [];
if (!name.contains('.') && packageMap.get(name) != null)
name = packageMap.get(name) + name;

var state:FlxState = Type.createInstance(Type.resolveClass(name), args);

if(state != null) {
if(load) {
LoadingState.loadAndSwitchState(() -> state, stopMusic);
} else {
if (stopMusic) FlxG.sound.music.stop();
FlxG.switchState(() -> state);
}
} else {
FunkinLua.luaTrace('switchState: State "$name" doesn\'t exist!', false, false, FlxColor.RED);
}
});
Lua_helper.add_callback(lua, "setSkipTransition", function(skipIn:Null<Bool>, skipOut:Null<Bool>) {
if(skipIn != null) FlxTransitionableState.skipNextTransIn = skipIn;
if(skipOut != null) FlxTransitionableState.skipNextTransOut = skipOut;
});
Lua_helper.add_callback(lua, "resetState", function() {
FlxG.resetState();
});
Lua_helper.add_callback(lua, "clearStoredMemory", function() {
Paths.clearStoredMemory();
});
Lua_helper.add_callback(lua, "clearStoredMemory", function() {
Paths.clearStoredMemory();
});
Lua_helper.add_callback(lua, "clearUnusedMemory", function() {
Paths.clearUnusedMemory();
});
}
#end

override function create()
{
instance = this;
newName = false;

persistentUpdate = persistentDraw = true;
initPsychCamera();

psychlua.ScriptHandler.startScripts();
#if LUA_ALLOWED psychlua.ScriptHandler.startLuasNamed('states/$name.lua'); #end
#if HSCRIPT_ALLOWED psychlua.ScriptHandler.startLuasNamed('states/$name.hx'); #end

super.create();
psychlua.ScriptHandler.callOnScripts('onCreatePost');
}

public function new(name:String)
{
CustomState.name = name;
newName = true;
super();
}

override function update(elapsed:Float)
{
psychlua.ScriptHandler.callOnScripts('onUpdate', [elapsed]);
super.update(elapsed);
psychlua.ScriptHandler.callOnScripts('onUpdatePost', [elapsed]);
}

override function destroy()
{
psychlua.ScriptHandler.destroyScripts();
if(!newName) name = 'unnamed';

super.destroy();
}
}
Loading

0 comments on commit f22862a

Please sign in to comment.