From 6dc138bebaffcf318fefe87cc5ea721bc450f7a9 Mon Sep 17 00:00:00 2001 From: YoshiCrafter29 Date: Mon, 17 Oct 2022 15:28:35 +0200 Subject: [PATCH] optimisation --- source/funkin/game/NoteGroup.hx | 59 +++++++++++++++++++++++++++++++++ source/funkin/game/PlayState.hx | 25 ++++++-------- source/funkin/import.hx | 3 ++ 3 files changed, 72 insertions(+), 15 deletions(-) create mode 100644 source/funkin/game/NoteGroup.hx diff --git a/source/funkin/game/NoteGroup.hx b/source/funkin/game/NoteGroup.hx new file mode 100644 index 00000000..d57a85dc --- /dev/null +++ b/source/funkin/game/NoteGroup.hx @@ -0,0 +1,59 @@ +package funkin.game; + +import flixel.FlxCamera; +import funkin.system.Conductor; +import flixel.group.FlxGroup.FlxTypedGroup; + +class NoteGroup extends FlxTypedGroup { + var __loopSprite:Note; + var i:Int = 0; + public override function update(elapsed:Float) { + i = 0; + __loopSprite = null; + while(i < length) { + __loopSprite = members[i]; + if (__loopSprite == null || !__loopSprite.exists || !__loopSprite.active) continue; + if (__loopSprite.strumTime - Conductor.songPosition > 1500) + break; + __loopSprite.update(elapsed); + i++; + } + } + + public override function draw() { + @:privateAccess var oldDefaultCameras = FlxCamera._defaultCameras; + @:privateAccess if (cameras != null) FlxCamera._defaultCameras = cameras; + + i = 0; + __loopSprite = null; + + while(i < length) { + __loopSprite = members[i]; + if (__loopSprite == null || !__loopSprite.exists || !__loopSprite.visible) continue; + if (__loopSprite.strumTime - Conductor.songPosition > 1500) break; + __loopSprite.draw(); + i++; + } + + @:privateAccess FlxCamera._defaultCameras = oldDefaultCameras; + } + + public override function forEach(noteFunc:Note->Void, recursive:Bool = false) { + i = 0; + __loopSprite = null; + + while(i < length) { + __loopSprite = members[i]; + if (__loopSprite == null || !__loopSprite.exists) continue; + if (__loopSprite.strumTime - Conductor.songPosition > 1500) break; + noteFunc(__loopSprite); + i++; + } + } + public override function forEachAlive(noteFunc:Note->Void, recursive:Bool = false) { + forEach(function(note) { + if (note.alive) noteFunc(note); + }, recursive); + } + +} \ No newline at end of file diff --git a/source/funkin/game/PlayState.hx b/source/funkin/game/PlayState.hx index 86a44d16..79edac59 100644 --- a/source/funkin/game/PlayState.hx +++ b/source/funkin/game/PlayState.hx @@ -81,8 +81,7 @@ class PlayState extends MusicBeatState public var gf:Character; public var boyfriend:Character; - public var notes:FlxTypedGroup; - public var unspawnNotes:Array = []; + public var notes:NoteGroup; public var strumLine:FlxSprite; public var curSection:Int = 0; @@ -476,7 +475,7 @@ class PlayState extends MusicBeatState FlxG.sound.list.add(vocals); - notes = new FlxTypedGroup(); + notes = new NoteGroup(); add(notes); var noteData:Array; @@ -504,8 +503,8 @@ class PlayState extends MusicBeatState } var oldNote:Note; - if (unspawnNotes.length > 0) - oldNote = unspawnNotes[Std.int(unspawnNotes.length - 1)]; + if (notes.length > 0) + oldNote = notes.members[Std.int(notes.members.length - 1)]; else oldNote = null; @@ -516,16 +515,16 @@ class PlayState extends MusicBeatState var susLength:Float = swagNote.sustainLength; susLength = susLength / Conductor.stepCrochet; - unspawnNotes.push(swagNote); + notes.add(swagNote); if (susLength > 0.75) susLength++; for (susNote in 0...Math.floor(susLength)) { - oldNote = unspawnNotes[Std.int(unspawnNotes.length - 1)]; + oldNote = notes.members[Std.int(notes.length - 1)]; var sustainNote:Note = new Note(daStrumTime + (Conductor.stepCrochet * susNote), daNoteData, oldNote, true); sustainNote.scrollFactor.set(); - unspawnNotes.push(sustainNote); + notes.add(sustainNote); sustainNote.mustPress = gottaHitNote; @@ -545,11 +544,7 @@ class PlayState extends MusicBeatState } daBeats += 1; } - - // trace(unspawnNotes.length); - // playerCounter += 1; - - unspawnNotes.sort(sortByShit); + notes.sort(function(i, n1, n2) {return sortByShit(n1, n2);}); generatedMusic = true; } @@ -1004,8 +999,8 @@ class PlayState extends MusicBeatState #end } - while(unspawnNotes[0] != null && unspawnNotes[0].strumTime - Conductor.songPosition < 1500) - notes.add(unspawnNotes.shift()); + // while(unspawnNotes[0] != null && unspawnNotes[0].strumTime - Conductor.songPosition < 1500) + // notes.add(unspawnNotes.shift()); if (generatedMusic) diff --git a/source/funkin/import.hx b/source/funkin/import.hx index e183684b..f027a37e 100644 --- a/source/funkin/import.hx +++ b/source/funkin/import.hx @@ -4,4 +4,7 @@ import funkin.system.MusicBeatState; import funkin.system.MusicBeatSubstate; import funkin.system.CoolUtil; import funkin.game.PlayState; + +using StringTools; +using funkin.system.CoolUtil; #end \ No newline at end of file