1f0302c93e578dc7ab830446c42f2221f5a428a9
1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.utils
.TimeUtils
;
5 import java
.util
.Arrays
;
7 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
8 import static ru
.deadsoftware
.cavedroid
.game
.GameItems
.*;
10 class GameFluidsThread
extends Thread
{
12 private static final int FLUID_UPDATE_INTERVAL_MS
= 100;
13 private static final int FLUID_STATES
= 5;
15 private static final int[] WATER_IDS
= {8, 60, 61, 62, 63};
16 private static final int[] LAVA_IDS
= {9, 64, 65, 66, 67};
18 private long fluidLastUpdateTimestamp
= 0;
20 private int getBlockState(int id
) {
21 return isWater(id
) ? Arrays
.binarySearch(WATER_IDS
, id
) : Arrays
.binarySearch(LAVA_IDS
, id
);
24 private int getNextBlockState(int id
) {
28 int state
= getBlockState(id
);
29 if (state
< FLUID_STATES
- 1) {
35 private int getNextBlockStateId(int id
) {
36 int nextState
= getNextBlockState(id
);
37 if (nextState
== -1) return 0;
39 return WATER_IDS
[nextState
];
41 return LAVA_IDS
[nextState
];
44 private int id(int x
, int y
) {
45 return GP
.world
.getForeMap(x
, y
);
48 private boolean sameFluid(int thisId
, int thatId
) {
49 return isFluid(thatId
) && isWater(thatId
) == isWater(thisId
);
52 private boolean noFluidNearby(int x
, int y
) {
53 return !isFluid(id(x
, y
- 1)) &&
54 (!isFluid(id(x
- 1, y
)) || id(x
- 1, y
) >= id(x
, y
)) &&
55 (!isFluid(id(x
+ 1, y
)) || id(x
+ 1, y
) >= id(x
, y
));
58 private boolean drainFluid(int x
, int y
) {
59 if (getBlockState(id(x
, y
)) > 0) {
60 if (noFluidNearby(x
, y
)) {
61 GP
.world
.setForeMap(x
, y
, getNextBlockStateId(id(x
, y
)));
63 if (!isFluid(id(x
, y
))) {
64 GP
.world
.setForeMap(x
, y
, 0);
71 private void flowFluidTo(int thisId
, int x
, int y
, int nextStateId
) {
72 int thatId
= id(x
, y
);
73 if (fluidCanFlowThere(thisId
, thatId
)) {
74 GP
.world
.setForeMap(x
, y
, nextStateId
);
75 } else if (isWater(thisId
) && isLava(thatId
)) {
76 if (getBlockState(thatId
) > 0) {
77 GP
.world
.setForeMap(x
, y
, 4); //cobblestone
79 GP
.world
.setForeMap(x
, y
, 68); //obsidian
81 } else if (isLava(thisId
) && isWater(thatId
)) {
82 GP
.world
.setForeMap(x
, y
, 1); //stone
86 private void flowFluid(int x
, int y
) {
88 if (getBlockState(id
) < FLUID_STATES
- 1 && getBlock(id(x
, y
+ 1)).hasCollision()) {
89 int nextState
= getNextBlockState(id
);
90 int nextStateId
= getNextBlockStateId(id
);
94 flowFluidTo(id
, x
- 1, y
, nextStateId
);
95 flowFluidTo(id
, x
+ 1, y
, nextStateId
);
97 flowFluidTo(id
, x
, y
+ 1, isWater(id
) ? WATER_IDS
[1] : LAVA_IDS
[1]);
102 private void updateFluids(int x
, int y
) {
103 if (!isFluid(id(x
, y
))) return;
104 if (drainFluid(x
, y
)) return;
108 private void fluidUpdater() {
109 int midScreen
= (int) (GP
.renderer
.getCamX() + GP
.renderer
.getWidth() / 2) / 16;
110 for (int y
= GP
.world
.getHeight() - 1; y
>= 0; y
--) {
111 for (int x
= 0; x
<= GP
.world
.getWidth() / 2; x
++) {
112 updateFluids(midScreen
+ x
, y
);
113 updateFluids(midScreen
- x
, y
);
118 private boolean timeToUpdate() {
119 if (TimeUtils
.timeSinceMillis(fluidLastUpdateTimestamp
) >= FLUID_UPDATE_INTERVAL_MS
) {
120 fluidLastUpdateTimestamp
= TimeUtils
.millis();
128 while (!this.isInterrupted()) {
129 if (timeToUpdate()) {