Files
chocomae/src/game/mouse/card_matching/game.js
T

541 lines
14 KiB
JavaScript

var Game = {
create: function() {
this.game.stage.backgroundColor = "#000000"; // '#4d4d4d';
sessionStorageManager.setIsNewAppHighestRecord(false);
var experienceAppTimer = new ExperienceAppTimer();
experienceAppTimer.setTimeOverListener(
(function() { this.gameOver(); }).bind(this)
);
// keyboard shortcut
this.keyboardShortcut = new KeyboardShortcut();
this.keyboardShortcut.addCallback(
Phaser.KeyCode.ESC, // keyCode
null, // keyDownHandler
(function() { this.back(); }).bind(this), // keyUpHandler
"card matching" // callerClassName
);
// top ui
var screenTopUI = new ScreenTopUI();
screenTopUI.makeBackButton( (function() { this.back(); }).bind(this) );
screenTopUI.makeFullScreenButton();
this.scoreManager = new ScoreManager();
this.scoreBoard = new ScoreBoard();
this.scoreManager.addOnChangeScoreListener(
(function(score) {
sessionStorageManager.setRecord(score);
this.scoreBoard.printScore(NumberUtil.numberWithCommas(score));
}).bind(this)
);
this.scoreManager.addOnChangeHighScoreListener(
(function(highScore) {
// console.log(highScore);
// sessionStorageManager.setAppHighestRecord(highScore);
sessionStorageManager.setIsNewBestRecrd(true);
// this.screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord());
}).bind(this)
);
this.stageTimer = new StageTimer(
Game.GAME_TIME_SEC,
(function() {
// this.isOnStage = false;
this.gameOver();
}).bind(this)
);
this.initVariables();
// game stage
game.stage.backgroundColor = '#4d4d4d';
graphics = game.add.graphics(0, 0);
this.plusScoreGroup = game.add.group();
// bottom ui
var screenBottomUI = new ScreenBottomUI();
screenBottomUI.printLeftTextWithAppHighestRecord(sessionStorageManager.getAppHighestRecord());
screenBottomUI.printCenterText(sessionStorageManager.getPlayingAppKoreanName());
screenBottomUI.printRightText(sessionStorageManager.getPlayerName());
this.startGame();
// // this.countDown();
},
initVariables() {
this.cards = [];
this.card_selected_edge;
this.card_characters = [];
this.card_characters_count;
this.initCardVariables();
this.playingStageNo;
this.activatedCardColumnNo;
this.activatedCardRowNo;
this.activatedCardCount;
this.clearedCardCount;
this.openCardInfoIndex = [];
this.openCardInfoCharacter = [];
this.reservedHideCard = [];
this.isCardSelected;
this.selectedCardCol;
this.selectedCardRow;
this.score;
this.scoreText;
this.highscore;
this.highscoreText;
this.plusScoreTextGroup;
this.stageNoticeText;
this.popup;
this.popupTween = null;
this.startButton;
this.timeEvent;
this.gameTimeEvents = [];
this.cardCharacterIndexList;
this.randStageCharacterList;
this.randCharacterListAfterShuffling;
},
initCardVariables() {
this.card_characters[0] = 'commoner';
this.card_characters[1] = 'elf';
this.card_characters[2] = 'knight';
this.card_characters[3] = 'orc';
this.card_characters[4] = 'wizard';
this.card_characters[5] = 'halloween';
this.card_characters[6] = 'holiday_bunny';
this.card_characters[7] = 'patrick';
this.card_characters[8] = 'santa';
this.card_characters[9] = 'basketball';
this.card_characters[10] = 'football';
this.card_characters[11] = 'gym';
this.card_characters[12] = 'hockey';
this.card_characters[13] = 'soccer';
this.card_characters_count = this.card_characters.length;
this.cardCharacterIndexList = Phaser.ArrayUtils.numberArray(0, this.card_characters_count - 1);
// console.log("cardCharacterIndexList : " + this.cardCharacterIndexList.length);
for(var i = 0; i < DudeCard.MAX_CARD_COLUMN_NO; i++){
for(var j = 0; j < DudeCard.MAX_CARD_ROW_NO; j++) {
var index = j * DudeCard.MAX_CARD_COLUMN_NO + i;
this.cards[index] = new DudeCard(
game, i, j,
this.card_characters,
(function(col, row) {
this.cardClicked(col, row);
}).bind(this)
);
this.cards[index].setScale(0.5);
this.cards[index].hide();
}
}
this.card_selected_edge = game.add.sprite(200, 400, 'card_edge');
this.card_selected_edge.anchor.set(0.5);
this.card_selected_edge.smoothed = false;
this.card_selected_edge.scale.set(0.5);
this.hideSelectedCard();
},
back: function() {
sessionStorageManager.resetPlayingAppData();
location.href = '../../web/client/menu_app.html';
},
/*
countDown() {
const style = { font: "bold 200px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" };
this.countDownText = game.add.text(0, 0, "", style);
this.countDownText.setTextBounds(0, 0, game.world.width, game.world.height);
this.countDownText.stroke = "#333";
this.countDownText.strokeThickness = 50;
this.countDownNumber = 3;
if(isDebugMode())
this.countDownNumber = 1;
this.tweenCountDown();
},
tweenCountDown() {
if(this.countDownNumber === 0) {
this.startGame();
return;
}
this.countDownText.text = this.countDownNumber.toString();
this.countDownText.alpha = 1;
var countDownTween = game.add.tween(this.countDownText);
countDownTween.to( { alpha: 0 }, 1000, Phaser.Easing.Linear.None, true);
countDownTween.onComplete.add(this.tweenCountDown, this);
this.countDownNumber--;
},
*/
startGame: function() {
this.playingStageNo = 1;
this.startStage();
this.stageTimer.start();
},
startStage() {
this.isCardSelected = false;
this.setOpenCardColumnRow();
this.activatedCardCount = this.activatedCardColumnNo * this.activatedCardRowNo;
this.clearedCardCount = 0;
this.initCards();
this.initOpenCardInfo();
},
setOpenCardColumnRow() {
switch(this.playingStageNo) {
case 1:
this.activatedCardColumnNo = 2;
this.activatedCardRowNo = 2;
break;
case 2:
this.activatedCardColumnNo = 3;
this.activatedCardRowNo = 2;
break;
case 3:
case 4:
this.activatedCardColumnNo = 4;
this.activatedCardRowNo = 2;
break;
case 5:
case 6:
this.activatedCardColumnNo = 4;
this.activatedCardRowNo = 3;
break;
case 7:
case 8:
case 9:
this.activatedCardColumnNo = 4;
this.activatedCardRowNo = 4;
break;
default:
this.activatedCardColumnNo = 5;
this.activatedCardRowNo = 4;
break;
}
},
initCards() {
this.randStageCharacterList = Phaser.ArrayUtils.shuffle(this.cardCharacterIndexList);
var characterListBeforeShuffling = [];
for(var i = 0; i < this.activatedCardCount; i++) {
var characterIndex = Math.floor(i / 2);
characterListBeforeShuffling[i] = this.randStageCharacterList[characterIndex];
}
this.randCharacterListAfterShuffling = Phaser.ArrayUtils.shuffle(characterListBeforeShuffling);
var randCharacterIndex = 0;
for(var i = 0; i < DudeCard.MAX_CARD_COLUMN_NO; i++) {
for(var j = 0; j < DudeCard.MAX_CARD_ROW_NO; j++) {
var index = j * DudeCard.MAX_CARD_COLUMN_NO + i;
if(i < this.activatedCardColumnNo && j < this.activatedCardRowNo) {
this.cards[index].show(this.activatedCardColumnNo, this.activatedCardRowNo);
this.cards[index].flipToBack();
// cards[index].setCharacter(1);
// cards[index].setCharacter(game.rnd.integerInRange(0, card_characters_count - 1));
this.cards[index].setCharacter(this.randCharacterListAfterShuffling[randCharacterIndex]);
randCharacterIndex++;
} else {
this.cards[index].hide();
}
}
}
},
initOpenCardInfo() {
this.openCardInfoIndex[0] = -1;
this.openCardInfoCharacter[0] = -1;
this.openCardInfoIndex[1] = -1;
this.openCardInfoCharacter[1] = -1;
},
hideSelectedCard() {
this.isCardSelected = false;
this.card_selected_edge.x = -100;
this.card_selected_edge.y = -100;
this.card_selected_edge.alpha = 0;
},
cardClicked(col, row) {
var index = this.getCardIndex(col, row);
if(this.isCardSelectEdgeActivated() == false) { // card select edge is not activated
this.showSelectedCard(col, row);
} else { // card select edge is activated
if(this.isSelectedBefore(col, row) == true) {
this.hideSelectedCard();
if(this.isFrontCard(col, row)) {
this.flipToBack(col, row);
if(this.isOpenedCard(col, row) == true) {
this.resetOpenCardInfo(col, row);
}
} else {
this.flipToFront(col, row);
if(this.isOpenedCard(col, row) == false) {
this.setOpenCardInfo(col, row);
if(this.isCardSelectEdgeAll() == true) {
if(this.isSameCharacterCard() == true) {
this.clearOpenedTwoCards();
} else {
this.resetOpenedTwoCards();
}
}
}
}
} else {
this.showSelectedCard(col, row);
}
}
// showOpenCardInfo();
this.selectedCardCol = col;
this.selectedCardRow = row;
},
isSelectedBefore(col, row) {
if(this.selectedCardCol == col && this.selectedCardRow == row)
return true;
return false;
},
getCardIndex(col, row) {
return index = row * DudeCard.MAX_CARD_COLUMN_NO + col;
},
isFrontCard(col, row) {
return this.cards[this.getCardIndex(col, row)].isFront();
},
flipToFront(col, row) {
var index = row * DudeCard.MAX_CARD_COLUMN_NO + col;
this.cards[this.getCardIndex(col, row)].flipToFront();
},
flipToBack(col, row) {
this.cards[this.getCardIndex(col, row)].flipToBack();
},
clearCards(col, row) {
var index = this.getCardIndex(col, row);
this.cards[index].hide();
index = this.selectedCardRow * DudeCard.MAX_CARD_COLUMN_NO + this.selectedCardCol;
this.cards[index].hide();
},
resetCards(col, row) {
var index = this.getCardIndex(col, row);
this.cards[index].show(col, row);
index = this.selectedCardRow * DudeCard.MAX_CARD_COLUMN_NO + this.selectedCardCol;
this.cards[index].hide(this.selectedCardCol, this.selectedCardRow);
},
showSelectedCard(col, row) {
this.isCardSelected = true;
var index = this.getCardIndex(col, row);
this.card_selected_edge.x = this.cards[index].x;
this.card_selected_edge.y = this.cards[index].y;
this.card_selected_edge.alpha = 1;
},
showOpenCardInfo() {
console.log(
"card 1 (" + this.openCardInfoIndex[0] + ", : " + this.openCardInfoCharacter[0] + "), card 2 ("
+ this.openCardInfoIndex[1] + ", " + this.openCardInfoCharacter[1] + ")");
},
isCardSelectEdgeActivated() {
return this.isCardSelected;
},
isCardSelectEdgeAll() {
if(this.openCardInfoIndex[0] < 0)
return false;
if(this.openCardInfoIndex[1] < 0)
return false;
return true;
},
isSameCharacterCard() {
if(this.openCardInfoIndex[0] < 0 || this.openCardInfoIndex[1] < 0)
return false;
if(this.openCardInfoCharacter[0] != this.openCardInfoCharacter[1])
return false;
return true;
},
clearOpenedTwoCards() {
var card1 = this.cards[this.openCardInfoIndex[0]];
this.resetOpenCardInfo(card1.getColumnNo(), card1.getRowNo());
this.reservedHideCard[0] = card1;
var card2 = this.cards[this.openCardInfoIndex[1]];
this.resetOpenCardInfo(card2.getColumnNo(), card2.getRowNo());
this.reservedHideCard[1] = card2;
// to do : show card 2 (flip to front) and wait 1 sec
// to do : hide card 1, card 2
// to do : add score, score point animation
var tempPlusScore = new PlusScore(card2.x, card2.y, this.getScore());
this.plusScoreGroup.add(tempPlusScore);
// card1.hide();
// card2.hide();
var hideOpenedCardTimeEvent = game.time.events.add(Phaser.Timer.SECOND * 0.5, this.hideOpenedCard, this);
this.gameTimeEvents[this.gameTimeEvents.length + 1] = hideOpenedCardTimeEvent;
},
hideOpenedCard() {
this.reservedHideCard[0].hide();
this.reservedHideCard[1].hide();
// this.addScore();
// this.updateScore();
this.scoreManager.plusScore(this.getScore());
// to do : check clear stage
this.clearedCardCount += 2;
if(this.activatedCardCount == this.clearedCardCount) {
// this.showStageNotice(this.playingStageNo + "단계 완료");
this.playingStageNo++;
var startStageTimeEvent = game.time.events.add(Phaser.Timer.SECOND * 1, this.startStage, this);
this.gameTimeEvents[this.gameTimeEvents.length + 1] = startStageTimeEvent;
}
},
resetOpenedCard() {
var card1 = this.cards[this.openCardInfoIndex[0]];
if(card1 == null)
return;
this.resetOpenCardInfo(card1.getColumnNo(), card1.getRowNo());
card1.flipToBack();
var card2 = this.cards[this.openCardInfoIndex[1]];
if(card2 == null)
return;
this.resetOpenCardInfo(card2.getColumnNo(), card2.getRowNo());
card2.flipToBack();
},
resetOpenedTwoCards() {
var resetOpenedCardTimeEvent = game.time.events.add(Phaser.Timer.SECOND * 0.5, this.resetOpenedCard, this);
this.gameTimeEvents[this.gameTimeEvents.length + 1] = resetOpenedCardTimeEvent;
},
setOpenCardInfo(col, row) {
var cardIndex = this.getCardIndex(col, row);
for(var i = 0; i < 2; i++) {
if(this.openCardInfoIndex[i] < 0) {
this.openCardInfoIndex[i] = this.cards[cardIndex].getCardIndex();
this.openCardInfoCharacter[i] = this.cards[cardIndex].getCharacter();
break;
}
}
},
resetOpenCardInfo(col, row) {
var cardIndex = this.getCardIndex(col, row);
for(var i = 0; i < 2; i++) {
if(this.openCardInfoIndex[i] == cardIndex) {
this.openCardInfoIndex[i] = -1;
this.openCardInfoCharacter[i] = -1;
}
}
},
isOpenedCard(col, row) {
var cardIndex = this.getCardIndex(col, row);
for(var i = 0; i < 2; i++) {
if(this.openCardInfoIndex[i] == cardIndex)
return true;
}
return false;
},
getScore: function() {
return this.playingStageNo * 10;
},
gameOver: function() {
this.activatedCardColumnNo = 0;
this.activatedCardRowNo = 0;
this.initCards();
game.time.events.remove(this.timeEvent);
this.hideSelectedCard();
for(var i = 0; i < this.gameTimeEvents.length; i++) {
game.time.events.remove(this.gameTimeEvents[i]);
}
this.gameTimeEvents = [];
var gameOverText = new GameOverText();
game.time.events.add(Phaser.Timer.SECOND * 2, this.goResult, this);
},
goResult: function() {
location.href = '../../web/client/result.html';
},
}
Game.GAME_TIME_SEC = 60;
Game.GAME_OVER_WAIT_TIME_MS = 2000;