1
0
Fork 0
mirror of https://github.com/ninjamuffin99/Funkin.git synced 2024-12-30 16:56:47 +00:00
Funkin/source/funkin/animate/FlxAnimate.hx

292 lines
7.3 KiB
Haxe
Raw Normal View History

package funkin.animate;
import funkin.animate.ParseAnimate.AnimJson;
import funkin.animate.ParseAnimate.Sprite;
import funkin.animate.ParseAnimate.Spritemap;
2021-09-01 19:34:20 +00:00
import flixel.FlxCamera;
import flixel.FlxSprite;
import flixel.graphics.FlxGraphic;
import flixel.graphics.frames.FlxAtlasFrames;
2021-09-01 19:34:20 +00:00
import flixel.graphics.frames.FlxFrame.FlxFrameAngle;
2021-09-06 19:30:19 +00:00
import flixel.group.FlxGroup;
2021-09-01 19:34:20 +00:00
import flixel.math.FlxMatrix;
import flixel.math.FlxPoint;
import flixel.math.FlxRect;
import flixel.system.FlxAssets.FlxGraphicAsset;
2021-08-29 19:56:46 +00:00
import haxe.format.JsonParser;
import openfl.Assets;
import openfl.display.BitmapData;
import openfl.geom.Matrix;
import openfl.geom.Rectangle;
2021-04-14 15:31:52 +00:00
class FlxAnimate extends FlxSymbol
{
2021-04-14 15:31:52 +00:00
// var myAnim:Animation;
// var animBitmap:BitmapData;
2021-09-01 19:34:20 +00:00
var jsonAnim:AnimJson;
2021-09-06 19:30:19 +00:00
var sprGrp:FlxTypedGroup<FlxSymbol>;
public function new(x:Float, y:Float)
{
2021-09-01 19:34:20 +00:00
super(x, y);
2021-09-06 19:30:19 +00:00
sprGrp = new FlxTypedGroup<FlxSymbol>();
var tests:Array<String> = ['tightBarsLol', 'tightestBars'];
var folder:String = tests[1];
2021-09-01 19:34:20 +00: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-02 00:04:07 +00:00
ParseAnimate.resetFrameList();
2021-09-06 19:30:19 +00:00
2021-09-01 19:34:20 +00:00
ParseAnimate.parseTimeline(jsonAnim.AN.TL, 0, 0);
2021-09-06 19:30:19 +00:00
generateSpriteShit();
2021-09-01 19:34:20 +00:00
/* var folder:String = 'tightestBars';
coolParse = cast Json.parse(Assets.getText(Paths.file('images/' + folder + '/Animation.json')));
2021-08-19 18:53:18 +00:00
2021-09-01 19:34:20 +00: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 15:31:52 +00:00
// frames
}
override function draw()
{
2021-08-29 19:56:46 +00:00
// having this commented out fixes some wacky scaling bullshit?
// or fixes drawing it twice?
2021-08-29 19:56:46 +00:00
// super.draw();
2021-09-01 19:34:20 +00:00
// renderFrame(coolParse.AN.TL, coolParse, true);
2021-06-16 01:15:06 +00:00
2021-09-01 19:34:20 +00:00
actualFrameRender();
}
2021-04-15 15:57:59 +00:00
2021-09-06 19:44:11 +00:00
/**
* Puts all the needed sprites into a FlxTypedGroup, and properly recycles them?
**/
2021-09-06 19:30:19 +00:00
function generateSpriteShit()
2021-09-01 19:34:20 +00:00
{
sprGrp.kill(); // kills group, maybe dont need to do this one so broadly? ehh whatev
2021-09-06 19:44:11 +00:00
2021-09-02 00:04:07 +00:00
for (frameSorted in ParseAnimate.frameList)
2021-04-15 15:57:59 +00:00
{
2021-09-02 00:04:07 +00:00
for (i in frameSorted)
2021-04-15 15:57:59 +00:00
{
2021-09-06 19:30:19 +00: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-02 00:04:07 +00:00
spr.frames = frames;
2021-09-02 03:39:37 +00:00
spr.frame = spr.frames.getByName(i.frameName); // this one is fine
spr.updateHitbox();
2021-09-01 19:34:20 +00:00
2021-09-08 04:06:49 +00:00
// move this? wont work here!
2021-09-01 19:34:20 +00:00
if (FlxG.keys.justPressed.I)
2021-09-02 03:39:37 +00:00
{
trace(i.frameName);
trace(i.depthString);
// trace("random lol: " + i.randomLol);
2021-09-02 03:39:37 +00:00
}
2021-09-02 00:04:07 +00:00
2021-09-06 19:44:11 +00:00
// cuz its in group, gets a lil fuckie when animated, need to go thru and properly reset each thing for shit like matrix!
// merely resets the matrix to normal ass one!
spr.transformMatrix.identity();
spr.setPosition();
/* for (swagMatrix in i.matrixArray)
{
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-09-06 19:44:11 +00:00
// i.fullMatrix.concat
spr.matrixExposed = true;
// trace(i.fullMatrix);
if (i.fullMatrix.a < 0)
2021-04-15 15:57:59 +00:00
{
trace('negative?');
trace(i.fullMatrix);
2021-04-15 15:57:59 +00:00
}
spr.transformMatrix.concat(i.fullMatrix);
if (i.fullMatrix.a < 0)
{
trace('negative?');
trace(i.fullMatrix);
trace(spr.transformMatrix);
}
// trace(spr.transformMatrix);
2021-09-02 00:04:07 +00:00
spr.origin.set();
/* for (trpShit in i.trpArray)
{
spr.origin.x -= trpShit[0];
spr.origin.y -= trpShit[1];
}
*/
// spr.alpha = 0.3;
2021-09-02 00:04:07 +00:00
spr.antialiasing = true;
2021-09-06 19:30:19 +00:00
sprGrp.add(spr);
spr.alpha = 0.5;
2021-09-02 00:04:07 +00:00
/* 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 19:34:20 +00:00
}
2021-04-15 15:57:59 +00:00
}
2021-09-06 19:30:19 +00: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 15:31:52 +00:00
}
2021-04-14 15:31:52 +00:00
// notes to self
// account for different layers
2021-04-15 01:52:43 +00:00
var playingAnim:Bool = false;
var frameTickTypeShit:Float = 0;
var animFrameRate:Int = 24;
2021-04-14 15:31:52 +00:00
2021-09-01 19:34:20 +00:00
// redo all the matrix animation stuff
2021-04-14 15:31:52 +00:00
override function update(elapsed:Float)
{
super.update(elapsed);
2021-04-15 01:52:43 +00: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 19:34:20 +00:00
ParseAnimate.resetFrameList();
ParseAnimate.parseTimeline(jsonAnim.AN.TL, 0, daFrame);
2021-09-06 19:44:11 +00:00
generateSpriteShit();
2021-04-15 01:52:43 +00:00
}
}
if (FlxG.keys.justPressed.RIGHT)
2021-09-01 19:34:20 +00:00
{
2021-04-15 01:52:43 +00:00
changeFrame(1);
2021-09-01 19:34:20 +00:00
ParseAnimate.resetFrameList();
ParseAnimate.parseTimeline(jsonAnim.AN.TL, 0, daFrame);
2021-09-06 19:30:19 +00:00
generateSpriteShit();
2021-09-01 19:34:20 +00:00
}
2021-04-15 01:52:43 +00: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 19:34:20 +00:00
var data:Spritemap;
var json:String = Description;
2021-09-01 19:34:20 +00:00
// trace(json);
2021-08-29 19:56:46 +00:00
var funnyJson:Dynamic = {};
if (Assets.exists(json))
2021-08-29 19:56:46 +00:00
funnyJson = JaySon.parseFile(json);
2021-08-29 19:56:46 +00:00
// trace(json);
2021-09-01 19:34:20 +00:00
// data = c
data = cast funnyJson;
2021-09-01 19:34:20 +00:00
for (sprite in data.ATLAS.SPRITES)
{
// probably nicer way to do this? Oh well
2021-09-01 19:34:20 +00: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 19:56:46 +00: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));
}
}