package; import flixel.FlxG; import flixel.FlxSprite; import flixel.graphics.frames.FlxAtlasFrames; import flixel.group.FlxSpriteGroup; import flixel.math.FlxMath; import flixel.util.FlxTimer; using StringTools; /** * Loosley based on FlxTypeText lolol */ class Alphabet extends FlxSpriteGroup { public var delay:Float = 0.05; public var paused:Bool = false; public var text:String = ""; var _finalText:String = ""; var _curText:String = ""; public var widthOfWords:Float = FlxG.width; var yMulti:Float = 1; // custom shit // amp, backslash, question mark, apostrophy, comma, angry faic, period var lastSprite:AlphaCharacter; var xPosResetted:Bool = false; var lastWasSpace:Bool = false; var splitWords:Array = []; var isBold:Bool = false; public function new(x:Float, y:Float, text:String = "", ?bold:Bool = false, typed:Bool = false) { super(x, y); _finalText = text; this.text = text; isBold = bold; if (text != "") { if (typed) { startTypedText(); } else { addText(); } } } public function addText() { doSplitWords(); for (character in splitWords) { // if (character.fastCodeAt() == " ") // { // } if (character == " ") { lastWasSpace = true; } if (AlphaCharacter.alphabet.contains(character.toLowerCase())) { var xPos:Float = 0; if (lastSprite != null) { xPos = lastSprite.x + lastSprite.width - 40; } if (lastWasSpace) { xPos += 40; lastWasSpace = false; } // var letter:AlphaCharacter = new AlphaCharacter(30 * loopNum, 0); var letter:AlphaCharacter = new AlphaCharacter(xPos, 0); if (isBold) letter.createBold(character); else letter.createLetter(character); add(letter); lastSprite = letter; } // loopNum += 1; } } function doSplitWords():Void { splitWords = _finalText.split(""); } public function startTypedText():Void { _finalText = text; doSplitWords(); // trace(arrayShit); var loopNum:Int = 0; var xPos:Float = 0; new FlxTimer().start(0.05, function(tmr:FlxTimer) { // trace(_finalText.fastCodeAt(loopNum) + " " + _finalText.charAt(loopNum)); if (_finalText.fastCodeAt(loopNum) == "\n".code) { yMulti += 1; xPosResetted = true; // xPos = 0; } if (splitWords[loopNum] == " ") { lastWasSpace = true; } if (AlphaCharacter.alphabet.contains(splitWords[loopNum].toLowerCase())) { if (lastSprite != null && !xPosResetted) { lastSprite.updateHitbox(); xPos += lastSprite.width + 3; // if (isBold) // xPos -= 80; } else { xPosResetted = false; } if (lastWasSpace) { xPos += 20; lastWasSpace = false; } // trace(_finalText.fastCodeAt(loopNum) + " " + _finalText.charAt(loopNum)); // var letter:AlphaCharacter = new AlphaCharacter(30 * loopNum, 0); var letter:AlphaCharacter = new AlphaCharacter(xPos, 55 * yMulti); if (isBold) { letter.createBold(splitWords[loopNum]); } else { letter.createLetter(splitWords[loopNum]); letter.x += 90; } add(letter); lastSprite = letter; } loopNum += 1; tmr.time = FlxG.random.float(0.05, 0.12); }, splitWords.length); } override function update(elapsed:Float) { super.update(elapsed); } } class AlphaCharacter extends FlxSprite { public static var alphabet:String = "abcdefghijklmnopqrstuvwxyz"; var numbers:String = "1234567890"; var symbols:String = "|~#$%()*+-:;<=>@[]^_"; public function new(x:Float, y:Float) { super(x, y); var tex = FlxAtlasFrames.fromSparrow(AssetPaths.alphabet__png, AssetPaths.alphabet__xml); frames = tex; antialiasing = true; } public function createBold(letter:String) { animation.addByPrefix(letter, letter.toUpperCase() + " bold", 24); animation.play(letter); updateHitbox(); } public function createLetter(letter:String):Void { var letterCase:String = "lowercase"; if (letter.toLowerCase() != letter) { letterCase = 'capital'; } animation.addByPrefix(letter, letter + " " + letterCase, 24); animation.play(letter); updateHitbox(); } }