* Lua Improvments * fix * fix 2 * fix 3 * fix 4 * fix 5 * fix 6 * Fix 7, please action dont fail * done * done 2 * done 3 * final * last fix * please last fix final * add `curSection` on lua * ready for pr * oops * oop2 * Lua Improvements 2 * Update Project.xml * fixed `lastCalledScript` * Attempt Fix 2 * Attempt fix 3 * shit * done i think * fuck * fuck 2
429 lines
13 KiB
Haxe
429 lines
13 KiB
Haxe
package psychlua;
|
|
|
|
import animateatlas.AtlasFrameMaker;
|
|
import openfl.display.BlendMode;
|
|
import Type.ValueType;
|
|
|
|
using StringTools;
|
|
|
|
typedef LuaTweenOptions = {
|
|
type:FlxTweenType,
|
|
startDelay:Float,
|
|
onUpdate:Null<String>,
|
|
onStart:Null<String>,
|
|
onComplete:Null<String>,
|
|
loopDelay:Float,
|
|
ease:EaseFunction
|
|
}
|
|
|
|
class LuaUtils
|
|
{
|
|
public static final Function_Stop:Dynamic = "##PSYCHLUA_FUNCTIONSTOP";
|
|
public static final Function_Continue:Dynamic = "##PSYCHLUA_FUNCTIONCONTINUE";
|
|
public static final Function_StopLua:Dynamic = "##PSYCHLUA_FUNCTIONSTOPLUA";
|
|
|
|
public static function getLuaTween(options:Dynamic)
|
|
{
|
|
if (options == null) options = {}
|
|
return {
|
|
type: getTweenTypeByString(options.type),
|
|
startDelay: options.startDelay,
|
|
onUpdate: options.onUpdate,
|
|
onStart: options.onStart,
|
|
onComplete: options.onComplete,
|
|
loopDelay: options.loopDelay,
|
|
ease: getTweenEaseByString(options.ease)
|
|
};
|
|
}
|
|
|
|
public static function setVarInArray(instance:Dynamic, variable:String, value:Dynamic, allowMaps:Bool = false):Any
|
|
{
|
|
var splitProps:Array<String> = variable.split('[');
|
|
if(splitProps.length > 1)
|
|
{
|
|
var target:Dynamic = null;
|
|
if(PlayState.instance.variables.exists(splitProps[0]))
|
|
{
|
|
var retVal:Dynamic = PlayState.instance.variables.get(splitProps[0]);
|
|
if(retVal != null)
|
|
target = retVal;
|
|
}
|
|
else target = Reflect.getProperty(instance, splitProps[0]);
|
|
|
|
for (i in 1...splitProps.length)
|
|
{
|
|
var j:Dynamic = splitProps[i].substr(0, splitProps[i].length - 1);
|
|
if(i >= splitProps.length-1) //Last array
|
|
target[j] = value;
|
|
else //Anything else
|
|
target = target[j];
|
|
}
|
|
return target;
|
|
}
|
|
|
|
if(allowMaps && isMap(instance))
|
|
{
|
|
instance.set(variable, value);
|
|
return value;
|
|
}
|
|
|
|
if(PlayState.instance.variables.exists(variable))
|
|
{
|
|
PlayState.instance.variables.set(variable, value);
|
|
return value;
|
|
}
|
|
Reflect.setProperty(instance, variable, value);
|
|
return value;
|
|
}
|
|
public static function getVarInArray(instance:Dynamic, variable:String, allowMaps:Bool = false):Any
|
|
{
|
|
var splitProps:Array<String> = variable.split('[');
|
|
if(splitProps.length > 1)
|
|
{
|
|
var target:Dynamic = null;
|
|
if(PlayState.instance.variables.exists(splitProps[0]))
|
|
{
|
|
var retVal:Dynamic = PlayState.instance.variables.get(splitProps[0]);
|
|
if(retVal != null)
|
|
target = retVal;
|
|
}
|
|
else
|
|
target = Reflect.getProperty(instance, splitProps[0]);
|
|
|
|
for (i in 1...splitProps.length)
|
|
{
|
|
var j:Dynamic = splitProps[i].substr(0, splitProps[i].length - 1);
|
|
target = target[j];
|
|
}
|
|
return target;
|
|
}
|
|
|
|
if(allowMaps && isMap(instance))
|
|
{
|
|
return instance.get(variable);
|
|
}
|
|
|
|
if(PlayState.instance.variables.exists(variable))
|
|
{
|
|
var retVal:Dynamic = PlayState.instance.variables.get(variable);
|
|
if(retVal != null)
|
|
return retVal;
|
|
}
|
|
return Reflect.getProperty(instance, variable);
|
|
}
|
|
|
|
public static function isMap(variable:Dynamic)
|
|
{
|
|
if(variable.exists != null && variable.keyValueIterator != null) return true;
|
|
return false;
|
|
}
|
|
|
|
public static function setGroupStuff(leArray:Dynamic, variable:String, value:Dynamic, ?allowMaps:Bool = false) {
|
|
var split:Array<String> = variable.split('.');
|
|
if(split.length > 1) {
|
|
var obj:Dynamic = Reflect.getProperty(leArray, split[0]);
|
|
for (i in 1...split.length-1)
|
|
obj = Reflect.getProperty(obj, split[i]);
|
|
|
|
leArray = obj;
|
|
variable = split[split.length-1];
|
|
}
|
|
if(allowMaps && isMap(leArray)) leArray.set(variable, value);
|
|
else Reflect.setProperty(leArray, variable, value);
|
|
return value;
|
|
}
|
|
public static function getGroupStuff(leArray:Dynamic, variable:String, ?allowMaps:Bool = false) {
|
|
var split:Array<String> = variable.split('.');
|
|
if(split.length > 1) {
|
|
var obj:Dynamic = Reflect.getProperty(leArray, split[0]);
|
|
for (i in 1...split.length-1)
|
|
obj = Reflect.getProperty(obj, split[i]);
|
|
|
|
leArray = obj;
|
|
variable = split[split.length-1];
|
|
}
|
|
|
|
if(allowMaps && isMap(leArray)) return leArray.get(variable);
|
|
return Reflect.getProperty(leArray, variable);
|
|
}
|
|
|
|
public static function getPropertyLoop(split:Array<String>, ?checkForTextsToo:Bool = true, ?getProperty:Bool=true, ?allowMaps:Bool = false):Dynamic
|
|
{
|
|
var obj:Dynamic = getObjectDirectly(split[0], checkForTextsToo);
|
|
var end = split.length;
|
|
if(getProperty) end = split.length-1;
|
|
|
|
for (i in 1...end) obj = getVarInArray(obj, split[i], allowMaps);
|
|
return obj;
|
|
}
|
|
|
|
public static function getObjectDirectly(objectName:String, ?checkForTextsToo:Bool = true, ?allowMaps:Bool = false):Dynamic
|
|
{
|
|
switch(objectName)
|
|
{
|
|
case 'this' | 'instance' | 'game':
|
|
return PlayState.instance;
|
|
|
|
default:
|
|
var obj:Dynamic = PlayState.instance.getLuaObject(objectName, checkForTextsToo);
|
|
if(obj == null) obj = getVarInArray(getTargetInstance(), objectName, allowMaps);
|
|
return obj;
|
|
}
|
|
}
|
|
|
|
inline public static function getTextObject(name:String):FlxText
|
|
{
|
|
return #if LUA_ALLOWED PlayState.instance.modchartTexts.exists(name) ? PlayState.instance.modchartTexts.get(name) : #end Reflect.getProperty(PlayState.instance, name);
|
|
}
|
|
|
|
public static function isOfTypes(value:Any, types:Array<Dynamic>)
|
|
{
|
|
for (type in types)
|
|
{
|
|
if(Std.isOfType(value, type)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static inline function getTargetInstance()
|
|
{
|
|
return PlayState.instance.isDead ? GameOverSubstate.instance : PlayState.instance;
|
|
}
|
|
|
|
public static inline function getLowestCharacterGroup():FlxSpriteGroup
|
|
{
|
|
var group:FlxSpriteGroup = PlayState.instance.gfGroup;
|
|
var pos:Int = PlayState.instance.members.indexOf(group);
|
|
|
|
var newPos:Int = PlayState.instance.members.indexOf(PlayState.instance.boyfriendGroup);
|
|
if(newPos < pos)
|
|
{
|
|
group = PlayState.instance.boyfriendGroup;
|
|
pos = newPos;
|
|
}
|
|
|
|
newPos = PlayState.instance.members.indexOf(PlayState.instance.dadGroup);
|
|
if(newPos < pos)
|
|
{
|
|
group = PlayState.instance.dadGroup;
|
|
pos = newPos;
|
|
}
|
|
return group;
|
|
}
|
|
|
|
public static function addAnimByIndices(obj:String, name:String, prefix:String, indices:Any = null, framerate:Int = 24, loop:Bool = false)
|
|
{
|
|
var obj:FlxSprite = cast getObjectDirectly(obj, false);
|
|
if(obj != null && obj.animation != null)
|
|
{
|
|
if(indices == null)
|
|
indices = [0];
|
|
else if(Std.isOfType(indices, String))
|
|
{
|
|
var strIndices:Array<String> = cast (indices, String).trim().split(',');
|
|
var myIndices:Array<Int> = [];
|
|
for (i in 0...strIndices.length) {
|
|
myIndices.push(Std.parseInt(strIndices[i]));
|
|
}
|
|
indices = myIndices;
|
|
}
|
|
|
|
obj.animation.addByIndices(name, prefix, indices, '', framerate, loop);
|
|
if(obj.animation.curAnim == null)
|
|
{
|
|
var dyn:Dynamic = cast obj;
|
|
if(dyn.playAnim != null) dyn.playAnim(name, true);
|
|
else dyn.animation.play(name, true);
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function loadFrames(spr:FlxSprite, image:String, spriteType:String)
|
|
{
|
|
switch(spriteType.toLowerCase().trim())
|
|
{
|
|
case "texture" | "textureatlas" | "tex":
|
|
spr.frames = AtlasFrameMaker.construct(image);
|
|
|
|
case "texture_noaa" | "textureatlas_noaa" | "tex_noaa":
|
|
spr.frames = AtlasFrameMaker.construct(image, null, true);
|
|
|
|
case "packer" | "packeratlas" | "pac":
|
|
spr.frames = Paths.getPackerAtlas(image);
|
|
|
|
default:
|
|
spr.frames = Paths.getSparrowAtlas(image);
|
|
}
|
|
}
|
|
|
|
public static function resetTextTag(tag:String) {
|
|
if(!PlayState.instance.modchartTexts.exists(tag)) {
|
|
return;
|
|
}
|
|
|
|
var target:FlxText = PlayState.instance.modchartTexts.get(tag);
|
|
target.kill();
|
|
PlayState.instance.remove(target, true);
|
|
target.destroy();
|
|
PlayState.instance.modchartTexts.remove(tag);
|
|
}
|
|
|
|
public static function resetSpriteTag(tag:String) {
|
|
#if LUA_ALLOWED
|
|
if(!PlayState.instance.modchartSprites.exists(tag)) {
|
|
return;
|
|
}
|
|
|
|
var target:ModchartSprite = PlayState.instance.modchartSprites.get(tag);
|
|
target.kill();
|
|
PlayState.instance.remove(target, true);
|
|
target.destroy();
|
|
PlayState.instance.modchartSprites.remove(tag);
|
|
#end
|
|
}
|
|
|
|
public static function cancelTween(tag:String) {
|
|
if(PlayState.instance.modchartTweens.exists(tag)) {
|
|
PlayState.instance.modchartTweens.get(tag).cancel();
|
|
PlayState.instance.modchartTweens.get(tag).destroy();
|
|
PlayState.instance.modchartTweens.remove(tag);
|
|
}
|
|
}
|
|
|
|
public static function cancelTimer(tag:String) {
|
|
if(PlayState.instance.modchartTimers.exists(tag)) {
|
|
var theTimer:FlxTimer = PlayState.instance.modchartTimers.get(tag);
|
|
theTimer.cancel();
|
|
theTimer.destroy();
|
|
PlayState.instance.modchartTimers.remove(tag);
|
|
}
|
|
}
|
|
|
|
public static function tweenPrepare(tag:String, vars:String) {
|
|
if(tag != null) cancelTween(tag);
|
|
var variables:Array<String> = vars.split('.');
|
|
var sexyProp:Dynamic = getObjectDirectly(variables[0]);
|
|
if(variables.length > 1) sexyProp = getVarInArray(getPropertyLoop(variables), variables[variables.length-1]);
|
|
return sexyProp;
|
|
}
|
|
|
|
public static function getBuildTarget():String
|
|
{
|
|
#if windows
|
|
return 'windows';
|
|
#elseif linux
|
|
return 'linux';
|
|
#elseif mac
|
|
return 'mac';
|
|
#elseif html5
|
|
return 'browser';
|
|
#elseif android
|
|
return 'android';
|
|
#elseif switch
|
|
return 'switch';
|
|
#else
|
|
return 'unknown';
|
|
#end
|
|
}
|
|
|
|
//buncho string stuffs
|
|
public static function getTweenTypeByString(?type:String = '') {
|
|
switch(type.toLowerCase().trim())
|
|
{
|
|
case 'backward': return FlxTweenType.BACKWARD;
|
|
case 'looping'|'loop': return FlxTweenType.LOOPING;
|
|
case 'persist': return FlxTweenType.PERSIST;
|
|
case 'pingpong': return FlxTweenType.PINGPONG;
|
|
}
|
|
return FlxTweenType.ONESHOT;
|
|
}
|
|
|
|
public static function getTweenEaseByString(?ease:String = '') {
|
|
switch(ease.toLowerCase().trim()) {
|
|
case 'backin': return FlxEase.backIn;
|
|
case 'backinout': return FlxEase.backInOut;
|
|
case 'backout': return FlxEase.backOut;
|
|
case 'bouncein': return FlxEase.bounceIn;
|
|
case 'bounceinout': return FlxEase.bounceInOut;
|
|
case 'bounceout': return FlxEase.bounceOut;
|
|
case 'circin': return FlxEase.circIn;
|
|
case 'circinout': return FlxEase.circInOut;
|
|
case 'circout': return FlxEase.circOut;
|
|
case 'cubein': return FlxEase.cubeIn;
|
|
case 'cubeinout': return FlxEase.cubeInOut;
|
|
case 'cubeout': return FlxEase.cubeOut;
|
|
case 'elasticin': return FlxEase.elasticIn;
|
|
case 'elasticinout': return FlxEase.elasticInOut;
|
|
case 'elasticout': return FlxEase.elasticOut;
|
|
case 'expoin': return FlxEase.expoIn;
|
|
case 'expoinout': return FlxEase.expoInOut;
|
|
case 'expoout': return FlxEase.expoOut;
|
|
case 'quadin': return FlxEase.quadIn;
|
|
case 'quadinout': return FlxEase.quadInOut;
|
|
case 'quadout': return FlxEase.quadOut;
|
|
case 'quartin': return FlxEase.quartIn;
|
|
case 'quartinout': return FlxEase.quartInOut;
|
|
case 'quartout': return FlxEase.quartOut;
|
|
case 'quintin': return FlxEase.quintIn;
|
|
case 'quintinout': return FlxEase.quintInOut;
|
|
case 'quintout': return FlxEase.quintOut;
|
|
case 'sinein': return FlxEase.sineIn;
|
|
case 'sineinout': return FlxEase.sineInOut;
|
|
case 'sineout': return FlxEase.sineOut;
|
|
case 'smoothstepin': return FlxEase.smoothStepIn;
|
|
case 'smoothstepinout': return FlxEase.smoothStepInOut;
|
|
case 'smoothstepout': return FlxEase.smoothStepOut;
|
|
case 'smootherstepin': return FlxEase.smootherStepIn;
|
|
case 'smootherstepinout': return FlxEase.smootherStepInOut;
|
|
case 'smootherstepout': return FlxEase.smootherStepOut;
|
|
}
|
|
return FlxEase.linear;
|
|
}
|
|
|
|
public static function blendModeFromString(blend:String):BlendMode {
|
|
switch(blend.toLowerCase().trim()) {
|
|
case 'add': return ADD;
|
|
case 'alpha': return ALPHA;
|
|
case 'darken': return DARKEN;
|
|
case 'difference': return DIFFERENCE;
|
|
case 'erase': return ERASE;
|
|
case 'hardlight': return HARDLIGHT;
|
|
case 'invert': return INVERT;
|
|
case 'layer': return LAYER;
|
|
case 'lighten': return LIGHTEN;
|
|
case 'multiply': return MULTIPLY;
|
|
case 'overlay': return OVERLAY;
|
|
case 'screen': return SCREEN;
|
|
case 'shader': return SHADER;
|
|
case 'subtract': return SUBTRACT;
|
|
}
|
|
return NORMAL;
|
|
}
|
|
|
|
public static function typeToString(type:Int):String {
|
|
#if LUA_ALLOWED
|
|
switch(type) {
|
|
case Lua.LUA_TBOOLEAN: return "boolean";
|
|
case Lua.LUA_TNUMBER: return "number";
|
|
case Lua.LUA_TSTRING: return "string";
|
|
case Lua.LUA_TTABLE: return "table";
|
|
case Lua.LUA_TFUNCTION: return "function";
|
|
}
|
|
if (type <= Lua.LUA_TNIL) return "nil";
|
|
#end
|
|
return "unknown";
|
|
}
|
|
|
|
public static function cameraFromString(cam:String):FlxCamera {
|
|
switch(cam.toLowerCase()) {
|
|
case 'camgame' | 'game': return PlayState.instance.camGame;
|
|
case 'camhud' | 'hud': return PlayState.instance.camHUD;
|
|
case 'camother' | 'other': return PlayState.instance.camOther;
|
|
}
|
|
var camera:Dynamic = PlayState.instance.variables.get(cam);
|
|
if (camera == null || !Std.isOfType(camera, FlxCamera)) camera = PlayState.instance.camGame;
|
|
return camera;
|
|
}
|
|
} |