Skip to content

Commit 4104944

Browse files
authored
Merge pull request #16 from ByteHamster/fixes
Small fixes
2 parents 90f1fcb + 88253b7 commit 4104944

5 files changed

Lines changed: 30 additions & 21 deletions

File tree

app/src/main/java/com/bytehamster/flowitgame/Main.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,11 @@ private void switchState() {
113113
protected void onResume() {
114114
super.onResume();
115115

116-
new Handler().postDelayed(() -> {
116+
glSurfaceView.postDelayed(() -> {
117117
glSurfaceView.getRenderer().onResume();
118118
glSurfaceView.onResume();
119119
glSurfaceView.invalidate();
120-
}, 200);
120+
}, 500);
121121
}
122122

123123
@Override

app/src/main/java/com/bytehamster/flowitgame/filler/BombFiller.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,19 @@ public void run() {
2626
fillTo = Converter.convertColor(levelData.fieldAt(col, row).getColor());
2727

2828
try {
29-
Thread.sleep(100);
29+
sleep(100);
3030
state.playSound(R.raw.fill);
3131

3232
doFill(col, row);
3333

34-
Thread.sleep(100);
34+
sleep(100);
3535

3636
doFill(col + 1, row);
3737
doFill(col, row + 1);
3838
doFill(col - 1, row);
3939
doFill(col, row - 1);
4040

41-
Thread.sleep(100);
41+
sleep(100);
4242

4343
doFill(col + 1, row - 1);
4444
doFill(col + 1, row + 1);

app/src/main/java/com/bytehamster/flowitgame/filler/DirectionFiller.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,7 @@ private void doFill(int col, int row) {
5656
somethingWasFilled = true;
5757
f.setModifier(fillTo);
5858
state.playSound(R.raw.fill);
59-
60-
try {
61-
Thread.sleep(40);
62-
} catch (InterruptedException e) {
63-
e.printStackTrace();
64-
}
59+
sleep(40);
6560

6661
x += dx;
6762
y += dy;

app/src/main/java/com/bytehamster/flowitgame/filler/Filler.java

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public abstract class Filler {
77
private Runnable onFinished = null;
8+
private long lastAction = 0;
89

910
public abstract void fill();
1011

@@ -18,27 +19,40 @@ public static Filler get(Level levelData, int col, int row, State state) {
1819
return new FloodFiller(levelData, col, row, state);
1920
case BOMB:
2021
return new BombFiller(levelData, col, row, state);
21-
case UP:
22-
return new DirectionFiller(levelData, col, row, 0, -1, state);
23-
case RIGHT:
24-
return new DirectionFiller(levelData, col, row, 1, 0, state);
25-
case LEFT:
26-
return new DirectionFiller(levelData, col, row, -1, 0, state);
27-
case DOWN:
28-
return new DirectionFiller(levelData, col, row, 0, 1, state);
22+
case UP: // Fall-through
2923
case ROTATE_UP:
3024
return new DirectionFiller(levelData, col, row, 0, -1, state);
25+
case RIGHT: // Fall-through
3126
case ROTATE_RIGHT:
3227
return new DirectionFiller(levelData, col, row, 1, 0, state);
28+
case LEFT: // Fall-through
3329
case ROTATE_LEFT:
3430
return new DirectionFiller(levelData, col, row, -1, 0, state);
31+
case DOWN: // Fall-through
3532
case ROTATE_DOWN:
3633
return new DirectionFiller(levelData, col, row, 0, 1, state);
3734
default:
3835
return null;
3936
}
4037
}
4138

39+
protected void sleep(long milliseconds) {
40+
long timePassed = System.currentTimeMillis() - lastAction;
41+
long timeLeft = milliseconds - timePassed;
42+
if (lastAction == 0) {
43+
timeLeft = milliseconds;
44+
}
45+
lastAction = System.currentTimeMillis();
46+
if (timeLeft <= 0) {
47+
return;
48+
}
49+
try {
50+
Thread.sleep(timeLeft);
51+
} catch (InterruptedException e) {
52+
e.printStackTrace();
53+
}
54+
}
55+
4256
public void setOnFinished(Runnable onFinished) {
4357
this.onFinished = onFinished;
4458
}

app/src/main/java/com/bytehamster/flowitgame/filler/FloodFiller.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ private void floodBFS(final int col, final int row) throws InterruptedException
7575
if (!neighbor.getField().isVisited()) {
7676
neighbor.getField().setVisited(true);
7777

78-
if(neighbor.getField().getModifier() == fillFrom) {
78+
if (neighbor.getField().getModifier() == fillFrom) {
7979
if (lastDistance != neighbor.distance) {
8080
lastDistance = neighbor.distance;
81-
Thread.sleep(60);
81+
sleep(60);
8282
state.playSound(R.raw.fill);
8383
}
8484
neighbor.getField().setModifier(fillTo);

0 commit comments

Comments
 (0)