1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-11-16 03:43:36 +00:00
Funkin/source/funkin/play/character/MultiSparrowCharacter.hx

133 lines
3.7 KiB
Haxe
Raw Normal View History

package funkin.play.character;
import flixel.graphics.frames.FlxAtlasFrames;
import flixel.graphics.frames.FlxFramesCollection;
import funkin.modding.events.ScriptEvent;
import funkin.util.assets.FlxAnimationUtil;
import funkin.play.character.CharacterData.CharacterRenderType;
/**
* For some characters which use Sparrow atlases, the spritesheets need to be split
* into multiple files. This character renderer concatenates these together into a single sprite.
2023-06-08 20:30:45 +00:00
*
* Examples in base game include BF Holding GF (most of the sprites are in one file
* but the death animation is in a separate file).
* Only example I can think of in mods is Tricky (which has a separate file for each animation).
2023-06-08 20:30:45 +00:00
*
* BaseCharacter has game logic, MultiSparrowCharacter has only rendering logic.
* KEEP THEM SEPARATE!
*/
class MultiSparrowCharacter extends BaseCharacter
{
2023-01-23 00:55:30 +00:00
public function new(id:String)
{
super(id, CharacterRenderType.MultiSparrow);
2023-01-23 00:55:30 +00:00
}
override function onCreate(event:ScriptEvent):Void
{
trace('Creating Multi-Sparrow character: ' + this.characterId);
buildSprites();
super.onCreate(event);
}
function buildSprites():Void
2023-01-23 00:55:30 +00:00
{
buildSpritesheet();
2023-01-23 00:55:30 +00:00
buildAnimations();
if (_data.isPixel)
{
2023-06-15 04:29:18 +00:00
this.isPixel = true;
2023-01-23 00:55:30 +00:00
this.antialiasing = false;
2024-06-20 03:47:11 +00:00
pixelPerfectRender = true;
pixelPerfectPosition = true;
2023-01-23 00:55:30 +00:00
}
else
{
2023-06-15 04:29:18 +00:00
this.isPixel = false;
2023-01-23 00:55:30 +00:00
this.antialiasing = true;
}
}
function buildSpritesheet():Void
2023-01-23 00:55:30 +00:00
{
var assetList = [];
2023-01-23 00:55:30 +00:00
for (anim in _data.animations)
{
if (anim.assetPath != null && !assetList.contains(anim.assetPath))
{
assetList.push(anim.assetPath);
}
}
2024-07-16 09:52:51 +00:00
var texture:FlxAtlasFrames = Paths.getSparrowAtlas(_data.assetPath);
if (texture == null)
{
trace('Multi-Sparrow atlas could not load PRIMARY texture: ${_data.assetPath}');
FlxG.log.error('Multi-Sparrow atlas could not load PRIMARY texture: ${_data.assetPath}');
return;
}
else
{
trace('Creating multi-sparrow atlas: ${_data.assetPath}');
texture.parent.destroyOnNoUse = false;
}
2023-01-23 00:55:30 +00:00
for (asset in assetList)
{
2024-07-16 09:52:51 +00:00
var subTexture:FlxAtlasFrames = Paths.getSparrowAtlas(asset);
2023-01-23 00:55:30 +00:00
// If we don't do this, the unused textures will be removed as soon as they're loaded.
if (subTexture == null)
2023-01-23 00:55:30 +00:00
{
trace('Multi-Sparrow atlas could not load subtexture: ${asset}');
2023-01-23 00:55:30 +00:00
}
else
{
trace('Concatenating multi-sparrow atlas: ${asset}');
subTexture.parent.destroyOnNoUse = false;
2023-01-23 00:55:30 +00:00
}
texture.addAtlas(subTexture);
2023-01-23 00:55:30 +00:00
}
this.frames = texture;
this.setScale(_data.scale);
2023-01-23 00:55:30 +00:00
}
function buildAnimations()
{
trace('[MULTISPARROWCHAR] Loading ${_data.animations.length} animations for ${characterId}');
// We need to swap to the proper frame collection before adding the animations, I think?
for (anim in _data.animations)
{
FlxAnimationUtil.addAtlasAnimation(this, anim);
if (anim.offsets == null)
{
setAnimationOffsets(anim.name, 0, 0);
}
else
{
setAnimationOffsets(anim.name, anim.offsets[0], anim.offsets[1]);
}
}
var animNames = this.animation.getNameList();
trace('[MULTISPARROWCHAR] Successfully loaded ${animNames.length} animations for ${characterId}');
}
public override function playAnimation(name:String, restart:Bool = false, ignoreOther:Bool = false, reverse:Bool = false):Void
2023-01-23 00:55:30 +00:00
{
// Make sure we ignore other animations if we're currently playing a forced one,
// unless we're forcing a new animation.
if (!this.canPlayOtherAnims && !ignoreOther) return;
2023-01-23 00:55:30 +00:00
super.playAnimation(name, restart, ignoreOther, reverse);
2023-01-23 00:55:30 +00:00
}
}