From 5891bb7f1649358e1c3dde674664e0a99c7ea658 Mon Sep 17 00:00:00 2001 From: MtH Date: Thu, 6 May 2021 14:08:51 +0200 Subject: [PATCH 1/2] custom camera for lerp fix --- source/GameOverSubstate.hx | 4 +- source/MainMenuState.hx | 3 +- source/PlayState.hx | 4 +- source/SwagCamera.hx | 101 +++++++++++++++++++++++++++++++++++ source/ui/PreferencesMenu.hx | 4 +- 5 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 source/SwagCamera.hx diff --git a/source/GameOverSubstate.hx b/source/GameOverSubstate.hx index 9bf0c466a..dfcf02f63 100644 --- a/source/GameOverSubstate.hx +++ b/source/GameOverSubstate.hx @@ -71,7 +71,7 @@ class GameOverSubstate extends MusicBeatSubstate override function update(elapsed:Float) { // makes the lerp non-dependant on the framerate - FlxG.camera.followLerp = CoolUtil.camLerpShit(0.01); + // FlxG.camera.followLerp = CoolUtil.camLerpShit(0.01); super.update(elapsed); @@ -99,7 +99,7 @@ class GameOverSubstate extends MusicBeatSubstate if (bf.animation.curAnim.name == 'firstDeath' && bf.animation.curAnim.curFrame == 12) { - FlxG.camera.follow(camFollow, LOCKON, CoolUtil.camLerpShit(0.01)); + FlxG.camera.follow(camFollow, LOCKON, 0.01); } switch (PlayState.storyWeek) diff --git a/source/MainMenuState.hx b/source/MainMenuState.hx index 1bb8747bc..ef4c4dd05 100644 --- a/source/MainMenuState.hx +++ b/source/MainMenuState.hx @@ -120,6 +120,7 @@ class MainMenuState extends MusicBeatState menuItem.y = top + spacing * i; } + FlxG.cameras.reset(new SwagCamera()); FlxG.camera.follow(camFollow, null, 0.06); // FlxG.camera.setScrollBounds(bg.x, bg.x + bg.width, bg.y, bg.y + bg.height * 1.2); @@ -249,7 +250,7 @@ class MainMenuState extends MusicBeatState override function update(elapsed:Float) { - FlxG.camera.followLerp = CoolUtil.camLerpShit(0.06); + // FlxG.camera.followLerp = CoolUtil.camLerpShit(0.06); if (FlxG.sound.music.volume < 0.8) { diff --git a/source/PlayState.hx b/source/PlayState.hx index a84619e9a..522755b00 100644 --- a/source/PlayState.hx +++ b/source/PlayState.hx @@ -169,7 +169,7 @@ class PlayState extends MusicBeatState FlxG.sound.cache(Paths.voices(PlayState.SONG.song)); // var gameCam:FlxCamera = FlxG.camera; - camGame = new FlxCamera(); + camGame = new SwagCamera(); camHUD = new FlxCamera(); camHUD.bgColor.alpha = 0; @@ -1860,7 +1860,7 @@ class PlayState extends MusicBeatState override public function update(elapsed:Float) { // makes the lerp non-dependant on the framerate - FlxG.camera.followLerp = CoolUtil.camLerpShit(0.04); + // FlxG.camera.followLerp = CoolUtil.camLerpShit(0.04); #if !debug perfectMode = false; diff --git a/source/SwagCamera.hx b/source/SwagCamera.hx new file mode 100644 index 000000000..826954baf --- /dev/null +++ b/source/SwagCamera.hx @@ -0,0 +1,101 @@ +package; + +import flixel.FlxCamera; +import flixel.FlxG; +import flixel.FlxSprite; +import flixel.math.FlxPoint; + +class SwagCamera extends FlxCamera +{ + /** + * properly follow framerate + * most of this just copied from FlxCamera, + * only lines 96 and 97 are changed + */ + override public function updateFollow():Void + { + // Either follow the object closely, + // or double check our deadzone and update accordingly. + if (deadzone == null) + { + target.getMidpoint(_point); + _point.addPoint(targetOffset); + focusOn(_point); + } + else + { + var edge:Float; + var targetX:Float = target.x + targetOffset.x; + var targetY:Float = target.y + targetOffset.y; + + if (style == SCREEN_BY_SCREEN) + { + if (targetX >= (scroll.x + width)) + { + _scrollTarget.x += width; + } + else if (targetX < scroll.x) + { + _scrollTarget.x -= width; + } + + if (targetY >= (scroll.y + height)) + { + _scrollTarget.y += height; + } + else if (targetY < scroll.y) + { + _scrollTarget.y -= height; + } + } + else + { + edge = targetX - deadzone.x; + if (_scrollTarget.x > edge) + { + _scrollTarget.x = edge; + } + edge = targetX + target.width - deadzone.x - deadzone.width; + if (_scrollTarget.x < edge) + { + _scrollTarget.x = edge; + } + + edge = targetY - deadzone.y; + if (_scrollTarget.y > edge) + { + _scrollTarget.y = edge; + } + edge = targetY + target.height - deadzone.y - deadzone.height; + if (_scrollTarget.y < edge) + { + _scrollTarget.y = edge; + } + } + + if ((target is FlxSprite)) + { + if (_lastTargetPosition == null) + { + _lastTargetPosition = FlxPoint.get(target.x, target.y); // Creates this point. + } + _scrollTarget.x += (target.x - _lastTargetPosition.x) * followLead.x; + _scrollTarget.y += (target.y - _lastTargetPosition.y) * followLead.y; + + _lastTargetPosition.x = target.x; + _lastTargetPosition.y = target.y; + } + + if (followLerp >= 60 / FlxG.updateFramerate) + { + scroll.copyFrom(_scrollTarget); // no easing + } + else + { + // THIS THE PART THAT ACTUALLY MATTERS LOL + scroll.x += (_scrollTarget.x - scroll.x) * CoolUtil.camLerpShit(followLerp); + scroll.y += (_scrollTarget.y - scroll.y) * CoolUtil.camLerpShit(followLerp); + } + } + } +} \ No newline at end of file diff --git a/source/ui/PreferencesMenu.hx b/source/ui/PreferencesMenu.hx index 2e6e5a313..0462184a3 100644 --- a/source/ui/PreferencesMenu.hx +++ b/source/ui/PreferencesMenu.hx @@ -23,7 +23,7 @@ class PreferencesMenu extends ui.OptionsState.Page { super(); - menuCamera = new FlxCamera(); + menuCamera = new SwagCamera(); FlxG.cameras.add(menuCamera, false); menuCamera.bgColor = 0x0; camera = menuCamera; @@ -148,7 +148,7 @@ class PreferencesMenu extends ui.OptionsState.Page { super.update(elapsed); - menuCamera.followLerp = CoolUtil.camLerpShit(0.05); + // menuCamera.followLerp = CoolUtil.camLerpShit(0.05); items.forEach(function(daItem:TextMenuItem) { From 123a7ae59b2b928006b8e11755c2e0a40a2ba8a4 Mon Sep 17 00:00:00 2001 From: MtH Date: Sat, 15 May 2021 23:39:13 +0200 Subject: [PATCH 2/2] boopin boppers.. head lerp --- source/CoolUtil.hx | 6 +++--- source/PlayState.hx | 4 ++-- source/SwagCamera.hx | 6 ++++-- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/source/CoolUtil.hx b/source/CoolUtil.hx index 1d5beeff9..4bbcd6bd3 100644 --- a/source/CoolUtil.hx +++ b/source/CoolUtil.hx @@ -57,10 +57,10 @@ class CoolUtil } /* - * just lerp that does camLerpShit for u so u dont have to do it every time + * frame dependant lerp kinda lol */ - public static function coolLerp(a:Float, b:Float, ratio:Float):Float + public static function coolLerp(base:Float, target:Float, ratio:Float):Float { - return FlxMath.lerp(a, b, camLerpShit(ratio)); + return base + camLerpShit(ratio) * (target - base); } } \ No newline at end of file diff --git a/source/PlayState.hx b/source/PlayState.hx index 522755b00..ee63736c1 100644 --- a/source/PlayState.hx +++ b/source/PlayState.hx @@ -1964,8 +1964,8 @@ class PlayState extends MusicBeatState // FlxG.watch.addQuick('VOL', vocals.amplitudeLeft); // FlxG.watch.addQuick('VOLRight', vocals.amplitudeRight); - iconP1.setGraphicSize(Std.int(FlxMath.lerp(150, iconP1.width, 0.85))); - iconP2.setGraphicSize(Std.int(FlxMath.lerp(150, iconP2.width, 0.85))); + iconP1.setGraphicSize(Std.int(CoolUtil.coolLerp(iconP1.width, 150, 0.15))); + iconP2.setGraphicSize(Std.int(CoolUtil.coolLerp(iconP2.width, 150, 0.15))); iconP1.updateHitbox(); iconP2.updateHitbox(); diff --git a/source/SwagCamera.hx b/source/SwagCamera.hx index 826954baf..34efffa9e 100644 --- a/source/SwagCamera.hx +++ b/source/SwagCamera.hx @@ -93,8 +93,10 @@ class SwagCamera extends FlxCamera else { // THIS THE PART THAT ACTUALLY MATTERS LOL - scroll.x += (_scrollTarget.x - scroll.x) * CoolUtil.camLerpShit(followLerp); - scroll.y += (_scrollTarget.y - scroll.y) * CoolUtil.camLerpShit(followLerp); + scroll.x = CoolUtil.coolLerp(scroll.x, _scrollTarget.x, followLerp); + scroll.y = CoolUtil.coolLerp(scroll.y, _scrollTarget.y, followLerp); + // scroll.x += (_scrollTarget.x - scroll.x) * CoolUtil.camLerpShit(followLerp); + // scroll.y += (_scrollTarget.y - scroll.y) * CoolUtil.camLerpShit(followLerp); } } }