Skip to content

Commit

Permalink
add ShakeModifier
Browse files Browse the repository at this point in the history
Suggested by bluecolorsin!
  • Loading branch information
MaybeMaru committed Jun 24, 2024
1 parent ad30826 commit 53a1249
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 65 deletions.
4 changes: 3 additions & 1 deletion source/funkin/util/frontend/ModchartManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ enum abstract Modifiers(String) from String to String {
var BEAT = "BEAT";
var REVERSE = "REVERSE";
var SWAP = "SWAP";
var SHAKE = "SHAKE";
}

class ModchartManager extends EventHandler
Expand All @@ -28,7 +29,8 @@ class ModchartManager extends EventHandler
TIPSY => () -> return new TipsyModifier(),
BEAT => () -> return new BeatModifier(),
REVERSE => () -> return new ReverseModifier(),
SWAP => () -> return new SwapModifier()
SWAP => () -> return new SwapModifier(),
SHAKE => () -> return new ShakeModifier()
];

public var strumLines:Map<Int, StrumLineGroup> = [];
Expand Down
40 changes: 40 additions & 0 deletions source/funkin/util/frontend/modifiers/ShakeModifier.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package funkin.util.frontend.modifiers;

import funkin.util.frontend.ModchartManager.Modifiers;

class ShakeModifier extends BasicModifier
{
public function new() {
super(SHAKE, false);
}

// Force shake running at 24 fps
var timeElapsed:Float = 0;

var shakeX:Float = 0;
var shakeY:Float = 0;

override function manageStrumUpdate(strum:NoteStrum, elapsed:Float, beat:Float) {
var percent:Float = data[0];
if (FunkMath.isZero(percent))
return;

var speed:Float = data[1];
timeElapsed += (elapsed * speed);

while (timeElapsed > (1 / 24)) {
timeElapsed -= (1 / 24);

shakeX = FlxG.random.float(-1, 1) * scaleWidth(percent);
shakeY = FlxG.random.float(-1, 1) * scaleHeight(percent);
}

strum.xModchart += shakeX;
strum.yModchart += shakeY;
}

// [percent]
override function getDefaultValues() {
return [0.1, 1];
}
}
94 changes: 30 additions & 64 deletions source/funkin/util/modding/FunkScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package funkin.util.modding;
import funkin.objects.NotesGroup;
import hscript.Script;
import flixel.util.FlxDestroyUtil.IFlxDestroyable;
import funkin.util.frontend.*;

enum abstract HscriptFunctionCallback(Bool) {
var CONTINUE_FUNCTION = true;
Expand Down Expand Up @@ -168,7 +169,7 @@ class FunkScript extends hscript.Script implements IFlxDestroyable
//HScript Functions

// DEPRECATED
set('importLib', function(classStr:String, packageStr:String = '', ?customName:String):Void {
set('importLib', (classStr:String, packageStr:String = '', ?customName:String) -> {
if(packageStr != '') packageStr += '.';
if (customName != null && !exists(customName)) {
warningPrint('importLib() is deprecated, use ``import ... as`` instead');
Expand All @@ -179,50 +180,30 @@ class FunkScript extends hscript.Script implements IFlxDestroyable
set(classStr, Type.resolveClass(packageStr + classStr));
});

set('closeScript', function () {
set('closeScript', () -> {
FlxG.signals.preUpdate.addOnce(() -> ModdingUtil.removeScript(this));
});

set('getBlendMode', function(blendType:String):openfl.display.BlendMode {
return ScriptUtil.stringToBlend(blendType);
});

set('parseJson', function (value:String):Dynamic {
return Json.parse(value);
});
set('getBlendMode', ScriptUtil.stringToBlend);
set('getEase', ScriptUtil.stringToEase);

set('stringifyJson', function (value:Dynamic, pretty:Bool = true):String {
set('parseJson', Json.parse);
set('stringifyJson', (value:Dynamic, pretty:Bool = true) -> {
return FunkyJson.stringify(value, pretty ? "\t" : null);
});

set('getPref', function(pref:String):Dynamic {
return Preferences.getPref(pref);
});

set('getKey', function(key:String):Bool {
return Controls.getKeyOld(key);
});
set('getPref', Preferences.getPref);
set('getKey', Controls.getKeyOld);

set('getSound', function (key:String):FlxSound {
return CoolUtil.getSound(key);
});

set('playSound', function (key:String, volume:Float = 1) {
CoolUtil.playSound(key, volume);
});

// Sounds stuff
set('getSound', CoolUtil.getSound);
set('playSound', CoolUtil.playSound);
set('pauseSounds', CoolUtil.pauseSounds);

// Needs pauseSounds() first
set('resumeSounds', CoolUtil.resumeSounds);

set('makeCutsceneManager', function (?targetSound:FlxSound) {
return funkin.util.frontend.CutsceneManager.makeManager(targetSound);
});

// Kinda useable, still a wip
set('makeModchartManager', function () {
final manager = funkin.util.frontend.ModchartManager.makeManager();
set('makeCutsceneManager', CutsceneManager.makeManager);
set('makeModchartManager', () -> {
final manager = ModchartManager.makeManager();
final instance = PlayState.instance;
if (instance != null) {
manager.setStrumLine(0, instance.notesGroup.opponentStrums);
Expand All @@ -231,7 +212,7 @@ class FunkScript extends hscript.Script implements IFlxDestroyable
return manager;
});

set('setObjMap', function(object:FlxObject, key:String):Void {
set('setObjMap', (object:FlxObject, key:String) -> {
ScriptUtil.objects.set(key, object);
});

Expand All @@ -250,16 +231,10 @@ class FunkScript extends hscript.Script implements IFlxDestroyable
set('getLayer', ScriptUtil.getLayer);
set('existsLayer', ScriptUtil.existsLayer);

set('cacheCharacter', function(name:String):Character {
final cachedChar = new Character(0, 0, name);
return cachedChar;
});

set('cacheImage', function(image:FlxGraphicAsset, ?library:String):FlxGraphicAsset {
return CoolUtil.cacheImage(image, library);
});
set('cacheImage', CoolUtil.cacheImage);
set('cacheCharacter', (name:String) -> return new Character(0, 0, name));

set('runEvent', function(name:String, ?values:Array<Dynamic>):Bool {
set('runEvent', (name:String, ?values:Array<Dynamic>) -> {
if (name == "runCode") // why would you-
return false;

Expand All @@ -279,50 +254,43 @@ class FunkScript extends hscript.Script implements IFlxDestroyable

// Script functions

set('addScript', function(path:String, ?tag:String):Null<FunkScript> {
return ModdingUtil.addScript(path, tag);
});

set('removeScript', function(tag:String):Void {
ModdingUtil.removeScriptByTag(tag);
});
set('addScript', ModdingUtil.addScript);
set('removeScript', ModdingUtil.removeScriptByTag);

set('getScriptVar', function(script:String, key:String):Dynamic {
set('getScriptVar', (script:String, key:String) -> {
var script = ModdingUtil.scriptsMap.get(script);
if (script.exists(key)) {
return script.get(key);
}
return null;
});

set('callScriptsFunction', function(func:String, ?args:Array<Dynamic>) {
ModdingUtil.addCall(func, args);
});
set('callScriptsFunction', ModdingUtil.addCall);

set('callScriptFunction', function(script:String, method:String, ?args:Array<Dynamic>):Dynamic {
set('callScriptFunction', (script:String, method:String, ?args:Array<Dynamic>) -> {
var script = ModdingUtil.scriptsMap.get(script);
if (script != null)
return script.safeCall(method, args);

return CONTINUE_FUNCTION;
});

set('addGlobalVar', function(key:String, _var:Dynamic, forced:Bool = false) {
set('addGlobalVar', (key:String, _var:Dynamic, forced:Bool = false) -> {
ModdingUtil.scripts.fastForEach((script, i) -> {
if (forced || !script.exists(key))
script.set(key, _var);
});
});

set('setGlobalVar', function (key:String, _var:Dynamic) {
globalVariables.set(key, _var);
set('setGlobalVar', (key:String, value:Dynamic) -> {
globalVariables.set(key, value);
});

set('existsGlobalVar', function (key:String) {
set('existsGlobalVar', (key:String) -> {
return globalVariables.exists(key);
});

set('getGlobalVar', function (key:String) {
set('getGlobalVar', (key:String) -> {
if (globalVariables.exists(key)) return globalVariables.get(key);
else {
warningPrint('Variable not found: $key');
Expand Down Expand Up @@ -374,9 +342,7 @@ class FunkScript extends hscript.Script implements IFlxDestroyable

// Custom state

set('switchCustomState', function (key:String, skipTransOpen:Bool = false, ?skipTransClose:Bool) {
ScriptUtil.switchCustomState(key, skipTransOpen, skipTransClose);
});
set('switchCustomState', ScriptUtil.switchCustomState);
}
}

Expand Down

0 comments on commit 53a1249

Please sign in to comment.