Add: ScoreManager, ScoreBoard

This commit is contained in:
2018-05-13 16:15:46 +09:00
parent b720f940ec
commit b89490facd
9 changed files with 196 additions and 27 deletions
+41
View File
@@ -0,0 +1,41 @@
class ScoreBoard {
constructor() {
let fontStyle = ScoreBoard.DEFAULT_TEXT_FONT;
fontStyle.align = "right";
fontStyle.boundsAlignH = "right";
this.label = game.add.text(0, 0, "점수 : ", fontStyle)
.setTextBounds(0, 0, game.world.width / 2, ScoreBoard.FONT_HEIGHT_PX);
fontStyle.align = "left";
fontStyle.boundsAlignH = "left";
this.scoreText = game.add.text(game.world.width / 2, 0, "0", fontStyle)
.setTextBounds(0, 0, game.world.width / 2, ScoreBoard.FONT_HEIGHT_PX);
// var grd = this.label.context.createLinearGradient(0, 0, 0, ScoreBoard.FONT_HEIGHT_PX);
// grd.addColorStop(0, '#8ED6FF');
// grd.addColorStop(1, '#004CB3');
// this.label.fill = grd;
// this.scoreText.fill = grd;
// this.setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
// this.label.stroke = '#000';
// this.label.strokeThickness = 3;
};
printScore(score) {
this.scoreText.text = score;
}
}
ScoreBoard.FONT_HEIGHT_PX = 70;
ScoreBoard.DEFAULT_TEXT_FONT = {
font: "38px Arial",
align: "center",
boundsAlignH: "center", // left, center. right
boundsAlignV: "middle", // top, middle, bottom
fill: "#fff"
};
+79
View File
@@ -0,0 +1,79 @@
class ScoreManager {
constructor() {
this.score = 0;
this.highScore = 0;
this.onChangeScoreFunctions = [];
this.onChangeHighScoreFunctions = [];
}
addOnChangeScoreFunctions(onChangeFunction) {
this.onChangeScoreFunctions.push(onChangeFunction);
}
removeOnChangeScoreFunctions(onChangeFunction) {
let itemIndex = this.onChangeScoreFunctions.indexOf(onChangeFunction);
if(itemIndex > -1)
this.onChangeScoreFunctions.splice(itemIndex, 1);
}
addOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
this.onChangeHighScoreFunctions.push(onChangeHighScoreFunction);
}
removeOnChangeHighScoreFunctions(onChangeHighScoreFunction) {
let itemIndex = this.onChangeHighScoreFunctions.indexOf(onChangeHighScoreFunction);
if(itemIndex > -1)
this.onChangeHighScoreFunctions.splice(itemIndex, 1);
}
removeAllFunctions() {
this.onChangeScoreFunctions = [];
this.onChangeHighScoreFunctions = [];
}
getScore() {
return this.score;
}
getHighScore() {
return this.highScore;
}
plusScore(amount) {
this.setScore(this.score + amount);
}
minusScore(amount) {
this.plusScore(-1 * amount);
}
setScore(value) {
let beforeScore = this.score;
this.score = value;
// update score
if(beforeScore !== this.score) {
if(this.onChangeScoreFunctions.length > 0) {
for(let i = 0; i < this.onChangeScoreFunctions.length; i++) {
this.onChangeScoreFunctions[i](this.score);
}
}
}
// update high score
if(this.score > this.highScore) {
this.highScore = this.score;
if(this.onChangeHighScoreFunctions.length > 0) {
for(let i = 0; i < this.onChangeHighScoreFunctions.length; i++) {
this.onChangeHighScoreFunctions[i](this.highScore);
}
}
}
}
}
+1 -1
View File
@@ -1,7 +1,7 @@
class ScoreText extends Phaser.Text {
constructor(x, y, score) {
super(game, x, y, "+" + score, ScoreText.DEFAULT_TEXT_FONT);
super(game, x, y, "+" + NumberUtil.numberWithCommas(score), ScoreText.DEFAULT_TEXT_FONT);
this.anchor.set(0.5);
this.inputEnabled = false;
+30 -21
View File
@@ -11,6 +11,33 @@ class ScreenBottom {
this.BOTTOM_BAR_TEXT_OFFSET = 10;
this.bottomAreaPositionY = this.game.world.height - this.BOTTOM_BAR_HEIGHT;
let style = { font: "32px Arial", fill: "#fff", align: "left", boundsAlignH: "left", boundsAlignV: "bottom" };
let posX = this.BOTTOM_BAR_TEXT_OFFSET;
this.leftText = this.game.add.text(posX, this.bottomAreaPositionY, "", style)
.setTextBounds(0, 0, this.BOTTOM_BAR_NAME_WIDTH, this.BOTTOM_BAR_HEIGHT)
.addColor('#ffffff', 0);
// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
// let style = { font: "32px Arial", fill: "#fff", align: "center", boundsAlignH: "center", boundsAlignV: "bottom" };
style.align = "center";
style.boundsAlignH = "center";
posX = game.world.width / 2 - this.BOTTOM_BAR_NAME_OFFSET_RIGHT;
this.centerText = this.game.add.text(0, this.bottomAreaPositionY, "", style)
.setTextBounds(0, 0, game.world.width, this.BOTTOM_BAR_HEIGHT)
.addColor('#ffffff', 0);
// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
// let style = { font: "32px Arial", fill: "#fff", align: "right", boundsAlignH: "right", boundsAlignV: "bottom" };
style.align = "right";
style.boundsAlignH = "right";
posX = game.world.width - this.BOTTOM_BAR_NAME_WIDTH - this.BOTTOM_BAR_TEXT_OFFSET;
this.rightText = this.game.add.text(posX, this.bottomAreaPositionY, "", style)
.setTextBounds(0, 0, this.BOTTOM_BAR_NAME_WIDTH, this.BOTTOM_BAR_HEIGHT)
.addColor('#ffffff', 0);
// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
}
makeBottomLine() {
@@ -25,33 +52,15 @@ class ScreenBottom {
}
printBottomLeftText(text) {
let style = { font: "32px Arial", fill: "#fff", align: "left", boundsAlignH: "left", boundsAlignV: "bottom" };
let posX = this.BOTTOM_BAR_TEXT_OFFSET;
this.game.add.text(posX, this.bottomAreaPositionY, text, style)
.setTextBounds(0, 0, this.BOTTOM_BAR_NAME_WIDTH, this.BOTTOM_BAR_HEIGHT)
.addColor('#ffffff', 0);
// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
this.leftText.text = text;
}
printBottomCenterText(text) {
let style = { font: "32px Arial", fill: "#fff", align: "center", boundsAlignH: "center", boundsAlignV: "bottom" };
let posX = game.world.width / 2 - this.BOTTOM_BAR_NAME_OFFSET_RIGHT;
this.game.add.text(0, this.bottomAreaPositionY, text, style)
.setTextBounds(0, 0, game.world.width, this.BOTTOM_BAR_HEIGHT)
.addColor('#ffffff', 0);
// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
this.centerText.text = text;
}
printBottomRightText(text) {
let style = { font: "32px Arial", fill: "#fff", align: "right", boundsAlignH: "right", boundsAlignV: "bottom" };
let posX = game.world.width - this.BOTTOM_BAR_NAME_WIDTH - this.BOTTOM_BAR_TEXT_OFFSET;
this.game.add.text(posX, this.bottomAreaPositionY, text, style)
.setTextBounds(0, 0, this.BOTTOM_BAR_NAME_WIDTH, this.BOTTOM_BAR_HEIGHT)
.addColor('#ffffff', 0);
// .setShadow(3, 3, 'rgba(0, 0, 0, 0.5)', 2);
this.rightText.text = text;
}
}