Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import entities.Button;
- import entities.weapons.BaseWeapon;
- import extensions.FlxButtonExt;
- import flixel.FlxG;
- import flixel.FlxState;
- import flixel.FlxSprite;
- import flixel.FlxObject;
- import flixel.addons.text.FlxTypeText;
- import flixel.addons.ui.FlxUI9SliceSprite;
- import flixel.system.FlxAssets;
- import flixel.text.FlxText;
- import flixel.group.FlxGroup;
- import flixel.tile.FlxTilemap;
- import flixel.tweens.FlxEase;
- import flixel.ui.FlxButton;
- import flixel.util.FlxColor;
- import flixel.math.FlxPoint;
- import flash.Lib;
- import flixel.util.FlxTimer;
- import flixel.math.FlxRect;
- import states.StateGameplay;
- import flixel.math.FlxRandom;
- import particles.ParticlesGroup;
- import particles.BaseParticle;
- import flixel.util.FlxSpriteUtil;
- import flash.display.Graphics;
- import flixel.tweens.FlxTween;
- import flash.display.BitmapData;
- import flash.filters.BitmapFilterQuality;
- import flash.filters.BlurFilter;
- import flash.geom.Point;
- import flash.geom.Rectangle;
- import flash.utils.Dictionary;
- import flixel.FlxCamera;
- import flash.display.BlendMode;
- import flash.geom.Matrix;
- class Dialog extends FlxGroup
- {
- private var txt_dialog:FlxTypeText;
- var spr_balloon:FlxUI9SliceSprite;
- var spr_pico:FlxSprite;
- var borderOffset:FlxPoint;
- var sequence:Int = 0;
- var btn_avance:FlxButtonExt;
- var isBig:Bool = false;
- public var trigger_end:String;
- public function new(Big:Bool = false)
- {
- super();
- isBig = Big;
- create();
- }
- var dialog:Array<String> = new Array();
- private function parseText(text:String):Void
- {
- if (dialog.length != 0)
- dialog = [];//empty array
- var lines:Array<String> = text.split("#");// split in all lines
- for (i in 0 ... lines.length)
- dialog.push(lines[i]);
- // trace("dialog string is " + dialog);
- }
- public function create():Void
- {
- // Make the screen background lighter for visibility sake
- FlxG.camera.bgColor = 0xFF999999;
- borderOffset = new FlxPoint(26, 18);
- // This is a 9-slice image with the image loaded and sliced
- var _graphic:String = "assets/img/ui/9slice/bubble_small.png";
- // This is the [x1,y1,x2,y2] slice points in the image.
- // x1,y1 is the top-left, x2,y2 is the bottom-right.
- var _slice:Array<Int> = [23, 39, 102, 69];
- if (isBig)
- {
- borderOffset = new FlxPoint(12, 12);//14.14
- spr_balloon = new FlxUI9SliceSprite(0, 70, "assets/img/ui/9slice/dialog_big.png", new Rectangle(0, 0, 800, 318), [31, 31, 69, 69]);
- }
- else
- spr_balloon = new FlxUI9SliceSprite(210, 70, _graphic, new Rectangle(0,0,100,50), _slice);
- add(spr_balloon);
- if (isBig)
- {
- var size:Float = FlxG.width * 0.6;//0.7
- txt_dialog = new FlxTypeText(spr_balloon.x + borderOffset.x / 2, spr_balloon.y + borderOffset.y, Std.int(size), "test", 18, true);
- txt_dialog.setFormat("assets/font/SF_Cartoonist_Hand_Bold.ttf", 28, FlxColor.BLACK, "left", FlxTextBorderStyle.OUTLINE, 0xaaD8D8D8);
- txt_dialog.alpha = 0.7;
- Utils.CenterToStage(spr_balloon);
- }
- else
- {
- txt_dialog = new FlxTypeText(spr_balloon.x + borderOffset.x / 2, spr_balloon.y + borderOffset.y, 200, "test", 18, true);
- txt_dialog.setFormat("assets/font/SF_Cartoonist_Hand_Bold.ttf", 26, FlxColor.BLACK, "center", FlxTextBorderStyle.SHADOW, FlxColor.WHITE);
- }
- // txt_dialog.delay = 0.1;
- txt_dialog.delay = 0.1;
- // txt_dialog.eraseDelay = 0.2;
- txt_dialog.eraseDelay = 1;
- txt_dialog.cursorBlinkSpeed = 1.0;
- txt_dialog.prefix = "";
- // txt_dialog.autoErase = true;
- //txt_dialog.autoErase = false;
- //txt_dialog.waitTime = 2.0;
- txt_dialog.setTypingVariation(0.75, true);
- add(txt_dialog);
- if (isBig)
- btn_avance = new FlxButtonExt(0, 0, null, advance, null, null, "assets/img/ui/avance_big.png", false);
- else
- btn_avance = new FlxButtonExt(0, 0, null, advance, null, null, "assets/img/ui/avance.png", false);
- add(btn_avance);
- btn_avance.scrollFactor.set(1, 1);
- spr_pico = new FlxSprite("assets/img/ui/9slice/bubble_pico.png");
- if (isBig)
- {
- spr_pico = new FlxSprite("assets/img/ui/dialog/char1.png");
- spr_pico.scale.set(0.3, 0.3);
- spr_pico.width *= spr_pico.scale.x;
- spr_pico.height *= spr_pico.scale.y;
- spr_pico.centerOffsets();
- }
- add(spr_pico);
- spr_pico.visible = false;
- btn_avance.visible = false;
- spr_balloon.visible = false;
- txt_dialog.visible = false;
- if (isBig)
- {
- spr_pico.scrollFactor.set();
- btn_avance.scrollFactor.set();
- spr_balloon.scrollFactor.set();
- txt_dialog.scrollFactor.set();
- }
- }
- public function start(dialogChain:String):Void
- {
- if ( GameMode.IS == GameMode.VERSUS || GameMode.IS_SURVIVAL)
- return;
- if (isBig && spr_balloon.alpha != 1&& spr_balloon.alpha != 0)
- {
- // trace("still transitioning - return");
- //might be still transitioning
- return;
- }
- sequence = 0;
- pausedTimer = 0;
- blahTimer = 0;
- mouthOpen = false;
- emotionPresent = false;
- var new_text:String = StringTools.replace(dialogChain, "player_name", Sys.getEnv("USERNAME"));
- parseText(new_text);
- // trace(dialog.length);
- if (dialog.length == 1)//this is in the case that is a one liner (no dialog chunks)
- {
- emotionPresent = false;
- var new_text_no_smileys:String = parseEmotions(new_text);
- if (emotionPresent)
- {
- parseText(new_text_no_smileys);
- // trace("new_text_no_smileys");
- }
- else
- {
- parseText(new_text);
- // trace("new_text");
- }
- }
- if (isBig)
- {
- spr_balloon.resize(txt_dialog.width + borderOffset.x*3+spr_pico.width, spr_pico.height + borderOffset.x*2); //uses avatar's height
- Utils.CenterToStage(spr_balloon);
- spr_balloon.alpha = 0;
- txt_dialog.alpha = 0;
- spr_pico.alpha = 0;
- btn_avance.alpha = 0;
- FlxTween.tween(spr_balloon, { alpha: 1 }, 0.5, { ease: FlxEase.quadOut } );
- FlxTween.tween(txt_dialog, { alpha: 0.7 }, 0.5, { ease: FlxEase.quadOut } );
- FlxTween.tween(spr_pico, { alpha: 1 }, 0.5, { ease: FlxEase.quadOut } );
- FlxTween.tween(btn_avance, { alpha: 1 }, 0.5, { ease: FlxEase.quadOut } );
- spr_balloon.y = FlxG.height-spr_balloon.height-30;
- FlxTween.tween(spr_balloon, { y: spr_balloon.y +20 }, 2, { ease: FlxEase.elasticOut } );
- }
- spr_pico.visible = true;
- btn_avance.visible = true;
- spr_balloon.visible = true;
- txt_dialog.visible = true;
- emotionPresent = false;
- var new_text:String = parseEmotions(dialog[sequence]);
- if (emotionPresent)
- {
- txt_dialog.resetText(new_text);
- // trace("dialog " + new_text);
- }
- else
- {
- txt_dialog.resetText(dialog[sequence].toString());
- // trace("dialog " + dialog[sequence].toString());
- }
- // txt_dialog.resetText(dialog[sequence].toString());
- txt_dialog.start(0.02, false, false, null, onComplete.bind("Fully typed"));
- }
- var pausedTimer:Float = 0;
- var blahTimer:Float = 0;
- var mouthOpen:Bool = false;
- var emotionPresent:Bool = false;
- var currentChar:String;
- override public function update(elapsed:Float):Void
- {
- super.update(elapsed);
- if (txt_dialog.paused&&txt_dialog.visible)
- {
- pausedTimer += elapsed;
- if (pausedTimer >= 1 + (txt_dialog.text.length/12))
- {
- // trace("waited "+ (1 + (txt_dialog.text.length/12)) +" seconds, skipping");
- pausedTimer = 0;
- advance();
- }
- if (!emotionPresent)
- {
- // if (StateGameplay.instance != null)
- // StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_neutral");
- }
- }
- else
- {
- if (txt_dialog.visible&&!emotionPresent||isBig&&spr_balloon.visible)
- {
- blahTimer += elapsed;
- var blahDelay:Float = 0.1;
- if (isBig)
- blahDelay = 0.3;
- if (blahTimer >= blahDelay)
- {
- blahTimer = 0;
- // trace("blah");
- if (isBig)
- {
- if(currentChar == "char1")
- FlxG.sound.play("assets/sounds/dialog/dialog" + FlxG.random.int(1, 9) + ".ogg");
- else
- FlxG.sound.play("assets/sounds/dialog/robot" + FlxG.random.int(1, 6) + ".ogg");
- }
- if (!mouthOpen)
- {
- mouthOpen = true;
- if (StateGameplay.instance != null)
- {
- // StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_speak");
- }
- }
- else
- {
- mouthOpen = false;
- if (StateGameplay.instance != null)
- {
- // StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_neutral");
- }
- }
- }
- }
- }
- if (FlxG.keys.justPressed.SPACE)
- {
- advance();
- }
- //if (FlxG.keys.justPressed.B)
- //{
- //start("COOL!#Hello player_name!#this is another line lal#this really makes me happy! :)#but this makes me sad :(");
- //}
- // trace("chars amouunt " + txt_dialog.text.length);
- // if(txt_dialog.text.length<8)
- // txt_dialog.width = 100;
- if(isBig)
- spr_balloon.resize(txt_dialog.width + borderOffset.x*3+spr_pico.width, spr_pico.height + borderOffset.x*2);//uses avatar's height
- else
- spr_balloon.resize(txt_dialog.width + borderOffset.x, txt_dialog.height + 40 + borderOffset.x);
- if (StateGameplay.instance != null && !isBig)
- {
- spr_balloon.x = StateGameplay.instance.player.x - spr_balloon.width / 2 + StateGameplay.instance.player.width/2;
- spr_balloon.y = StateGameplay.instance.player.y - spr_balloon.height;
- txt_dialog.x = spr_balloon.x + borderOffset.x / 2;
- txt_dialog.y = spr_balloon.y + borderOffset.y;
- }
- Utils.CenterToObject(btn_avance, spr_balloon);
- btn_avance.x = spr_balloon.x + spr_balloon.width;
- btn_avance.y = txt_dialog.y + txt_dialog.height/2-btn_avance.height/2;
- Utils.CenterToObject(spr_pico, spr_balloon);
- spr_pico.y = spr_balloon.y + spr_balloon.height - spr_pico.height;
- if (isBig)
- {
- // Utils.CenterToStage(spr_balloon);
- Utils.CenterToObject(spr_pico, spr_balloon);
- spr_pico.x = spr_balloon.x + borderOffset.x;
- txt_dialog.x = spr_pico.x+spr_pico.width+borderOffset.x;
- txt_dialog.y = spr_pico.y - borderOffset.x / 2;
- btn_avance.x = spr_balloon.x + spr_balloon.width-btn_avance.width*1.3;
- btn_avance.y = spr_balloon.y + spr_balloon.height-btn_avance.height*1.3;
- }
- if (btn_avance.visible)
- {
- // StateGameplay.instance.player.player_jump = false;
- if (btn_avance.status > 0)
- {
- StateGameplay.instance.player.canShoot = false;
- FlxG.mouse.load("assets/img/ui/cursor_pointerFlat.png",1);
- }
- else
- {
- FlxG.mouse.load("assets/img/crossair_red.png",1,-16,-16);
- StateGameplay.instance.player.canShoot = true;
- }
- }
- }
- private function advance():Void
- {
- pausedTimer = 0;
- if (!txt_dialog.paused)
- {
- txt_dialog.skip();
- }
- else
- {
- if (sequence == 0)
- {
- /// _typeText.start(0.02, false, false, null, onComplete.bind("Fully typed"));
- }
- else
- {
- //additional texts
- if (sequence > dialog.length-1)
- {
- if (isBig)
- {
- FlxTween.tween(txt_dialog, { alpha: 0 }, 0.3, { ease: FlxEase.quadIn } );
- FlxTween.tween(spr_pico, { alpha: 0 }, 0.3, { ease: FlxEase.quadIn } );
- FlxTween.tween(btn_avance, { alpha: 0 }, 0.3, { ease: FlxEase.quadIn } );
- FlxTween.tween(spr_balloon, { alpha:0 }, 0.3, { ease: FlxEase.quadIn, onComplete: function(t:FlxTween)
- {
- spr_balloon.visible = false;
- txt_dialog.visible = false;
- spr_pico.visible = false;
- btn_avance.visible = false;
- FlxG.mouse.load("assets/img/crossair_red.png", 1, -16, -16);
- StateGameplay.instance.player.canShoot = true;
- } } );
- }
- else
- {
- spr_balloon.visible = false;
- txt_dialog.visible = false;
- spr_pico.visible = false;
- btn_avance.visible = false;
- FlxG.mouse.load("assets/img/crossair_red.png", 1, -16, -16);
- StateGameplay.instance.player.canShoot = true;
- }
- // if (StateGameplay.instance != null)
- // StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_neutral");
- if (StateGameplay.instance.dialog.trigger_end != null)
- {
- if (Button.triggerFound(trigger_end))
- {
- //do the trigger thingy
- trace("doing the trigger thingy");
- StateGameplay.instance.dialog.trigger_end = null;
- }
- else
- {
- StateGameplay.instance.dialog.start(trigger_end);
- trace("new dialog created");
- StateGameplay.instance.dialog.trigger_end = null;
- }
- }
- return;
- }
- else
- {
- emotionPresent = false;
- var new_text:String = parseEmotions(dialog[sequence]);
- if (emotionPresent)
- {
- txt_dialog.resetText(new_text);
- // trace("dialog " + new_text);
- }
- else
- {
- txt_dialog.resetText(dialog[sequence].toString());
- // trace("dialog " + dialog[sequence].toString());
- }
- txt_dialog.start(0.02, false, false, null, onComplete.bind("Fully typed"));
- }
- }
- }
- }
- private function updateAvatar(imageName:String):Void
- {
- spr_pico.loadGraphic("assets/img/ui/dialog/"+imageName+".png");
- spr_pico.scale.set(0.3, 0.3);
- spr_pico.width *= spr_pico.scale.x;
- spr_pico.height *= spr_pico.scale.y;
- spr_pico.centerOffsets();
- emotionPresent = true;
- currentChar = imageName;
- }
- private function parseEmotions(textToChange:String):String
- {
- if (textToChange.indexOf("char1") >= 0)
- {
- updateAvatar("char1");
- return StringTools.replace(textToChange, "char1", "");
- }
- if (textToChange.indexOf("char2") >= 0)
- {
- updateAvatar("char2");
- return StringTools.replace(textToChange, "char2", "");
- }
- if (textToChange.indexOf(":)") >= 0)
- {
- // if (StateGameplay.instance != null)
- // StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_happy");
- emotionPresent = true;
- return StringTools.replace(textToChange, ":)", "");
- }
- if (textToChange.indexOf(":(") >= 0)
- {
- // if (StateGameplay.instance != null)
- // StateGameplay.instance.player.spine.skeleton.setAttachment("head_addons", "alien/masks/fito_sad");
- emotionPresent = true;
- return StringTools.replace(textToChange, ":(", "");
- }
- return "error";
- }
- private function pauseCallback():Void
- {
- txt_dialog.paused = !txt_dialog.paused;
- }
- private function eraseCallback():Void
- {
- txt_dialog.erase(0.01, false, null, onComplete.bind("Fully erased"));
- }
- private function onComplete(Text:String):Void
- {
- txt_dialog.paused = true;
- sequence++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement