music and preloading

This commit is contained in:
Kade M
2021-07-06 14:14:07 -07:00
parent c442e23b1c
commit 57609fc599
8 changed files with 158 additions and 81 deletions

View File

@ -1,5 +1,12 @@
package;
import lime.app.Application;
#if windows
import Discord.DiscordClient;
#end
import openfl.display.BitmapData;
import openfl.utils.Assets;
import flixel.ui.FlxBar;
import haxe.Exception;
import flixel.tweens.FlxEase;
import flixel.tweens.FlxTween;
@ -25,15 +32,26 @@ class Caching extends MusicBeatState
var toBeDone = 0;
var done = 0;
var loaded = false;
var text:FlxText;
var kadeLogo:FlxSprite;
public static var bitmapData:Map<String,BitmapData>;
var images = [];
var music = [];
var charts = [];
override function create()
{
FlxG.mouse.visible = false;
FlxG.worldBounds.set(0,0);
bitmapData = new Map<String,BitmapData>();
text = new FlxText(FlxG.width / 2, FlxG.height / 2 + 300,0,"Loading...");
text.size = 34;
text.alignment = FlxTextAlign.CENTER;
@ -48,50 +66,35 @@ class Caching extends MusicBeatState
kadeLogo.alpha = 0;
add(kadeLogo);
add(text);
PlayerSettings.init();
trace('starting caching..');
sys.thread.Thread.create(() -> {
cache();
});
#if windows
DiscordClient.initialize();
Application.current.onExit.add (function (exitCode) {
DiscordClient.shutdown();
});
#end
Highscore.load();
FlxG.save.bind('funkin', 'ninjamuffin99');
KadeEngineData.initSave();
super.create();
}
var calledDone = false;
override function update(elapsed)
{
if (toBeDone != 0 && done != toBeDone)
if (FlxG.save.data.cacheImages)
{
var alpha = HelperFunctions.truncateFloat(done / toBeDone * 100,2) / 100;
kadeLogo.alpha = alpha;
text.alpha = alpha;
text.text = "Loading... (" + done + "/" + toBeDone + ")";
}
trace("caching images...");
super.update(elapsed);
}
function cache()
{
var images = [];
var music = [];
var charts = [];
trace("caching images...");
for (i in FileSystem.readDirectory(FileSystem.absolutePath("assets/shared/images/characters")))
{
if (!i.endsWith(".png"))
continue;
images.push(i);
for (i in FileSystem.readDirectory(FileSystem.absolutePath("assets/shared/images/characters")))
{
if (!i.endsWith(".png"))
continue;
images.push(i);
}
}
trace("caching music...");
@ -104,13 +107,60 @@ class Caching extends MusicBeatState
toBeDone = Lambda.count(images) + Lambda.count(music);
var bar = new FlxBar(10,FlxG.height - 50,FlxBarFillDirection.LEFT_TO_RIGHT,FlxG.width,40,null,"done",0,toBeDone);
bar.color = FlxColor.PURPLE;
add(bar);
add(kadeLogo);
add(text);
trace('starting caching..');
// update thread
sys.thread.Thread.create(() -> {
while(!loaded)
{
if (toBeDone != 0 && done != toBeDone)
{
var alpha = HelperFunctions.truncateFloat(done / toBeDone * 100,2) / 100;
kadeLogo.alpha = alpha;
text.alpha = alpha;
text.text = "Loading... (" + done + "/" + toBeDone + ")";
}
}
});
// cache thread
sys.thread.Thread.create(() -> {
cache();
});
super.create();
}
var calledDone = false;
override function update(elapsed)
{
super.update(elapsed);
}
function cache()
{
trace("LOADING: " + toBeDone + " OBJECTS.");
for (i in images)
{
var replaced = i.replace(".png","");
FlxG.bitmap.add(Paths.image("characters/" + replaced,"shared"));
trace("cached " + replaced);
var data:BitmapData = BitmapData.fromFile("assets/shared/images/characters/" + i);
trace('id ' + replaced + ' file - assets/shared/images/characters/' + i + ' ${data.width}');
bitmapData.set(replaced,data);
done++;
}
@ -125,6 +175,10 @@ class Caching extends MusicBeatState
trace("Finished caching...");
loaded = true;
trace(Assets.cache.hasBitmapData('GF_assets'));
FlxG.switchState(new TitleState());
}

View File

@ -32,7 +32,7 @@ class Character extends FlxSprite
{
case 'gf':
// GIRLFRIEND CODE
tex = Paths.getSparrowAtlas('characters/GF_assets');
tex = Paths.getSparrowAtlas('GF_assets','shared',true);
frames = tex;
animation.addByPrefix('cheer', 'GF Cheer', 24, false);
animation.addByPrefix('singLEFT', 'GF left note', 24, false);
@ -51,7 +51,7 @@ class Character extends FlxSprite
playAnim('danceRight');
case 'gf-christmas':
tex = Paths.getSparrowAtlas('characters/gfChristmas');
tex = Paths.getSparrowAtlas('gfChristmas','shared',true);
frames = tex;
animation.addByPrefix('cheer', 'GF Cheer', 24, false);
animation.addByPrefix('singLEFT', 'GF left note', 24, false);
@ -70,7 +70,7 @@ class Character extends FlxSprite
playAnim('danceRight');
case 'gf-car':
tex = Paths.getSparrowAtlas('characters/gfCar');
tex = Paths.getSparrowAtlas('gfCar','shared',true);
frames = tex;
animation.addByIndices('singUP', 'GF Dancing Beat Hair blowing CAR', [0], "", 24, false);
animation.addByIndices('danceLeft', 'GF Dancing Beat Hair blowing CAR', [30, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], "", 24, false);
@ -82,7 +82,7 @@ class Character extends FlxSprite
playAnim('danceRight');
case 'gf-pixel':
tex = Paths.getSparrowAtlas('characters/gfPixel');
tex = Paths.getSparrowAtlas('gfPixel','shared',true);
frames = tex;
animation.addByIndices('singUP', 'GF IDLE', [2], "", 24, false);
animation.addByIndices('danceLeft', 'GF IDLE', [30, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14], "", 24, false);
@ -98,7 +98,7 @@ class Character extends FlxSprite
case 'dad':
// DAD ANIMATION LOADING CODE
tex = Paths.getSparrowAtlas('characters/DADDY_DEAREST', 'shared');
tex = Paths.getSparrowAtlas('DADDY_DEAREST','shared',true);
frames = tex;
animation.addByPrefix('idle', 'Dad idle dance', 24);
animation.addByPrefix('singUP', 'Dad Sing Note UP', 24);
@ -110,7 +110,7 @@ class Character extends FlxSprite
playAnim('idle');
case 'spooky':
tex = Paths.getSparrowAtlas('characters/spooky_kids_assets');
tex = Paths.getSparrowAtlas('spooky_kids_assets','shared',true);
frames = tex;
animation.addByPrefix('singUP', 'spooky UP NOTE', 24, false);
animation.addByPrefix('singDOWN', 'spooky DOWN note', 24, false);
@ -123,7 +123,7 @@ class Character extends FlxSprite
playAnim('danceRight');
case 'mom':
tex = Paths.getSparrowAtlas('characters/Mom_Assets');
tex = Paths.getSparrowAtlas('Mom_Assets','shared',true);
frames = tex;
animation.addByPrefix('idle', "Mom Idle", 24, false);
@ -139,7 +139,7 @@ class Character extends FlxSprite
playAnim('idle');
case 'mom-car':
tex = Paths.getSparrowAtlas('characters/momCar');
tex = Paths.getSparrowAtlas('momCar','shared',true);
frames = tex;
animation.addByPrefix('idle', "Mom Idle", 24, false);
@ -154,7 +154,7 @@ class Character extends FlxSprite
playAnim('idle');
case 'monster':
tex = Paths.getSparrowAtlas('characters/Monster_Assets');
tex = Paths.getSparrowAtlas('Monster_Assets','shared',true);
frames = tex;
animation.addByPrefix('idle', 'monster idle', 24, false);
animation.addByPrefix('singUP', 'monster up note', 24, false);
@ -165,7 +165,7 @@ class Character extends FlxSprite
loadOffsetFile(curCharacter);
playAnim('idle');
case 'monster-christmas':
tex = Paths.getSparrowAtlas('characters/monsterChristmas');
tex = Paths.getSparrowAtlas('monsterChristmas','shared',true);
frames = tex;
animation.addByPrefix('idle', 'monster idle', 24, false);
animation.addByPrefix('singUP', 'monster up note', 24, false);
@ -176,7 +176,7 @@ class Character extends FlxSprite
loadOffsetFile(curCharacter);
playAnim('idle');
case 'pico':
tex = Paths.getSparrowAtlas('characters/Pico_FNF_assetss');
tex = Paths.getSparrowAtlas('Pico_FNF_assetss','shared',true);
frames = tex;
animation.addByPrefix('idle', "Pico Idle Dance", 24);
animation.addByPrefix('singUP', 'pico Up note0', 24, false);
@ -207,7 +207,7 @@ class Character extends FlxSprite
flipX = true;
case 'bf':
var tex = Paths.getSparrowAtlas('characters/BOYFRIEND', 'shared');
var tex = Paths.getSparrowAtlas('BOYFRIEND','shared',true);
frames = tex;
trace(tex.frames.length);
@ -236,7 +236,7 @@ class Character extends FlxSprite
flipX = true;
case 'bf-christmas':
var tex = Paths.getSparrowAtlas('characters/bfChristmas');
var tex = Paths.getSparrowAtlas('bfChristmas','shared',true);
frames = tex;
animation.addByPrefix('idle', 'BF idle dance', 24, false);
animation.addByPrefix('singUP', 'BF NOTE UP0', 24, false);
@ -255,7 +255,7 @@ class Character extends FlxSprite
flipX = true;
case 'bf-car':
var tex = Paths.getSparrowAtlas('characters/bfCar');
var tex = Paths.getSparrowAtlas('bfCar','shared',true);
frames = tex;
animation.addByPrefix('idle', 'BF idle dance', 24, false);
animation.addByPrefix('singUP', 'BF NOTE UP0', 24, false);
@ -272,7 +272,7 @@ class Character extends FlxSprite
flipX = true;
case 'bf-pixel':
frames = Paths.getSparrowAtlas('characters/bfPixel');
frames = Paths.getSparrowAtlas('bfPixel','shared',true);
animation.addByPrefix('idle', 'BF IDLE', 24, false);
animation.addByPrefix('singUP', 'BF UP NOTE', 24, false);
animation.addByPrefix('singLEFT', 'BF LEFT NOTE', 24, false);
@ -297,7 +297,7 @@ class Character extends FlxSprite
flipX = true;
case 'bf-pixel-dead':
frames = Paths.getSparrowAtlas('characters/bfPixelsDEAD');
frames = Paths.getSparrowAtlas('bfPixelsDEAD','shared',true);
animation.addByPrefix('singUP', "BF Dies pixel", 24, false);
animation.addByPrefix('firstDeath', "BF Dies pixel", 24, false);
animation.addByPrefix('deathLoop', "Retry Loop", 24, true);
@ -313,7 +313,7 @@ class Character extends FlxSprite
flipX = true;
case 'senpai':
frames = Paths.getSparrowAtlas('characters/senpai');
frames = Paths.getSparrowAtlas('senpai','shared',true);
animation.addByPrefix('idle', 'Senpai Idle', 24, false);
animation.addByPrefix('singUP', 'SENPAI UP NOTE', 24, false);
animation.addByPrefix('singLEFT', 'SENPAI LEFT NOTE', 24, false);
@ -329,7 +329,7 @@ class Character extends FlxSprite
antialiasing = false;
case 'senpai-angry':
frames = Paths.getSparrowAtlas('characters/senpai');
frames = Paths.getSparrowAtlas('senpai','shared',true);
animation.addByPrefix('idle', 'Angry Senpai Idle', 24, false);
animation.addByPrefix('singUP', 'Angry Senpai UP NOTE', 24, false);
animation.addByPrefix('singLEFT', 'Angry Senpai LEFT NOTE', 24, false);
@ -345,7 +345,7 @@ class Character extends FlxSprite
antialiasing = false;
case 'spirit':
frames = Paths.getPackerAtlas('characters/spirit');
frames = Paths.getPackerAtlas('spirit','shared',true);
animation.addByPrefix('idle', "idle spirit_", 24, false);
animation.addByPrefix('singUP', "up_", 24, false);
animation.addByPrefix('singRIGHT', "right_", 24, false);
@ -362,7 +362,7 @@ class Character extends FlxSprite
antialiasing = false;
case 'parents-christmas':
frames = Paths.getSparrowAtlas('characters/mom_dad_christmas_assets');
frames = Paths.getSparrowAtlas('mom_dad_christmas_assets','shared',true);
animation.addByPrefix('idle', 'Parent Christmas Idle', 24, false);
animation.addByPrefix('singUP', 'Parent Up Note Dad', 24, false);
animation.addByPrefix('singDOWN', 'Parent Down Note Dad', 24, false);

View File

@ -64,7 +64,6 @@ class FreeplayState extends MusicBeatState
{
var data:Array<String> = initSonglist[i].split(':');
var meta = new SongMetadata(data[0], Std.parseInt(data[2]), data[1]);
#if debug
songs.push(meta);
var format = StringTools.replace(meta.songName, " ", "-");
switch (format) {
@ -78,24 +77,7 @@ class FreeplayState extends MusicBeatState
FreeplayState.loadDiff(2,format,meta.songName,diffs);
FreeplayState.songData.set(meta.songName,diffs);
trace('loaded diffs for ' + meta.songName);
#else
if(Std.parseInt(data[2]) <= FlxG.save.data.weekUnlocked - 1)
{
songs.push(meta);
var format = StringTools.replace(meta.songName, " ", "-");
switch (format) {
case 'Dad-Battle': format = 'Dadbattle';
case 'Philly-Nice': format = 'Philly';
}
var diffs = [];
FreeplayState.loadDiff(0,format,meta.songName,diffs);
FreeplayState.loadDiff(1,format,meta.songName,diffs);
FreeplayState.loadDiff(2,format,meta.songName,diffs);
FreeplayState.songData.set(meta.songName,diffs);
trace('loaded diffs for ' + meta.songName);
}
#end
}
//trace("\n" + diffList);

View File

@ -97,6 +97,9 @@ class KadeEngineData
if (FlxG.save.data.optimize == null)
FlxG.save.data.optimize = false;
if (FlxG.save.data.cacheImages == null)
FlxG.save.data.cacheImages = false;
var gamepad:FlxGamepad = FlxG.gamepads.lastActive;
KeyBinds.gamepad = gamepad != null;

View File

@ -119,6 +119,29 @@ class CpuStrums extends Option
}
class GraphicLoading extends Option
{
public function new(desc:String)
{
super();
description = desc;
}
public override function press():Bool
{
FlxG.save.data.cacheImages = !FlxG.save.data.cacheImages;
display = updateDisplay();
return true;
}
private override function updateDisplay():String
{
return FlxG.save.data.cacheImages ? "Preload Characters" : "Do not Preload Characters";
}
}
class DownscrollOption extends Option
{
public function new(desc:String)

View File

@ -1,5 +1,8 @@
package;
import openfl.utils.Assets;
import flixel.graphics.FlxGraphic;
import flixel.system.FlxAssets.FlxGraphicAsset;
import flixel.FlxG;
import flixel.graphics.frames.FlxAtlasFrames;
import openfl.utils.AssetType;
@ -125,13 +128,25 @@ class Paths
return 'assets/fonts/$key';
}
inline static public function getSparrowAtlas(key:String, ?library:String)
inline static public function getSparrowAtlas(key:String, ?library:String, ?isCharacter:Bool = false)
{
if (isCharacter)
return FlxAtlasFrames.fromSparrow(imageCached(key), file('images/characters/$key.xml', library));
return FlxAtlasFrames.fromSparrow(image(key, library), file('images/$key.xml', library));
}
inline static public function getPackerAtlas(key:String, ?library:String)
inline static public function imageCached(key:String):FlxGraphic
{
var data = FlxGraphic.fromBitmapData(Caching.bitmapData.get(key));
trace('finding ${key} - ${data.bitmap}');
return data;
}
inline static public function getPackerAtlas(key:String, ?library:String, ?isCharacter:Bool = false)
{
if (isCharacter)
return FlxAtlasFrames.fromSpriteSheetPacker(imageCached(key), file('images/$key.txt', library));
return FlxAtlasFrames.fromSpriteSheetPacker(image(key, library), file('images/$key.txt', library));
}
}