var PlayerChoiceGraphic = Container.expand(function (choice) {
var self = Container.call(this);
var graphic = self.createAsset(choice, 'Player choice graphic', 0.5, 0.5);
self.getChoice = function () {
return graphic;
};
});
var EnemyChoiceGraphic = Container.expand(function (choice) {
var self = Container.call(this);
var graphic = self.createAsset(choice, 'Enemy choice graphic', 0.5, 0.5);
self.getChoice = function () {
return graphic;
};
});
var Enemy = Container.expand(function (type, game) {
var self = Container.call(this);
self.choice = null;
self.type = type;
self.game = game;
self.choose = function () {
var choices;
switch (self.type) {
case 1:
self.name = 'Stone Lover';
choices = ['stone', 'stone', 'scissors', 'paper'];
break;
case 2:
self.name = 'Scissors Fan';
choices = ['scissors', 'scissors', 'stone', 'paper'];
break;
case 3:
self.name = 'Paper Enthusiast';
choices = ['paper', 'paper', 'scissors', 'stone'];
break;
case 4:
self.name = 'Stonephobic';
choices = ['scissors', 'stone', 'stone', 'paper', 'paper'];
break;
case 5:
self.name = 'Scissorshater';
choices = ['stone', 'scissors', 'scissors', 'paper', 'paper'];
break;
case 6:
self.name = 'PaperIsUgly';
choices = ['paper', 'scissors', 'scissors', 'stone', 'stone'];
break;
case 7:
self.name = 'Copycat';
choices = self.game.lastPlayerChoice ? [self.game.lastPlayerChoice] : ['scissors', 'stone', 'paper'];
break;
case 8:
self.name = 'One Step Ahead';
choices = self.game.lastPlayerChoice ? [self.game.lastPlayerChoice === 'scissors' ? 'stone' : self.game.lastPlayerChoice === 'stone' ? 'paper' : 'scissors'] : ['scissors', 'stone', 'paper'];
break;
case 9:
self.name = 'Contrarian';
choices = self.game.lastPlayerChoice ? [self.game.lastPlayerChoice === 'scissors' ? 'paper' : self.game.lastPlayerChoice === 'stone' ? 'scissors' : 'stone'] : ['scissors', 'stone', 'paper'];
break;
default:
self.name = 'Balanced';
choices = ['scissors', 'stone', 'paper'];
}
self.choice = choices[Math.floor(Math.random() * choices.length)];
};
});
var Player = Container.expand(function () {
var self = Container.call(this);
self.choice = null;
self.choose = function (choice) {
self.choice = choice;
};
});
var Game = Container.expand(function () {
var self = Container.call(this);
self.resetGame = function () {
self.playerScore = 0;
self.enemyScore = 0;
self.lastPlayerChoice = null;
if (self.playerChoiceGraphic) {
self.playerChoiceGraphic.destroy();
}
if (self.enemyChoiceGraphic) {
self.enemyChoiceGraphic.destroy();
}
var possibleEnemyTypes = [1, 2, 3, 4, 5, 6, 7, 8, 9].filter(function (type) {
return self.enemyTypesDefeated.indexOf(type) === -1;
});
var enemyType = possibleEnemyTypes[Math.floor(Math.random() * possibleEnemyTypes.length)];
enemy = self.addChild(new Enemy(enemyType, self));
player = self.addChild(new Player());
playerScoreTxt.setText('Player Score: ' + self.playerScore);
enemyScoreTxt.setText('Enemy Score: ' + self.enemyScore);
};
self.enemyTypesDefeated = [];
self.totalWins = 0;
self.playerScore = 0;
self.enemyScore = 0;
var enemyType = Math.floor(Math.random() * 3) + 1;
var enemy = self.addChild(new Enemy(enemyType, self));
var player = self.addChild(new Player());
self.lastPlayerChoice = null;
var resultTxt = new Text2('', {
size: 150,
fill: "#ffffff"
});
resultTxt.anchor.set(.5, 0);
LK.gui.topCenter.addChild(resultTxt);
var playerScoreTxt = new Text2('Player Score: 0', {
size: 100,
fill: "#ffffff"
});
playerScoreTxt.anchor.set(0.5, 1);
playerScoreTxt.x = 2048 / 2 - 300;
playerScoreTxt.y = 2732 / 2 - 100;
self.addChild(playerScoreTxt);
self.playerChoiceGraphic = null;
var enemyScoreTxt = new Text2('Enemy Score: 0', {
size: 100,
fill: "#ffffff"
});
enemyScoreTxt.anchor.set(0.5, 1);
enemyScoreTxt.x = 2048 / 2 - 300;
enemyScoreTxt.y = 2732 / 2 + 100;
self.addChild(enemyScoreTxt);
var totalWinsTxt = new Text2('Total Wins: 0', {
size: 100,
fill: "#ffffff"
});
totalWinsTxt.anchor.set(0.5, 1);
totalWinsTxt.x = 2048 / 2 + 300;
totalWinsTxt.y = 2732 / 2 - 200;
self.addChild(totalWinsTxt);
totalWinsTxt.on('down', function () {
var defeatedEnemies = self.enemyTypesDefeated.map(function (type) {
switch (type) {
case 1:
return 'Stone Lover';
case 2:
return 'Scissors Fan';
case 3:
return 'Paper Enthusiast';
case 4:
return 'Stonephobic';
case 5:
return 'Scissorshater';
case 6:
return 'PaperIsUgly';
case 7:
return 'Copycat';
case 8:
return 'One Step Ahead';
case 9:
return 'Contrarian';
default:
return 'Balanced';
}
}).join(', ');
var popup = LK.getAsset('popup', 'Popup with defeated enemies', .5, .5);
var popupText = new Text2(defeatedEnemies, {
size: 50,
fill: "#ffffff"
});
popupText.anchor.set(.5, .5);
popup.addChild(popupText);
self.addChild(popup);
popup.on('down', function () {
popup.destroy();
});
});
enemy.x = 2048 / 2 - 200;
enemy.y = 2732 / 2;
player.x = 2048 / 2 + 200;
player.y = 2732 / 2;
var choices = ['scissors', 'stone', 'paper'];
choices.forEach(function (choice, index) {
var choiceBtn = LK.getAsset(choice, 'Choice button', .5, .5);
choiceBtn.scale.set(2.0);
choiceBtn.x = 2048 / 3 * index + 2048 / 6 - choiceBtn.width / 2;
choiceBtn.y = 2732 - choiceBtn.height - 100;
self.addChild(choiceBtn);
choiceBtn.on('down', function () {
enemy.choose();
player.choose(choice);
self.lastPlayerChoice = choice;
var result = '';
if (player.choice === enemy.choice) {
result = 'Draw!';
} else if (player.choice === 'scissors' && enemy.choice === 'paper' || player.choice === 'stone' && enemy.choice === 'scissors' || player.choice === 'paper' && enemy.choice === 'stone') {
result = 'You win!';
self.playerScore++;
} else {
result = 'You lose!';
self.enemyScore++;
}
resultTxt.setText(result);
playerScoreTxt.setText('Player Score: ' + self.playerScore);
enemyScoreTxt.setText('Enemy Score: ' + self.enemyScore);
if (self.playerScore >= 10) {
resultTxt.setText('You won\nagainst ' + enemy.name + '!');
self.enemyTypesDefeated.push(enemy.type);
self.totalWins++;
if (self.enemyTypesDefeated.length === 9) {
resultTxt.setText('Congratulations!\nYou have defeated\nall enemy types!');
LK.showGameOver();
} else {
self.resetGame();
}
} else if (self.enemyScore >= 10) {
resultTxt.setText('You lost\nagainst ' + enemy.name + '\nGame Over!');
LK.showGameOver();
}
totalWinsTxt.setText('Total Wins: ' + self.totalWins);
if (self.playerChoiceGraphic) {
self.playerChoiceGraphic.destroy();
}
self.playerChoiceGraphic = self.addChild(new PlayerChoiceGraphic(player.choice));
self.playerChoiceGraphic.x = 2048 / 2 + 300;
self.playerChoiceGraphic.y = playerScoreTxt.y - 50;
if (self.enemyChoiceGraphic) {
self.enemyChoiceGraphic.destroy();
}
self.enemyChoiceGraphic = self.addChild(new EnemyChoiceGraphic(enemy.choice));
self.enemyChoiceGraphic.x = 2048 / 2 + 300;
self.enemyChoiceGraphic.y = 2732 / 2 + 50;
});
});
});