Skip to content

Commit 48effad

Browse files
committed
Improve Chunk
1 parent 0737a70 commit 48effad

File tree

3 files changed

+22
-40
lines changed

3 files changed

+22
-40
lines changed

sg2-core-jfx/src/cz/hartrik/sg2/engine/render/Highlighter.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public void highlightActiveChunks() {
3333
}
3434

3535
public void highlightChunk(Chunk chunk) {
36-
for (int x = chunk.getBoundMinX(); x <= chunk.getBoundMaxX(); x++) {
37-
highlight(x, chunk.getBoundMinY());
38-
highlight(x, chunk.getBoundMaxY());
36+
for (int x = chunk.getTopLeftX(); x <= chunk.getBottomRightX(); x++) {
37+
highlight(x, chunk.getTopLeftY());
38+
highlight(x, chunk.getBottomRightY());
3939
}
4040

41-
for (int y = chunk.getBoundMinY() + 1; y < chunk.getBoundMaxY(); y++) {
42-
highlight(chunk.getBoundMaxX(), y);
43-
highlight(chunk.getBoundMinX(), y);
41+
for (int y = chunk.getTopLeftY() + 1; y < chunk.getBottomRightY(); y++) {
42+
highlight(chunk.getBottomRightX(), y);
43+
highlight(chunk.getTopLeftX(), y);
4444
}
4545
}
4646

sg2-core/src/cz/hartrik/sg2/world/Chunk.java

+15-33
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@
22
package cz.hartrik.sg2.world;
33

44
import java.io.Serializable;
5-
import java.util.function.Consumer;
65

76
/**
87
* Spravuje čtvercovou oblast elementů uvnitř {@link ElementArea}.
98
* Jeho hlavním úkolem je sdělovat, zda byl alespoň jeden z elementů uvnitř
109
* chunku během cyklu změněn.
1110
*
12-
* @version 2016-06-21
11+
* @version 2017-08-12
1312
* @author Patrik Harag
1413
*/
1514
public class Chunk implements Serializable {
@@ -21,23 +20,20 @@ public class Chunk implements Serializable {
2120
private final int x; // horizontální souřadnice oblasti
2221
private final int y; // vertikální souřadnice oblasti
2322

24-
private final ElementArea elementArea;
25-
26-
private final int startX, startY;
27-
private final int endX, endY;
23+
private final int topLeftX, topLeftY;
24+
private final int bottomRightX, bottomRightY;
2825

2926
private volatile boolean changed = true;
3027

31-
public Chunk(ElementArea elementArea, int size, int x, int y) {
32-
this.elementArea = elementArea;
28+
public Chunk(int size, int x, int y) {
3329
this.size = size;
3430
this.x = x;
3531
this.y = y;
3632

37-
this.startX = x * size;
38-
this.startY = y * size;
39-
this.endX = startX + size - 1;
40-
this.endY = startY + size - 1;
33+
this.topLeftX = x * size;
34+
this.topLeftY = y * size;
35+
this.bottomRightX = topLeftX + size - 1;
36+
this.bottomRightY = topLeftY + size - 1;
4137
}
4238

4339
public int getSize() {
@@ -48,30 +44,16 @@ public int getSize() {
4844
public final void change(boolean b) { changed = b; }
4945
public final boolean isChanged() { return changed; }
5046

51-
public final int getX() { return x; }
52-
public final int getY() { return y; }
47+
public final int getChunkX() { return x; }
48+
public final int getChunkY() { return y; }
5349

54-
public final int getBoundMaxX() { return endX; }
55-
public final int getBoundMinX() { return startX; }
56-
public final int getBoundMaxY() { return endY; }
57-
public final int getBoundMinY() { return startY; }
50+
public final int getTopLeftX() { return topLeftX; }
51+
public final int getTopLeftY() { return topLeftY; }
52+
public final int getBottomRightX() { return bottomRightX; }
53+
public final int getBottomRightY() { return bottomRightY; }
5854

5955
public final boolean contains(int x, int y) {
60-
return x >= startX && x <= endX && y >= startY && y <= endY;
61-
}
62-
63-
// iterátory
64-
65-
public void forEach(PointConsumer iterator) {
66-
for (int nY = startY; nY < endY; nY++)
67-
for (int nX = startX; nX < endX; nX++)
68-
iterator.accept(nX, nY);
69-
}
70-
71-
public void forEach(Consumer<Element> consumer) {
72-
for (int nY = startY; nY < endY; nY++)
73-
for (int nX = startX; nX < endX; nX++)
74-
consumer.accept(elementArea.get(x, y));
56+
return x >= topLeftX && x <= bottomRightX && y >= topLeftY && y <= bottomRightY;
7557
}
7658

7759
}

sg2-core/src/cz/hartrik/sg2/world/ChunkedArea.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public ChunkedArea(ChunkedArea elementArea) {
5252
private void initChunks() {
5353
for (int y = 0; y < verChunkCount; y++)
5454
for (int x = 0; x < horChunkCount; x++)
55-
chunks[x + (y * horChunkCount)] = new Chunk(this, chunkSize, x, y);
55+
chunks[x + (y * horChunkCount)] = new Chunk(chunkSize, x, y);
5656
}
5757

5858
// základní metody...

0 commit comments

Comments
 (0)