Created
October 19, 2009 02:39
-
-
Save richtaur/213044 to your computer and use it in GitHub Desktop.
zombie game in Diggy?
This file contains hidden or 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
(function() { | |
var SCREEN_WIDTH = 480, | |
SCREEN_HEIGHT = 360, | |
SPRITE_SIZE = 24, | |
TILE_SIZE = 24, | |
TILES_WIDTH = 20, | |
TILES_HEIGHT = 15; | |
var game, | |
map, | |
screens; | |
var init = function() { | |
// Setup DGE | |
DGE.set('stage', 'l4d'); | |
DGE.set('default-dims', TILE_SIZE, TILE_SIZE); | |
DGE.set('tile-dims', TILE_SIZE); | |
DGE.init({ | |
base : 'img/', | |
stage : 'l4d' | |
}); | |
/* | |
screens = new DGE.Screens({}, | |
'battle' | |
); | |
*/ | |
// screens.battle.init(function() { | |
// Initialize the map Sprite | |
map.sprite = new DGE.Sprite({ | |
// addTo : this, | |
width : (TILES_WIDTH * TILE_SIZE), | |
height : (TILES_HEIGHT * TILE_SIZE) | |
}); | |
// }); | |
// screens.battle.init(); | |
// Begin the DGE main game loop | |
DGE.main.start(); | |
// TODO - get rid of this, find a more elegant way | |
// screens.battle.show(); | |
map.init(); | |
newGame(); | |
// /TODO | |
}; | |
var map = { | |
init : function() { | |
// Make the new tile map | |
this.tiles = DGE.sprites.tileMap(DGE.Sprite, TILES_WIDTH, TILES_HEIGHT, { | |
//addTo : this.sprite, | |
group : 'tiles', | |
image : 'concrete.png', | |
width : SPRITE_SIZE, | |
height : SPRITE_SIZE | |
}); | |
}, | |
load : function(num) { | |
var freeCoords = [], | |
i, | |
m = L4D.maps[num], | |
tile, x, y; | |
for (y = 0; y < TILES_HEIGHT; y++) { | |
for (x = 0; x < TILES_WIDTH; x++) { | |
tile = m.data[y][x]; | |
this.tiles[y][x].image(L4D.tiles[tile]); | |
if (tile == 'o') freeCoords.push({ | |
tx : x, | |
ty : y, | |
}); | |
} | |
} | |
this.survivors = []; | |
for (i = 0; i < m.survivors; i++) { | |
this.survivors.push(new Survivor({ | |
txy : freeCoords.shift() | |
})); | |
}; | |
this.numSurvivors = this.survivors.length; | |
this.zombies = []; | |
for (i = 0; i < m.zombies; i++) { | |
this.zombies.push(new Zombie({ | |
tx : DGE.rand(m.spawn.tx1, m.spawn.tx2), | |
ty : DGE.rand(m.spawn.ty1, m.spawn.ty2) | |
})); | |
} | |
this.numZombies = this.zombies.length; | |
// TODO clean this up, get it into main() | |
setInterval(function() { | |
map.zombies.sort(function(a, b) { | |
return (a.y - b.y); | |
}); | |
for (var i = 0; i < map.numZombies; i++) { | |
map.zombies[i].front(); | |
} | |
}, 100); | |
} | |
}; | |
var newGame = function() { | |
map.load(0); | |
}; | |
var Survivor = DGE.extend(DGE.Sprite, function(conf) { | |
this.init(conf, { | |
group : 'survivors', | |
image : 'zoey_d.gif' | |
}); | |
}); | |
var Zombie = DGE.extend(DGE.Sprite, function(conf) { | |
this.init(conf, { | |
group : 'zombies', | |
image : 'zombie_u.gif', | |
move : DGE.move.angle, | |
ping : function() { | |
if (DGE.rand(0, 50) == 1) { | |
var survivor = map.survivors[DGE.rand(0, (map.survivors.length - 1))]; | |
this.speed(DGE.rand(1, 3) / 8); | |
this.angleTo(survivor, true); | |
} | |
} | |
}); | |
}); | |
var zombies = { | |
getSpeed : function() { | |
var p = DGE.rand(1, 100), | |
speed; | |
if (p < 50) { | |
speed = DGE.rand(1, 5) / DGE.rand(2, 10); | |
} else if (p < 80) { | |
speed = 1; | |
} else if (p < 90) { | |
speed = DGE.rand(1, 5) / DGE.rand(1, 2); | |
} else { | |
speed = DGE.rand(1, 5) / DGE.rand(8, 10); | |
} | |
return speed; | |
} | |
}; | |
init(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment