Merge pull request #286 from FunkinCrew/bugfix/story-menu-animations

Reuse LevelProps (this fixes animations when switching weeks)
This commit is contained in:
Cameron Taylor 2024-01-16 06:27:18 -05:00 committed by GitHub
commit 6564aba2c1
3 changed files with 58 additions and 30 deletions

View file

@ -180,9 +180,9 @@ class Level implements IRegistryEntry<LevelData>
return difficulties; return difficulties;
} }
public function buildProps():Array<LevelProp> public function buildProps(?existingProps:Array<LevelProp>):Array<LevelProp>
{ {
var props:Array<LevelProp> = []; var props:Array<LevelProp> = existingProps == null ? [] : [for (x in existingProps) x];
if (_data.props.length == 0) return props; if (_data.props.length == 0) return props;
@ -190,11 +190,22 @@ class Level implements IRegistryEntry<LevelData>
{ {
var propData = _data.props[propIndex]; var propData = _data.props[propIndex];
var propSprite:Null<LevelProp> = LevelProp.build(propData); // Attempt to reuse the `LevelProp` object.
if (propSprite == null) continue; // This prevents animations from resetting.
var existingProp:Null<LevelProp> = props[propIndex];
if (existingProp != null)
{
existingProp.propData = propData;
existingProp.x = propData.offsets[0] + FlxG.width * 0.25 * propIndex;
}
else
{
var propSprite:Null<LevelProp> = LevelProp.build(propData);
if (propSprite == null) continue;
propSprite.x += FlxG.width * 0.25 * propIndex; propSprite.x = propData.offsets[0] + FlxG.width * 0.25 * propIndex;
props.push(propSprite); props.push(propSprite);
}
} }
return props; return props;

View file

@ -6,9 +6,26 @@ import funkin.data.level.LevelData;
class LevelProp extends Bopper class LevelProp extends Bopper
{ {
public function new(danceEvery:Int) public var propData(default, set):Null<LevelPropData> = null;
function set_propData(value:LevelPropData):LevelPropData
{ {
super(danceEvery); // Only reset the prop if the asset path has changed.
if (propData == null || value.assetPath != this.propData.assetPath)
{
this.visible = (value != null);
this.propData = value;
danceEvery = this.propData.danceEvery;
applyData();
}
return this.propData;
}
public function new(propData:LevelPropData)
{
super(propData.danceEvery);
this.propData = propData;
} }
public function playConfirm():Void public function playConfirm():Void
@ -16,50 +33,51 @@ class LevelProp extends Bopper
playAnimation('confirm', true, true); playAnimation('confirm', true, true);
} }
public static function build(propData:Null<LevelPropData>):Null<LevelProp> function applyData():Void
{ {
if (propData == null) return null;
var isAnimated:Bool = propData.animations.length > 0; var isAnimated:Bool = propData.animations.length > 0;
var prop:LevelProp = new LevelProp(propData.danceEvery);
if (isAnimated) if (isAnimated)
{ {
// Initalize sprite frames. // Initalize sprite frames.
// Sparrow atlas only LEL. // Sparrow atlas only LEL.
prop.frames = Paths.getSparrowAtlas(propData.assetPath); this.frames = Paths.getSparrowAtlas(propData.assetPath);
} }
else else
{ {
// Initalize static sprite. // Initalize static sprite.
prop.loadGraphic(Paths.image(propData.assetPath)); this.loadGraphic(Paths.image(propData.assetPath));
// Disables calls to update() for a performance boost. // Disables calls to update() for a performance boost.
prop.active = false; this.active = false;
} }
if (prop.frames == null || prop.frames.numFrames == 0) if (this.frames == null || this.frames.numFrames == 0)
{ {
trace('ERROR: Could not build texture for level prop (${propData.assetPath}).'); trace('ERROR: Could not build texture for level prop (${propData.assetPath}).');
return null; return;
} }
var scale:Float = propData.scale * (propData.isPixel ? 6 : 1); var scale:Float = propData.scale * (propData.isPixel ? 6 : 1);
prop.scale.set(scale, scale); this.scale.set(scale, scale);
prop.antialiasing = !propData.isPixel; this.antialiasing = !propData.isPixel;
prop.alpha = propData.alpha; this.alpha = propData.alpha;
prop.x = propData.offsets[0]; this.x = propData.offsets[0];
prop.y = propData.offsets[1]; this.y = propData.offsets[1];
FlxAnimationUtil.addAtlasAnimations(prop, propData.animations); FlxAnimationUtil.addAtlasAnimations(this, propData.animations);
for (propAnim in propData.animations) for (propAnim in propData.animations)
{ {
prop.setAnimationOffsets(propAnim.name, propAnim.offsets[0], propAnim.offsets[1]); this.setAnimationOffsets(propAnim.name, propAnim.offsets[0], propAnim.offsets[1]);
} }
prop.dance(); this.dance();
prop.animation.paused = true; this.animation.paused = true;
}
return prop; public static function build(propData:Null<LevelPropData>):Null<LevelProp>
{
if (propData == null) return null;
return new LevelProp(propData);
} }
} }

View file

@ -636,8 +636,7 @@ class StoryMenuState extends MusicBeatState
function updateProps():Void function updateProps():Void
{ {
levelProps.clear(); for (prop in currentLevel.buildProps(levelProps.members))
for (prop in currentLevel.buildProps())
{ {
prop.zIndex = 1000; prop.zIndex = 1000;
levelProps.add(prop); levelProps.add(prop);