Basic dialogue box support
This commit is contained in:
94
src/Dialogue.hx
Normal file
94
src/Dialogue.hx
Normal file
@ -0,0 +1,94 @@
|
||||
class Dialogue extends h2d.Object {
|
||||
|
||||
var game : Game;
|
||||
var bg : h2d.ScaleGrid;
|
||||
var tf : h2d.Text;
|
||||
var timer : haxe.Timer;
|
||||
var int : h2d.Interactive;
|
||||
public var width(default, set) : Int;
|
||||
public var height(default, set) : Int;
|
||||
public var text(default, set) : String;
|
||||
var textPos : Int = 0;
|
||||
var chan : hxd.snd.Channel;
|
||||
|
||||
// public function new(width:Int, height:Int, text : String,sfx:hxd.res.Sound) {
|
||||
public function new(width:Int, height:Int, text : String) {
|
||||
super();
|
||||
// if( sfx != null )
|
||||
// chan = sfx.play(true);
|
||||
if( text == null ) text = "NULL";
|
||||
game = Game.inst;
|
||||
game.scene.add(this, 1);
|
||||
bg = new h2d.ScaleGrid(Res.ui.toTile(), 5, 5, this);
|
||||
bg.colorKey = 0xFF00FF;
|
||||
tf = new h2d.Text(game.font, this);
|
||||
tf.scaleX = 2;
|
||||
tf.scaleY = 2;
|
||||
tf.y = 5;
|
||||
tf.x = 7;
|
||||
tf.dropShadow = { dx : 0, dy : 1, color : 0, alpha : 0.3 };
|
||||
int = new h2d.Interactive(0,0,this);
|
||||
int.onClick = function(_) click();
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.text = text;
|
||||
}
|
||||
|
||||
override function onRemove() {
|
||||
super.onRemove();
|
||||
if( chan != null )
|
||||
chan.stop();
|
||||
timer.stop();
|
||||
}
|
||||
|
||||
function updateText() {
|
||||
if( textPos == text.length ) {
|
||||
timer.stop();
|
||||
onReady();
|
||||
if( chan != null ) chan.stop();
|
||||
return;
|
||||
}
|
||||
if( chan != null ) {
|
||||
switch( text.charCodeAt(textPos) ) {
|
||||
case " ".code, "\n".code: chan.volume = 0;
|
||||
default: if( chan.volume == 0 ) chan.volume = 1 else chan.volume *= 0.9;
|
||||
}
|
||||
}
|
||||
textPos++;
|
||||
tf.text = text.substr(0, textPos);
|
||||
}
|
||||
|
||||
public function click() {
|
||||
if( textPos == text.length ) onClick() else if( textPos < text.length ) { textPos = text.length; tf.text = text; updateText(); };
|
||||
}
|
||||
|
||||
public dynamic function onClick() {
|
||||
}
|
||||
|
||||
public dynamic function onReady() {
|
||||
}
|
||||
|
||||
function set_text(t) {
|
||||
text = t;
|
||||
timer = new haxe.Timer(30);
|
||||
timer.run = updateText;
|
||||
tf.text = "";
|
||||
textPos = 0;
|
||||
return t;
|
||||
}
|
||||
|
||||
function set_width(w:Int) {
|
||||
bg.width = w;
|
||||
int.width = w;
|
||||
tf.maxWidth = w - 14;
|
||||
tf.text = text;
|
||||
return width = w;
|
||||
}
|
||||
|
||||
function set_height(h:Int) {
|
||||
bg.height = h;
|
||||
int.height = h;
|
||||
return height = h;
|
||||
}
|
||||
|
||||
}
|
26
src/Game.hx
26
src/Game.hx
@ -5,13 +5,39 @@ class Game extends hxd.App {
|
||||
public var scene : h2d.Scene;
|
||||
public var font : h2d.Font;
|
||||
public var world : World;
|
||||
public var curDialog : h2d.Object;
|
||||
|
||||
override function init() {
|
||||
scene = s2d;
|
||||
s2d.setFixedSize(Const.W, Const.H + 12);
|
||||
world = new World(Res.map, Res.tiles);
|
||||
s2d.add(world.root, 0);
|
||||
font = Res.minecraftia_regular_6.toFont();
|
||||
|
||||
dialog([
|
||||
"(you slowly wake up)",
|
||||
"...",
|
||||
"where am I?",
|
||||
"(the cold vastness of spaaaaace)"
|
||||
]);
|
||||
}
|
||||
|
||||
public static var inst : Game;
|
||||
|
||||
function dialog( t : Array<String> ) {
|
||||
if( t.length == 0 ) {
|
||||
return;
|
||||
}
|
||||
var d = new Dialogue(Const.W, 100, t[0]);
|
||||
d.y = Const.H - d.height;
|
||||
d.onClick = function() {
|
||||
d.remove();
|
||||
curDialog = null;
|
||||
var t2 = t.copy();
|
||||
t2.shift();
|
||||
dialog(t2);
|
||||
};
|
||||
curDialog = d;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user