Browse Source

fixed animation on zilch

master
Lurkars 8 years ago
parent
commit
97347dcdae
  1. 48
      js/game.js
  2. 56
      js/interface.js
  3. 42
      style/main.css

48
js/game.js

@ -1,25 +1,33 @@
/**
* Game (Model + Controller)
*/
function Game(Interface) { function Game(Interface) {
var self = this; var self = this;
// get interface
self.Interface = new Interface; self.Interface = new Interface;
// register event callbacks
self.Interface.on("restart", self.restart.bind(this)); self.Interface.on("restart", self.restart.bind(this));
self.Interface.on("takePoints", self.takePoints.bind(this)); self.Interface.on("takePoints", self.takePoints.bind(this));
self.Interface.on("addPoints", self.addPoints.bind(this)); self.Interface.on("addPoints", self.addPoints.bind(this));
self.Interface.on("rollDices", self.rollDices.bind(this)); self.Interface.on("rollDices", self.rollDices.bind(this));
self.Interface.on("toggleDice", self.toggleDice.bind(this)); self.Interface.on("toggleDice", self.toggleDice.bind(this));
self.Interface.on("toggleCpu", self.toggleCpu.bind(this));
self.setup(); self.setup();
} }
/**
* Restart game (reset)
*/
Game.prototype.restart = function() { Game.prototype.restart = function() {
this.Interface.setup(); this.Interface.setup();
this.setup(); this.setup();
}; };
/**
* Setup new game
*/
Game.prototype.setup = function() { Game.prototype.setup = function() {
var self = this; var self = this;
self.player = { self.player = {
@ -74,10 +82,16 @@ Game.prototype.setup = function() {
}; };
/**
* Random helper for integer numbers
*/
Game.prototype.random = function(int) { Game.prototype.random = function(int) {
return Math.floor((Math.random() * int)); return Math.floor((Math.random() * int));
}; };
/**
* Roll dices
*/
Game.prototype.rollDices = function(all) { Game.prototype.rollDices = function(all) {
var self = this; var self = this;
self.Interface.clearMessage(); self.Interface.clearMessage();
@ -102,6 +116,9 @@ Game.prototype.rollDices = function(all) {
} }
} }
self.Interface.setDices(self.dices);
if (rollCount == 0) { if (rollCount == 0) {
self.rollDices(true); self.rollDices(true);
} else if (self.calculatePoints(true) == 0) { } else if (self.calculatePoints(true) == 0) {
@ -184,7 +201,6 @@ Game.prototype.rollDices = function(all) {
self.Interface.animateDices(self.dices, self.animationTime); self.Interface.animateDices(self.dices, self.animationTime);
self.Interface.disableTakePoints(true); self.Interface.disableTakePoints(true);
self.Interface.disableRollDices(true); self.Interface.disableRollDices(true);
self.Interface.setDices(self.dices);
if (!self.playing) { if (!self.playing) {
self.cpuPlay(); self.cpuPlay();
@ -192,6 +208,9 @@ Game.prototype.rollDices = function(all) {
} }
}; };
/**
* Toggle dice selection
*/
Game.prototype.toggleDice = function(diceIndex) { Game.prototype.toggleDice = function(diceIndex) {
var self = this; var self = this;
var dice = self.dices[diceIndex]; var dice = self.dices[diceIndex];
@ -243,6 +262,9 @@ Game.prototype.toggleDice = function(diceIndex) {
self.Interface.setPoints(self.points + points); self.Interface.setPoints(self.points + points);
}; };
/**
* Calculate points on selected dices or all dices
*/
Game.prototype.calculatePoints = function(all) { Game.prototype.calculatePoints = function(all) {
var self = this; var self = this;
var result = [0, 0, 0, 0, 0, 0]; var result = [0, 0, 0, 0, 0, 0];
@ -299,7 +321,9 @@ Game.prototype.calculatePoints = function(all) {
return points; return points;
} }
/**
* Add points for current player
*/
Game.prototype.addPoints = function() { Game.prototype.addPoints = function() {
var self = this; var self = this;
@ -318,6 +342,9 @@ Game.prototype.addPoints = function() {
self.Interface.setPoints(self.points); self.Interface.setPoints(self.points);
}; };
/**
* Take points and end round
*/
Game.prototype.takePoints = function() { Game.prototype.takePoints = function() {
var self = this; var self = this;
@ -348,6 +375,9 @@ Game.prototype.takePoints = function() {
self.endRound(); self.endRound();
}; };
/**
* End round for current player
*/
Game.prototype.endRound = function() { Game.prototype.endRound = function() {
var self = this; var self = this;
@ -403,6 +433,9 @@ Game.prototype.endRound = function() {
} }
/**
* CPU playing logic
*/
Game.prototype.cpuPlay = function() { Game.prototype.cpuPlay = function() {
var self = this; var self = this;
self.Interface.disableRestart(true); self.Interface.disableRestart(true);
@ -451,8 +484,5 @@ Game.prototype.cpuPlay = function() {
} }
Game.prototype.toggleCpu = function() {
}
// create new game
var game = new Game(Interface); var game = new Game(Interface);

56
js/interface.js

@ -1,9 +1,14 @@
/**
* UI
*/
function Interface() { function Interface() {
var self = this; var self = this;
// Event Objects for callbacks
self.events = {}; self.events = {};
// get DOM elements
self.restartButton = document.querySelector('#restart-button'); self.restartButton = document.querySelector('#restart-button');
self.playerScoreContainer = document.querySelector('#player-score-container'); self.playerScoreContainer = document.querySelector('#player-score-container');
@ -22,9 +27,7 @@ function Interface() {
self.message = document.querySelector('#message'); self.message = document.querySelector('#message');
self.dicesButton.classList.add("disabled");
self.pointsButton.classList.add("disabled");
// add click events
self.restartButton.addEventListener("click", function() { self.restartButton.addEventListener("click", function() {
if (!this.classList.contains('disabled')) { if (!this.classList.contains('disabled')) {
self.fireEvent("restart"); self.fireEvent("restart");
@ -45,6 +48,7 @@ function Interface() {
} }
}); });
// add click events for dices
for (var diceIndex = 0; diceIndex < 6; diceIndex++) { for (var diceIndex = 0; diceIndex < 6; diceIndex++) {
var diceContainer = self.dices[diceIndex]; var diceContainer = self.dices[diceIndex];
diceContainer.diceIndex = diceIndex; diceContainer.diceIndex = diceIndex;
@ -58,6 +62,9 @@ function Interface() {
this.setup(); this.setup();
}; };
/**
* setup init state
*/
Interface.prototype.setup = function() { Interface.prototype.setup = function() {
this.dicesButton.classList.add("disabled"); this.dicesButton.classList.add("disabled");
this.pointsButton.classList.add("disabled"); this.pointsButton.classList.add("disabled");
@ -65,6 +72,9 @@ Interface.prototype.setup = function() {
}; };
/**
* register callbacks for events
*/
Interface.prototype.on = function(event, callback) { Interface.prototype.on = function(event, callback) {
if (!this.events[event]) { if (!this.events[event]) {
this.events[event] = []; this.events[event] = [];
@ -72,6 +82,9 @@ Interface.prototype.on = function(event, callback) {
this.events[event].push(callback); this.events[event].push(callback);
}; };
/**
* fire events and execute callbacks
*/
Interface.prototype.fireEvent = function(event, data) { Interface.prototype.fireEvent = function(event, data) {
var callbacks = this.events[event]; var callbacks = this.events[event];
if (callbacks) { if (callbacks) {
@ -81,6 +94,9 @@ Interface.prototype.fireEvent = function(event, data) {
} }
}; };
/**
* Set dices
*/
Interface.prototype.setDices = function(dices) { Interface.prototype.setDices = function(dices) {
var self = this; var self = this;
@ -108,12 +124,11 @@ Interface.prototype.setDices = function(dices) {
diceContainer.classList.remove('invalid'); diceContainer.classList.remove('invalid');
} }
} }
}; };
/**
* fire dices animation
*/
Interface.prototype.animateDices = function(dices, timeout, callback) { Interface.prototype.animateDices = function(dices, timeout, callback) {
var self = this; var self = this;
for (var diceIndex = 0; diceIndex < 6; diceIndex++) { for (var diceIndex = 0; diceIndex < 6; diceIndex++) {
@ -134,6 +149,9 @@ Interface.prototype.animateDices = function(dices, timeout, callback) {
}, timeout ? timeout : 0); }, timeout ? timeout : 0);
}; };
/**
* set disabled state of 'restart' button
*/
Interface.prototype.disableRestart = function(disabled) { Interface.prototype.disableRestart = function(disabled) {
if (disabled) { if (disabled) {
this.restartButton.classList.add("disabled") this.restartButton.classList.add("disabled")
@ -142,6 +160,9 @@ Interface.prototype.disableRestart = function(disabled) {
} }
}; };
/**
* set disabled state of 'take points' button
*/
Interface.prototype.disableTakePoints = function(disabled) { Interface.prototype.disableTakePoints = function(disabled) {
if (disabled) { if (disabled) {
this.pointsButton.classList.add("disabled") this.pointsButton.classList.add("disabled")
@ -150,6 +171,9 @@ Interface.prototype.disableTakePoints = function(disabled) {
} }
}; };
/**
* set disabled state of 'roll dices' button
*/
Interface.prototype.disableRollDices = function(disabled) { Interface.prototype.disableRollDices = function(disabled) {
if (disabled) { if (disabled) {
this.dicesButton.classList.add("disabled") this.dicesButton.classList.add("disabled")
@ -158,10 +182,16 @@ Interface.prototype.disableRollDices = function(disabled) {
} }
}; };
/**
* set points
*/
Interface.prototype.setPoints = function(points) { Interface.prototype.setPoints = function(points) {
this.points.innerHTML = points; this.points.innerHTML = points;
}; };
/**
* set playing status (Player or CPU)
*/
Interface.prototype.setPlaying = function(playing) { Interface.prototype.setPlaying = function(playing) {
if (playing) { if (playing) {
this.playing = true; this.playing = true;
@ -174,6 +204,9 @@ Interface.prototype.setPlaying = function(playing) {
} }
}; };
/**
* set player data
*/
Interface.prototype.setPlayer = function(player) { Interface.prototype.setPlayer = function(player) {
this.playerScore.innerHTML = player.score; this.playerScore.innerHTML = player.score;
var zilchs = ''; var zilchs = '';
@ -184,6 +217,9 @@ Interface.prototype.setPlayer = function(player) {
}; };
/**
* set cpu data
*/
Interface.prototype.setCpu = function(cpu) { Interface.prototype.setCpu = function(cpu) {
this.cpuScore.innerHTML = cpu.score; this.cpuScore.innerHTML = cpu.score;
var zilchs = ''; var zilchs = '';
@ -194,6 +230,9 @@ Interface.prototype.setCpu = function(cpu) {
this.cpuZilch.innerHTML = zilchs; this.cpuZilch.innerHTML = zilchs;
}; };
/**
* show message with timeout and callback
*/
Interface.prototype.showMessage = function(message, timeout, callback) { Interface.prototype.showMessage = function(message, timeout, callback) {
var self = this; var self = this;
@ -210,6 +249,9 @@ Interface.prototype.showMessage = function(message, timeout, callback) {
} }
}; };
/**
* hide current message
*/
Interface.prototype.clearMessage = function(callback) { Interface.prototype.clearMessage = function(callback) {
this.message.classList.remove('visible'); this.message.classList.remove('visible');
if (callback) { if (callback) {

42
style/main.css

@ -236,23 +236,23 @@ hr {
} }
.dice.duration1.animate:after { .dice.duration1.animate:after {
animation-delay: 0.3s;
-webkit-animation-delay: 0.1s;
-moz-animation-delay: 0.1s;
-o-animation-delay: 0.1s;
-ms-animation-delay: 0.1s;
animation-delay: 0.2s;
-webkit-animation-delay: 0.2s;
-moz-animation-delay: 0.2s;
-o-animation-delay: 0.2s;
-ms-animation-delay: 0.2s;
} }
.dice.duration1.animate:after { .dice.duration1.animate:after {
animation-delay: 0.6s;
-webkit-animation-delay: 0.1s;
-moz-animation-delay: 0.1s;
-o-animation-delay: 0.1s;
-ms-animation-delay: 0.1s;
animation-delay: 0.3s;
-webkit-animation-delay: 0.3s;
-moz-animation-delay: 0.3s;
-o-animation-delay: 0.3s;
-ms-animation-delay: 0.3s;
} }
.dice.duration3.animate:after { .dice.duration3.animate:after {
animation-delay: 0.7s;
animation-delay: 0.1s;
-webkit-animation-delay: 0.1s; -webkit-animation-delay: 0.1s;
-moz-animation-delay: 0.1s; -moz-animation-delay: 0.1s;
-o-animation-delay: 0.1s; -o-animation-delay: 0.1s;
@ -260,19 +260,19 @@ hr {
} }
.dice.duration4.animate:after { .dice.duration4.animate:after {
animation-delay: 1.2s;
-webkit-animation-delay: 0.1s;
-moz-animation-delay: 0.1s;
-o-animation-delay: 0.1s;
-ms-animation-delay: 0.1s;
animation-delay: 0.3s;
-webkit-animation-delay: 0.3s;
-moz-animation-delay: 0.3s;
-o-animation-delay: 0.3s;
-ms-animation-delay: 0.3s;
} }
.dice.duration5.animate:after { .dice.duration5.animate:after {
animation-delay: 1.5s;
-webkit-animation-delay: 0.1s;
-moz-animation-delay: 0.1s;
-o-animation-delay: 0.1s;
-ms-animation-delay: 0.1s;
animation-delay: 0.2s;
-webkit-animation-delay: 0.2s;
-moz-animation-delay: 0.2s;
-o-animation-delay: 0.2s;
-ms-animation-delay: 0.2s;
} }
.message { .message {

Loading…
Cancel
Save