This commit is contained in:
Andrea Cavalli 2018-10-16 22:26:05 +02:00
parent cd6768d608
commit 76d9d77e13
3 changed files with 38 additions and 46 deletions

View File

@ -12,7 +12,6 @@ public class ButtonInfo {
public void press() { public void press() {
if (pressedCount <= releasedCount) { if (pressedCount <= releasedCount) {
System.out.println("press");
pressedCount = releasedCount + 1; pressedCount = releasedCount + 1;
unreadCount++; unreadCount++;
} }
@ -21,7 +20,6 @@ public class ButtonInfo {
public void release() { public void release() {
releasedCount++; releasedCount++;
pressedCount = releasedCount; pressedCount = releasedCount;
System.out.println("release" + releasedCount);
} }
public int readPressed() { public int readPressed() {

View File

@ -39,7 +39,8 @@ public class TetrisGame {
nextTetromino.fixInitialPosition(); nextTetromino.fixInitialPosition();
} }
public void update(float dt, ButtonInfo leftPressed, ButtonInfo rightPressed, ButtonInfo downPressed, ButtonInfo upPressed, ButtonInfo okPressed, ButtonInfo backPressed) { public void update(float dt, ButtonInfo leftPressed, ButtonInfo rightPressed, ButtonInfo downPressed,
ButtonInfo upPressed, ButtonInfo okPressed, ButtonInfo backPressed) {
currentTime += dt; currentTime += dt;
tickTimer += dt; tickTimer += dt;
leftTimer += dt; leftTimer += dt;
@ -59,35 +60,31 @@ public class TetrisGame {
} else { } else {
leftTimer = 0; leftTimer = 0;
} }
if (rightPressed.isPressedNow()) {
if (rightPressed.hasUnreadData()) { if (rightPressed.hasUnreadData()) {
for (int i = rightPressed.readPressed(); i > 0; i--) { for (int i = rightPressed.readPressed(); i > 0; i--) {
move(this.currentTetromino, 1, 0, 0); move(this.currentTetromino, 1, 0, 0);
} }
rightTimer = -MOVE_TIMER; rightTimer = -MOVE_TIMER;
} else { } else if (rightPressed.isPressedNow()) {
while (rightTimer >= MOVE_TIMER) { while (rightTimer >= MOVE_TIMER) {
rightTimer -= MOVE_TIMER; rightTimer -= MOVE_TIMER;
move(this.currentTetromino, 1, 0, 0); move(this.currentTetromino, 1, 0, 0);
} }
}
} else { } else {
rightTimer = 0; rightTimer = 0;
} }
if (upPressed.isPressedNow()) {
if (upPressed.hasUnreadData()) { if (upPressed.hasUnreadData()) {
for (int i = upPressed.readPressed(); i > 0; i--) { for (int i = upPressed.readPressed(); i > 0; i--) {
move(this.currentTetromino, 0, 0, 1); move(this.currentTetromino, 0, 0, 1);
} }
upTimer = -MOVE_TIMER; upTimer = -MOVE_TIMER;
} else { } else if (upPressed.isPressedNow()) {
while (upTimer >= MOVE_TIMER) { while (upTimer >= MOVE_TIMER) {
upTimer -= MOVE_TIMER; upTimer -= MOVE_TIMER;
move(this.currentTetromino, 0, 0, 1); move(this.currentTetromino, 0, 0, 1);
} }
}
} else { } else {
rightTimer = 0; upTimer = 0;
} }
if (downPressed.isPressedNow()) { if (downPressed.isPressedNow()) {
downPressed.readPressed(); downPressed.readPressed();
@ -110,7 +107,8 @@ public class TetrisGame {
renderGrid(); renderGrid();
} }
public void gameTick(ButtonInfo leftPressed, ButtonInfo rightPressed, ButtonInfo downPressed, ButtonInfo okPressed, ButtonInfo backPressed) { public void gameTick(ButtonInfo leftPressed, ButtonInfo rightPressed, ButtonInfo downPressed, ButtonInfo okPressed,
ButtonInfo backPressed) {
if (move(this.currentTetromino, 0, 1, 0)) { if (move(this.currentTetromino, 0, 1, 0)) {
} else { } else {
@ -127,7 +125,7 @@ public class TetrisGame {
} }
private void checkLines() { private void checkLines() {
for(int i = HEIGHT - 1; i >= 0; i--) { for (int i = HEIGHT - 1; i >= 0; i--) {
boolean scored = true; boolean scored = true;
while (scored) { while (scored) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < WIDTH; x++) {
@ -141,7 +139,7 @@ public class TetrisGame {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < WIDTH; x++) {
int y = i; int y = i;
while (y > 0) { while (y > 0) {
this.grid[x + (y) * WIDTH] = this.grid[x + (y-1) * WIDTH]; this.grid[x + (y) * WIDTH] = this.grid[x + (y - 1) * WIDTH];
y--; y--;
} }
} }
@ -154,9 +152,9 @@ public class TetrisGame {
byte rot = (byte) ((t.getRotation() + dRotation) % 4); byte rot = (byte) ((t.getRotation() + dRotation) % 4);
boolean[] block = t.getRenderedBlock(rot); boolean[] block = t.getRenderedBlock(rot);
int blockSize = t.getTetrominoGridSize(); int blockSize = t.getTetrominoGridSize();
int half1 = (int) Math.floor(((double)t.getTetrominoGridSize())/2d); int half1 = (int) Math.floor(((double) t.getTetrominoGridSize()) / 2d);
int half2 = blockSize - half1; int half2 = blockSize - half1;
byte aX = (byte)(t.getX()+dX), aY = (byte)(t.getY()+dY); byte aX = (byte) (t.getX() + dX), aY = (byte) (t.getY() + dY);
int blockX = 0, blockY = 0; int blockX = 0, blockY = 0;
for (int x = aX - half1; x < aX + half2; x++) { for (int x = aX - half1; x < aX + half2; x++) {
for (int y = aY - half1; y < aY + half2; y++) { for (int y = aY - half1; y < aY + half2; y++) {
@ -185,7 +183,7 @@ public class TetrisGame {
drawCurrentTetromino(this.renderedGrid); drawCurrentTetromino(this.renderedGrid);
for (int y = 0; y < HEIGHT; y++) { for (int y = 0; y < HEIGHT; y++) {
for (int x = 0; x < WIDTH; x++) { for (int x = 0; x < WIDTH; x++) {
final int offset = x+y*WIDTH; final int offset = x + y * WIDTH;
renderedGrid[offset] = hovergrid[offset] != null ? hovergrid[offset] : renderedGrid[offset]; renderedGrid[offset] = hovergrid[offset] != null ? hovergrid[offset] : renderedGrid[offset];
} }
} }
@ -199,7 +197,7 @@ public class TetrisGame {
private Tetromino generateRandomTetromino() { private Tetromino generateRandomTetromino() {
int s = (int) (Math.random() * 7); int s = (int) (Math.random() * 7);
final byte middleX = (byte)((WIDTH - 1)/2), middleY = 0, rotation = (byte) (Math.random() * 4); final byte middleX = (byte) ((WIDTH - 1) / 2), middleY = 0, rotation = (byte) (Math.random() * 4);
switch (s) { switch (s) {
case 0: case 0:
return new TetrominoICyan(middleX, middleY, rotation); return new TetrominoICyan(middleX, middleY, rotation);

View File

@ -124,16 +124,12 @@ public class HtmlEngine implements GraphicEngine {
evt.preventDefault(); evt.preventDefault();
new Thread(() -> { new Thread(() -> {
Keyboard.debugKey(keyNames .getOrDefault(evt.getKey(), evt.getKeyCode()), false); Keyboard.debugKey(keyNames .getOrDefault(evt.getKey(), evt.getKeyCode()), false);
System.out.println(evt.getKeyCode());
System.out.println("" + (int) evt.getKey().charAt(0));
}).start(); }).start();
}); });
HtmlEngine.document.addEventListener("keyup", (final KeyboardEvent evt) -> { HtmlEngine.document.addEventListener("keyup", (final KeyboardEvent evt) -> {
evt.preventDefault(); evt.preventDefault();
new Thread(() -> { new Thread(() -> {
Keyboard.debugKey(keyNames .getOrDefault(evt.getKey(), evt.getKeyCode()), true); Keyboard.debugKey(keyNames .getOrDefault(evt.getKey(), evt.getKeyCode()), true);
System.out.println(evt.getKeyCode());
System.out.println("" + (int) evt.getKey().charAt(0));
}).start(); }).start();
}); });
keyInput.addEventListener("input", (final Event evt) -> { keyInput.addEventListener("input", (final Event evt) -> {