Skip to content

Commit 42f45c0

Browse files
committed
DAS preload during count down.
1 parent ca3b41c commit 42f45c0

File tree

1 file changed

+113
-94
lines changed

1 file changed

+113
-94
lines changed

tetris.js

Lines changed: 113 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -621,16 +621,37 @@ var fallingPiece = new (function() {
621621
landed = false;
622622
}
623623
this.newPiece = function() {
624-
// TODO Do this better.
625-
//for property in pieces, this.prop = piece.prop
626-
this.tetro = pieces[grabBag[inc]].tetro;
627-
this.kickData = pieces[grabBag[inc]].kickData;
628-
this.x = pieces[grabBag[inc]].x;
629-
this.y = pieces[grabBag[inc]].y;
630-
this.index = pieces[grabBag[inc]].index;
631-
632-
this.active = true;
633-
newPiece = true;
624+
if (!this.active) {
625+
626+
// TODO Do this better.
627+
//for property in pieces, this.prop = piece.prop
628+
this.tetro = pieces[grabBag[inc]].tetro;
629+
this.kickData = pieces[grabBag[inc]].kickData;
630+
this.x = pieces[grabBag[inc]].x;
631+
this.y = pieces[grabBag[inc]].y;
632+
this.index = pieces[grabBag[inc]].index;
633+
634+
this.active = true;
635+
newPiece = true;
636+
637+
// Determine if we need another grab bag.
638+
//TODO Do this better. (make grabbag object)
639+
if (inc < 6) {
640+
inc++;
641+
} else {
642+
grabBag = grabBag.slice(-7);
643+
grabBag.push.apply(grabBag, randomGenerator());
644+
inc = 0;
645+
}
646+
drawPreview();
647+
648+
// Check for blockout.
649+
if (!moveValid(0, 0, this.tetro)) {
650+
gameState = 9;
651+
msg.innerHTML = 'BLOCK OUT!';
652+
menu(3);
653+
}
654+
}
634655
}
635656
this.rotate = function(direction) {
636657

@@ -672,8 +693,69 @@ var fallingPiece = new (function() {
672693
}
673694
}
674695
}
696+
this.checkShift = function() {
697+
if (keysDown & flags.moveLeft && !(lastKeys & flags.moveLeft)) {
698+
this.shiftDelay = 0;
699+
this.arrDelay = 0;
700+
shiftReleased = true;
701+
shift = -1;
702+
} else if (keysDown & flags.moveRight && !(lastKeys & flags.moveRight)) {
703+
this.shiftDelay = 0;
704+
this.arrDelay = 0;
705+
shiftReleased = true;
706+
shift = 1;
707+
}
708+
//shift released
709+
if (shift === 1 &&
710+
!(keysDown & flags.moveRight) && lastKeys & flags.moveRight && keysDown & flags.moveLeft) {
711+
this.shiftDelay = 0;
712+
this.arrDelay = 0;
713+
shiftReleased = true;
714+
shift = -1;
715+
} else if (shift === -1 &&
716+
!(keysDown & flags.moveLeft) && lastKeys & flags.moveLeft && keysDown & flags.moveRight) {
717+
this.shiftDelay = 0;
718+
this.arrDelay = 0;
719+
shiftReleased = true;
720+
shift = 1;
721+
} else if (
722+
!(keysDown & flags.moveRight) && lastKeys & flags.moveRight && keysDown & flags.moveLeft) {
723+
shift = -1;
724+
} else if (
725+
!(keysDown & flags.moveLeft) && lastKeys & flags.moveLeft && keysDown & flags.moveRight) {
726+
shift = 1;
727+
} else if ((!(keysDown & flags.moveLeft) && lastKeys & flags.moveLeft) ||
728+
(!(keysDown & flags.moveRight) && lastKeys & flags.moveRight)) {
729+
this.shiftDelay = 0;
730+
this.arrDelay = 0;
731+
shiftReleased = true;
732+
shift = 0;
733+
}
734+
if (shift) {
735+
// 1. When key pressed instantly move over once.
736+
if (shiftReleased) {
737+
this.shift(shift);
738+
this.shiftDelay++;
739+
shiftReleased = false;
740+
// 2. Apply DAS delay
741+
} else if (this.shiftDelay < settings.DAS) {
742+
this.shiftDelay++;
743+
// 3. Once the delay is complete, move over once.
744+
// Inc delay so this doesn't run again.
745+
} else if (this.shiftDelay === settings.DAS && settings.DAS !== 0) {
746+
this.shift(shift);
747+
if (settings.ARR !== 0) this.shiftDelay++;
748+
// 4. Apply ARR delay
749+
} else if (this.arrDelay < settings.ARR) {
750+
this.arrDelay++;
751+
// 5. If ARR Delay is full, move piece, and reset delay and repeat.
752+
} else if (this.arrDelay === settings.ARR && settings.ARR !== 0) {
753+
this.shift(shift);
754+
}
755+
}
756+
}
675757
this.shift = function(direction) {
676-
fallingPiece.arrDelay = 0;
758+
this.arrDelay = 0;
677759
if (settings.ARR === 0 && this.shiftDelay === settings.DAS) {
678760
for (var i = 1; i < 10; i++) {
679761
if (!moveValid(i * direction, 0, this.tetro)) {
@@ -1125,28 +1207,7 @@ function update() {
11251207
fallingPiece.hold();
11261208
}
11271209

1128-
if (!fallingPiece.active) {
1129-
1130-
fallingPiece.newPiece();
1131-
1132-
// Determine if we need another grab bag.
1133-
//TODO Do this better. (make grabbag object)
1134-
if (inc < 6) {
1135-
inc++;
1136-
} else {
1137-
grabBag = grabBag.slice(-7);
1138-
grabBag.push.apply(grabBag, randomGenerator());
1139-
inc = 0;
1140-
}
1141-
drawPreview();
1142-
1143-
// Check for blockout.
1144-
if (!moveValid(0, 0, fallingPiece.tetro)) {
1145-
gameState = 9;
1146-
msg.innerHTML = 'BLOCK OUT!';
1147-
menu(3);
1148-
}
1149-
}
1210+
fallingPiece.newPiece();
11501211

11511212
if (flags.rotLeft & keysDown && !(lastKeys & flags.rotLeft)) {
11521213
fallingPiece.rotate(-1);
@@ -1157,67 +1218,7 @@ function update() {
11571218
fallingPiece.rotate(1);
11581219
}
11591220

1160-
// shift pressed
1161-
if (keysDown & flags.moveLeft && !(lastKeys & flags.moveLeft)) {
1162-
fallingPiece.shiftDelay = 0;
1163-
fallingPiece.arrDelay = 0;
1164-
shiftReleased = true;
1165-
shift = -1;
1166-
} else if (keysDown & flags.moveRight && !(lastKeys & flags.moveRight)) {
1167-
fallingPiece.shiftDelay = 0;
1168-
fallingPiece.arrDelay = 0;
1169-
shiftReleased = true;
1170-
shift = 1;
1171-
}
1172-
//shift released
1173-
if (shift === 1 &&
1174-
!(keysDown & flags.moveRight) && lastKeys & flags.moveRight && keysDown & flags.moveLeft) {
1175-
fallingPiece.shiftDelay = 0;
1176-
fallingPiece.arrDelay = 0;
1177-
shiftReleased = true;
1178-
shift = -1;
1179-
} else if (shift === -1 &&
1180-
!(keysDown & flags.moveLeft) && lastKeys & flags.moveLeft && keysDown & flags.moveRight) {
1181-
fallingPiece.shiftDelay = 0;
1182-
fallingPiece.arrDelay = 0;
1183-
shiftReleased = true;
1184-
shift = 1;
1185-
} else if (
1186-
!(keysDown & flags.moveRight) && lastKeys & flags.moveRight && keysDown & flags.moveLeft) {
1187-
shift = -1;
1188-
} else if (
1189-
!(keysDown & flags.moveLeft) && lastKeys & flags.moveLeft && keysDown & flags.moveRight) {
1190-
shift = 1;
1191-
} else if ((!(keysDown & flags.moveLeft) && lastKeys & flags.moveLeft) ||
1192-
(!(keysDown & flags.moveRight) && lastKeys & flags.moveRight)) {
1193-
fallingPiece.shiftDelay = 0;
1194-
fallingPiece.arrDelay = 0;
1195-
shiftReleased = true;
1196-
shift = 0;
1197-
}
1198-
1199-
if (shift) {
1200-
// 1. When key pressed instantly move over once.
1201-
if (shiftReleased) {
1202-
fallingPiece.shift(shift);
1203-
fallingPiece.shiftDelay++;
1204-
shiftReleased = false;
1205-
// 2. Apply DAS delay
1206-
} else if (fallingPiece.shiftDelay < settings.DAS) {
1207-
fallingPiece.shiftDelay++;
1208-
// 3. Once the delay is complete, move over once.
1209-
// Inc delay so this doesn't run again.
1210-
} else if (fallingPiece.shiftDelay === settings.DAS && settings.DAS !== 0) {
1211-
fallingPiece.shift(shift);
1212-
if (settings.ARR !== 0) fallingPiece.shiftDelay++;
1213-
// 4. Apply ARR delay
1214-
} else if (fallingPiece.arrDelay < settings.ARR) {
1215-
fallingPiece.arrDelay++;
1216-
// 5. If ARR Delay is full, move piece, and reset delay and repeat.
1217-
} else if (fallingPiece.arrDelay === settings.ARR && settings.ARR !== 0) {
1218-
fallingPiece.shift(shift);
1219-
}
1220-
}
1221+
fallingPiece.checkShift();
12211222

12221223
if (flags.moveDown & keysDown) {
12231224
fallingPiece.shiftDown();
@@ -1275,6 +1276,7 @@ function gameLoop() {
12751276
lastPos = fallingPiece.pos;
12761277
newPiece = false;
12771278
} else if (gameState === 2) {
1279+
// Count Down
12781280
if (frame < 50) {
12791281
if (msg.innerHTML !== 'READY') msg.innerHTML = 'READY';
12801282
} else if (frame < 100) {
@@ -1284,6 +1286,23 @@ function gameLoop() {
12841286
gameState = 0;
12851287
startTime = Date.now();
12861288
}
1289+
// DAS Preload TODO
1290+
if (lastKeys !== keysDown && !watchingReplay) {
1291+
replayKeys[frame] = keysDown;
1292+
} else if (frame in replayKeys) {
1293+
keysDown = replayKeys[frame];
1294+
}
1295+
if (keysDown & flags.moveLeft) {
1296+
lastKeys = keysDown;
1297+
fallingPiece.shiftDelay = settings.DAS;
1298+
shiftReleased = false;
1299+
shift = -1;
1300+
} else if (keysDown & flags.moveRight) {
1301+
lastKeys = keysDown;
1302+
fallingPiece.shiftDelay = settings.DAS;
1303+
shiftReleased = false;
1304+
shift = 1;
1305+
}
12871306
} else if (toGreyRow >= 2){
12881307
/**
12891308
* Fade to grey animation played when player loses.

0 commit comments

Comments
 (0)