Change BGScrollingText to a single FlxText

BGScrollingText is now a single FlxText drawn multiple time in a single frame which is more optimized that a FlxSpriteGroup with multiple FlxSprites taking the text's graphic
This commit is contained in:
Furo
2025-12-06 21:23:46 -05:00
committed by Hundrec
parent 2c588b6a93
commit 67846d6c4b
2 changed files with 54 additions and 96 deletions
+48 -90
View File
@@ -1,125 +1,83 @@
package funkin.ui.freeplay;
import flixel.FlxObject;
import flixel.group.FlxSpriteGroup;
import flixel.text.FlxText;
import flixel.util.FlxSort;
import flixel.FlxSprite;
import flixel.util.FlxDestroyUtil;
import flixel.util.FlxColor;
import flixel.math.FlxPoint;
// its kinda like marqeee html lol!
@:nullSafety
class BGScrollingText extends FlxSpriteGroup
class BGScrollingText extends FlxText
{
var grpTexts:FlxTypedSpriteGroup<FlxSprite>;
var sourceText:FlxText;
var _textPositions:Array<FlxPoint> = [];
var _positionCache:FlxPoint = FlxPoint.get();
public var widthShit:Float = FlxG.width;
public var placementOffset:Float = 20;
public var speed:Float = 1;
public var size(default, set):Int = 48;
public var funnyColor(default, set):FlxColor = 0xFFFFFFFF;
public function new(x:Float, y:Float, text:String, widthShit:Float = 100, ?bold:Bool = false, ?size:Int = 48)
{
super(x, y);
grpTexts = new FlxTypedSpriteGroup<FlxSprite>();
super(x, y, 0, text, size);
_positionCache = FlxPoint.get(x, y);
font = "5by7";
this.bold = bold ?? false;
this.widthShit = widthShit;
// Only keep one FlxText graphic at a time for batching
sourceText = new FlxText(0, 0, 0, text, size ?? this.size);
sourceText.font = "5by7";
sourceText.bold = bold ?? false;
@:privateAccess
sourceText.regenGraphic();
regenGraphic();
var needed:Int = Math.ceil(widthShit / sourceText.frameWidth) + 1;
var needed:Int = Math.ceil(widthShit / frameWidth) + 1;
for (i in 0...needed)
{
var coolText = new FlxSprite((i * sourceText.frameWidth) + (i * 20), 0);
grpTexts.add(coolText);
}
if (size != null) this.size = size;
add(grpTexts);
}
function reloadGraphics()
{
if (grpTexts != null)
{
@:privateAccess
sourceText.regenGraphic();
grpTexts.forEach(function(txt:FlxSprite) {
txt.loadGraphic(sourceText.graphic);
txt.updateHitbox();
});
_textPositions.push(FlxPoint.get((i * frameWidth) + (i * 20), 0));
}
}
function set_size(value:Int):Int
override public function update(elapsed:Float):Void
{
sourceText.size = value;
reloadGraphics();
this.size = value;
return value;
}
function set_funnyColor(value:FlxColor):FlxColor
{
sourceText.color = value;
reloadGraphics();
this.funnyColor = value;
return value;
}
override public function update(elapsed:Float)
{
for (txt in grpTexts.group)
{
if (txt == null) continue;
txt.x -= 1 * (speed * (elapsed / (1 / 60)));
if (speed > 0)
{
if (txt.x < -txt.frameWidth)
{
txt.x = grpTexts.group.members[grpTexts.length - 1].x + grpTexts.group.members[grpTexts.length - 1].frameWidth + placementOffset;
sortTextShit();
}
}
else
{
if (txt.x > txt.frameWidth * 2)
{
txt.x = grpTexts.group.members[0].x - grpTexts.group.members[0].frameWidth - placementOffset;
sortTextShit();
}
}
}
super.update(elapsed);
for (txtPosition in _textPositions)
{
if (txtPosition == null) continue;
txtPosition.x -= 1 * (speed * (elapsed / (1 / 60)));
if (speed > 0) // Going left
{
if (txtPosition.x < -frameWidth)
{
txtPosition.x = _textPositions[_textPositions.length - 1].x + frameWidth + placementOffset;
sortTextShit();
}
}
else // Going right
{
if (txtPosition.x > frameWidth * 2)
{
txtPosition.x = _textPositions[0].x - frameWidth - placementOffset;
sortTextShit();
}
}
}
}
override public function draw():Void
{
_positionCache.set(x, y);
for (position in _textPositions)
{
setPosition(_positionCache.x + position.x, _positionCache.y + position.y);
super.draw();
}
setPosition(_positionCache.x, _positionCache.y);
}
function sortTextShit():Void
{
grpTexts.sort(function(Order:Int, Obj1:FlxObject, Obj2:FlxObject) {
return FlxSort.byValues(Order, Obj1.x, Obj2.x);
_textPositions.sort(function(Obj1:FlxPoint, Obj2:FlxPoint) {
return FlxSort.byValues(FlxSort.ASCENDING, Obj1.x, Obj2.x);
});
}
override function destroy():Void
{
super.destroy();
sourceText = FlxDestroyUtil.destroy(sourceText);
}
}
@@ -131,27 +131,27 @@ class NewCharacterCard extends BackingCard
darkBg = new FlxSprite(0, 0).loadGraphic(bitmap);
add(darkBg);
friendFoe.funnyColor = 0xFF139376;
friendFoe.color = 0xFF139376;
friendFoe.speed = -4;
add(friendFoe);
newUnlock1.funnyColor = 0xFF99BDF2;
newUnlock1.color = 0xFF99BDF2;
newUnlock1.speed = 2;
add(newUnlock1);
waiting.funnyColor = 0xFF40EA84;
waiting.color = 0xFF40EA84;
waiting.speed = -2;
add(waiting);
newUnlock2.funnyColor = 0xFF99BDF2;
newUnlock2.color = 0xFF99BDF2;
newUnlock2.speed = 2;
add(newUnlock2);
friendFoe2.funnyColor = 0xFF139376;
friendFoe2.color = 0xFF139376;
friendFoe2.speed = -4;
add(friendFoe2);
newUnlock3.funnyColor = 0xFF99BDF2;
newUnlock3.color = 0xFF99BDF2;
newUnlock3.speed = 2;
add(newUnlock3);