File size: 5,887 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 |
FIELD_OFFSET_X = 180;
FIELD_OFFSET_Y = 12;
function TetrisControl() {
var tetris = new Tetris(this);
this.setup = function () {
tetris.setup();
};
this.update = function () {
tetris.update();
};
this.draw = function () {
tetris.draw();
};
this.restart = function() {
// create a new Tetris object
tetris = new Tetris(this);
// emulate an initial setup condition and the first loop
tetris.setup();
tetris.update();
};
}
function Tetris(controller) {
var background = null,
game = null,
timeOffset = 0,
lastEscapeState = false,
startPauseTime = 0,
paused = false,
lastPaused = false,
gameOver = false,
mouseClick = null,
self = this,
continueButton = null,
restartButton = null,
lastTime = null,
dTime = null,
gameEndTty = new TtyBlock('gameEndDiv', 10, 20, 1);
this.setup = function () {
// find the keys to stop
var stoppedKeys = [],
curAction, i;
for (curAction in inputAssignments) {
stoppedKeys = stoppedKeys.concat(inputAssignments[curAction]);
}
jaws.preventDefaultKeys(stoppedKeys);
Tetris.currentInstance = self;
game = new Game(inputAssignments, autoRepeatConfig, thresholdConfig);
continueButton = new Button({image: 'media/buttons/continue.png', x: 250, y: 150});
restartButton = new Button({image: 'media/buttons/restart.png', x: 250, y: 200});
background = new Background();
timeOffset = (new Date()).getTime();
};
this.update = function() {
var realTime = (new Date()).getTime(),
escapePressed = jaws.pressed('esc'),
scoreObject;
if (lastTime === null) {
dTime = 0;
lastTime = realTime;
} else {
dTime = realTime - lastTime;
lastTime = realTime;
}
if (!paused && !gameOver) {
// see if the game should be pased
if (escapePressed && (!lastEscapeState)) {
// go into pause mode
startPauseTime = realTime;
paused = true;
} else {
game.update(realTime - timeOffset);
// see if the game is over
scoreObject = game.getResults();
if (scoreObject) {
gameOver = true;
// make the game end visible
document.getElementById('gameEndContainer').setAttribute('class', 'gameEndOutputVisible');
gameEndTty.addLine('GOOD GAME!!!');
gameEndTty.addLine('');
gameEndTty.addLine('');
if (scoreObject.won) {
gameEndTty.addLine('You Win!');
} else {
gameEndTty.addLine('Better Luck Next Time');
}
gameEndTty.addLine('');
gameEndTty.addLine('');
/*
gameEndTty.addLine('Re-directing you to');
gameEndTty.addLine('the score screen...');
*/
gameEndTty.addLine('Your score was:');
gameEndTty.addLine(scoreObject.score.toString());
gameEndTty.addLine('');
gameEndTty.addLine('');
//sendScoreRequest(scoreObject.score);
window.setTimeout(function() {
document.getElementById('gameEndContainer').setAttribute('class', 'gameEndOutputHidden');
controller.restart();
}, 6000);
}
}
} else if (paused) {
// see if the escape key was hit
if (escapePressed && (!lastEscapeState)) {
// change the time offset
timeOffset += realTime - startPauseTime;
paused = false;
}
// see if any buttons were pressed
if (mouseClick) {
if (continueButton.isClicked(mouseClick.x, mouseClick.y)) {
// change the time offset
timeOffset += realTime - startPauseTime;
paused = false;
}
if (restartButton.isClicked(mouseClick.x, mouseClick.y)) {
// restart the game
controller.restart();
return;
}
}
} else {
// TODO: nothing???
}
lastEscapeState = escapePressed;
mouseClick = null;
};
this.draw = function() {
if (!paused && !gameOver) {
// draw the game
background.draw(lastPaused);
if (lastPaused) {
lastPaused = false;
Block.invalidateAll();
}
game.draw(dTime);
Block.invalidFlushed();
} else if (paused) {
// draw the game
background.draw();
game.draw(dTime);
//draw the pause menu
continueButton.draw();
restartButton.draw();
lastPaused = true;
} else {
// continue to draw the game for game over
// draw the game
background.draw();
game.draw(dTime);
}
gameEndTty.draw(dTime);
};
this.mouseClicked = function(x, y) {
mouseClick = {x: x, y: y};
};
}
window.onload = function () {
loadGameControls();
jaws.assets.add('media/blueblock.png');
jaws.assets.add('media/cyanblock.png');
jaws.assets.add('media/greenblock.png');
jaws.assets.add('media/orangeblock.png');
jaws.assets.add('media/purpleblock.png');
jaws.assets.add('media/redblock.png');
jaws.assets.add('media/yellowblock.png');
jaws.assets.add('media/greyblock.png');
jaws.assets.add('media/emptyblock.png');
jaws.assets.add('media/buttons/continue.png');
jaws.assets.add('media/buttons/restart.png');
jaws.assets.add('media/background/backdrop.png');
jaws.assets.add('media/background/topbar.png');
jaws.start(TetrisControl);
};
var redirCode;
function redirectToScore() {
window.location.replace('/scoreScreen.html?tempRef=' + redirCode);
}
function sendScoreRequest(score) {
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
redirCode = xmlhttp.responseText;
setTimeout('redirectToScore();', 4000);
}
}
// World's 3rd most piss-poor obfustication technique
// A serious real-time/replay game monitor is needed
xmlhttp.open("POST", "/score/reportScore?gthbyu="+(score*17), true);
xmlhttp.send();
}
|