Permalink
Browse files

various fixes

  • Loading branch information...
codepo8 committed Oct 7, 2012
1 parent f4b2be9 commit d4f6b0e8f1f964c30f814853d5746592172afa56
Showing with 99 additions and 71 deletions.
  1. +99 −71 index.html
View
@@ -38,6 +38,9 @@
<div id="gamebody"></div>
<script>
+(function(){
+
+/* Game overall */
var Game = {
players: {},
@@ -56,6 +59,52 @@
gamebody: document.querySelector('#gamebody')
};
+function initGame() {
+ createGrid();
+ createBlocks('food', Game.maxGoodies, 50);
+ createBlocks('barrier', Game.maxBarriers, 0);
+ createPlayer('bob');
+ createPlayer('joe');
+}
+
+function createGrid() {
+ Game.gamebody.style.width = (Game.grid.x * Game.grid.tilesize) + 'px';
+ Game.gamebody.style.height = (Game.grid.y * Game.grid.tilesize) + 'px';
+ Game.gamebody.classList.add('active');
+ for(var i = 0; i < Game.grid.x; i++) {
+ Game.grid.tiles[i] = {};
+ for(var j = 0; j < Game.grid.y; j++) {
+ Game.grid.tiles[i][j] = false;
+ }
+ }
+}
+
+function getFreeRandomTile() {
+ var x = 0,
+ y = 0;
+ do {
+ x = ~~(Math.random() * Game.grid.x),
+ y = ~~(Math.random() * Game.grid.y);
+ } while (Game.grid.tiles[x][y] !== false);
+ return {x:x, y:y}
+}
+
+function shiftwindow(x,y) {
+ window.scrollTo(
+ x * Game.grid.tilesize - Game.grid.tilesize,
+ y * Game.grid.tilesize - Game.grid.tilesize
+ );
+}
+
+function kill(gameitem) {
+ console.log(gameitem);
+ gameitem.sprite.parentNode.removeChild(gameitem.sprite);
+ delete Game.players[gameitem.name];
+ Game.grid.tiles[gameitem.x][gameitem.y] = false;
+}
+
+/* Players */
+
Player = function(){
this.name = '';
this.x = 0;
@@ -75,14 +124,6 @@
}
};
-Block = function(){
- this.x = 0;
- this.y = 0;
- this.value = 0;
- this.type = null;
- this.sprite = null;
-};
-
function createPlayer(name) {
var player = new Player(),
position = getFreeRandomTile();
@@ -120,15 +161,28 @@
player.sprite.style.top = (player.y * Game.grid.tilesize) + 'px';
}
-function renderBlock(block) {
- var sprite = document.createElement('div');
- sprite.classList.add('sprite');
- sprite.classList.add(block.type);
- block.sprite = sprite;
- block.sprite.dataset.value = block.value;
- block.sprite.style.left = (block.x * Game.grid.tilesize) + 'px';
- block.sprite.style.top = (block.y * Game.grid.tilesize) + 'px';
- gamebody.appendChild(sprite);
+function movePlayer(x, y) {
+ var player, test;
+ if (x >=0 && x < Game.grid.x && y >= 0 && y < Game.grid.y) {
+ player = Game.activePlayer;
+ if (player.health > 0) {
+ test = Game.grid.tiles[x][y];
+ if (test) {
+ if (test.type === 'food'){
+ player.health += test.value + -Game.moveEnergy;
+ player.moveTo(x, y);
+ window.setTimeout(
+ function(){
+ kill(test);
+ }
+ , 500);
+ }
+ } else {
+ player.moveTo(x, y);
+ shiftwindow(x, y);
+ }
+ }
+ }
}
function switchPlayer(newplayer) {
@@ -139,6 +193,17 @@
Game.activePlayer.sprite.classList.add('current');
shiftwindow(Game.activePlayer.x, Game.activePlayer.y);
}
+
+/* Blocks */
+
+Block = function(){
+ this.x = 0;
+ this.y = 0;
+ this.value = 0;
+ this.type = null;
+ this.sprite = null;
+};
+
function createBlocks(type, amount, value) {
var block;
while (amount--) {
@@ -153,37 +218,26 @@
}
}
-function getFreeRandomTile() {
- var x = 0,
- y = 0;
- do {
- x = ~~(Math.random() * Game.grid.x),
- y = ~~(Math.random() * Game.grid.y);
- } while (Game.grid.tiles[x][y] !== false);
- return {x:x, y:y}
+function renderBlock(block) {
+ var sprite = document.createElement('div');
+ sprite.classList.add('sprite');
+ sprite.classList.add(block.type);
+ block.sprite = sprite;
+ block.sprite.dataset.value = block.value;
+ block.sprite.style.left = (block.x * Game.grid.tilesize) + 'px';
+ block.sprite.style.top = (block.y * Game.grid.tilesize) + 'px';
+ gamebody.appendChild(sprite);
}
-function createGrid() {
- Game.gamebody.style.width = (Game.grid.x * Game.grid.tilesize) + 'px';
- Game.gamebody.style.height = (Game.grid.y * Game.grid.tilesize) + 'px';
- Game.gamebody.classList.add('active');
- for(var i = 0; i < Game.grid.x; i++) {
- Game.grid.tiles[i] = {};
- for(var j = 0; j < Game.grid.y; j++) {
- Game.grid.tiles[i][j] = false;
- }
- }
-}
+/* Event Listeners */
document.body.addEventListener('keydown', function(ev) {
var player = Game.activePlayer,
x = player.x,
y = player.y,
sx = x,
sy = y,
- health = player.health,
- movement = true,
- test = false;
+ movement = true;
switch (ev.which) {
case 32: // space
break;
@@ -203,50 +257,24 @@
movement = false;
break;
}
- if (movement && health > 0) {
- test = Game.grid.tiles[sx][sy];
- if (test) {
- if (test.type === 'food'){
- player.health += test.value + -Game.moveEnergy;
- player.moveTo(sx, sy);
- kill(test);
- }
- } else {
- player.moveTo(sx, sy);
- shiftwindow(sx,sy);
- }
+ if (movement) {
+ movePlayer(sx, sy);
}
ev.preventDefault();
}, false);
-function shiftwindow(x,y) {
- window.scrollTo(
- x * Game.grid.tilesize - Game.grid.tilesize,
- y * Game.grid.tilesize - Game.grid.tilesize
- );
-}
-
-function kill(gameitem) {
- gameitem.sprite.parentNode.removeChild(gameitem.sprite);
- delete Game.players[gameitem.name];
- Game.grid.tiles[gameitem.x][gameitem.y] = false;
-}
document.body.addEventListener('click', function(ev){
var t = ev.target;
if (t.dataset.name) {
switchPlayer(t.dataset.name);
}
},false);
-function initGame() {
- createGrid();
- createBlocks('food', Game.maxGoodies, 50);
- createBlocks('barrier', Game.maxBarriers, 0);
- createPlayer('bob');
- createPlayer('joe');
-}
+window.addEventListener('load', function(ev){
+ initGame();
+},false);
-initGame();
+})();
</script>
</body>
</html>

0 comments on commit d4f6b0e

Please sign in to comment.