File size: 7,285 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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
function Game(inputMapping, autoRepeat, threshold) {
var thisObject = this,
i;
this.firstLoop = true;
this.blocks = [];
this.controlGroup = null;
// make the preview blocks
this.previewBlocks = [];
for (i = 0; i < 4; i += 1) {
this.previewBlocks.push(new Block({blockX: -10, blockY: -10, preview: true}));
}
this.scoreOutput = new TtyBlock("scoreDiv", 3);
this.linesOutput = new TtyBlock("linesDiv", 3);
this.levelOutput = new TtyBlock("levelDiv", 3);
this.tickerOutput = new TtyBlock("tickerDiv", 5);
this.scoreTracker = new ScoreTracker(this.scoreOutput, this.linesOutput, this.levelOutput, this.tickerOutput);
this.dropPeriod = this.scoreTracker.getLevelPeriod();
this.timeToNextDrop = this.dropPeriod;
// TODO: find the official values for these constants
this.keyChargeTime = threshold;
this.keyRepeatTime = autoRepeat;
this.bottomTimer = null;
this.bottomLockTime = 500;
this.lastBottomedState = false;
this.lastTime = null;
this.gameLost = false;
// evenly distributed random piece generator
this.previewLength = 5;
this.randBag = new RandomBag(this.previewLength);
// make the preview blocks
this.previewGroups = [];
for (i = 0; i < this.previewLength; i += 1) {
this.previewGroups.push(new PreviewGroup(330, 70 * i + 35));
}
this.swapGroup = null;
this.swapAllowed = true;
// the currently occupied positions, number of blocks at a position
// indexed by the position as a string
this.occupiedPositions = {};
this.input = {
shiftLeft: {
autoRepeat: true,
handler: function () {
if (thisObject.controlGroup.shift(true)) {
thisObject.resetLockCounter(true);
}
}
},
shiftRight: {
autoRepeat: true,
handler: function() {
if (thisObject.controlGroup.shift(false)) {
thisObject.resetLockCounter(true);
}
}
},
softDrop: {
autoRepeat: true,
preCharged: true,
handler: function() {
thisObject.dropBlock();
thisObject.scoreTracker.softDrop();
}
},
hardDrop: { handler: function() {
var dist = thisObject.controlGroup.fall();
thisObject.scoreTracker.hardDrop(dist);
thisObject.lockBlocks();
}},
rotateLeft: { handler: function() {
if (thisObject.controlGroup.turn(false)) {
thisObject.resetLockCounter(true);
}
}},
rotateRight: { handler: function() {
if (thisObject.controlGroup.turn(true)) {
thisObject.resetLockCounter(true);
}
}},
swap: { handler: function() {
thisObject.swap();
}}
};
this.inputMapping = inputMapping;
}
/**
* drops a new block into the game
*/
Game.prototype.newBlock = function (calledBySwap) {
var thisObject = this,
shape = this.randBag.popQueue(),
newBlocks = [],
curBlock,
i;
this.dropPeriod = this.scoreTracker.getLevelPeriod();
// create some new blocks
for (i = 0; i < 4; i += 1) {
curBlock = new Block({blockX: -10, blockY: -10, shape: shape, occupiedPositions: this.occupiedPositions});
newBlocks.push(curBlock);
this.blocks.push(curBlock);
}
this.controlGroup = new ControlGroup(newBlocks, shape, function(x, y){
return thisObject.isLegalPosition(x, y);
});
if (this.controlGroup.isIllegalStart) {
this.gameLost = true;
}
if (!calledBySwap) {
// the user is allowed to swap blocks again
this.swapAllowed = true;
}
this.updatePreviews(this.randBag.getQueue());
};
/**
* processes the input keys
* @param {Number} dTime - the time in milliseconds since the last frame
*/
Game.prototype.processInput = function(dTime) {
var curInput,
keyName,
curKeys,
pressed,
curInput,
i;
for (actionType in this.inputMapping) {
curKeys = this.inputMapping[actionType];
curInput = this.input[actionType];
pressed = false;
for (i = 0; i < curKeys.length; i += 1) {
if (jaws.pressed(curKeys[i])) {
pressed = true;
}
}
// if the key is down
if (pressed) {
// if it is a 'press' frame
if (!curInput.lastState) {
curInput.handler();
curInput.lastState = true;
curInput.charged = (curInput.preCharged ? true : false);
curInput.holdTime = 0;
}
// if it supports auto-repeat
if (curInput.autoRepeat) {
curInput.holdTime += dTime;
// if not charged and past the charge time
if ((!curInput.charged) && (curInput.holdTime > this.keyChargeTime)) {
// call the handler, and reset the hold time
curInput.holdTime -= this.keyChargeTime;
curInput.handler();
curInput.charged = true;
}
// if charged and past the repeat time
if (curInput.charged && (curInput.holdTime > this.keyRepeatTime)) {
curInput.holdTime -= this.keyRepeatTime;
curInput.handler();
}
}
} else {
// it was released
curInput.lastState = false;
}
}
};
Game.prototype.update = function(time) {
var curTime,
dTime,
i;
// if the first block needs to be made
if (this.firstLoop) {
this.firstLoop = false;
this.newBlock();
this.lastTime = time;
}
curTime = time;
dTime = curTime - this.lastTime;
this.lastTime = curTime;
this.processInput(dTime);
if (!this.controlGroup.isBottomed()) {
this.lastBottomedState = false;
this.applyGravity(dTime);
} else {
// if it has just touched hte bottom
if (!this.lastBottomedState) {
this.resetLockCounter(false);
} else {
this.bottomTimer -= dTime;
if (this.bottomTimer <= 0 || this.slideCount >= 15) {
this.lockBlocks();
}
}
this.lastBottomedState = true;
}
// update the position of the preview blocks
if (this.controlGroup) {
// ask the control group to move the preview blocks
this.controlGroup.configurePreviewBlocks(this.previewBlocks);
} else {
// if there is no contorl group, just move them off the screen
for (i = 0; i < 4; i += 1) {
this.previewBlocks[i].setPosition(-10, -10);
}
}
};
/**
* Renders the entire game scene
*/
Game.prototype.draw = function(dTime) {
var i;
this.scoreOutput.draw(dTime);
this.linesOutput.draw(dTime);
this.levelOutput.draw(dTime);
this.tickerOutput.draw(dTime);
// draw the preview blocks
for (i = 0; i < 4; i += 1) {
this.previewBlocks[i].drawIfInvalid();
}
// draw the swap block
if (this.swapGroup) {
this.swapGroup.draw();
}
// draw the queue
for (i = 0; i < this.previewGroups.length; i += 1) {
this.previewGroups[i].draw();
}
for (i = 0; i < this.blocks.length; i += 1) {
this.blocks[i].drawIfInvalid();
}
};
/**
* Returns true iff the given position can be moved into
* @param {Number} x - the x position
* @param {Number} y - the y position
* @returns {Boolean} true iff the new position is legal
*/
Game.prototype.isLegalPosition = function (x, y) {
// if there is a block in the way
if (this.occupiedPositions[x+','+y]) {
return false;
}
// if it's on the field
if (x >= 10 || x < 0 || y >= 20) {
return false;
}
return true;
};
/**
* drops the controlled blocks by one
*/
Game.prototype.dropBlock = function (causedByGravity) {
if (!causedByGravity) {
this.timeToNextDrop = this.dropPeriod;
}
this.controlGroup.drop();
};
|