Browse Source

Clock and other fixes

Load_Fix
Robert Chen 11 years ago
parent
commit
642f032f27
  1. 14
      js/main.js
  2. 70
      js/model.js
  3. 46
      js/screens.js
  4. 118
      js/ui.js
  5. BIN
      res/Base_Game_Screen.png

14
js/main.js

@ -11,18 +11,6 @@ function GameState(){ @@ -11,18 +11,6 @@ function GameState(){
createjs.Ticker.addEventListener( "tick", gameLoop );
function gameLoop(){
if( ( new Date().getTime() - that.oldTime ) > 1000 ){
// It's been at least one second, do logic loop depending on difference
console.log("One second");
// Maintain our own internal clock
that.currentTime+=1000;
that.oldTime = new Date().getTime();
}
that.mainUI.draw();
}
@ -35,6 +23,8 @@ function GameUI( canvasElem, gameState ){ @@ -35,6 +23,8 @@ function GameUI( canvasElem, gameState ){
var that = this;
this.stage = new createjs.Stage( canvasElem );
// this.stage.enableMouseOver(36);
this.activeScreenName = "EndingScreen";
this.activeScreenObj = {};

70
js/model.js

@ -17,7 +17,7 @@ function TurkeyLayer( name, percentRadius, turkeyModel, ovenModel ){ @@ -17,7 +17,7 @@ function TurkeyLayer( name, percentRadius, turkeyModel, ovenModel ){
turkeyModel.totalRadius,
that.initialTemp,
ovenModel.tempInfini,
globalTime );
ovenModel.globalTime );
that.initialTemp = that.finalTemperature;
}
}
@ -32,7 +32,7 @@ function TurkeyModel( weight, ovenModel ){ @@ -32,7 +32,7 @@ function TurkeyModel( weight, ovenModel ){
this.totalRadius = UtilityFunctions.calculateRadius( weight, this.density );
this.totalLayers = [ new TurkeyLayer("Skin", 0.85, this, ovenModel ),
new TurkeyLayer("Body", 0.45, this, ovenModel ),
new TurkeyLayer("Core", 0.01, this, ovenModel ) ];
new TurkeyLayer("Core", 0.05, this, ovenModel ) ];
// Whenever temperature is changed
this.updateLayerTemps = function() {
@ -42,31 +42,56 @@ function TurkeyModel( weight, ovenModel ){ @@ -42,31 +42,56 @@ function TurkeyModel( weight, ovenModel ){
}
}
function OvenModel() {
this.tempInfini=20; //C
function OvenModel( turkeyWeight, gameState ) {
var that = this;
this.tempInfini = 20; //C
this.setTemp = 20;
this.globalTime = 0;
var turkey = new TurkeyModel( 8, this );
var proportional = 0.1; // This value is arbitrary to how fast you want the temperatures to converge. (Or oscillate, which could be realistic as well)
var errorTolerance = 5; //Stove is accurate to 1 degree Celcius Should hopefully oscillate below that value.
// Equalize temp will need to be sent each time iteration
this.equalizeTemp= function(){
var error = Math.abs(this.setTemp-this.tempInfini);
if( this.setTemp>this.tempInfini ){
this.tempInfini = this.tempInfini + error*proportional;
}
else if( this.setTemp<this.tempInfini ){
this.tempInfini = this.tempInfini - error*proportional;
}
this.changeTemp = function(setTemp) {
this.setTemp = setTemp;
}
if( error>errorTolerance ) {
return (true) //Need to run the Heat Calculations again next cycle
}
}
return {
// Equalize temp will need to be sent each time iteration
this.equalizeTemp = function() {
var error = Math.abs(this.setTemp-this.tempInfini);
if( this.setTemp>this.tempInfini ){
this.tempInfini = this.tempInfini + error*proportional;
}
else if( this.setTemp<this.tempInfini ){
this.tempInfini = this.tempInfini - error*proportional;
}
changeTemp: function(setTemp){
console.log("temp changed to " + setTemp);
that.setTemp = setTemp;
},
secondTick: function(){
if ( that.equalizeTemp() ) {
if( error>errorTolerance ) {
return (true) //Need to run the Heat Calculations again next cycle
}
}
// Turn on oven light
gameState.pubsub.publish( "OvenLight", "On" );
//Reset the model's time calculation if there are major changes in the tolerance of the temperature
that.globalTime = 0;
}
else {
// Turn off oven light
gameState.pubsub.publish( "OvenLight", "Off" );
that.globalTime = that.globalTime + 60;
}
console.log( that.tempInfini )
turkey.updateLayerTemps();
}
}
}
@ -189,7 +214,7 @@ UtilityFunctions = { @@ -189,7 +214,7 @@ UtilityFunctions = {
return ( (celsius*(9/5)) + 32 );
},
F2C: function( farenheit ) {
return ( (farenheit*(9/5)) + 32 );
return ( (farenheit-32) *(5/9) );
},
lbs2kgs: function(){
return pounds * 0.453592
@ -197,7 +222,7 @@ UtilityFunctions = { @@ -197,7 +222,7 @@ UtilityFunctions = {
}
//Running the Program Stuff
/*
var ovenObject = new OvenModel();
var turkey = new TurkeyModel( 8, ovenObject );
@ -214,3 +239,4 @@ function time() { @@ -214,3 +239,4 @@ function time() {
turkey.updateLayerTemps();
}
*/

46
js/screens.js

@ -48,25 +48,10 @@ function MainScreen( stage, gameState ){ @@ -48,25 +48,10 @@ function MainScreen( stage, gameState ){
this.background = new createjs.Bitmap( "res/Main.png" );
stage.addChild( this.background );
// buttons info/credits/start
var infoButton = new createjs.Shape();
infoButton.graphics.beginFill("#ffffff").drawRect(13, 445, 222, 65);
infoButton.alpha = 0.1;
infoButton.addEventListener( "click", function(){ gameState.pubsub.publish( "SwitchScreen", "InfoHelpScreen" ) } );
var creditsButton = new createjs.Shape();
creditsButton.graphics.beginFill("#ffffff").drawRect(13, 515, 222, 65);
creditsButton.alpha = 0.1;
creditsButton.addEventListener( "click", function(){ gameState.pubsub.publish( "SwitchScreen", "CreditsScreen" ) } );
var startButton = new createjs.Shape();
startButton.graphics.beginFill("#ffffff").drawRect(564, 520, 222, 65);
startButton.alpha = 0.1;
startButton.addEventListener( "click", function(){ gameState.pubsub.publish( "SwitchScreen", "DifficultyScreen" ) } );
stage.addChild( infoButton );
stage.addChild( creditsButton );
stage.addChild( startButton );
// buttons info/credits/start
stage.addChild( new Button( stage, gameState, 13, 445, 222, 65, "SwitchScreen", "InfoHelpScreen" ) );
stage.addChild( new Button( stage, gameState, 13, 515, 222, 65, "SwitchScreen", "CreditsScreen" ) );
stage.addChild( new Button( stage, gameState, 564, 520, 222, 65, "SwitchScreen", "DifficultyScreen" ) );
this.uiElems = [];
@ -90,18 +75,9 @@ function DifficultyScreen( stage, gameState ){ @@ -90,18 +75,9 @@ function DifficultyScreen( stage, gameState ){
this.background = new createjs.Bitmap( "res/Difficulty-Selection.png" );
stage.addChild( this.background );
var easyButton = new createjs.Shape();
easyButton.graphics.beginFill("#ffffff").drawRect(170, 40, 450, 105);
easyButton.alpha = 0.1;
easyButton.addEventListener( "click", function(){ gameState.pubsub.publish( "SwitchScreen", "KitchenScreen" ) } );
var hardButton = new createjs.Shape();
hardButton.graphics.beginFill("#ffffff").drawRect(170, 150, 450, 105);
hardButton.alpha = 0.1;
hardButton.addEventListener( "click", function(){ gameState.pubsub.publish( "SwitchScreen", "KitchenScreen" ) } );
stage.addChild( easyButton );
stage.addChild( hardButton );
// Easy/Hard Button
stage.addChild( new Button( stage, gameState, 170, 40, 450, 105, "SwitchScreen", "KitchenScreen" ) );
stage.addChild( new Button( stage, gameState, 170, 150, 450, 105, "SwitchScreen", "KitchenScreen" ) );
return {
blit : function(){
@ -116,10 +92,14 @@ function DifficultyScreen( stage, gameState ){ @@ -116,10 +92,14 @@ function DifficultyScreen( stage, gameState ){
function KitchenScreen( stage, gameState ){
var that = this;
this.background = new createjs.Bitmap( "res/kitchen.png" );
stage.addChild( this.background );
this.uiElems = [];
this.uiElems.push( new OvenUI( stage ) );
this.uiElems.push( new ClockUI( stage, gameState ) )
this.uiElems.push( new OvenUI( stage, gameState ) );
this.uiElems.push( new ClockUI( stage, gameState ) );
this.uiElems.push( new WindowUI( stage, gameState ) )
this.uiElems.push( new DialogUI( stage ) );
return {

118
js/ui.js

@ -1,13 +1,14 @@ @@ -1,13 +1,14 @@
function ClockUI( stage, gameState ){
var that = this;
this.minuteRadius = 40;
this.minuteRadius = 30;
this.hourRadius = 0.7 * this.minuteRadius;
this.clockX = 300;
this.clockY = 100;
this.clockX = 246;
this.clockY = 146;
this.getClockAngles = function( ){
var currTime = new Date( gameState.currentTime );
var hourAngle = 720 * ( currTime.getHours() / 24 ) - 90;
var minuteAngle = 360 * ( currTime.getMinutes() / 60 ) - 90;
return [ hourAngle, minuteAngle ];
@ -43,30 +44,76 @@ function ClockUI( stage, gameState ){ @@ -43,30 +44,76 @@ function ClockUI( stage, gameState ){
}
function OvenUI( stage ){
function OvenUI( stage, gameState ){
var that = this;
this.ovenLight = new createjs.Shape();
this.analogClock = "";
this.text = new createjs.Text( "325F", "50px Arial", "#ff7700" );
this.text.x = 70;
this.text.y = 100;
this.text.textBaseline = "alphabetic";
// Important Model
var ovenModel = new OvenModel( 8, gameState );
var ovenLight = new createjs.Shape();
ovenLight.graphics.beginFill( "black" ).drawCircle( 181, 126, 2 );
// Oven light control
this.changeOvenLight = function( state ){
if( state == "On" ){
ovenLight.visible = false;
} else {
ovenLight.visible = true;
}
}
gameState.pubsub.subscribe( "OvenLight", this.changeOvenLight );
var temperatureText = new createjs.Text( "325", "40px Arial", "#ff7700" );
temperatureText.x = 50;
temperatureText.y = 147;
temperatureText.textBaseline = "alphabetic";
//Create a Shape DisplayObject.
this.circle = new createjs.Shape();
this.circle.graphics.beginFill( "red" ).drawCircle( 0, 0, 40 );
this.ovenLight.graphics.beginFill( "red" ).drawCircle( 223, 73, 5 );
this.circle.x = 0;
this.circle.y = 0;
//Set position of Shape instance.
this.circle.x = this.circle.y = 50;
this.picture = new createjs.Bitmap( "res/Base_Game_Screen.png" );
//this.picture.scaleX = this.picture.scaleY = 0.5;
stage.addChild( this.picture );
stage.addChild( this.circle );
stage.addChild( this.ovenLight );
stage.addChild( ovenLight );
stage.addChild( new Button( stage, gameState, 45, 163, 41, 17, "ChangeTemperature", "Up" ) );
stage.addChild( new Button( stage, gameState, 95, 163, 41, 17, "ChangeTemperature", "Down" ) );
stage.addChild( temperatureText );
this.changeTemperature = function( direction ){
if( temperatureText.text == "OFF" && direction == "Up" ) temperatureText.text = "150";
if( !( temperatureText.text == "OFF" && direction == "Down" ) ){
var temp = ( direction == "Up" ? parseInt(temperatureText.text)+25 : parseInt(temperatureText.text)-25);
// Check lower bound for OFF
temp = temp < 150 ? temp = "OFF" : temp;
// Check upper bound
// Set up to 500, then it's BROIL @ 980
temperatureText.text = temp;
}
// Tell our model to set the actual temperature
ovenModel.changeTemp( UtilityFunctions.F2C( temperatureText.text == "OFF" ? 150 : parseInt( temperatureText.text ) ) );
}
// change temperature, this one's for the UI
gameState.pubsub.subscribe( "ChangeTemperature", this.changeTemperature );
this.secondTick = function(){
ovenModel.secondTick();
gameState.currentTime += 1000;
}
setInterval(this.secondTick, 500);
stage.addChild( this.text );
return {
tick: function(){
// Circle will move 10 units to the right.
@ -77,3 +124,40 @@ function OvenUI( stage ){ @@ -77,3 +124,40 @@ function OvenUI( stage ){
}
}
}
function WindowUI( stage, gameState ){
return {
tick: function(){}
}
}
function Item(){
/*img.onPress = function(e) {
document.body.style.cursor='move';
offset = {x:e.stageX - e.target.x, y:e.stageY - e.target.y};
e.onMouseMove = drag;
}*/
}
function Button( stage, gameState, x_orig, y_orig, x_dest, y_dest, eventCmd, arg ){
var that = this;
var infoButton = new createjs.Shape();
infoButton.graphics.beginFill("#ffffff").drawRect(x_orig, y_orig, x_dest, y_dest);
infoButton.alpha = 0.5;
infoButton.addEventListener( "click", function(){ gameState.pubsub.publish( eventCmd, arg ) } );
infoButton.onMouseOver = function(e) {
alert("mouseover");
document.body.style.cursor='pointer';
}
infoButton.onMouseOut = function(e) {
document.body.style.cursor='default';
}
return infoButton;
}

BIN
res/Base_Game_Screen.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 185 KiB

Loading…
Cancel
Save