File size: 3,204 Bytes
1e40c2a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
// TODO: constants file???
var BLOCK_WIDTH = 24;
function Block(config) {
var parent, key;
config = config || {};
this.boX = (config.boardOriginX || 0) + FIELD_OFFSET_X;
this.boY = (config.boardOriginY || 0) + FIELD_OFFSET_Y;
this.blockX = config.blockX;
this.blockY = config.blockY;
this.occupiedPositions = config.occupiedPositions;
this.addOccupied(this.blockX, this.blockY);
Block.invalidSpaces[this.blockX + "," + this.blockY] = true;
config.x = this.boX + BLOCK_WIDTH * this.blockX;
config.y = this.boY + BLOCK_WIDTH * this.blockY;
if (config.preview) {
config.image = 'media/greyblock.png';
} else if (config.empty) {
config.image = 'media/emptyblock.png';
}else {
config.image = SHAPES[config.shape].image;
}
parent = new jaws.Sprite(config);
for (key in parent) {
this[key] = parent[key];
}
}
Block.invalidSpaces = {};
Block.allInvalidated = false;
Block.invalidFlushed = function() {
Block.invalidSpaces = {};
Block.allInvalidated = false;
};
Block.invalidateAll = function() {
Block.allInvalidated = true;
};
Block.prototype.setColor = function(shape, preview) {
if (preview) {
this.setImage('media/greyblock.png');
} else {
this.setImage(SHAPES[shape].image);
}
Block.invalidSpaces[this.blockX + "," + this.blockY] = true;
};
Block.prototype.moveBlock = function(dx, dy) {
Block.invalidSpaces[this.blockX + "," + this.blockY] = true;
this.removeOccupied(this.blockX, this.blockY);
this.blockX += dx;
this.blockY += dy;
Block.invalidSpaces[this.blockX + "," + this.blockY] = true;
this.addOccupied(this.blockX, this.blockY);
this.x += dx * BLOCK_WIDTH;
this.y += dy * BLOCK_WIDTH;
};
Block.prototype.setPosition = function(blockX, blockY) {
Block.invalidSpaces[this.blockX + "," + this.blockY] = true;
this.removeOccupied(this.blockX, this.blockY);
this.blockX = blockX;
this.blockY = blockY;
Block.invalidSpaces[this.blockX + "," + this.blockY] = true;
this.addOccupied(this.blockX, this.blockY);
this.x = this.boX + blockX * BLOCK_WIDTH;
this.y = this.boY + blockY * BLOCK_WIDTH;
};
Block.prototype.getX = function() { return this.blockX; };
Block.prototype.getY = function() { return this.blockY; };
Block.prototype.isPosition = function(x, y) {
return this.blockX === x && this.blockY === y;
};
Block.prototype.drawIfInvalid = function() {
if (Block.invalidSpaces[this.blockX + "," + this.blockY] || Block.allInvalidated || this.blockY < 0) {
this.draw();
}
};
Block.prototype.kill = function() {
Block.invalidSpaces[this.blockX + "," + this.blockY] = true;
this.removeOccupied(this.blockX, this.blockY);
};
Block.prototype.removeOccupied = function(x, y) {
var posString = x + ',' + y;
if (this.occupiedPositions && this.occupiedPositions[posString]) {
this.occupiedPositions[posString] -= 1;
}
};
Block.prototype.addOccupied = function(x, y) {
var posString = x + ',' + y;
if (this.occupiedPositions) {
if (this.occupiedPositions[posString] === undefined) {
this.occupiedPositions[posString] = 0;
}
this.occupiedPositions[posString] += 1;
}
};
|