Commit 26692d9d authored by Matthew Odle's avatar Matthew Odle

refactor text displays, metrics, and hud

parent dcf5e257
/*jslint white: true */
var metrics = {
floatingPoints : [],
floatingPointCycleDuration : 50,
lives : {player1 : 0, player2 : 0},
extraLivesGained : 0,
currentLevel : {},
score : {},
score : {player1 : 0, player2 : 0},
lastScore : 0,
init : function() {
this.currentLevel = knobsAndLevers.game.startLevel;
let scoreParams = Object.assign({}, knobsAndLevers.text.baseParams);
scoreParams.x = 100;
scoreParams.y = knobsAndLevers.text.gameInfoHeight;
this.score.player1 = new Component(scoreParams);
this.score.player1.value = 0;
scoreParams.x = 200;
this.score.player2 = new Component(scoreParams);
this.score.player2.value = 0;
this.lives.player1 = knobsAndLevers.player.defaultLives;
this.lives.player2 = knobsAndLevers.player.defaultLives;
this.livesMarker = Object.assign({}, templates.marker);
console.log("metrics initialized");
},
......@@ -27,15 +17,15 @@ var metrics = {
this.manageTier();
},
changeScore : function(change) {
this.score.player1.value += change;
this.score.player1.value = this.score.player1.value < 0 ? 0 : this.score.player1.value;
this.score.player1 += change;
this.score.player1 = this.score.player1 < 0 ? 0 : this.score.player1;
},
manageTier : function() {
this.setTier();
this.manageLives();
},
setTier : function() {
let newTier = Math.floor((this.score.player1.value + 1) / knobsAndLevers.game.tier.incrementScore) + 1;
let newTier = Math.floor((this.score.player1 + 1) / knobsAndLevers.game.tier.incrementScore) + 1;
if (!newTier) {
throw('problem calculating tier');
};
......@@ -79,8 +69,7 @@ var metrics = {
this.lives.player1 = knobsAndLevers.player.defaultLives;
this.lives.player2 = knobsAndLevers.player.defaultLives;
this.currentLevel = 1;
this.lastScore = this.score.player1.value;
this.score.player1.value = 0;
this.score.player2.value = 0;
this.lastScore = this.score.player1;
this.score.player1 = 0;
},
};
/*jslint white: true */
var texts = {
textParams : {},
init : function() {
this.livesDisplay = new Component(this.getLivesParams());
this.level = new Component(this.getLevelParams());
this.pausedMessage = new Component(this.getPausedMessageParams());
this.diedText = new Component(this.getDiedTextParams());
this.gameOver = new Component(this.getGameOverTextParams());
this.setTextParams();
this.buildBackgrounds(this.textParams);
this.createElements();
console.log("texts initialized");
},
copyBaseMessageParams : function() {
return Object.assign({}, knobsAndLevers.text.baseParams);
setTextParams : function() {
this.textParams = {
level : Object.assign(this.getDefaults(), {
x : knobsAndLevers.canvas.width * 0.6,
y : this.getDefaults().gameInfoHeight,
}),
lives : Object.assign(this.getDefaults(), {
x : 720,
y : this.getDefaults().gameInfoHeight,
}),
score : Object.assign(this.getDefaults(), {
x : 100,
y : this.getDefaults().gameInfoHeight,
}),
died : Object.assign(this.getDefaults(), {
fontSize : (knobsAndLevers.general.gridSquareSideLength * 1.5) + "px",
y : knobsAndLevers.canvas.height * 0.75,
backgroundMultipliers : {
y : 1.01,
height : 1.8,
},
}),
paused : Object.assign(this.getDefaults(), {
y : knobsAndLevers.canvas.height * 0.9,
backgroundMultipliers : {
y : 1.01,
height : 1,
},
}),
gameOver : Object.assign(this.getDefaults(), {
fontSize : '50px',
y : knobsAndLevers.canvas.height * 0.5,
backgroundMultipliers : {
y : 1.05,
height : 3,
},
}),
};
},
copyBaseTextBackgroundParams : function() {
return Object.assign({}, knobsAndLevers.text.baseBackgroundParams);
getDefaults : function() {
return supporting.clone(knobsAndLevers.text.baseParams);
},
getLevelParams : function() {
let levelParams = this.copyBaseMessageParams();
levelParams.x = game.gameArea.canvas.width * 0.6;
levelParams.y = knobsAndLevers.text.gameInfoHeight;
return levelParams;
buildBackgrounds : function(params) {
Object.keys(params).forEach(key => {
let multipliers = params[key].backgroundMultipliers;
if (!multipliers) {
return;
};
let defaults = supporting.clone(knobsAndLevers.text.baseBackgroundParams);
defaults.height *= multipliers.height;
defaults.y = (params[key].y - defaults.height) * multipliers.y;
params[key].background = new Component(defaults);
});
},
getLivesParams : function() {
let livesParams = this.copyBaseMessageParams();
livesParams.x = 720;
livesParams.y = knobsAndLevers.text.gameInfoHeight;
return livesParams;
createElements : function() {
this.lives = new Component(this.textParams.lives);
this.score = new Component(this.textParams.score);
this.level = new Component(this.textParams.level);
this.pausedMessage = new Component(this.textParams.paused);
this.diedText = new Component(this.textParams.died);
this.gameOver = new Component(this.textParams.gameOver);
},
getDiedTextParams : function() {
let diedTextParams = this.copyBaseMessageParams();
diedTextParams.fontSize = (knobsAndLevers.general.gridSquareSideLength * 1.5) + "px";
diedTextParams.y = game.gameArea.canvas.height * 0.75;
let backgroundParams = this.copyBaseTextBackgroundParams();
backgroundParams.height *= 1.5;
backgroundParams.y = (diedTextParams.y - backgroundParams.height) * 1.01;
diedTextParams.background = new Component(backgroundParams);
return diedTextParams;
},
getPausedMessageParams : function() {
let pausedMessageTextParams = this.copyBaseMessageParams();
pausedMessageTextParams.y = game.gameArea.canvas.height * 0.9;
let backgroundParams = this.copyBaseTextBackgroundParams();
backgroundParams.y = (pausedMessageTextParams.y - backgroundParams.height) * 1.01;
pausedMessageTextParams.background = new Component(backgroundParams);
return pausedMessageTextParams;
},
getGameOverTextParams : function() {
let gameOverTextParams = this.copyBaseMessageParams();
gameOverTextParams.fontSize = "50px";
gameOverTextParams.color = "navy";
gameOverTextParams.y = game.gameArea.canvas.height * 0.5;
let backgroundParams = this.copyBaseTextBackgroundParams();
backgroundParams.height *= 3;
backgroundParams.y = (gameOverTextParams.y - backgroundParams.height) * 1.05;
gameOverTextParams.background = new Component(backgroundParams);
return gameOverTextParams;
}
}
};
......@@ -318,18 +318,18 @@ var knobsAndLevers = {
fontSize : "20px",
color : "black",
extraArgs : {type:"text"},
gameInfoHeight : 40,
},
baseBackgroundParams : {
color : "lightgrey",
x : 0,
extraArgs : {type:"background"},
},
gameInfoHeight : 40,
init : function(configs) {
this.baseParams.x = configs.canvas.width * 0.25;
this.baseBackgroundParams.height = configs.general.gridSquareSideLength;
this.baseBackgroundParams.width = configs.canvas.width;
this.gameInfoHeight = configs.general.gridSquareSideLength * 1.3;
this.baseParams.gameInfoHeight = configs.general.gridSquareSideLength * 1.3;
console.log('text defaults initialized');
},
},
......
......@@ -26,14 +26,14 @@ describe('HUD SPEC: ', () => {
metrics.init();
metrics.lives.player1 = 10;
let expected = metrics.lives.player1;
spyOn(texts.livesDisplay, 'update');
spyOn(texts.lives, 'update');
spyOn(metrics.livesMarker, 'update');
testObj.updateLives();
expect(texts.livesDisplay.text).toEqual(expected);
expect(texts.lives.text).toEqual(expected);
expect(metrics.livesMarker.update).toHaveBeenCalled();
expect(texts.livesDisplay.update).toHaveBeenCalled();
expect(texts.lives.update).toHaveBeenCalled();
});
xit('updateLevel updates level', () => {
metrics.init();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment