Skip to content

Commit

Permalink
i tried optimizing something
Browse files Browse the repository at this point in the history
im trying to figure out how to improve the performance of note spawning, hitting and stuff
  • Loading branch information
JordanSantiagoYT committed Dec 28, 2024
1 parent 80f748b commit 673e431
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 101 deletions.
27 changes: 8 additions & 19 deletions source/Character.hx
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class Character extends FlxSprite
}
}

var anim:String;
override function update(elapsed:Float)
{
if (ClientPrefs.ffmpegMode) elapsed = 1 / ClientPrefs.targetFPS;
Expand All @@ -307,7 +308,7 @@ class Character extends FlxSprite
heyTimer -= elapsed * PlayState.instance.playbackRate;
if(heyTimer <= 0)
{
var anim:String = getAnimationName();
anim = getAnimationName();
if(specialAnim && (anim == 'hey' || anim == 'cheer'))
{
specialAnim = false;
Expand Down Expand Up @@ -364,9 +365,9 @@ class Character extends FlxSprite
}
}

var name:String = getAnimationName();
if(isAnimationFinished() && animOffsets.exists('$name-loop'))
playAnim('$name-loop');
anim = getAnimationName();
if(isAnimationFinished() && animOffsets.exists('$anim-loop'))
playAnim('$anim-loop');

super.update(elapsed);
}
Expand Down Expand Up @@ -442,6 +443,7 @@ class Character extends FlxSprite
}
}

var daOffset = null;
public function playAnim(AnimName:String, Force:Bool = false, Reversed:Bool = false, Frame:Int = 0):Void
{
specialAnim = false;
Expand All @@ -454,12 +456,11 @@ class Character extends FlxSprite
atlas.anim.play(AnimName, Force, Reversed, Frame);
atlas.update(0);
}

_lastPlayedAnimation = AnimName;

final daOffset = animOffsets.get(AnimName);
if (animOffsets.exists(AnimName))
if (hasAnimation(AnimName))
{
daOffset = animOffsets.get(AnimName);
offset.set(daOffset[0], daOffset[1]);
}
else
Expand All @@ -468,18 +469,12 @@ class Character extends FlxSprite
if (curCharacter.startsWith('gf'))
{
if (AnimName == 'singLEFT')
{
danced = true;
}
else if (AnimName == 'singRIGHT')
{
danced = false;
}

if (AnimName == 'singUP' || AnimName == 'singDOWN')
{
danced = !danced;
}
}
}

Expand Down Expand Up @@ -601,21 +596,15 @@ class Boyfriend extends Character
if (!debugMode && animation.curAnim != null)
{
if (animation.curAnim.name.startsWith('sing'))
{
holdTimer += elapsed;
}
else
holdTimer = 0;

if (animation.curAnim.name.endsWith('miss') && isAnimationFinished() && !debugMode)
{
playAnim('idle', true, false, 10);
}

if (animation.curAnim.name == 'firstDeath' && isAnimationFinished() && startedDeath)
{
playAnim('deathLoop');
}
}

super.update(elapsed);
Expand Down
53 changes: 23 additions & 30 deletions source/Note.hx
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,14 @@ class Note extends FlxSprite

var changeSize:Bool = false;
private function set_texture(value:String):String {
if (value.length == 0) value = Paths.defaultSkin;
if (!pixelNote && texture != value)
{
changeSize = false;
if (!Paths.noteSkinFramesMap.exists(value)) Paths.initNote(4, value);
try {
if (frames != @:privateAccess Paths.noteSkinFramesMap.get(value)) frames = @:privateAccess Paths.noteSkinFramesMap.get(value);
if (animation != @:privateAccess Paths.noteSkinAnimsMap.get(value)) inline animation.copyFrom(@:privateAccess Paths.noteSkinAnimsMap.get(value));
}
catch (e) {
frames = @:privateAccess Paths.noteSkinFramesMap.get(Paths.defaultSkin);
animation.copyFrom(@:privateAccess Paths.noteSkinAnimsMap.get(Paths.defaultSkin));
}
if (frames != @:privateAccess Paths.noteSkinFramesMap.get(value)) frames = @:privateAccess Paths.noteSkinFramesMap.get(value);
if (animation != @:privateAccess Paths.noteSkinAnimsMap.get(value)) animation.copyFrom(@:privateAccess Paths.noteSkinAnimsMap.get(value));

if (!changeSize)
{
changeSize = true;
Expand All @@ -195,15 +191,11 @@ class Note extends FlxSprite

public function defaultRGB()
{
var arr:Array<FlxColor> = ClientPrefs.arrowRGB[noteData];
if(pixelNote) arr = ClientPrefs.arrowRGBPixel[noteData];
noteColor = inline initializeGlobalRGBShader(noteData);

if (noteData > -1 && noteData <= arr.length)
{
rgbShader.r = arr[0];
rgbShader.g = arr[1];
rgbShader.b = arr[2];
}
rgbShader.r = noteColor.r;
rgbShader.g = noteColor.g;
rgbShader.b = noteColor.b;
}

private function set_noteType(value:String):String {
Expand All @@ -229,11 +221,9 @@ class Note extends FlxSprite
y -= 2000;
antialiasing = ClientPrefs.globalAntialiasing && !pixelNote;

if(noteData > -1) {
if (ClientPrefs.showNotes)
{
texture = Paths.defaultSkin;
}
if(inEditor) {
if (ClientPrefs.showNotes) texture = Paths.defaultSkin;

if (ClientPrefs.enableColorShader)
{
try{ rgbShader = new RGBShaderReference(this, initializeGlobalRGBShader(noteData, this)); }
Expand Down Expand Up @@ -521,13 +511,16 @@ class Note extends FlxSprite
_lastValidChecked = '';
}

var noteColor:RGBPalette;
var superCoolColor = null;
var arr:Array<Int> = [255, 255, 255];
public function updateRGBColors()
{
if (rgbShader == null && useRGBShader) rgbShader = new RGBShaderReference(this, initializeGlobalRGBShader(noteData, this));
else switch(ClientPrefs.noteColorStyle)
{
case 'Rainbow':
var superCoolColor = new FlxColor(0xFFFF0000);
superCoolColor = new FlxColor(0xFFFF0000);
superCoolColor.hue = (strumTime / 5000 * 360) % 360;
rgbShader.r = superCoolColor;
rgbShader.g = FlxColor.WHITE;
Expand All @@ -539,7 +532,7 @@ class Note extends FlxSprite
case 'Char-Based':
if (PlayState.instance != null)
{
var arr:Array<Int> = CoolUtil.getHealthColors(doOppStuff ? PlayState.instance.dad : PlayState.instance.boyfriend);
arr = CoolUtil.getHealthColors(doOppStuff ? PlayState.instance.dad : PlayState.instance.boyfriend);
if (gfNote) arr = CoolUtil.getHealthColors(PlayState.instance.gf);
if (noteData > -1)
{
Expand All @@ -552,7 +545,7 @@ class Note extends FlxSprite
else defaultRGB();

default:

}
if (noteType == 'Hurt Note' && rgbShader != null)
{
Expand Down Expand Up @@ -591,7 +584,7 @@ class Note extends FlxSprite
texture = chartNoteData.texture;
shouldCenterOffsets = false;
}
if ((chartNoteData.noteskin.length < 1 && chartNoteData.texture.length < 1) && chartNoteData.texture != Paths.defaultSkin)
if ((chartNoteData.noteskin.length < 1 && chartNoteData.texture.length < 1) && texture != Paths.defaultSkin)
{
texture = Paths.defaultSkin;
useRGBShader = ClientPrefs.enableColorShader;
Expand Down Expand Up @@ -627,7 +620,7 @@ class Note extends FlxSprite
if (ClientPrefs.enableColorShader && useRGBShader)
{
if (rgbShader == null) rgbShader = new RGBShaderReference(this, initializeGlobalRGBShader(noteData, this));
else updateRGBColors();
updateRGBColors();
}

if (noteType == 'Hurt Note' && !ClientPrefs.enableColorShader)
Expand All @@ -646,7 +639,7 @@ class Note extends FlxSprite
}
}

if (!PlayState.isPixelStage && !changeSize)
if (!changeSize && !PlayState.isPixelStage)
{
changeSize = true;
setGraphicSize(Std.int(width * 0.7));
Expand All @@ -663,7 +656,7 @@ class Note extends FlxSprite
else {
animation.play(colArray[noteData % 4] + 'Scroll');
if (!copyAngle) copyAngle = true;
offsetX = 0; //Juuuust in case we recycle a sustain note to a regular note
offsetX = 0; //Just in case we recycle a sustain note to a regular note
if (useRGBShader && shouldCenterOffsets)
{
centerOffsets();
Expand All @@ -676,12 +669,12 @@ class Note extends FlxSprite
if (!mustPress)
{
visible = ClientPrefs.opponentStrums;
alpha = ClientPrefs.middleScroll ? ClientPrefs.oppNoteAlpha : 1;
alpha = multAlpha = ClientPrefs.middleScroll ? ClientPrefs.oppNoteAlpha : 1;
}
else
{
if (!visible) visible = true;
if (alpha != 1) alpha = 1;
for (i in [alpha, multAlpha]) if (i != 1) i = 1;
}
if (flipY) flipY = false;
}
Expand Down
86 changes: 34 additions & 52 deletions source/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1610,9 +1610,8 @@ class PlayState extends MusicBeatState
Paths.music(Paths.formatToSongPath(ClientPrefs.pauseMusic));

if(cpuControlled && ClientPrefs.randomBotplayText && ClientPrefs.botTxtStyle != 'Hide' && botplayTxt != null && ffmpegInfo != 'Frame Time')
{
botplayTxt.text = theListBotplay[FlxG.random.int(0, theListBotplay.length - 1)];
}

if (botplayTxt != null) ogBotTxt = botplayTxt.text;

resetRPC();
Expand Down Expand Up @@ -2149,29 +2148,32 @@ class PlayState extends MusicBeatState
});
}

var fps:Float = 60;
var bfCanPan:Bool = false;
var dadCanPan:Bool = false;
var clear:Bool = false;
function camPanRoutine(anim:String = 'singUP', who:String = 'bf'):Void {
if (SONG.notes[curSection] != null)
{
var fps:Float = FlxG.updateFramerate;
final bfCanPan:Bool = SONG.notes[curSection].mustHitSection;
final dadCanPan:Bool = !SONG.notes[curSection].mustHitSection;
var clear:Bool = false;
switch (who) {
case 'bf' | 'boyfriend': clear = bfCanPan;
case 'oppt' | 'dad': clear = dadCanPan;
}
//FlxG.elapsed is stinky poo poo for this, it just makes it look jank as fuck
if (clear) {
if (fps == 0) fps = 1;
switch (anim.split('-')[0])
{
case 'singUP': moveCamTo[1] = -40*ClientPrefs.panIntensity*240*playbackRate/fps;
case 'singDOWN': moveCamTo[1] = 40*ClientPrefs.panIntensity*240*playbackRate/fps;
case 'singLEFT': moveCamTo[0] = -40*ClientPrefs.panIntensity*240*playbackRate/fps;
case 'singRIGHT': moveCamTo[0] = 40*ClientPrefs.panIntensity*240*playbackRate/fps;
fps = FlxG.updateFramerate;
bfCanPan = SONG.notes[curSection].mustHitSection;
dadCanPan = !SONG.notes[curSection].mustHitSection;
switch (who) {
case 'bf' | 'boyfriend': clear = bfCanPan;
case 'oppt' | 'dad': clear = dadCanPan;
}
//FlxG.elapsed is stinky poo poo for this, it just makes it look jank as fuck
if (clear) {
if (fps == 0) fps = 1;
switch (anim.split('-')[0])
{
case 'singUP': moveCamTo[1] = -40*ClientPrefs.panIntensity*240*playbackRate/fps;
case 'singDOWN': moveCamTo[1] = 40*ClientPrefs.panIntensity*240*playbackRate/fps;
case 'singLEFT': moveCamTo[0] = -40*ClientPrefs.panIntensity*240*playbackRate/fps;
case 'singRIGHT': moveCamTo[0] = 40*ClientPrefs.panIntensity*240*playbackRate/fps;
}
}
}
}
}

var startTimer:FlxTimer;
Expand Down Expand Up @@ -3835,6 +3837,7 @@ class PlayState extends MusicBeatState
spawnedNote = (unspawnNotes[notesAddedCount].isSustainNote ? sustainNotes : notes).recycle(Note);
spawnedNote.setupNoteData(unspawnNotes[notesAddedCount]);
}

if (!ClientPrefs.noSpawnFunc) callOnLuas('onSpawnNote', [(!unspawnNotes[notesAddedCount].isSustainNote ? notes.members.indexOf(notes.members[0]) : sustainNotes.members.indexOf(sustainNotes.members[0])), unspawnNotes[notesAddedCount].noteData, unspawnNotes[notesAddedCount].noteType, unspawnNotes[notesAddedCount].isSustainNote]);
notesAddedCount++;
}
Expand Down Expand Up @@ -4027,18 +4030,20 @@ class PlayState extends MusicBeatState
iconP2.updateHitbox();
}

var percent:Float = 0;
var center:Float = 0;
public dynamic function updateIconsPosition()
{
if (ClientPrefs.smoothHealth)
{
final percent:Float = 1 - (ClientPrefs.smoothHPBug ? (displayedHealth / maxHealth) : (FlxMath.bound(displayedHealth, 0, maxHealth) / maxHealth));
percent = 1 - (ClientPrefs.smoothHPBug ? (displayedHealth / maxHealth) : (FlxMath.bound(displayedHealth, 0, maxHealth) / maxHealth));

iconP1.x = 0 + healthBar.x + (healthBar.width * percent) + (150 * iconP1.scale.x - 150) / 2 - iconOffset;
iconP2.x = 0 + healthBar.x + (healthBar.width * percent) - (150 * iconP2.scale.x) / 2 - iconOffset * 2;
}
else //mb forgot to include this
{
final center:Float = healthBar.x + (healthBar.width * (FlxMath.remapToRange(healthBar.percent, 0, 100, 100, 0) * 0.01));
center = healthBar.x + (healthBar.width * (FlxMath.remapToRange(healthBar.percent, 0, 100, 100, 0) * 0.01));
iconP1.x = center + (150 * iconP1.scale.x - 150) / 2 - iconOffset;
iconP2.x = center - (150 * iconP2.scale.x) / 2 - iconOffset * 2;
}
Expand Down Expand Up @@ -5033,6 +5038,8 @@ class PlayState extends MusicBeatState
if (noteDiff > ClientPrefs.goodWindow && ClientPrefs.shitGivesMiss && ClientPrefs.ratingIntensity == 'Harsh') noteMiss(note);
if (noteDiff > ClientPrefs.sickWindow && ClientPrefs.shitGivesMiss && ClientPrefs.ratingIntensity == 'Very Harsh')noteMiss(note);
}

var separatedScore:Array<Dynamic> = [];
private function popUpScore(note:Note = null, ?miss:Bool = false):Void
{
popUpsFrame += 1;
Expand Down Expand Up @@ -5090,7 +5097,7 @@ class PlayState extends MusicBeatState
var tempCombo:Float = (combo > 0 ? combo : -combo);
var tempComboAlt:Float = tempCombo;

final separatedScore:Array<Dynamic> = [];
separatedScore = [];
while(tempCombo >= 10)
{
separatedScore.unshift(Math.ffloor(tempCombo / 10) % 10);
Expand Down Expand Up @@ -5562,36 +5569,11 @@ class PlayState extends MusicBeatState
{
//first, process whether or not the note should be hit. this prevents pointless strum following
if (!daNote.mustPress && !daNote.hitByOpponent && !daNote.ignoreNote && daNote.strumTime <= Conductor.songPosition)
{
if (!ClientPrefs.showcaseMode || ClientPrefs.charsAndBG) opponentNoteHit(daNote);
if (ClientPrefs.showcaseMode && !ClientPrefs.charsAndBG)
{
if (!daNote.isSustainNote) {
enemyHits += 1 * polyphony;
if (ClientPrefs.showNPS) {
oppNotesHitArray.push(1 * polyphony);
oppNotesHitDateArray.push(Conductor.songPosition);
}
}
invalidateNote(daNote);
}
}
opponentNoteHit(daNote);

if(daNote.mustPress) {
if((cpuControlled || usingBotEnergy && strumsHeld[daNote.noteData]) && daNote.strumTime <= Conductor.songPosition && !daNote.ignoreNote) {
if (!ClientPrefs.showcaseMode || ClientPrefs.charsAndBG) goodNoteHit(daNote);
if (ClientPrefs.showcaseMode && !ClientPrefs.charsAndBG)
{
if (!daNote.isSustainNote) {
totalNotesPlayed += 1 * polyphony;
if (ClientPrefs.showNPS) {
notesHitArray.push(1 * polyphony);
notesHitDateArray.push(Conductor.songPosition);
}
}
invalidateNote(daNote);
}
}
if((cpuControlled || usingBotEnergy && strumsHeld[daNote.noteData]) && daNote.strumTime <= Conductor.songPosition && !daNote.ignoreNote)
goodNoteHit(daNote);
}
if (!daNote.exists) return;

Expand Down Expand Up @@ -5860,7 +5842,7 @@ class PlayState extends MusicBeatState
if (daNote.animSuffix.length > 0 && oppChar.hasAnimation(animToPlay + daNote.animSuffix))
animToPlay = singAnimations[Std.int(Math.abs(daNote.noteData))] + daNote.animSuffix;

if (ClientPrefs.cameraPanning) inline camPanRoutine(animToPlay, (!opponentChart ? 'dad' : 'bf'));
if (ClientPrefs.cameraPanning) camPanRoutine(animToPlay, (!opponentChart ? 'dad' : 'bf'));

if (oppChar != null)
{
Expand Down

0 comments on commit 673e431

Please sign in to comment.