Advertisement
EconomicSerg

Alphabet

Jul 29th, 2021
1,960
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 6.41 KB | None | 0 0
  1. package;
  2.  
  3. import flixel.FlxG;
  4. import flixel.FlxSprite;
  5. import flixel.graphics.frames.FlxAtlasFrames;
  6. import flixel.group.FlxSpriteGroup;
  7. import flixel.math.FlxMath;
  8. import flixel.util.FlxTimer;
  9.  
  10. using StringTools;
  11.  
  12. /**
  13.  * Loosley based on FlxTypeText lolol
  14.  */
  15. class Alphabet extends FlxSpriteGroup
  16. {
  17.     public var delay:Float = 0.05;
  18.     public var paused:Bool = false;
  19.  
  20.     // for menu shit
  21.     public var targetY:Float = 0;
  22.     public var isMenuItem:Bool = false;
  23.  
  24.     public var text:String = "";
  25.  
  26.     var _finalText:String = "";
  27.     var _curText:String = "";
  28.  
  29.     public var widthOfWords:Float = FlxG.width;
  30.  
  31.     var yMulti:Float = 1;
  32.  
  33.     // custom shit
  34.     // amp, backslash, question mark, apostrophy, comma, angry faic, period
  35.     var lastSprite:AlphaCharacter;
  36.     var xPosResetted:Bool = false;
  37.     var lastWasSpace:Bool = false;
  38.  
  39.     var splitWords:Array<String> = [];
  40.  
  41.     var isBold:Bool = false;
  42.  
  43.     public function new(x:Float, y:Float, text:String = "", ?bold:Bool = false, typed:Bool = false)
  44.     {
  45.         super(x, y);
  46.  
  47.         _finalText = text;
  48.         this.text = text;
  49.         isBold = bold;
  50.  
  51.         if (text != "")
  52.         {
  53.             if (typed)
  54.             {
  55.                 startTypedText();
  56.             }
  57.             else
  58.             {
  59.                 addText();
  60.             }
  61.         }
  62.     }
  63.  
  64.     public function addText()
  65.     {
  66.         doSplitWords();
  67.  
  68.         var xPos:Float = 0;
  69.         for (character in splitWords)
  70.         {
  71.             // if (character.fastCodeAt() == " ")
  72.             // {
  73.             // }
  74.  
  75.             if (character == " " || character == "-")
  76.             {
  77.                 lastWasSpace = true;
  78.             }
  79.  
  80.             if (AlphaCharacter.alphabet.indexOf(character.toLowerCase()) != -1)
  81.                 // if (AlphaCharacter.alphabet.contains(character.toLowerCase()))
  82.             {
  83.                 if (lastSprite != null)
  84.                 {
  85.                     xPos = lastSprite.x + lastSprite.width;
  86.                 }
  87.  
  88.                 if (lastWasSpace)
  89.                 {
  90.                     xPos += 40;
  91.                     lastWasSpace = false;
  92.                 }
  93.  
  94.                 // var letter:AlphaCharacter = new AlphaCharacter(30 * loopNum, 0);
  95.                 var letter:AlphaCharacter = new AlphaCharacter(xPos, 0);
  96.  
  97.                 if (isBold)
  98.                     letter.createBold(character);
  99.                 else
  100.                 {
  101.                     letter.createLetter(character);
  102.                 }
  103.  
  104.                 add(letter);
  105.  
  106.                 lastSprite = letter;
  107.             }
  108.  
  109.             // loopNum += 1;
  110.         }
  111.     }
  112.  
  113.     function doSplitWords():Void
  114.     {
  115.         splitWords = _finalText.split("");
  116.     }
  117.  
  118.     public var personTalking:String = 'gf';
  119.  
  120.     public function startTypedText():Void
  121.     {
  122.         _finalText = text;
  123.         doSplitWords();
  124.  
  125.         // trace(arrayShit);
  126.  
  127.         var loopNum:Int = 0;
  128.  
  129.         var xPos:Float = 0;
  130.         var curRow:Int = 0;
  131.  
  132.         new FlxTimer().start(0.05, function(tmr:FlxTimer)
  133.         {
  134.             // trace(_finalText.fastCodeAt(loopNum) + " " + _finalText.charAt(loopNum));
  135.             if (_finalText.fastCodeAt(loopNum) == "\n".code)
  136.             {
  137.                 yMulti += 1;
  138.                 xPosResetted = true;
  139.                 xPos = 0;
  140.                 curRow += 1;
  141.             }
  142.  
  143.             if (splitWords[loopNum] == " ")
  144.             {
  145.                 lastWasSpace = true;
  146.             }
  147.  
  148.             #if (haxe >= "4.0.0")
  149.             var isNumber:Bool = AlphaCharacter.numbers.contains(splitWords[loopNum]);
  150.             var isSymbol:Bool = AlphaCharacter.symbols.contains(splitWords[loopNum]);
  151.             #else
  152.             var isNumber:Bool = AlphaCharacter.numbers.indexOf(splitWords[loopNum]) != -1;
  153.             var isSymbol:Bool = AlphaCharacter.symbols.indexOf(splitWords[loopNum]) != -1;
  154.             #end
  155.  
  156.             if (AlphaCharacter.alphabet.indexOf(splitWords[loopNum].toLowerCase()) != -1 || isNumber || isSymbol)
  157.                 // if (AlphaCharacter.alphabet.contains(splitWords[loopNum].toLowerCase()) || isNumber || isSymbol)
  158.  
  159.             {
  160.                 if (lastSprite != null && !xPosResetted)
  161.                 {
  162.                     lastSprite.updateHitbox();
  163.                     xPos += lastSprite.width + 3;
  164.                     // if (isBold)
  165.                     // xPos -= 80;
  166.                 }
  167.                 else
  168.                 {
  169.                     xPosResetted = false;
  170.                 }
  171.  
  172.                 if (lastWasSpace)
  173.                 {
  174.                     xPos += 20;
  175.                     lastWasSpace = false;
  176.                 }
  177.                 // trace(_finalText.fastCodeAt(loopNum) + " " + _finalText.charAt(loopNum));
  178.  
  179.                 // var letter:AlphaCharacter = new AlphaCharacter(30 * loopNum, 0);
  180.                 var letter:AlphaCharacter = new AlphaCharacter(xPos, 55 * yMulti);
  181.                 letter.row = curRow;
  182.                 if (isBold)
  183.                 {
  184.                     letter.createBold(splitWords[loopNum]);
  185.                 }
  186.                 else
  187.                 {
  188.                     if (isNumber)
  189.                     {
  190.                         letter.createNumber(splitWords[loopNum]);
  191.                     }
  192.                     else if (isSymbol)
  193.                     {
  194.                         letter.createSymbol(splitWords[loopNum]);
  195.                     }
  196.                     else
  197.                     {
  198.                         letter.createLetter(splitWords[loopNum]);
  199.                     }
  200.  
  201.                     letter.x += 90;
  202.                 }
  203.  
  204.                 if (FlxG.random.bool(40))
  205.                 {
  206.                     var daSound:String = "GF_";
  207.                     FlxG.sound.play(Paths.soundRandom(daSound, 1, 4));
  208.                 }
  209.  
  210.                 add(letter);
  211.  
  212.                 lastSprite = letter;
  213.             }
  214.  
  215.             loopNum += 1;
  216.  
  217.             tmr.time = FlxG.random.float(0.04, 0.09);
  218.         }, splitWords.length);
  219.     }
  220.  
  221.     override function update(elapsed:Float)
  222.     {
  223.         if (isMenuItem)
  224.         {
  225.             var scaledY = FlxMath.remapToRange(targetY, 0, 1, 0, 1.3);
  226.  
  227.             y = FlxMath.lerp(y, (scaledY * 120) + (FlxG.height * 0.48), 0.16);
  228.             x = FlxMath.lerp(x, (targetY * 20) + 90, 0.16);
  229.         }
  230.  
  231.         super.update(elapsed);
  232.     }
  233. }
  234.  
  235. class AlphaCharacter extends FlxSprite
  236. {
  237.     public static var alphabet:String = "abcdefghijklmnopqrstuvwxyz";
  238.  
  239.     public static var numbers:String = "1234567890";
  240.  
  241.     public static var symbols:String = "|~#$%()*+-:;<=>@[]^_.,'!?";
  242.  
  243.     public var row:Int = 0;
  244.  
  245.     public function new(x:Float, y:Float)
  246.     {
  247.         super(x, y);
  248.         var tex = Paths.getSparrowAtlas('alphabet');
  249.         frames = tex;
  250.  
  251.         antialiasing = true;
  252.     }
  253.  
  254.     public function createBold(letter:String)
  255.     {
  256.         animation.addByPrefix(letter, letter.toUpperCase() + " bold", 24);
  257.         animation.play(letter);
  258.         updateHitbox();
  259.     }
  260.  
  261.     public function createLetter(letter:String):Void
  262.     {
  263.         var letterCase:String = "lowercase";
  264.         if (letter.toLowerCase() != letter)
  265.         {
  266.             letterCase = 'capital';
  267.         }
  268.  
  269.         animation.addByPrefix(letter, letter + " " + letterCase, 24);
  270.         animation.play(letter);
  271.         updateHitbox();
  272.  
  273.         FlxG.log.add('the row' + row);
  274.  
  275.         y = (110 - height);
  276.         y += row * 60;
  277.     }
  278.  
  279.     public function createNumber(letter:String):Void
  280.     {
  281.         animation.addByPrefix(letter, letter, 24);
  282.         animation.play(letter);
  283.  
  284.         updateHitbox();
  285.     }
  286.  
  287.     public function createSymbol(letter:String)
  288.     {
  289.         switch (letter)
  290.         {
  291.             case '.':
  292.                 animation.addByPrefix(letter, 'period', 24);
  293.                 animation.play(letter);
  294.                 y += 50;
  295.             case "'":
  296.                 animation.addByPrefix(letter, 'apostraphie', 24);
  297.                 animation.play(letter);
  298.                 y -= 0;
  299.             case "?":
  300.                 animation.addByPrefix(letter, 'question mark', 24);
  301.                 animation.play(letter);
  302.             case "!":
  303.                 animation.addByPrefix(letter, 'exclamation point', 24);
  304.                 animation.play(letter);
  305.         }
  306.  
  307.         updateHitbox();
  308.     }
  309. }
  310.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement