Skip to content

Commit

Permalink
starting to work on note shit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fyrid19 committed Sep 4, 2024
1 parent 8d9c56b commit ac1b95b
Show file tree
Hide file tree
Showing 12 changed files with 199 additions and 26 deletions.
44 changes: 41 additions & 3 deletions source/backend/assets/FunkinCache.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import flixel.graphics.FlxGraphic;
import flash.media.Sound;
import lime.utils.Assets;

import openfl.utils.ByteArray;

class FunkinCache {
public var graphicCache:Map<String, FlxGraphic> = [];
var backupGraphicCache:Map<String, FlxGraphic> = [];
public var soundCache:Map<String, Sound> = [];
public var backupGraphicCache:Map<String, FlxGraphic> = [];
public var backupSoundCache:Map<String, Sound> = [];
var backupSoundCache:Map<String, Sound> = [];

public function new() {
graphicCache = new Map<String, FlxGraphic>();
Expand Down Expand Up @@ -76,9 +78,45 @@ class FunkinCache {
}
}

// experimental
public function cacheGraphicByte(key:String) {
var bitmap:BitmapData;
var bytes:ByteArray = new ByteArray();

if (graphicCache.exists(key)) {
trace('Graphic already cached');
return;
}

if (backupGraphicCache.exists(key)) {
var graphic = backupGraphicCache.get(key);
backupGraphicCache.remove(key);
graphicCache.set(key, graphic);
return;
}

try (bytes = ByteArray.fromFile(key))
catch (e) {
trace('Graphic not loaded: ' + key);
return;
}

// bytes.deflate();
try (bitmap = BitmapData.fromBytes(bytes))
catch (e) {
trace('ByteArray cannot be read');
return;
}

var graphic:FlxGraphic = FlxGraphic.fromBitmapData(bitmap, false, key);
graphic.persist = true;
graphicCache.set(key, graphic);
trace('Graphic cached: ' + key);
}

public function getGraphic(key:String) {
if (!graphicCache.exists(key))
cacheGraphic(key);
cacheGraphicByte(key);
return graphicCache.get(key);
}

Expand Down
35 changes: 32 additions & 3 deletions source/backend/assets/Paths.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import openfl.utils.Assets as OpenFlAssets;
import openfl.system.System;
import lime.utils.Assets;

// caching system is like a mix of funkin and flashcache so credits to them i guess
// caching system is like a mix of funkin and flashcache/psych so credits to them i guess
@:access(openfl.display.BitmapData)
class Paths {
inline public static var SOUND_EXT = #if web "mp3" #else "ogg" #end; // Sound file type

Expand All @@ -30,8 +31,16 @@ class Paths {
assetCache = new FunkinCache();
}

@:access(flixel.system.frontEnds.BitmapFrontEnd._cache)
public static function clearCaches() {
OpenFlAssets.cache.clear('assets/songs');
for (key in FlxG.bitmap._cache.keys()) {
if (!assetCache.graphicCache.exists(key)) {
if (FlxG.bitmap.get(key).bitmap.__texture != null)
FlxG.bitmap.get(key).bitmap.__texture.dispose();
FlxG.bitmap.remove(FlxG.bitmap.get(key));
}
}
FlxG.bitmap.dumpCache(); // i am desperate to clean memory
assetCache.clearCaches();
System.gc();
Expand Down Expand Up @@ -130,7 +139,27 @@ class Paths {
inline public static function noteSkin(key:String, ?library:String)
return dataFile('skins/$key.json', library);

// im gonna make these all into one function later
inline public static function getAtlas(key:String, ?ext:String = 'xml', ?library:String) {
var path:String = file('images/$key', library);
path = OpenFlAssets.exists('$path.$ext') ? path : file('$key');
var graphic:FlxGraphic = image(key, library);
if (OpenFlAssets.exists('$path.$ext')) {
if (ext == 'xml') {
return FlxAtlasFrames.fromSparrow(graphic, '$path.$ext');
} else if (ext == 'txt') {
return FlxAtlasFrames.fromSpriteSheetPacker(graphic, '$path.$ext');
} else if (ext == 'json') {
return FlxAtlasFrames.fromAseprite(graphic, '$path.$ext');
} else {
trace('${ext.toUpperCase()} files are not supported');
return null;
}
} else {
trace('File not found: $path.$ext');
return null;
}
}

inline public static function getSparrowAtlas(key:String, ?library:String) {
var ext:String = 'xml';
var path:String = file('images/$key.$ext', library);
Expand Down Expand Up @@ -176,7 +205,7 @@ class Paths {
// multiple spritesheet support (credits to NeeEoo for base code)
// this assumes all your spritesheet file types are the same
inline public static function getMultiSpritesheetFrames(keys:Array<String>, ?type:String = 'xml', ?library:String) {
var framesToReturn:FlxAtlasFrames;
var framesToReturn:FlxFramesCollection = new FlxFramesCollection(null);
for (i in 0...keys.length) {
switch (type) {
case 'xml' | 'sparrow':
Expand Down
8 changes: 8 additions & 0 deletions source/funkin/modding/FunkinScript.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package funkin.modding;

/**
* The basics for a modded script, both Lua AND HScript
*/
class FunkinScript {

}
5 changes: 0 additions & 5 deletions source/funkin/modding/hscript/FunkinScript.hx

This file was deleted.

2 changes: 1 addition & 1 deletion source/funkin/objects/notes/Note.hx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Note extends FlxSprite {
updateHitbox();

default:
frames = Paths.getSparrowAtlas('NOTE_assets');
frames = Paths.getAtlas('NOTE_assets');

animation.addByPrefix('greenScroll', 'green instance');
animation.addByPrefix('redScroll', 'red instance');
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/objects/notes/NoteBasic.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ interface NoteBasic {
public var strumTime:Float;
public var noteType:String;
public var noteTypeID:Int;
public var noteData:Direction;
public var noteDirection:Direction;
public var strumLine:StrumLine;
public var noteSkin:NoteSkin;
}
12 changes: 6 additions & 6 deletions source/funkin/objects/notes/NoteNew.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class NoteNew extends FlxSprite implements NoteBasic {
public var noteType:String = ''; // The name of the note type (Nothing for default)
public var noteTypeID:Int = 0; // The ID of the note type (0 is default)
public var noteSkin:NoteSkin; // Self explanatory, note type skin overrides this
public var noteData:Direction = 0; // 0 = Left, 1 = Down, 2 = Up, 3 = Right, can add more in NoteData
public var noteDirection:Direction = 0; // 0 = Left, 1 = Down, 2 = Up, 3 = Right, can add more in NoteData
public var strumLine:StrumLine; // The strum line the note is assigned to

public var hasSustain:Bool; // If the note has a sustain trail or not
Expand All @@ -23,7 +23,7 @@ class NoteNew extends FlxSprite implements NoteBasic {

public static var swagWidth:Float = 160 * 0.7; // Used for positioning the note

public function new(strumTime:Float, noteData:Direction, ?hasSustain:Bool = false, ?noteType:Int = 0, strumLine:StrumLine) {
public function new(strumTime:Float, noteDirection:Direction, ?hasSustain:Bool = false, ?noteType:Int = 0, strumLine:StrumLine) {
super();

x += 50;
Expand All @@ -32,7 +32,7 @@ class NoteNew extends FlxSprite implements NoteBasic {

this.hasSustain = hasSustain;
this.strumTime = strumTime;
this.noteData = noteData;
this.noteDirection = noteDirection;
this.strumLine = strumLine;
noteSkin.parse();

Expand All @@ -48,20 +48,20 @@ class NoteNew extends FlxSprite implements NoteBasic {

antialiasing = noteSkin.antialiasing;

var color:String = noteData.getColor();
var color:String = noteDirection.getColor();
if (noteSkin.atlasIncluded) {
frames = Paths.getSparrowAtlas(noteSkin.path);
animation.addByPrefix('scroll', '$color instance');
setGraphicSize(Std.int(width * 0.7));
updateHitbox();
} else {
loadGraphic(Paths.image(noteSkin.path), true, noteSkin.extraData[0], noteSkin.extraData[1]);
animation.add('scroll', [4 + noteData]);
animation.add('scroll', [4 + noteDirection]);
setGraphicSize(Std.int(width * noteSkin.extraData[4]));
updateHitbox();
}

x += swagWidth * noteData;
x += swagWidth * noteDirection;
animation.play('scroll');
}

Expand Down
58 changes: 57 additions & 1 deletion source/funkin/objects/notes/StrumLine.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,61 @@
package funkin.objects.notes;

import funkin.objects.notes.NoteBasic;
import funkin.objects.notes.NoteNew as Note;
import funkin.objects.notes.SustainTrail;
import flixel.util.FlxSort;

class StrumLine extends FlxSpriteGroup {
public var scrollSpeed:Float = 1.0;
public var scrollSpeed:Float;
public var playerStrum:Bool;
public var downScroll:Bool;

public var strumNotes:FlxTypedGroup<StrumNote>;
public var notes:FlxTypedGroup<NoteBasic>;
public var unspawnNotes:Array<NoteBasic>;

public function new(scrollSpeed:Float = 1.0, playerStrum:Bool = false, ?downScroll:Bool) {
this.scrollSpeed = scrollSpeed;
this.playerStrum = playerStrum;
this.downScroll = downScroll != null ? downScroll : FunkinData.data.get('downScroll');

unspawnNotes = new Array<NoteBasic>();

strumNotes = new FlxTypedGroup<StrumNote>();
add(strumNotes);

notes = new FlxTypedGroup<NoteBasic>();
add(notes);
}

public function sortNotes() {
notes.sort(sortNotesByTime, downScroll ? FlxSort.ASCENDING : FlxSort.DESCENDING);
}

public function addNotes(notes:Array<Note>) {
for (note in notes) { // this is just to make sure none of the data is like fucked up or smth
var strumTime:Float = note.strumTime;
var noteDirection:Direction = note.noteDirection;
var playerNote:Bool = note.playerNote;
var hasSustain:Bool = note.hasSustain;
var sustainLength:Float = note.sustainLength;
var noteType:Int = note.noteType;

// player note? in MY cpu strum?
// im the old strum, i want normal notes!
if (playerNote != playerStrum) playerNote = playerStrum;

// cant have a sustain note with no length
if (hasSustain && sustainLength == 0) hasSustain = false;
sustainLength = sustainLength / Conductor.stepCrochet;

// not sure if this causes like memory leaks or anything
var newNote:Note = new Note(strumTime, noteDirection, hasSustain, noteType, this);
newNote.scrollFactor.set();
unspawnNotes.push(newNote);
}
}

function sortNotesByTime(order:Int = FlxSort.ASCENDING, Obj1:Note, Obj2:Note)
return FlxSort.byValues(order, Obj1.strumTime, Obj2.strumTime);
}
6 changes: 3 additions & 3 deletions source/funkin/objects/notes/SustainTrail.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ class SustainTrail extends FlxStrip implements NoteBasic {
public var noteType:String = ''; // The name of the note type (Nothing for default)
public var noteTypeID:Int = 0; // The ID of the note type (0 is default)
public var noteSkin:NoteSkin; // Self explanatory, note type skin overrides this
public var noteData:Direction = 0; // 0 = Left, 1 = Down, 2 = Up, 3 = Right, can add more in NoteData
public var noteDirection:Direction = 0; // 0 = Left, 1 = Down, 2 = Up, 3 = Right, can add more in NoteData
public var strumLine:StrumLine; // The strum line the note is assigned to
public var parentNote:NoteNew; // The note the sustain trail belongs to

public var sustainLength(default, set):Float = 0; // The length of the sustain

// loadGraphic(Paths.image(noteSkin.extraPaths[0]), true, noteSkin.extraData[2], noteSkin.extraData[3]);

public function new(strumLine:StrumLine, noteData:Int, sustainLength:Float) {
public function new(strumLine:StrumLine, noteDirection:Int, sustainLength:Float) {
super();

this.strumLine = strumLine;
this.noteData = noteData;
this.noteDirection = noteDirection;
this.sustainLength = sustainLength;

alpha = FunkinData.data.get('sustainAlpha');
Expand Down
32 changes: 32 additions & 0 deletions source/funkin/sound/FunkinSound.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package funkin.sound;

import flixel.sound.FlxSound;

@:structInit
class SoundParams {
// Parameters
var looped:Bool;
var forceRestart:Bool;
var partialParams:Array<Float>;
var autoDestroy:Bool;
var persist:Bool;

// Callbacks
var onComplete:Void->Void;
var onStart:Void->Void;
var onPause:Void->Void;
var onResume:Void->Void;
}

/**
* FlxSound with some extra features
*/
@:access(flixel.sound.FlxSound)
class FunkinSound {
var _sound:FlxSound;

public function play(path:String, params:SoundParams) {
_sound = new FlxSound();
_sound.play(params.forceRestart);
}
}
9 changes: 6 additions & 3 deletions source/funkin/states/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class PlayState extends MusicBeatState

Conductor.mapBPMChanges(SONG);
Conductor.changeBPM(SONG.bpm);

Paths.clearCaches();

foregroundSprites = new FlxTypedGroup<BGSprite>();
Expand Down Expand Up @@ -1331,13 +1331,16 @@ class PlayState extends MusicBeatState
video.screenCenter();
}
});
video.bitmap.onEndReached.add(onComplete);
video.bitmap.onEndReached.add(video.destroy);
add(video);

if (video.load(Paths.video(videoPath)))
if (video.load(Paths.video(videoPath))) {
new FlxTimer().start(0.001, (t) -> video.play());
else
} else {
trace('Video not loaded!');
onComplete();
}
}
#end

Expand Down
12 changes: 12 additions & 0 deletions source/funkin/states/debug/TestPlayState.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package funkin.states.debug;

import funkin.objects.Song.SwagSong;

class TestPlayState extends MusicBeatState {
public var strumLines:FlxTypedGroup<StrumLine>;
public static var SONG:SwagSong;

override function create() {

}
}

0 comments on commit ac1b95b

Please sign in to comment.