Funkin/source/animate/FlxAnimate.hx

271 lines
7 KiB
Haxe
Raw Normal View History

package animate;
2021-04-14 11:31:52 -04:00
// import animateAtlasPlayer.assets.AssetManager;
// import animateAtlasPlayer.core.Animation;
2021-09-01 15:34:20 -04:00
import animate.ParseAnimate.AnimJson;
import animate.ParseAnimate.Sprite;
import animate.ParseAnimate.Spritemap;
import flixel.FlxCamera;
import flixel.FlxSprite;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxAtlasFrames;
2021-09-01 15:34:20 -04:00
import flixel.graphics.frames.FlxFrame.FlxFrameAngle;
2021-09-06 15:30:19 -04:00
import flixel.group.FlxGroup;
2021-09-01 15:34:20 -04:00
import flixel.math.FlxMatrix;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.system.FlxAssets.FlxGraphicAsset;
2021-08-29 15:56:46 -04:00
import haxe.format.JsonParser;
import openfl.Assets;
import openfl.display.BitmapData;
import openfl.geom.Rectangle;
2021-04-14 11:31:52 -04:00
class FlxAnimate extends FlxSymbol
{
2021-04-14 11:31:52 -04:00
// var myAnim:Animation;
// var animBitmap:BitmapData;
2021-09-01 15:34:20 -04:00
var jsonAnim:AnimJson;
2021-09-06 15:30:19 -04:00
var sprGrp:FlxTypedGroup<FlxSymbol>;
public function new(x:Float, y:Float)
{
2021-09-01 15:34:20 -04:00
super(x, y);
2021-09-06 15:30:19 -04:00
sprGrp = new FlxTypedGroup<FlxSymbol>();
2021-09-06 15:44:11 -04:00
var folder:String = "tightBarsLol";
2021-09-01 15:34:20 -04:00
frames = FlxAnimate.fromAnimate(Paths.file('images/' + folder + "/spritemap1.png"), Paths.file('images/$folder/spritemap1.json'));
jsonAnim = cast CoolUtil.coolJSON(Assets.getText(Paths.file('images/$folder/Animation.json')));
ParseAnimate.generateSymbolmap(jsonAnim.SD.S);
2021-09-01 20:04:07 -04:00
ParseAnimate.resetFrameList();
2021-09-06 15:30:19 -04:00
2021-09-01 15:34:20 -04:00
ParseAnimate.parseTimeline(jsonAnim.AN.TL, 0, 0);
2021-09-06 15:30:19 -04:00
generateSpriteShit();
2021-09-01 15:34:20 -04:00
/* var folder:String = 'tightestBars';
coolParse = cast Json.parse(Assets.getText(Paths.file('images/' + folder + '/Animation.json')));
2021-08-19 14:53:18 -04:00
2021-09-01 15:34:20 -04:00
// reverses the layers, for proper rendering!
coolParse.AN.TL.L.reverse();
super(x, y, coolParse);
frames = FlxAnimate.fromAnimate(Paths.file('images/' + folder + '/spritemap1.png'), Paths.file('images/' + folder + '/spritemap1.json'));
*/
2021-04-14 11:31:52 -04:00
// frames
}
override function draw()
{
2021-08-29 15:56:46 -04:00
// having this commented out fixes some wacky scaling bullshit?
// or fixes drawing it twice?
2021-08-29 15:56:46 -04:00
// super.draw();
2021-09-01 15:34:20 -04:00
// renderFrame(coolParse.AN.TL, coolParse, true);
2021-06-15 21:15:06 -04:00
2021-09-01 15:34:20 -04:00
actualFrameRender();
}
2021-04-15 11:57:59 -04:00
2021-09-06 15:44:11 -04:00
/**
* Puts all the needed sprites into a FlxTypedGroup, and properly recycles them?
**/
2021-09-06 15:30:19 -04:00
function generateSpriteShit()
2021-09-01 15:34:20 -04:00
{
2021-09-06 15:44:11 -04:00
sprGrp.kill(); // kills group, maybe dont need to do this one so broadly?
// just used for sum default shit?
var normalSpr:FlxSymbol = new FlxSymbol(0, 0);
2021-09-01 20:04:07 -04:00
for (frameSorted in ParseAnimate.frameList)
2021-04-15 11:57:59 -04:00
{
2021-09-06 15:30:19 -04:00
if (FlxG.keys.justPressed.I)
{
trace("\n\n\n\n");
trace('NEW LAYER TYPE SHIT');
trace(frameSorted.length);
// trace(i.depthString);
// trace("random lol: " + i.randomLol);
}
2021-09-01 20:04:07 -04:00
for (i in frameSorted)
2021-04-15 11:57:59 -04:00
{
2021-09-06 15:30:19 -04:00
// instead of making them every frame, regenerate when needed?
var spr:FlxSymbol = sprGrp.recycle(FlxSymbol); // redo this to recycle from a list later
2021-09-01 20:04:07 -04:00
spr.frames = frames;
2021-09-01 23:39:37 -04:00
spr.frame = spr.frames.getByName(i.frameName); // this one is fine
2021-09-01 15:34:20 -04:00
if (FlxG.keys.justPressed.I)
2021-09-01 23:39:37 -04:00
{
trace(i.frameName);
trace(i.depthString);
// trace("random lol: " + i.randomLol);
2021-09-01 23:39:37 -04:00
}
2021-09-01 20:04:07 -04:00
2021-09-06 15:44:11 -04:00
// cuz its in group, gets a lil fuckie when animated, need to go thru and properly reset each thing for shit like matrix!
spr.transformMatrix.copyFrom(normalSpr.transformMatrix);
2021-09-01 23:39:37 -04:00
for (swagMatrix in i.matrixArray)
2021-04-15 11:57:59 -04:00
{
2021-09-01 20:04:07 -04:00
var alsoSwag:FlxMatrix = new FlxMatrix(swagMatrix[0], swagMatrix[1], swagMatrix[4], swagMatrix[5], swagMatrix[12], swagMatrix[13]);
spr.matrixExposed = true;
spr.transformMatrix.concat(alsoSwag);
2021-04-15 11:57:59 -04:00
}
2021-09-06 14:50:04 -04:00
// spr.alpha = 0.3;
2021-09-01 20:04:07 -04:00
spr.origin.set();
spr.antialiasing = true;
2021-09-06 15:30:19 -04:00
sprGrp.add(spr);
// trace(sprGrp);
// trace(spr);
2021-09-01 20:04:07 -04:00
// spr.alpha = 0.5;
/* if (i == "0225")
{
trace('FUNNY MATRIX!');
trace(spr._matrix);
trace("\n\n MATRIX MAP");
for (m in ParseAnimate.matrixMap.get("0225"))
{
trace(m);
}
trace('\n\n');
}*/
2021-09-01 15:34:20 -04:00
}
2021-04-15 11:57:59 -04:00
}
2021-09-06 15:30:19 -04:00
// trace(sprGrp.length);
}
// fix render order of ALL layers!
// seperate frameList into layers
// go thru animate file to see how it should all be ordered
// per frame symbol stuff to fix lip sync (in ParseAnimate?)
// definitely need to dig through Animate.json stuff
// something with TRP stuff, look through tighterBars (GF scene)
// redo map stuff incase there's multiple assets
// ONE CENTRAL THING FOR THIS DUMBASS BULLSHIT
// sorted framelist put it all in there, then make i actually mean something
function actualFrameRender()
{
sprGrp.draw();
2021-04-14 11:31:52 -04:00
}
2021-04-14 11:31:52 -04:00
// notes to self
// account for different layers
2021-04-14 21:52:43 -04:00
var playingAnim:Bool = false;
var frameTickTypeShit:Float = 0;
var animFrameRate:Int = 24;
2021-04-14 11:31:52 -04:00
2021-09-01 15:34:20 -04:00
// redo all the matrix animation stuff
2021-04-14 11:31:52 -04:00
override function update(elapsed:Float)
{
super.update(elapsed);
2021-04-14 21:52:43 -04:00
if (FlxG.keys.justPressed.SPACE)
playingAnim = !playingAnim;
if (playingAnim)
{
frameTickTypeShit += elapsed;
// prob fix this framerate thing for higher framerates?
if (frameTickTypeShit >= 1 / 24)
{
changeFrame(1);
frameTickTypeShit = 0;
2021-09-01 15:34:20 -04:00
ParseAnimate.resetFrameList();
ParseAnimate.parseTimeline(jsonAnim.AN.TL, 0, daFrame);
2021-09-06 15:44:11 -04:00
generateSpriteShit();
2021-04-14 21:52:43 -04:00
}
}
if (FlxG.keys.justPressed.RIGHT)
2021-09-01 15:34:20 -04:00
{
2021-04-14 21:52:43 -04:00
changeFrame(1);
2021-09-01 15:34:20 -04:00
ParseAnimate.resetFrameList();
ParseAnimate.parseTimeline(jsonAnim.AN.TL, 0, daFrame);
2021-09-06 15:30:19 -04:00
generateSpriteShit();
2021-09-01 15:34:20 -04:00
}
2021-04-14 21:52:43 -04:00
if (FlxG.keys.justPressed.LEFT)
changeFrame(-1);
}
/**
* PARSES THE 'spritemap1.png' or whatever into a FlxAtlasFrames!!!
*/
public static function fromAnimate(Source:FlxGraphicAsset, Description:String):FlxAtlasFrames
{
var graphic:FlxGraphic = FlxG.bitmap.add(Source);
if (graphic == null)
return null;
var frames:FlxAtlasFrames = FlxAtlasFrames.findFrame(graphic);
if (frames != null)
return frames;
if (graphic == null || Description == null)
return null;
frames = new FlxAtlasFrames(graphic);
2021-09-01 15:34:20 -04:00
var data:Spritemap;
var json:String = Description;
2021-09-01 15:34:20 -04:00
// trace(json);
2021-08-29 15:56:46 -04:00
var funnyJson:Dynamic = {};
if (Assets.exists(json))
2021-08-29 15:56:46 -04:00
funnyJson = JaySon.parseFile(json);
2021-08-29 15:56:46 -04:00
// trace(json);
2021-09-01 15:34:20 -04:00
// data = c
data = cast funnyJson;
2021-09-01 15:34:20 -04:00
for (sprite in data.ATLAS.SPRITES)
{
// probably nicer way to do this? Oh well
2021-09-01 15:34:20 -04:00
var swagSprite:Sprite = sprite.SPRITE;
var rect = FlxRect.get(swagSprite.x, swagSprite.y, swagSprite.w, swagSprite.h);
var size = new Rectangle(0, 0, rect.width, rect.height);
var offset = FlxPoint.get(-size.left, -size.top);
var sourceSize = FlxPoint.get(size.width, size.height);
frames.addAtlasFrame(rect, sourceSize, offset, swagSprite.name);
}
return frames;
}
}
// handy json function that has some hashlink fix, see the thing in CoolUtils file to see the link / where i stole it from
2021-08-29 15:56:46 -04:00
class JaySon
{
public static function parseFile(name:String)
{
var cont = Assets.getText(name);
function is(n:Int, what:Int)
return cont.charCodeAt(n) == what;
return JsonParser.parse(cont.substr(if (is(0, 65279)) /// looks like a HL target, skipping only first character here:
1 else if (is(0, 239) && is(1, 187) && is(2, 191)) /// it seems to be Neko or PHP, start from position 3:
3 else /// all other targets, that prepare the UTF string correctly
0));
}
}