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

387 lines
10 KiB
Haxe
Raw Normal View History

package funkin.ui.transition;
2023-04-05 02:24:00 +00:00
2023-04-05 02:38:45 +00:00
import flixel.FlxSprite;
2023-04-05 02:24:00 +00:00
import haxe.Json;
2023-04-05 04:56:02 +00:00
import lime.utils.Assets;
import funkin.graphics.FunkinSprite;
2023-04-05 21:35:19 +00:00
// import flxtyped group
import funkin.ui.MusicBeatSubState;
2023-05-17 20:42:58 +00:00
import funkin.ui.story.StoryMenuState;
2023-04-05 21:35:19 +00:00
import flixel.group.FlxGroup.FlxTypedGroup;
import flixel.util.FlxTimer;
import flixel.FlxG;
2023-04-06 05:39:27 +00:00
import flixel.math.FlxMath;
2023-04-05 21:35:19 +00:00
import flixel.util.FlxSort;
2023-04-06 05:39:27 +00:00
import flixel.util.FlxSignal;
import funkin.ui.mainmenu.MainMenuState;
2023-04-06 05:39:27 +00:00
import flixel.addons.transition.FlxTransitionableState;
import openfl.display.BitmapData;
import funkin.ui.freeplay.FreeplayState;
2023-04-06 05:39:27 +00:00
import openfl.geom.Matrix;
2024-03-23 21:50:48 +00:00
import funkin.audio.FunkinSound;
2023-04-06 05:39:27 +00:00
import openfl.display.Sprite;
import openfl.display.Bitmap;
import flixel.FlxState;
2023-04-05 02:24:00 +00:00
2023-10-11 11:43:44 +00:00
using Lambda;
using StringTools;
class StickerSubState extends MusicBeatSubState
2023-04-05 02:24:00 +00:00
{
2023-04-06 05:39:27 +00:00
public var grpStickers:FlxTypedGroup<StickerSprite>;
2023-04-05 21:35:19 +00:00
2023-04-06 05:39:27 +00:00
// yes... a damn OpenFL sprite!!!
public var dipshit:Sprite;
/**
* The state to switch to after the stickers are done.
* This is a FUNCTION so we can pass it directly to `FlxG.switchState()`,
* and we can add constructor parameters in the caller.
*/
var targetState:StickerSubState->FlxState;
2023-10-11 11:43:44 +00:00
// what "folders" to potentially load from (as of writing only "keys" exist)
var soundSelections:Array<String> = [];
// what "folder" was randomly selected
var soundSelection:String = "";
var sounds:Array<String> = [];
public function new(?oldStickers:Array<StickerSprite>, ?targetState:StickerSubState->FlxState):Void
2023-04-05 02:24:00 +00:00
{
super();
this.targetState = (targetState == null) ? ((sticker) -> new MainMenuState()) : targetState;
2023-10-11 11:43:44 +00:00
// todo still
// make sure that ONLY plays mp3/ogg files
// if there's no mp3/ogg file, then it regenerates/reloads the random folder
var assetsInList = openfl.utils.Assets.list();
var soundFilterFunc = function(a:String) {
return a.startsWith('assets/shared/sounds/stickersounds/');
};
soundSelections = assetsInList.filter(soundFilterFunc);
soundSelections = soundSelections.map(function(a:String) {
return a.replace('assets/shared/sounds/stickersounds/', '').split('/')[0];
});
// cracked cleanup... yuchh...
for (i in soundSelections)
{
while (soundSelections.contains(i))
{
soundSelections.remove(i);
}
soundSelections.push(i);
}
trace(soundSelections);
soundSelection = FlxG.random.getObject(soundSelections);
var filterFunc = function(a:String) {
return a.startsWith('assets/shared/sounds/stickersounds/' + soundSelection + '/');
};
var assetsInList3 = openfl.utils.Assets.list();
sounds = assetsInList3.filter(filterFunc);
for (i in 0...sounds.length)
{
sounds[i] = sounds[i].replace('assets/shared/sounds/', '');
sounds[i] = sounds[i].substring(0, sounds[i].lastIndexOf('.'));
}
trace(sounds);
2023-04-05 21:35:19 +00:00
grpStickers = new FlxTypedGroup<StickerSprite>();
add(grpStickers);
// makes the stickers on the most recent camera, which is more often than not... a UI camera!!
2023-06-09 20:54:13 +00:00
// grpStickers.cameras = [FlxG.cameras.list[FlxG.cameras.list.length - 1]];
grpStickers.cameras = FlxG.cameras.list;
2023-04-06 05:39:27 +00:00
if (oldStickers != null)
{
for (sticker in oldStickers)
{
grpStickers.add(sticker);
}
degenStickers();
}
else
regenStickers();
}
public function degenStickers():Void
{
grpStickers.cameras = FlxG.cameras.list;
2024-02-12 21:50:40 +00:00
/*
if (dipshit != null)
{
FlxG.removeChild(dipshit);
dipshit = null;
}
*/
if (grpStickers.members == null || grpStickers.members.length == 0)
2023-04-06 05:39:27 +00:00
{
2024-02-12 21:50:40 +00:00
switchingState = false;
close();
return;
2023-04-06 05:39:27 +00:00
}
for (ind => sticker in grpStickers.members)
{
new FlxTimer().start(sticker.timing, _ -> {
sticker.visible = false;
2023-10-11 11:43:44 +00:00
var daSound:String = FlxG.random.getObject(sounds);
2024-03-23 21:50:48 +00:00
FunkinSound.playOnce(Paths.sound(daSound));
2023-04-06 05:39:27 +00:00
if (grpStickers == null || ind == grpStickers.members.length - 1)
2023-04-06 05:39:27 +00:00
{
switchingState = false;
close();
}
});
}
}
function regenStickers():Void
{
if (grpStickers.members.length > 0)
{
grpStickers.clear();
}
2023-04-05 04:56:02 +00:00
var stickerInfo:StickerInfo = new StickerInfo('stickers-set-1');
2023-04-10 20:05:37 +00:00
var stickers:Map<String, Array<String>> = new Map<String, Array<String>>();
2023-04-05 02:38:45 +00:00
for (stickerSets in stickerInfo.getPack("all"))
{
2023-04-10 20:05:37 +00:00
stickers.set(stickerSets, stickerInfo.getStickers(stickerSets));
}
var xPos:Float = -100;
var yPos:Float = -100;
while (xPos <= FlxG.width)
{
var stickerSet:String = FlxG.random.getObject(stickers.keyValues());
var sticker:String = FlxG.random.getObject(stickers.get(stickerSet));
var sticky:StickerSprite = new StickerSprite(0, 0, stickerInfo.name, sticker);
sticky.visible = false;
sticky.x = xPos;
sticky.y = yPos;
xPos += sticky.frameWidth * 0.5;
if (xPos >= FlxG.width)
2023-04-05 04:56:02 +00:00
{
2023-04-10 20:05:37 +00:00
if (yPos <= FlxG.height)
2023-04-05 21:35:19 +00:00
{
2023-04-10 20:05:37 +00:00
xPos = -100;
yPos += FlxG.random.float(70, 120);
2023-04-05 21:35:19 +00:00
}
2023-04-05 04:56:02 +00:00
}
2023-04-05 21:35:19 +00:00
2023-04-10 20:05:37 +00:00
sticky.angle = FlxG.random.int(-60, 70);
grpStickers.add(sticky);
2023-04-05 21:35:19 +00:00
}
2023-04-06 05:39:27 +00:00
FlxG.random.shuffle(grpStickers.members);
2023-04-10 20:05:37 +00:00
// var stickerCount:Int = 0;
// for (w in 0...6)
// {
// var xPos:Float = FlxG.width * (w / 6);
// for (h in 0...6)
// {
// var yPos:Float = FlxG.height * (h / 6);
// var sticker = grpStickers.members[stickerCount];
// xPos -= sticker.width / 2;
// yPos -= sticker.height * 0.9;
// sticker.x = xPos;
// sticker.y = yPos;
// stickerCount++;
// }
// }
// for (ind => sticker in grpStickers.members)
// {
// sticker.x = (ind % 8) * sticker.width;
// var yShit:Int = Math.floor(ind / 8);
// sticker.y += yShit * sticker.height;
// // scales it juuuust a smidge
// sticker.y += 20 * yShit;
// }
2023-04-06 05:39:27 +00:00
// another damn for loop... apologies!!!
for (ind => sticker in grpStickers.members)
{
sticker.timing = FlxMath.remapToRange(ind, 0, grpStickers.members.length, 0, 0.9);
new FlxTimer().start(sticker.timing, _ -> {
if (grpStickers == null) return;
2023-04-06 05:39:27 +00:00
sticker.visible = true;
2023-10-11 11:43:44 +00:00
var daSound:String = FlxG.random.getObject(sounds);
2024-03-23 21:50:48 +00:00
FunkinSound.playOnce(Paths.sound(daSound));
2023-04-06 05:39:27 +00:00
var frameTimer:Int = FlxG.random.int(0, 2);
// always make the last one POP
if (ind == grpStickers.members.length - 1) frameTimer = 2;
new FlxTimer().start((1 / 24) * frameTimer, _ -> {
2023-07-13 04:37:54 +00:00
if (sticker == null) return;
2023-04-06 05:39:27 +00:00
sticker.scale.x = sticker.scale.y = FlxG.random.float(0.97, 1.02);
if (ind == grpStickers.members.length - 1)
{
switchingState = true;
2024-02-13 06:38:11 +00:00
2023-04-06 05:39:27 +00:00
FlxTransitionableState.skipNextTransIn = true;
FlxTransitionableState.skipNextTransOut = true;
2024-02-12 21:50:40 +00:00
// I think this grabs the screen and puts it under the stickers?
// Leaving this commented out rather than stripping it out because it's cool...
/*
dipshit = new Sprite();
var scrn:BitmapData = new BitmapData(FlxG.width, FlxG.height, true, 0x00000000);
var mat:Matrix = new Matrix();
scrn.draw(grpStickers.cameras[0].canvas, mat);
2023-04-06 05:39:27 +00:00
2024-02-12 21:50:40 +00:00
var bitmap:Bitmap = new Bitmap(scrn);
2023-04-06 05:39:27 +00:00
2024-02-12 21:50:40 +00:00
dipshit.addChild(bitmap);
// FlxG.addChildBelowMouse(dipshit);
*/
2023-04-06 05:39:27 +00:00
FlxG.switchState(() -> {
// TODO: Rework this asset caching stuff
// NOTE: This has to come AFTER the state switch,
// otherwise the game tries to render destroyed sprites!
FunkinSprite.preparePurgeCache();
FunkinSprite.purgeCache();
return targetState(this);
});
2023-04-06 05:39:27 +00:00
}
});
});
}
2023-04-05 21:35:19 +00:00
grpStickers.sort((ord, a, b) -> {
return FlxSort.byValues(ord, a.timing, b.timing);
});
2023-04-06 05:39:27 +00:00
// centers the very last sticker
var lastOne:StickerSprite = grpStickers.members[grpStickers.members.length - 1];
lastOne.updateHitbox();
lastOne.angle = 0;
lastOne.screenCenter();
}
override public function update(elapsed:Float):Void
{
super.update(elapsed);
// if (FlxG.keys.justPressed.ANY)
// {
// regenStickers();
// }
2023-04-06 05:39:27 +00:00
}
var switchingState:Bool = false;
override public function close():Void
{
if (switchingState) return;
super.close();
}
override public function destroy():Void
{
if (switchingState) return;
super.destroy();
2023-04-05 02:38:45 +00:00
}
}
class StickerSprite extends FunkinSprite
2023-04-05 02:38:45 +00:00
{
2023-04-05 21:35:19 +00:00
public var timing:Float = 0;
2023-04-05 02:38:45 +00:00
public function new(x:Float, y:Float, stickerSet:String, stickerName:String):Void
{
2023-04-10 20:05:37 +00:00
super(x, y);
loadTexture('transitionSwag/' + stickerSet + '/' + stickerName);
2023-04-10 20:05:37 +00:00
updateHitbox();
scrollFactor.set();
2023-04-05 02:24:00 +00:00
}
}
class StickerInfo
{
public var name:String;
public var artist:String;
2023-04-05 02:38:45 +00:00
public var stickers:Map<String, Array<String>>;
public var stickerPacks:Map<String, Array<String>>;
2023-04-05 02:24:00 +00:00
public function new(stickerSet:String):Void
{
2023-04-05 04:56:02 +00:00
var path = Paths.file('images/transitionSwag/' + stickerSet + '/stickers.json');
var json = Json.parse(Assets.getText(path));
// doin this dipshit nonsense cuz i dunno how to deal with casting a json object with
// a dash in its name (sticker-packs)
var jsonInfo:StickerShit = cast json;
2023-04-05 02:38:45 +00:00
this.name = jsonInfo.name;
this.artist = jsonInfo.artist;
2023-04-05 04:56:02 +00:00
stickerPacks = new Map<String, Array<String>>();
for (field in Reflect.fields(json.stickerPacks))
{
var stickerFunny = json.stickerPacks;
var stickerStuff = Reflect.field(stickerFunny, field);
stickerPacks.set(field, cast stickerStuff);
}
// creates a similar for loop as before but for the stickers
stickers = new Map<String, Array<String>>();
for (field in Reflect.fields(json.stickers))
{
var stickerFunny = json.stickers;
var stickerStuff = Reflect.field(stickerFunny, field);
stickers.set(field, cast stickerStuff);
}
}
public function getStickers(stickerName:String):Array<String>
{
return this.stickers[stickerName];
2023-04-05 02:24:00 +00:00
}
2023-04-05 02:38:45 +00:00
public function getPack(packName:String):Array<String>
{
return this.stickerPacks[packName];
}
2023-04-05 02:24:00 +00:00
}
2023-04-05 02:38:45 +00:00
// somethin damn cute just for the json to cast to!
typedef StickerShit =
2023-04-05 02:24:00 +00:00
{
2023-04-05 02:38:45 +00:00
name:String,
artist:String,
stickers:Map<String, Array<String>>,
stickerPacks:Map<String, Array<String>>
2023-04-05 02:24:00 +00:00
}