You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.1 KiB

function DialogueSequence( sequence ){
var targetStory = story[sequence] ? story[sequence].slice(0) : ( messages[sequence] ? messages[sequence].slice(0) : [] );
return {
next: function(){
11 years ago
return targetStory.shift().split(": ");
},
more: function(){
return targetStory.length > 0;
}
}
}
function DialogUI( stage, gameState ){
var that = this;
// Dialog States
var DIALOG_RECEDING = 0;
var DIALOG_SHOWING = 1;
var DIALOG_PAUSING = 2;
var MILLIS_PER_CHAR = 100;
11 years ago
var peopleImg = {
"Boyfriend": new createjs.Bitmap("res/people/Boyfriend.png"),
"Brother": new createjs.Bitmap("res/people/Brother.png"),
"Cat": new createjs.Bitmap("res/people/Cat.png"),
"Dad": new createjs.Bitmap("res/people/Dad.png"),
"Girlfriend": new createjs.Bitmap("res/people/Girlfriend.png"),
"Grandma": new createjs.Bitmap("res/people/Grandma.png"),
"Grandpa": new createjs.Bitmap("res/people/Grandpa.png"),
"Mom": new createjs.Bitmap("res/people/Mom.png"),
"Female": new createjs.Bitmap("res/people/PlayerFemale.png"),
11 years ago
"Male": new createjs.Bitmap("res/people/PlayerMale.png"),
"Turkey": new createjs.Bitmap("res/people/TurkeyGhost.png")
11 years ago
};
var dialogueList = Object.keys(story);
this.dialogSpeed = 30;
this.dialogState = DIALOG_PAUSING;
this.dialogMotionQueue = [DIALOG_RECEDING];
this.currDialogueSeq = new DialogueSequence("Null");
dialogQueue = [];
this.dialogBox = new createjs.Bitmap("res/screens/GUI/DialogueBox.png");
11 years ago
this.dialogBox.x = 0;
this.dialogBox.y = 250;
this.currentFace = peopleImg["Male"];
this.currentFace.x = 0;
this.textContent = new createjs.Text( "", "24px Arial", "black" );
this.textContent.x = 205;
this.textContent.y = 705;
this.textContent.lineWidth = 565;
this.textContent.lineHeight = 30;
this.textContent.textBaseline = "alphabetic";
this.dialogBox.addEventListener( "mouseover", function(){ document.body.style.cursor='pointer'; } );
this.dialogBox.addEventListener( "mouseout", function(){ document.body.style.cursor='default'; } );
this.dialogBox.addEventListener( "click", function(){ setTimeout( clickEvent, 100); });
this.textContent.addEventListener( "mouseover", function(){ document.body.style.cursor='pointer'; } );
this.textContent.addEventListener( "mouseout", function(){ document.body.style.cursor='default'; } );
this.textContent.addEventListener( "click", function(){ setTimeout( clickEvent, 100); });
this.endFunc = function(){};
this.showDialog= function( textSeq ){
11 years ago
if(DEBUG) console.log("showing"+ textSeq);
11 years ago
if( !peopleImg["Me"] ){
peopleImg["Me"] = peopleImg[gameState.gender];
}
if( !peopleImg["Spouse"] ){
if( gameState.gender == "Male" ){
peopleImg["Spouse"] = peopleImg["Girlfriend"] ;
}else{
peopleImg["Spouse"] = peopleImg["Boyfriend"] ;
}
}
if( textSeq.seq == "custom" ){
11 years ago
messages["custom"] = ["Me: " + textSeq.customText ];
}
11 years ago
if( textSeq.random ){
that.showRandomConvo();
return;
}
that.currDialogueSeq = new DialogueSequence( textSeq.seq );
11 years ago
var nextDialogue = that.currDialogueSeq.next();
that.endFunc = textSeq.endFunc || function(){};
11 years ago
that.textContent.text=nextDialogue[1].replace(/\[GenderPronoun\]/g, gameState.pronoun ).replace(/\[Player\]/g, gameState.name );
11 years ago
that.currentFace.y = 250;
that.currentFace = peopleImg[nextDialogue[0]] || that.currentFace;
that.autoAdvance = textSeq.autoAdvance;
that.dialogMotionQueue = [DIALOG_SHOWING];
}
11 years ago
this.showRandomConvo = function(){
// No more stories, thanks for playing
if( dialogueList.length == 0 ) return;
dialogueList = Object.keys(story);
var randomKey = UtilityFunctions.randRange(0, dialogueList.length);
11 years ago
// check if there is something going on
if( !that.currDialogueSeq.more() ){
11 years ago
if(DEBUG) console.log("random story");
this.showDialog( {seq: dialogueList[ randomKey ] || "Dad Tells a bad Joke", autoAdvance:true } );
delete story[ dialogueList[ randomKey ] ];
gameState.dialogueHeard++;
11 years ago
}
}
gameState.pubsub.subscribe( "ShowDialog", this.showDialog );
// negate double setTimeout if clicked
var oldTime = new Date().getTime();
var delayCounter = 0;
var clickEvent = function( timer ){
11 years ago
if( !peopleImg["Me"] ){
peopleImg["Me"] = peopleImg[gameState.gender];
}
// if there is more dialogue text, then keep going, otherwise, recede
if( that.currDialogueSeq.more() ){
11 years ago
var nextDialogue = that.currDialogueSeq.next();
that.dialogMotionQueue.push(DIALOG_SHOWING);
that.textContent.text=nextDialogue[1];
11 years ago
if(DEBUG) console.log("showing face:" +nextDialogue[0] );
11 years ago
// swap out face immediately
that.currentFace.y = 250;
that.currentFace = peopleImg[nextDialogue[0]] || that.currentFace;
that.currentFace.y = 0;
}else{
// pause and close dialog
setTimeout( function(){
that.dialogMotionQueue.push(DIALOG_RECEDING);
if( that.endFunc )
that.endFunc();
}, 250 );
}
11 years ago
delayCounter = 0;
oldTime = new Date().getTime();
}
stage.addChild( this.dialogBox );
stage.addChild( this.textContent );
11 years ago
for(var i in peopleImg ){
peopleImg[i].alpha = 1;
peopleImg[i].y = 250;
stage.addChild( peopleImg[i] );
}
return {
tick: function(){
delayCounter = new Date().getTime() - oldTime;
11 years ago
if( that.autoAdvance == true && that.dialogBox.y ==0 && delayCounter > ( (that.textContent.text.length * MILLIS_PER_CHAR) < 2000 ? 2000 : (that.textContent.text.length * MILLIS_PER_CHAR) ) ){
clickEvent();
}
if( that.dialogState == DIALOG_RECEDING ){
that.dialogBox.y+=that.dialogSpeed;
11 years ago
that.textContent.y += that.dialogSpeed;
that.currentFace.y += that.dialogSpeed;
11 years ago
//if(DEBUG) console.log( "Receding" + that.dialogBox.y );
}
if( that.dialogState == DIALOG_SHOWING ){
that.dialogBox.y-=that.dialogSpeed;
11 years ago
that.textContent.y -= that.dialogSpeed;
that.currentFace.y -= that.dialogSpeed;
11 years ago
//if(DEBUG) console.log( "Advancing" + that.dialogBox.y );
}
// toggle states
11 years ago
if( that.dialogBox.y > 250 && that.dialogState == DIALOG_RECEDING ){
that.dialogBox.y = 250;
that.textContent.y = 735;
11 years ago
that.currentFace.y = 250;
that.dialogState = DIALOG_PAUSING;
11 years ago
//if(DEBUG) console.log( "Pausing on recede" + that.dialogBox.y );
}
11 years ago
if( that.dialogBox.y < 0 && that.dialogState == DIALOG_SHOWING ){
that.dialogBox.y = 0;
that.textContent.y = 480;
11 years ago
that.currentFace.y = 0;
that.dialogState = DIALOG_PAUSING;
11 years ago
//if(DEBUG) console.log( "Pausing on showing" + that.dialogBox.y );
}
/* next states if there are any on the queue */
if( that.dialogMotionQueue.length > 0 && that.dialogState == DIALOG_PAUSING ){
that.dialogState = that.dialogMotionQueue.shift();
}
},
minDialog: function(){
that.dialogMotionQueue.push( DIALOG_RECEDING );
},
maxDialog: function(){
that.dialogMotionQueue.push( DIALOG_SHOWING );
},
render: function(){
stage.addChild( that.dialogBox );
stage.addChild( that.textContent );
11 years ago
for(var i in peopleImg ){
peopleImg[i].alpha = 1;
stage.addChild( peopleImg[i] );
}
}
}
}