Advertisement
Guest User

Dialog.hx

a guest
Jan 24th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Haxe 14.72 KB | None | 0 0
  1. package;
  2.  
  3. import entities.Button;
  4. import entities.weapons.BaseWeapon;
  5. import extensions.FlxButtonExt;
  6. import flixel.FlxG;
  7. import flixel.FlxState;
  8. import flixel.FlxSprite;
  9. import flixel.FlxObject;
  10. import flixel.addons.text.FlxTypeText;
  11. import flixel.addons.ui.FlxUI9SliceSprite;
  12. import flixel.system.FlxAssets;
  13. import flixel.text.FlxText;
  14. import flixel.group.FlxGroup;
  15. import flixel.tile.FlxTilemap;
  16. import flixel.tweens.FlxEase;
  17. import flixel.ui.FlxButton;
  18. import flixel.util.FlxColor;
  19. import flixel.math.FlxPoint;
  20. import flash.Lib;
  21. import flixel.util.FlxTimer;
  22. import flixel.math.FlxRect;
  23. import states.StateGameplay;
  24. import flixel.math.FlxRandom;
  25. import particles.ParticlesGroup;
  26. import particles.BaseParticle;
  27. import flixel.util.FlxSpriteUtil;
  28. import flash.display.Graphics;
  29. import flixel.tweens.FlxTween;
  30.  
  31. import flash.display.BitmapData;
  32. import flash.filters.BitmapFilterQuality;
  33. import flash.filters.BlurFilter;
  34. import flash.geom.Point;
  35. import flash.geom.Rectangle;
  36. import flash.utils.Dictionary;
  37. import flixel.FlxCamera;
  38. import flash.display.BlendMode;
  39. import flash.geom.Matrix;
  40.  
  41. class Dialog extends FlxGroup
  42. {
  43.     private var txt_dialog:FlxTypeText;
  44.     var spr_balloon:FlxUI9SliceSprite;
  45.     var spr_pico:FlxSprite;
  46.     var borderOffset:FlxPoint;
  47.    
  48.     var sequence:Int = 0;
  49.     var btn_avance:FlxButtonExt;
  50.     var isBig:Bool = false;
  51.     public var trigger_end:String;
  52.    
  53.     public function new(Big:Bool = false)
  54.     {
  55.         super();
  56.        
  57.         isBig = Big;
  58.        
  59.         create();
  60.     }
  61.  
  62.     var dialog:Array<String> = new Array();
  63.    
  64.     private function parseText(text:String):Void
  65.     {  
  66.         if (dialog.length != 0)
  67.         dialog = [];//empty array
  68.        
  69.         var lines:Array<String> = text.split("#");// split in all lines
  70.        
  71.         for (i in 0 ... lines.length)
  72.         dialog.push(lines[i]);
  73.  
  74.     //  trace("dialog string is " + dialog);
  75.     }
  76.        
  77.     public function create():Void
  78.     {
  79.         // Make the screen background lighter for visibility sake
  80.         FlxG.camera.bgColor = 0xFF999999;
  81.  
  82.         borderOffset = new FlxPoint(26, 18);
  83.  
  84.         // This is a 9-slice image with the image loaded and sliced
  85.         var _graphic:String = "assets/img/ui/9slice/bubble_small.png";
  86.         // This is the [x1,y1,x2,y2] slice points in the image.
  87.         // x1,y1 is the top-left, x2,y2 is the bottom-right.
  88.         var _slice:Array<Int> = [23, 39, 102, 69];
  89.    
  90.         if (isBig)
  91.         {
  92.             borderOffset = new FlxPoint(12, 12);//14.14
  93.             spr_balloon = new FlxUI9SliceSprite(0, 70, "assets/img/ui/9slice/dialog_big.png", new Rectangle(0, 0, 800, 318), [31, 31, 69, 69]);
  94.         }
  95.         else
  96.         spr_balloon = new FlxUI9SliceSprite(210, 70, _graphic, new Rectangle(0,0,100,50), _slice);
  97.        
  98.         add(spr_balloon);
  99.  
  100.         if (isBig)
  101.         {      
  102.             var size:Float = FlxG.width * 0.6;//0.7
  103.            
  104.             txt_dialog = new FlxTypeText(spr_balloon.x + borderOffset.x / 2, spr_balloon.y + borderOffset.y, Std.int(size), "test", 18, true);
  105.             txt_dialog.setFormat("assets/font/SF_Cartoonist_Hand_Bold.ttf", 28, FlxColor.BLACK, "left", FlxTextBorderStyle.OUTLINE, 0xaaD8D8D8);
  106.             txt_dialog.alpha = 0.7;
  107.            
  108.                 Utils.CenterToStage(spr_balloon);
  109.         }
  110.         else
  111.         {
  112.             txt_dialog = new FlxTypeText(spr_balloon.x + borderOffset.x / 2, spr_balloon.y + borderOffset.y, 200, "test", 18, true);
  113.             txt_dialog.setFormat("assets/font/SF_Cartoonist_Hand_Bold.ttf", 26, FlxColor.BLACK, "center", FlxTextBorderStyle.SHADOW, FlxColor.WHITE);
  114.         }
  115.        
  116.     //  txt_dialog.delay = 0.1;
  117.         txt_dialog.delay = 0.1;
  118.     //  txt_dialog.eraseDelay = 0.2;
  119.         txt_dialog.eraseDelay = 1;
  120.         txt_dialog.cursorBlinkSpeed = 1.0;
  121.         txt_dialog.prefix = "";
  122.     //  txt_dialog.autoErase = true;
  123.         //txt_dialog.autoErase = false;
  124.         //txt_dialog.waitTime = 2.0;
  125.         txt_dialog.setTypingVariation(0.75, true);
  126.        
  127.         add(txt_dialog);
  128.        
  129.         if (isBig)
  130.         btn_avance = new FlxButtonExt(0, 0, null, advance, null, null, "assets/img/ui/avance_big.png", false);
  131.         else
  132.         btn_avance = new FlxButtonExt(0, 0, null, advance, null, null, "assets/img/ui/avance.png", false);
  133.  
  134.         add(btn_avance);   
  135.         btn_avance.scrollFactor.set(1, 1);
  136.        
  137.         spr_pico = new FlxSprite("assets/img/ui/9slice/bubble_pico.png");
  138.        
  139.         if (isBig)
  140.         {
  141.         spr_pico = new FlxSprite("assets/img/ui/dialog/char1.png");
  142.        
  143.         spr_pico.scale.set(0.3, 0.3);
  144.         spr_pico.width *= spr_pico.scale.x;
  145.         spr_pico.height *= spr_pico.scale.y;
  146.         spr_pico.centerOffsets();
  147.        
  148.         }
  149.         add(spr_pico);
  150.        
  151.        
  152.         spr_pico.visible = false;
  153.         btn_avance.visible = false;
  154.         spr_balloon.visible = false;
  155.         txt_dialog.visible = false;
  156.        
  157.         if (isBig)
  158.         {
  159.             spr_pico.scrollFactor.set();
  160.             btn_avance.scrollFactor.set();
  161.             spr_balloon.scrollFactor.set();
  162.             txt_dialog.scrollFactor.set();
  163.         }
  164.     }
  165.        
  166.     public function start(dialogChain:String):Void
  167.     {      
  168.         if ( GameMode.IS == GameMode.VERSUS ||    GameMode.IS_SURVIVAL)
  169.         return;
  170.        
  171.         if (isBig && spr_balloon.alpha != 1&& spr_balloon.alpha != 0)
  172.         {
  173.         //  trace("still transitioning - return");
  174.             //might be still transitioning
  175.             return;
  176.         }
  177.            
  178.         sequence = 0;
  179.         pausedTimer = 0;
  180.         blahTimer = 0;
  181.         mouthOpen = false;
  182.         emotionPresent = false;
  183.        
  184.         var new_text:String = StringTools.replace(dialogChain, "player_name",  Sys.getEnv("USERNAME"));
  185.        
  186.         parseText(new_text);
  187.        
  188.     //  trace(dialog.length);
  189.        
  190.         if (dialog.length == 1)//this is in the case that is a one liner (no dialog chunks)
  191.         {          
  192.             emotionPresent = false;
  193.            
  194.             var new_text_no_smileys:String = parseEmotions(new_text);
  195.  
  196.             if (emotionPresent)
  197.             {
  198.                 parseText(new_text_no_smileys);
  199.             //  trace("new_text_no_smileys");
  200.             }
  201.             else
  202.             {
  203.                 parseText(new_text);
  204.             //  trace("new_text");
  205.             }
  206.         }
  207.            
  208.         if (isBig)
  209.         {
  210.             spr_balloon.resize(txt_dialog.width + borderOffset.x*3+spr_pico.width, spr_pico.height + borderOffset.x*2); //uses avatar's height
  211.             Utils.CenterToStage(spr_balloon);
  212.            
  213.             spr_balloon.alpha = 0;
  214.             txt_dialog.alpha = 0;
  215.             spr_pico.alpha = 0;
  216.             btn_avance.alpha = 0;
  217.            
  218.             FlxTween.tween(spr_balloon, { alpha: 1 }, 0.5, { ease: FlxEase.quadOut } );
  219.             FlxTween.tween(txt_dialog, { alpha: 0.7 }, 0.5, { ease: FlxEase.quadOut } );
  220.             FlxTween.tween(spr_pico, { alpha: 1 }, 0.5, { ease: FlxEase.quadOut } );
  221.             FlxTween.tween(btn_avance, { alpha: 1 }, 0.5, { ease: FlxEase.quadOut } );
  222.  
  223.             spr_balloon.y  = FlxG.height-spr_balloon.height-30;
  224.        
  225.             FlxTween.tween(spr_balloon, { y: spr_balloon.y +20 }, 2, { ease: FlxEase.elasticOut } );
  226.         }
  227.        
  228.         spr_pico.visible = true;
  229.         btn_avance.visible = true;
  230.         spr_balloon.visible = true;
  231.         txt_dialog.visible = true;
  232.        
  233.         emotionPresent = false;
  234.                    
  235.         var new_text:String = parseEmotions(dialog[sequence]);
  236.        
  237.         if (emotionPresent)
  238.         {
  239.             txt_dialog.resetText(new_text);
  240.         //  trace("dialog " + new_text);
  241.         }
  242.         else
  243.         {
  244.             txt_dialog.resetText(dialog[sequence].toString());     
  245.         //  trace("dialog " + dialog[sequence].toString());
  246.         }
  247.        
  248.     //  txt_dialog.resetText(dialog[sequence].toString());
  249.        
  250.         txt_dialog.start(0.02, false, false, null, onComplete.bind("Fully typed"));
  251.     }
  252.    
  253.     var pausedTimer:Float = 0;
  254.     var blahTimer:Float = 0;
  255.     var mouthOpen:Bool = false;
  256.     var emotionPresent:Bool = false;
  257.     var currentChar:String;
  258.    
  259.     override public function update(elapsed:Float):Void
  260.     {
  261.         super.update(elapsed);
  262.        
  263.         if (txt_dialog.paused&&txt_dialog.visible)
  264.         {
  265.             pausedTimer += elapsed;
  266.            
  267.             if (pausedTimer >= 1 + (txt_dialog.text.length/12))
  268.             {          
  269.             //  trace("waited "+ (1 + (txt_dialog.text.length/12)) +" seconds, skipping");
  270.                 pausedTimer = 0;
  271.                
  272.                 advance();
  273.             }
  274.            
  275.             if (!emotionPresent)
  276.             {
  277.             //  if (StateGameplay.instance != null)
  278.             //  StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_neutral");
  279.             }
  280.         }
  281.         else
  282.         {
  283.             if (txt_dialog.visible&&!emotionPresent||isBig&&spr_balloon.visible)
  284.             {
  285.                 blahTimer += elapsed;
  286.                
  287.                 var blahDelay:Float = 0.1;
  288.                 if (isBig)
  289.                 blahDelay = 0.3;
  290.                 if (blahTimer >= blahDelay)
  291.                 {
  292.                     blahTimer = 0;
  293.                 //  trace("blah");
  294.                    
  295.                     if (isBig)
  296.                     {
  297.                         if(currentChar == "char1")
  298.                         FlxG.sound.play("assets/sounds/dialog/dialog" + FlxG.random.int(1, 9) + ".ogg");
  299.                         else
  300.                         FlxG.sound.play("assets/sounds/dialog/robot" + FlxG.random.int(1, 6) + ".ogg");
  301.                     }
  302.                    
  303.                     if (!mouthOpen)
  304.                     {
  305.                         mouthOpen = true;
  306.                        
  307.                         if (StateGameplay.instance != null)
  308.                         {
  309.                         //  StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_speak");
  310.                         }
  311.                     }
  312.                     else
  313.                     {
  314.                         mouthOpen = false;
  315.                        
  316.                         if (StateGameplay.instance != null)
  317.                         {
  318.                         //  StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_neutral");
  319.                         }
  320.                     }
  321.                 }
  322.             }
  323.         }
  324.        
  325.         if (FlxG.keys.justPressed.SPACE)
  326.         {
  327.             advance();
  328.         }  
  329.        
  330.         //if (FlxG.keys.justPressed.B)
  331.         //{
  332.             //start("COOL!#Hello player_name!#this is another line lal#this really makes me happy! :)#but this makes me sad :(");
  333.         //}
  334.        
  335.        
  336.     //  trace("chars amouunt " + txt_dialog.text.length);
  337.        
  338.     //  if(txt_dialog.text.length<8)
  339.     //  txt_dialog.width = 100;
  340.    
  341.         if(isBig)
  342.         spr_balloon.resize(txt_dialog.width + borderOffset.x*3+spr_pico.width, spr_pico.height + borderOffset.x*2);//uses avatar's height
  343.         else
  344.         spr_balloon.resize(txt_dialog.width + borderOffset.x, txt_dialog.height + 40 + borderOffset.x);
  345.  
  346.         if (StateGameplay.instance != null && !isBig)
  347.         {
  348.             spr_balloon.x = StateGameplay.instance.player.x - spr_balloon.width / 2 + StateGameplay.instance.player.width/2;
  349.             spr_balloon.y = StateGameplay.instance.player.y - spr_balloon.height;
  350.            
  351.             txt_dialog.x = spr_balloon.x + borderOffset.x / 2;
  352.             txt_dialog.y = spr_balloon.y + borderOffset.y;
  353.         }
  354.        
  355.         Utils.CenterToObject(btn_avance, spr_balloon);
  356.         btn_avance.x = spr_balloon.x + spr_balloon.width;
  357.         btn_avance.y = txt_dialog.y + txt_dialog.height/2-btn_avance.height/2;
  358.  
  359.         Utils.CenterToObject(spr_pico, spr_balloon);
  360.         spr_pico.y = spr_balloon.y + spr_balloon.height - spr_pico.height;
  361.        
  362.         if (isBig)
  363.         {
  364.         //  Utils.CenterToStage(spr_balloon);
  365.                
  366.             Utils.CenterToObject(spr_pico, spr_balloon);
  367.             spr_pico.x = spr_balloon.x + borderOffset.x;
  368.    
  369.             txt_dialog.x = spr_pico.x+spr_pico.width+borderOffset.x;
  370.             txt_dialog.y = spr_pico.y - borderOffset.x / 2;
  371.            
  372.             btn_avance.x = spr_balloon.x + spr_balloon.width-btn_avance.width*1.3;
  373.             btn_avance.y = spr_balloon.y + spr_balloon.height-btn_avance.height*1.3;
  374.         }
  375.        
  376.         if (btn_avance.visible)
  377.         {
  378.                     //      StateGameplay.instance.player.player_jump = false;
  379.  
  380.             if (btn_avance.status > 0)
  381.             {
  382.                 StateGameplay.instance.player.canShoot = false;
  383.                 FlxG.mouse.load("assets/img/ui/cursor_pointerFlat.png",1);
  384.             }
  385.             else
  386.             {
  387.                 FlxG.mouse.load("assets/img/crossair_red.png",1,-16,-16);
  388.  
  389.                 StateGameplay.instance.player.canShoot = true;
  390.             }
  391.         }
  392.     }
  393.    
  394.     private function advance():Void
  395.     {
  396.         pausedTimer = 0;
  397.        
  398.         if (!txt_dialog.paused)
  399.         {
  400.             txt_dialog.skip();
  401.         }
  402.         else
  403.         {
  404.             if (sequence == 0)
  405.             {
  406.         ///      _typeText.start(0.02, false, false, null, onComplete.bind("Fully typed"));
  407.             }
  408.             else
  409.             {
  410.                 //additional texts
  411.                 if (sequence > dialog.length-1)
  412.                 {
  413.                     if (isBig)
  414.                     {
  415.                         FlxTween.tween(txt_dialog, { alpha: 0 }, 0.3, { ease: FlxEase.quadIn } );
  416.                         FlxTween.tween(spr_pico, { alpha: 0 }, 0.3, { ease: FlxEase.quadIn } );
  417.                         FlxTween.tween(btn_avance, { alpha: 0 }, 0.3, { ease: FlxEase.quadIn } );
  418.  
  419.                         FlxTween.tween(spr_balloon, { alpha:0 }, 0.3, { ease: FlxEase.quadIn, onComplete: function(t:FlxTween)
  420.                         {
  421.                             spr_balloon.visible = false;
  422.                             txt_dialog.visible = false;
  423.                             spr_pico.visible = false;
  424.                             btn_avance.visible = false;
  425.                            
  426.                             FlxG.mouse.load("assets/img/crossair_red.png", 1, -16, -16);
  427.                             StateGameplay.instance.player.canShoot = true;
  428.                         } } );
  429.                     }
  430.                     else
  431.                     {
  432.                         spr_balloon.visible = false;
  433.                         txt_dialog.visible = false;
  434.                         spr_pico.visible = false;
  435.                         btn_avance.visible = false;
  436.                        
  437.                         FlxG.mouse.load("assets/img/crossair_red.png", 1, -16, -16);
  438.                             StateGameplay.instance.player.canShoot = true;
  439.                     }
  440.                    
  441.                 //  if (StateGameplay.instance != null)
  442.                 //  StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_neutral");
  443.  
  444.                     if (StateGameplay.instance.dialog.trigger_end != null)
  445.                     {
  446.                         if (Button.triggerFound(trigger_end))
  447.                         {
  448.                             //do the trigger thingy
  449.                             trace("doing the trigger thingy");
  450.                             StateGameplay.instance.dialog.trigger_end = null;
  451.                         }
  452.                         else
  453.                         {
  454.                             StateGameplay.instance.dialog.start(trigger_end);
  455.                             trace("new dialog created");
  456.                             StateGameplay.instance.dialog.trigger_end = null;
  457.                         }
  458.                     }
  459.                    
  460.                     return;
  461.                 }
  462.                 else
  463.                 {
  464.                     emotionPresent = false;
  465.                    
  466.                     var new_text:String = parseEmotions(dialog[sequence]);
  467.                    
  468.                     if (emotionPresent)
  469.                     {
  470.                         txt_dialog.resetText(new_text);
  471.                     //  trace("dialog " + new_text);
  472.                     }
  473.                     else
  474.                     {
  475.                         txt_dialog.resetText(dialog[sequence].toString());     
  476.                     //  trace("dialog " + dialog[sequence].toString());
  477.                     }
  478.  
  479.                     txt_dialog.start(0.02, false, false, null, onComplete.bind("Fully typed"));
  480.                 }              
  481.             }
  482.         }
  483.     }
  484.    
  485.     private function updateAvatar(imageName:String):Void
  486.     {
  487.         spr_pico.loadGraphic("assets/img/ui/dialog/"+imageName+".png");
  488.    
  489.         spr_pico.scale.set(0.3, 0.3);
  490.         spr_pico.width *= spr_pico.scale.x;
  491.         spr_pico.height *= spr_pico.scale.y;
  492.         spr_pico.centerOffsets();
  493.        
  494.         emotionPresent = true;
  495.        
  496.         currentChar = imageName;
  497.     }
  498.    
  499.     private function parseEmotions(textToChange:String):String
  500.     {
  501.         if (textToChange.indexOf("char1") >= 0)
  502.         {
  503.             updateAvatar("char1");
  504.  
  505.             return  StringTools.replace(textToChange, "char1",  "");
  506.         }  
  507.        
  508.         if (textToChange.indexOf("char2") >= 0)
  509.         {
  510.             updateAvatar("char2");
  511.  
  512.             return  StringTools.replace(textToChange, "char2",  "");
  513.         }
  514.        
  515.         if (textToChange.indexOf(":)") >= 0)
  516.         {
  517.         //  if (StateGameplay.instance != null)
  518.         //  StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_happy");
  519.            
  520.             emotionPresent = true;
  521.            
  522.             return  StringTools.replace(textToChange, ":)",  "");
  523.         }
  524.        
  525.         if (textToChange.indexOf(":(") >= 0)
  526.         {
  527.         //  if (StateGameplay.instance != null)
  528.         //  StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_sad");
  529.            
  530.             emotionPresent = true;
  531.            
  532.             return StringTools.replace(textToChange, ":(",  "");
  533.         }
  534.        
  535.         return "error";
  536.     }
  537.    
  538.     private function pauseCallback():Void
  539.     {
  540.         txt_dialog.paused = !txt_dialog.paused;
  541.     }
  542.    
  543.     private function eraseCallback():Void
  544.     {
  545.         txt_dialog.erase(0.01, false, null, onComplete.bind("Fully erased"));
  546.     }
  547.  
  548.     private function onComplete(Text:String):Void
  549.     {
  550.         txt_dialog.paused = true;
  551.        
  552.         sequence++;
  553.     }
  554. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement