Skip to content

Commit

Permalink
Merge branch 'master' into gh-pages
Browse files Browse the repository at this point in the history
  • Loading branch information
LynxyssCZ committed Jun 18, 2014
2 parents 8e8a13f + e03866d commit e120931
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 6 deletions.
Binary file modified pacman.js
Binary file not shown.
47 changes: 43 additions & 4 deletions src/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Game.Map = function (game, display, width, height) {
Game.Map.prototype.clear = function() {
this._objectDefs = [];
this._objects = [[]];
this._dirty = [[]];
this._dynamics = [];
this._player = null;
this._foodCount = 0;
Expand All @@ -35,7 +36,10 @@ Game.Map.prototype.getFoodCount = function() {
return this._foodCount;
}

Game.Map.prototype.eatFood = function(count) {
Game.Map.prototype.eatFood = function(nx, ny) {
var x = Math.floor(nx);
var y = Math.floor(ny);
this.destroyObject(x, y);
this._foodCount--;
}

Expand All @@ -56,10 +60,13 @@ Game.Map.prototype.placeObject = function (nx, ny, type) {
if ( x == this.getPlayerX() && y == this.getPlayerY() ) throw "Cell occupied by a player!";
if (typeof(this._objects[x]) === 'undefined') { // Row not instanced yet
this._objects[x] = [];
this._dirty[x] = [];
this._objects[x][y] = type;
this._dirty[x][y] = true;
if (type == 'food') {this._foodCount++;};
} else if (typeof(this._objects[x][y]) === 'undefined') { // Cell empty
this._objects[x][y] = type;
this._dirty[x][y] = true;
if (type == 'food') {this._foodCount++;};
} else { // Cell Is full
throw "Cell " + x +":"+ y + " is already full!";
Expand All @@ -73,8 +80,11 @@ Game.Map.prototype.destroyObject = function(nx, ny) {
if (typeof(this._objects[x]) !== 'undefined') { // Row is instanced
if (typeof(this._objects[x][y]) !== 'undefined') { // Cell is not empty
delete this._objects[x][y];
this._dirty[x][y] = true;
return true;
}
}
return false;
}

Game.Map.prototype.placePlayer = function (nx, ny, tile) {
Expand Down Expand Up @@ -106,7 +116,8 @@ Game.Map.prototype.getDefinition = function(type) {

// Redraw the whole map
Game.Map.prototype.draw = function() {
this._display.clear();
//this._display.clear();
/**
for (var row in this._objects) {
for (var coll in this._objects[row]) {
var objectDef = this._objectDefs[this._objects[row][coll]];
Expand All @@ -117,14 +128,37 @@ Game.Map.prototype.draw = function() {
this._display.drawTile( row, coll, objectDef.tile, 0, false );
}
}
else if( objectDef.color ) {
else if( objectDef.color ) {
this._display.drawBlock( row , coll , 1, 1, objectDef.color);
}
};
};
*/
var dirties = 0;
for(var coll in this._dirty) {
for(var row in this._dirty[coll]) {
var objectDef = this._objectDefs[this._objects[coll][row]];
delete this._dirty[coll][row];
dirties++;
if (typeof(objectDef) === 'undefined') {
this._display.clearTile(coll, row);
continue;
};
if ( objectDef.tile ) {
if ( objectDef.frame ) {
this._display.drawTile( coll, row, objectDef.tile, objectDef.frame, true );
} else {
this._display.drawTile( coll, row, objectDef.tile, 0, true );
}
}
else if( objectDef.color ) {
this._display.drawBlock( coll , row , 1, 1, objectDef.color);
}
}
}
for (key in this._dynamics) {
var curr = this._dynamics[key];
this._display.drawTile( curr.getX(), curr.getY(), curr.getTile(), curr.getFrame(), false );
this._display.drawTile( curr.getX(), curr.getY(), curr.getTile(), curr.getFrame(), true );
};
if (this._player) {this._display.drawTile( this._player.getX(), this._player.getY(), this._player.getTile(), this._player.getFrame(), true );};
}
Expand Down Expand Up @@ -164,6 +198,9 @@ Game.Map.prototype.getObjectOn = function(nx, ny) {
Game.Map.prototype.playerMoveTo = function(nx, ny) {
var x = Math.floor(nx);
var y = Math.floor(ny);
// Flag dirty block around player
this._dirty[this._player.getX()][this._player.getY()] = true;
this._dirty[x][y] = true;
// Check dynamics collisions first
for (key in this._dynamics) {
var curr = this._dynamics[key];
Expand Down Expand Up @@ -205,7 +242,9 @@ Game.Map.prototype.act = function() {
return;
};
for(key in this._dynamics) {
this._dirty[this._dynamics[key].getX()][this._dynamics[key].getY()] = true;
this._dynamics[key].act();
this._dirty[this._dynamics[key].getX()][this._dynamics[key].getY()] = true;
}
if (this._player.killed) {
this._game.lock();
Expand Down
2 changes: 2 additions & 0 deletions src/map_objects/food.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
Game.Map.Food = {
tile: 5,
frame: 1,
value: 10,
onCollision: function(me, player) {
me.value = this.value;
player.nom(me);
return true;
}
Expand Down
9 changes: 7 additions & 2 deletions src/map_objects/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Game.Player = function (x, y, tile, map, game) {
this.killed = false;
this._game = game;
this._map = map;
this._score = 0;
this.tile = tile;
this._dir = 0;
this._animation = 0;
Expand All @@ -29,10 +30,14 @@ Game.Player.prototype.killedBy = function(killer) {
};

Game.Player.prototype.nom = function ( food ) {
this._map.destroyObject(food.x, food.y);
this._map.eatFood();
this._score+=food.value;
this._map.eatFood(food.x, food.y);
};

Game.Player.prototype.getScore = function() {
return this._score;
}

Game.Player.prototype.getY = function() {
return this._y
};
Expand Down

0 comments on commit e120931

Please sign in to comment.