Skip to content

Commit

Permalink
preview different animations
Browse files Browse the repository at this point in the history
  • Loading branch information
lemz1 committed Nov 19, 2024
1 parent 01f20ba commit 30e4a05
Show file tree
Hide file tree
Showing 2 changed files with 171 additions and 152 deletions.
173 changes: 156 additions & 17 deletions source/funkin/ui/debug/char/components/dialogs/ResultsAnimDialog.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,184 @@ import haxe.ui.components.Button;
import funkin.data.freeplay.player.PlayerData;
import funkin.data.freeplay.player.PlayerRegistry;
import funkin.play.scoring.Scoring.ScoringRank;
import funkin.graphics.adobeanimate.FlxAtlasSprite;
import funkin.graphics.FunkinSprite;

@:build(haxe.ui.macros.ComponentMacros.build("assets/exclude/data/ui/char-creator/dialogs/results-anim-dialog.xml"))
@:access(funkin.ui.debug.char.pages.CharCreatorResultsPage)
class ResultsAnimDialog extends DefaultPageDialog
{
var rankAnimationDataMap:Map<ScoringRank, Array<PlayerResultsAnimationData>> = [
PERFECT_GOLD => [],
PERFECT => [],
EXCELLENT => [],
GREAT => [],
GOOD => [],
SHIT => [],
];
public var currentRank(get, never):ScoringRank;

public var characterAtlasAnimations(get, never):Array<
{
sprite:FlxAtlasSprite,
delay:Float,
forceLoop:Bool
}>;

public var characterSparrowAnimations(get, never):Array<
{
sprite:FunkinSprite,
delay:Float
}>;

var rankAnimationDataMap:Map<ScoringRank, Array<PlayerResultsAnimationData>> = [];

var characterAtlasAnimationsMap:Map<ScoringRank, Array<
{
sprite:FlxAtlasSprite,
delay:Float,
forceLoop:Bool
}>> = [];
var characterSparrowAnimationsMap:Map<ScoringRank, Array<
{
sprite:FunkinSprite,
delay:Float
}>> = [];

var rankAnimationBox:AddRankAnimationDataBox;

var currentRank(get, never):ScoringRank;
var previousRank:ScoringRank;

override public function new(daPage:CharCreatorResultsPage)
{
super(daPage);

if (daPage.data.importedPlayerData != null)
var charId = daPage.data.importedPlayerData ?? "";
var currentChar = PlayerRegistry.instance.fetchEntry(charId);
for (rank in [PERFECT_GOLD, PERFECT, EXCELLENT, GREAT, GOOD, SHIT])
{
var currentChar = PlayerRegistry.instance.fetchEntry(daPage.data.importedPlayerData);
for (rank in rankAnimationDataMap.keys())
{
var playerAnimations = currentChar?.getResultsAnimationDatas(rank) ?? [];
rankAnimationDataMap.set(rank, playerAnimations);
}
var playerAnimations = currentChar?.getResultsAnimationDatas(rank) ?? [];
rankAnimationDataMap.set(rank, playerAnimations);
createAnimations(rank, playerAnimations);
}

rankAnimationBox = new AddRankAnimationDataBox();
rankAnimationView.addComponent(rankAnimationBox);

rankDropdown.selectedIndex = 0;
rankDropdown.onChange = _ -> rankAnimationBox.useAnimationData(rankAnimationDataMap[currentRank]);
rankDropdown.onChange = function(_) {
if (previousRank == currentRank) return;

daPage.stopTimers();

for (atlas in characterAtlasAnimationsMap[previousRank])
atlas.sprite.visible = false;

for (sprite in characterSparrowAnimationsMap[previousRank])
sprite.sprite.visible = false;

rankAnimationBox.useAnimationData(rankAnimationDataMap[currentRank]);
previousRank = currentRank;
}

rankAnimationBox.useAnimationData(rankAnimationDataMap[currentRank]);
previousRank = currentRank;
}

function createAnimations(rank:ScoringRank, playerAnimations:Array<PlayerResultsAnimationData>):Void
{
var atlasAnimations = [];
var sparrowAnimations = [];
for (animData in playerAnimations)
{
if (animData == null) continue;

var animPath:String = Paths.stripLibrary(animData.assetPath);
var animLibrary:String = Paths.getLibrary(animData.assetPath);
var offsets = animData.offsets ?? [0, 0];
switch (animData.renderType)
{
case 'animateatlas':
var animation:FlxAtlasSprite = new FlxAtlasSprite(offsets[0], offsets[1], Paths.animateAtlas(animPath, animLibrary));
animation.zIndex = animData.zIndex ?? 500;

animation.scale.set(animData.scale ?? 1.0, animData.scale ?? 1.0);

if (!(animData.looped ?? true))
{
// Animation is not looped.
animation.onAnimationComplete.add((_name:String) -> {
trace("AHAHAH 2");
if (animation != null)
{
animation.anim.pause();
}
});
}
else if (animData.loopFrameLabel != null)
{
animation.onAnimationComplete.add((_name:String) -> {
trace("AHAHAH 2");
if (animation != null)
{
animation.playAnimation(animData.loopFrameLabel ?? '', true, false, true); // unpauses this anim, since it's on PlayOnce!
}
});
}
else if (animData.loopFrame != null)
{
animation.onAnimationComplete.add((_name:String) -> {
if (animation != null)
{
trace("AHAHAH");
animation.anim.curFrame = animData.loopFrame ?? 0;
animation.anim.play(); // unpauses this anim, since it's on PlayOnce!
}
});
}

// Hide until ready to play.
animation.visible = false;
// Queue to play.
atlasAnimations.push(
{
sprite: animation,
delay: animData.delay ?? 0.0,
forceLoop: (animData.loopFrame ?? -1) == 0
});
// Add to the scene.
this.page.add(animation);
case 'sparrow':
var animation:FunkinSprite = FunkinSprite.createSparrow(offsets[0], offsets[1], animPath);
animation.animation.addByPrefix('idle', '', 24, false, false, false);

if (animData.loopFrame != null)
{
animation.animation.finishCallback = (_name:String) -> {
if (animation != null)
{
animation.animation.play('idle', true, false, animData.loopFrame ?? 0);
}
}
}

// Hide until ready to play.
animation.visible = false;
// Queue to play.
sparrowAnimations.push(
{
sprite: animation,
delay: animData.delay ?? 0.0
});
// Add to the scene.
this.page.add(animation);
}
}

characterAtlasAnimationsMap.set(rank, atlasAnimations);
characterSparrowAnimationsMap.set(rank, sparrowAnimations);
}

function get_characterAtlasAnimations()
{
return characterAtlasAnimationsMap[currentRank];
}

function get_characterSparrowAnimations()
{
return characterSparrowAnimationsMap[currentRank];
}

function get_currentRank():ScoringRank
Expand Down
150 changes: 15 additions & 135 deletions source/funkin/ui/debug/char/pages/CharCreatorResultsPage.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ class CharCreatorResultsPage extends CharCreatorDefaultPage
{
var data:WizardGenerateParams;

var previewRank:ScoringRank = PERFECT_GOLD;

var dialogMap:Map<ResultsDialogType, DefaultPageDialog>;

override public function new(state:CharCreatorState, data:WizardGenerateParams)
Expand All @@ -42,7 +40,6 @@ class CharCreatorResultsPage extends CharCreatorDefaultPage
dialogMap.set(RankAnims, new ResultsAnimDialog(this));

initFunkinUI();
initAnimations();

refresh();
}
Expand All @@ -65,16 +62,13 @@ class CharCreatorResultsPage extends CharCreatorDefaultPage

if (FlxG.keys.justPressed.SPACE)
{
while (animTimers.length > 0)
{
var timer = animTimers.shift();
timer.cancel();
timer.destroy();
}
stopTimers();

refresh(); // just to be sure

for (atlas in characterAtlasAnimations[previewRank])
var animDialog:ResultsAnimDialog = cast dialogMap[RankAnims];

for (atlas in animDialog.characterAtlasAnimations)
{
atlas.sprite.visible = false;
animTimers.push(new FlxTimer().start(atlas.delay, _ -> {
Expand All @@ -84,7 +78,7 @@ class CharCreatorResultsPage extends CharCreatorDefaultPage
}));
}

for (sprite in characterSparrowAnimations[previewRank])
for (sprite in animDialog.characterSparrowAnimations)
{
sprite.sprite.visible = false;
animTimers.push(new FlxTimer().start(sprite.delay, _ -> {
Expand All @@ -96,6 +90,16 @@ class CharCreatorResultsPage extends CharCreatorDefaultPage
}
}

public function stopTimers():Void
{
while (animTimers.length > 0)
{
var timer = animTimers.shift();
timer.cancel();
timer.destroy();
}
}

var difficulty:FlxSprite;
var songName:FlxBitmapText;
var clearPercentSmall:ClearPercentCounter;
Expand Down Expand Up @@ -199,130 +203,6 @@ class CharCreatorResultsPage extends CharCreatorDefaultPage
add(score);
}

var characterAtlasAnimations:Map<ScoringRank, Array<
{
sprite:FlxAtlasSprite,
delay:Float,
forceLoop:Bool
}>> = [];
var characterSparrowAnimations:Map<ScoringRank, Array<
{
sprite:FunkinSprite,
delay:Float
}>> = [];

function initAnimations():Void
{
if (data.importedPlayerData != null)
{
var charId = data.importedPlayerData;
createAnimationsForRank(charId, PERFECT_GOLD);
createAnimationsForRank(charId, PERFECT);
createAnimationsForRank(charId, EXCELLENT);
createAnimationsForRank(charId, GREAT);
createAnimationsForRank(charId, GOOD);
createAnimationsForRank(charId, SHIT);
}
else {}
}

function createAnimationsForRank(charId:String, rank:ScoringRank):Void
{
var currentChar = PlayerRegistry.instance.fetchEntry(charId);
var playerAnimations = currentChar?.getResultsAnimationDatas(rank) ?? [];

var atlasAnimations = [];
var sparrowAnimations = [];
for (animData in playerAnimations)
{
if (animData == null) continue;

var animPath:String = Paths.stripLibrary(animData.assetPath);
var animLibrary:String = Paths.getLibrary(animData.assetPath);
var offsets = animData.offsets ?? [0, 0];
switch (animData.renderType)
{
case 'animateatlas':
var animation:FlxAtlasSprite = new FlxAtlasSprite(offsets[0], offsets[1], Paths.animateAtlas(animPath, animLibrary));
animation.zIndex = animData.zIndex ?? 500;

animation.scale.set(animData.scale ?? 1.0, animData.scale ?? 1.0);

if (!(animData.looped ?? true))
{
// Animation is not looped.
animation.onAnimationComplete.add((_name:String) -> {
trace("AHAHAH 2");
if (animation != null)
{
animation.anim.pause();
}
});
}
else if (animData.loopFrameLabel != null)
{
animation.onAnimationComplete.add((_name:String) -> {
trace("AHAHAH 2");
if (animation != null)
{
animation.playAnimation(animData.loopFrameLabel ?? '', true, false, true); // unpauses this anim, since it's on PlayOnce!
}
});
}
else if (animData.loopFrame != null)
{
animation.onAnimationComplete.add((_name:String) -> {
if (animation != null)
{
trace("AHAHAH");
animation.anim.curFrame = animData.loopFrame ?? 0;
animation.anim.play(); // unpauses this anim, since it's on PlayOnce!
}
});
}

// Hide until ready to play.
animation.visible = false;
// Queue to play.
atlasAnimations.push(
{
sprite: animation,
delay: animData.delay ?? 0.0,
forceLoop: (animData.loopFrame ?? -1) == 0
});
// Add to the scene.
add(animation);
case 'sparrow':
var animation:FunkinSprite = FunkinSprite.createSparrow(offsets[0], offsets[1], animPath);
animation.animation.addByPrefix('idle', '', 24, false, false, false);

if (animData.loopFrame != null)
{
animation.animation.finishCallback = (_name:String) -> {
if (animation != null)
{
animation.animation.play('idle', true, false, animData.loopFrame ?? 0);
}
}
}

// Hide until ready to play.
animation.visible = false;
// Queue to play.
sparrowAnimations.push(
{
sprite: animation,
delay: animData.delay ?? 0.0
});
// Add to the scene.
add(animation);
}
}

characterAtlasAnimations.set(rank, atlasAnimations);
characterSparrowAnimations.set(rank, sparrowAnimations);
}

function refresh():Void
{
sort(SortUtil.byZIndex, FlxSort.ASCENDING);
Expand Down

0 comments on commit 30e4a05

Please sign in to comment.