-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7aa74e0
commit 0b26f6a
Showing
15 changed files
with
4,045 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
var GM; | ||
|
||
// Wait till the browser is ready to render the game (avoids glitches) | ||
window.requestAnimationFrame(function () { | ||
GM = new GameManager(8, KeyboardInputManager, HTMLActuator, LocalScoreManager); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
|
||
var auto_move_flag = false; | ||
var auto_move_time; | ||
|
||
function start_auto_move(){ | ||
auto_move_flag = true; | ||
auto_move(); | ||
} | ||
|
||
function auto_move(){ | ||
if ( auto_move_flag === false ) | ||
return; | ||
var direction = Math.floor( Math.random() * 4 ); | ||
GM.move( direction ); | ||
setTimeout( "auto_move()", auto_move_time ); | ||
} | ||
|
||
function stop_auto_move(){ | ||
auto_move_flag = false; | ||
} | ||
|
||
window.requestAnimationFrame(function(){ | ||
document.getElementById("auto-move-run").addEventListener("click",function(){ | ||
var time = parseInt( document.getElementById("auto-move-input-time").value ); | ||
if ( !isNaN( time ) ){ | ||
auto_move_time = time; | ||
if ( auto_move_flag === false ){ | ||
start_auto_move(); | ||
} | ||
} | ||
}); | ||
document.getElementById("auto-move-stop").addEventListener("click",function(){ | ||
stop_auto_move(); | ||
}); | ||
}); |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
function Grid(size) { | ||
this.size = size; | ||
|
||
this.cells = []; | ||
|
||
this.build(); | ||
} | ||
|
||
// Build a grid of the specified size | ||
Grid.prototype.build = function () { | ||
for (var x = 0; x < this.size; x++) { | ||
var row = this.cells[x] = []; | ||
|
||
for (var y = 0; y < this.size; y++) { | ||
row.push(null); | ||
} | ||
} | ||
}; | ||
|
||
// Find the first available random position | ||
Grid.prototype.randomAvailableCell = function () { | ||
var cells = this.availableCells(); | ||
|
||
if (cells.length) { | ||
return cells[Math.floor(Math.random() * cells.length)]; | ||
} | ||
}; | ||
|
||
Grid.prototype.availableCells = function () { | ||
var cells = []; | ||
|
||
this.eachCell(function (x, y, tile) { | ||
if (!tile) { | ||
cells.push({ x: x, y: y }); | ||
} | ||
}); | ||
|
||
return cells; | ||
}; | ||
|
||
// Call callback for every cell | ||
Grid.prototype.eachCell = function (callback) { | ||
for (var x = 0; x < this.size; x++) { | ||
for (var y = 0; y < this.size; y++) { | ||
callback(x, y, this.cells[x][y]); | ||
} | ||
} | ||
}; | ||
|
||
// Check if there are any cells available | ||
Grid.prototype.cellsAvailable = function () { | ||
return !!this.availableCells().length; | ||
}; | ||
|
||
// Check if the specified cell is taken | ||
Grid.prototype.cellAvailable = function (cell) { | ||
return !this.cellOccupied(cell); | ||
}; | ||
|
||
Grid.prototype.cellOccupied = function (cell) { | ||
return !!this.cellContent(cell); | ||
}; | ||
|
||
Grid.prototype.cellContent = function (cell) { | ||
if (this.withinBounds(cell)) { | ||
return this.cells[cell.x][cell.y]; | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
// Inserts a tile at its position | ||
Grid.prototype.insertTile = function (tile) { | ||
this.cells[tile.x][tile.y] = tile; | ||
}; | ||
|
||
Grid.prototype.removeTile = function (tile) { | ||
this.cells[tile.x][tile.y] = null; | ||
}; | ||
|
||
Grid.prototype.withinBounds = function (position) { | ||
return position.x >= 0 && position.x < this.size && | ||
position.y >= 0 && position.y < this.size; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
function HTMLActuator() { | ||
this.tileContainer = document.querySelector(".tile-container"); | ||
this.scoreContainer = document.querySelector(".score-container"); | ||
this.bestContainer = document.querySelector(".best-container"); | ||
this.messageContainer = document.querySelector(".game-message"); | ||
this.sharingContainer = document.querySelector(".score-sharing"); | ||
|
||
this.score = 0; | ||
} | ||
|
||
HTMLActuator.prototype.actuate = function (grid, metadata) { | ||
var self = this; | ||
|
||
window.requestAnimationFrame(function () { | ||
self.clearContainer(self.tileContainer); | ||
|
||
grid.cells.forEach(function (column) { | ||
column.forEach(function (cell) { | ||
if (cell) { | ||
self.addTile(cell); | ||
} | ||
}); | ||
}); | ||
|
||
self.updateScore(metadata.score); | ||
self.updateBestScore(metadata.bestScore); | ||
|
||
if (metadata.terminated) { | ||
if (metadata.over) { | ||
self.message(false); // You lose | ||
} else if (metadata.won) { | ||
self.message(true); // You win! | ||
} | ||
} | ||
|
||
}); | ||
}; | ||
|
||
// Continues the game (both restart and keep playing) | ||
HTMLActuator.prototype.continue = function () { | ||
if (typeof ga !== "undefined") { | ||
ga("send", "event", "game", "restart"); | ||
} | ||
|
||
this.clearMessage(); | ||
}; | ||
|
||
HTMLActuator.prototype.clearContainer = function (container) { | ||
while (container.firstChild) { | ||
container.removeChild(container.firstChild); | ||
} | ||
}; | ||
|
||
HTMLActuator.prototype.addTile = function (tile) { | ||
var self = this; | ||
|
||
var wrapper = document.createElement("div"); | ||
var inner = document.createElement("div"); | ||
var position = tile.previousPosition || { x: tile.x, y: tile.y }; | ||
var positionClass = this.positionClass(position); | ||
|
||
// We can't use classlist because it somehow glitches when replacing classes | ||
var classes = ["tile", "tile-" + tile.value, positionClass]; | ||
|
||
if (tile.value > 3388997632) classes.push("tile-super"); | ||
|
||
this.applyClasses(wrapper, classes); | ||
|
||
inner.classList.add("tile-inner"); | ||
inner.textContent = tile.value; | ||
|
||
if (tile.previousPosition) { | ||
// Make sure that the tile gets rendered in the previous position first | ||
window.requestAnimationFrame(function () { | ||
classes[2] = self.positionClass({ x: tile.x, y: tile.y }); | ||
self.applyClasses(wrapper, classes); // Update the position | ||
}); | ||
} else if (tile.mergedFrom) { | ||
classes.push("tile-merged"); | ||
this.applyClasses(wrapper, classes); | ||
|
||
// Render the tiles that merged | ||
tile.mergedFrom.forEach(function (merged) { | ||
self.addTile(merged); | ||
}); | ||
} else { | ||
classes.push("tile-new"); | ||
this.applyClasses(wrapper, classes); | ||
} | ||
|
||
// Add the inner part of the tile to the wrapper | ||
wrapper.appendChild(inner); | ||
|
||
// Put the tile on the board | ||
this.tileContainer.appendChild(wrapper); | ||
}; | ||
|
||
HTMLActuator.prototype.applyClasses = function (element, classes) { | ||
element.setAttribute("class", classes.join(" ")); | ||
}; | ||
|
||
HTMLActuator.prototype.normalizePosition = function (position) { | ||
return { x: position.x + 1, y: position.y + 1 }; | ||
}; | ||
|
||
HTMLActuator.prototype.positionClass = function (position) { | ||
position = this.normalizePosition(position); | ||
return "tile-position-" + position.x + "-" + position.y; | ||
}; | ||
|
||
HTMLActuator.prototype.updateScore = function (score) { | ||
this.clearContainer(this.scoreContainer); | ||
|
||
var difference = score - this.score; | ||
this.score = score; | ||
|
||
this.scoreContainer.textContent = this.score; | ||
|
||
if (difference > 0) { | ||
var addition = document.createElement("div"); | ||
addition.classList.add("score-addition"); | ||
addition.textContent = "+" + difference; | ||
|
||
this.scoreContainer.appendChild(addition); | ||
} | ||
}; | ||
|
||
HTMLActuator.prototype.updateBestScore = function (bestScore) { | ||
this.bestContainer.textContent = bestScore; | ||
}; | ||
|
||
HTMLActuator.prototype.message = function (won) { | ||
var type = won ? "game-won" : "game-over"; | ||
var message = won ? "You win!" : "Your balls disappeared!"; | ||
|
||
if (typeof ga !== "undefined") { | ||
ga("send", "event", "game", "end", type, this.score); | ||
} | ||
|
||
this.messageContainer.classList.add(type); | ||
this.messageContainer.getElementsByTagName("p")[0].textContent = message; | ||
|
||
this.clearContainer(this.sharingContainer); | ||
this.sharingContainer.appendChild(this.scoreTweetButton()); | ||
twttr.widgets.load(); | ||
}; | ||
|
||
HTMLActuator.prototype.clearMessage = function () { | ||
// IE only takes one value to remove at a time. | ||
this.messageContainer.classList.remove("game-won"); | ||
this.messageContainer.classList.remove("game-over"); | ||
}; | ||
|
||
HTMLActuator.prototype.scoreTweetButton = function () { | ||
var tweet = document.createElement("a"); | ||
tweet.classList.add("twitter-share-button"); | ||
tweet.setAttribute("href", "https://twitter.com/share"); | ||
tweet.setAttribute("data-via", "gabrielecirulli"); | ||
tweet.innerHTML = "<s>Tweet</s> X"; | ||
|
||
var text = "I scored " + this.score + " points at 2048, a game where you " + | ||
"join numbers to score high! #2048game"; | ||
tweet.setAttribute("data-text", text); | ||
|
||
return tweet; | ||
}; |
Oops, something went wrong.