DEADSOFTWARE

Fix code style
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameProc.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input;
5 import com.badlogic.gdx.utils.TimeUtils;
6 import ru.deadsoftware.cavecraft.CaveGame;
7 import ru.deadsoftware.cavecraft.GameScreen;
8 import ru.deadsoftware.cavecraft.game.mobs.FallingGravel;
9 import ru.deadsoftware.cavecraft.game.mobs.FallingSand;
10 import ru.deadsoftware.cavecraft.game.mobs.Mob;
11 import ru.deadsoftware.cavecraft.game.mobs.Pig;
12 import ru.deadsoftware.cavecraft.game.objects.Drop;
13 import ru.deadsoftware.cavecraft.game.objects.Player;
14 import ru.deadsoftware.cavecraft.misc.AppState;
15 import ru.deadsoftware.cavecraft.misc.Assets;
17 import java.io.Serializable;
18 import java.util.ArrayList;
20 public class GameProc implements Serializable {
22 public static double RUN_TIME = 0;
24 public static boolean DO_UPD = false;
25 public static int UPD_X = -1, UPD_Y = -1;
27 public Player player;
29 public ArrayList<Mob> mobs;
30 public ArrayList<Drop> drops;
32 public transient GameWorld world;
33 public transient GameRenderer renderer;
34 public transient GamePhysics physics;
36 public int cursorX, cursorY;
37 public int invSlot;
38 public int ctrlMode;
39 public int creativeScroll, maxCreativeScroll;
40 public int blockDmg = 0;
42 public boolean isTouchDown, isKeyDown, swim;
43 public int touchDownX, touchDownY, keyDownCode;
44 public int touchDownButton;
45 public long touchDownTime;
47 public GameProc() {
48 world = new GameWorld();
49 world.generate(1024, 256);
50 player = new Player(world.getSpawnPoint());
51 drops = new ArrayList<Drop>();
52 mobs = new ArrayList<Mob>();
53 for (int i = 0; i < 16; i++) {
54 mobs.add(new Pig(i * 256, 196 * 16));
55 }
56 physics = new GamePhysics(this);
57 if (CaveGame.TOUCH) {
58 renderer = new GameRenderer(this, 320,
59 320 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
60 } else {
61 ctrlMode = 1;
62 renderer = new GameRenderer(this, 480,
63 480 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
64 }
65 maxCreativeScroll = Items.ITEMS.size() / 8;
66 GameSaver.save(this);
67 }
69 public void resetRenderer() {
70 if (CaveGame.TOUCH) {
71 renderer = new GameRenderer(this, 320,
72 320 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
73 } else {
74 renderer = new GameRenderer(this, 480,
75 480 * ((float) GameScreen.getHeight() / GameScreen.getWidth()));
76 }
77 }
79 private boolean isAutoselectable(int x, int y) {
80 return (world.getForeMap(x, y) > 0 &&
81 Items.BLOCKS.getValueAt(world.getForeMap(x, y)).collision);
82 }
84 private void moveCursor() {
85 int pastX = cursorX, pastY = cursorY;
86 if (ctrlMode == 0 && CaveGame.TOUCH) {
87 cursorX = (int) (player.position.x + player.texWidth / 2) / 16;
88 if (player.dir == 0) cursorX--;
89 else cursorX++;
90 cursorY = (int) (player.position.y + player.texWidth) / 16;
91 if (!isAutoselectable(cursorX, cursorY)) {
92 cursorY++;
93 }
94 if (!isAutoselectable(cursorX, cursorY)) {
95 cursorY++;
96 }
97 if (!isAutoselectable(cursorX, cursorY)) {
98 if (player.dir == 0) cursorX++;
99 else cursorX--;
101 } else if (!CaveGame.TOUCH) {
102 cursorX = (int) (Gdx.input.getX() *
103 (renderer.camera.viewportWidth / GameScreen.getWidth()) + renderer.camera.position.x) / 16;
104 cursorY = (int) (Gdx.input.getY() *
105 (renderer.camera.viewportHeight / GameScreen.getHeight()) + renderer.camera.position.y) / 16;
106 if ((Gdx.input.getX() *
107 (renderer.camera.viewportWidth / GameScreen.getWidth()) + renderer.camera.position.x) < 0)
108 cursorX--;
110 if (pastX != cursorX || pastY != cursorY) blockDmg = 0;
113 private void checkCursorBounds() {
114 if (cursorY < 0) cursorY = 0;
115 if (cursorY >= world.getHeight()) cursorY = world.getHeight() - 1;
116 if (ctrlMode == 1) {
117 if (cursorX * 16 + 8 < player.position.x + player.texWidth / 2)
118 player.dir = 0;
119 if (cursorX * 16 + 8 > player.position.x + player.texWidth / 2)
120 player.dir = 1;
124 private void updateFluids(int x, int y) {
125 if (Items.isWater(world.getForeMap(x, y)) && world.getForeMap(x, y) != 8) {
126 if (world.getForeMap(x, y) == 60) {
127 if (!Items.isWater(world.getForeMap(x, y - 1)))
128 world.setForeMap(x, y, world.getForeMap(x, y) + 1);
129 } else if ((!Items.isWater(world.getForeMap(x - 1, y)) ||
130 (Items.isWater(world.getForeMap(x, y)) && world.getForeMap(x - 1, y) >= world.getForeMap(x, y))) &&
131 (!Items.isWater(world.getForeMap(x + 1, y)) ||
132 (Items.isWater(world.getForeMap(x, y)) && world.getForeMap(x + 1, y) >= world.getForeMap(x, y)))) {
133 world.setForeMap(x, y, world.getForeMap(x, y) + 1);
135 if (world.getForeMap(x, y) > 63) world.setForeMap(x, y, 0);
138 if (world.getForeMap(x, y) == 8 || world.getForeMap(x, y) == 60) {
139 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) ||
140 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
141 world.setForeMap(x, y + 1, 60);
142 updateBlock(x, y + 2);
143 } else if (Items.isLava(world.getForeMap(x, y + 1))) {
144 if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4);
145 else world.setForeMap(x, y + 1, 68);
146 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
147 if (world.getForeMap(x + 1, y) == 0 ||
148 (!Items.BLOCKS.getValueAt(world.getForeMap(x + 1, y)).collision && !Items.isFluid(world.getForeMap(x + 1, y))) ||
149 (Items.isWater(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 61)) {
150 world.setForeMap(x + 1, y, 61);
151 updateBlock(x + 1, y + 1);
152 } else if (Items.isLava(world.getForeMap(x + 1, y))) {
153 if (world.getForeMap(x + 1, y) > 9) world.setForeMap(x + 1, y, 4);
154 else world.setForeMap(x + 1, y, 68);
155 } else if (world.getForeMap(x + 1, y) == 61 && (world.getForeMap(x + 2, y) == 8 || world.getForeMap(x + 2, y) == 60))
156 world.setForeMap(x + 1, y, 8);
158 if (world.getForeMap(x - 1, y) == 0 ||
159 (!Items.BLOCKS.getValueAt(world.getForeMap(x - 1, y)).collision && !Items.isFluid(world.getForeMap(x - 1, y))) ||
160 (Items.isWater(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 61)) {
161 world.setForeMap(x - 1, y, 61);
162 updateBlock(x - 1, y + 1);
163 } else if (Items.isLava(world.getForeMap(x - 1, y))) {
164 if (world.getForeMap(x - 1, y) > 9) world.setForeMap(x - 1, y, 4);
165 else world.setForeMap(x - 1, y, 68);
166 } else if (world.getForeMap(x - 1, y) == 61 && (world.getForeMap(x - 2, y) == 8 || world.getForeMap(x - 2, y) == 60))
167 world.setForeMap(x - 1, y, 8);
169 return;
171 if (world.getForeMap(x, y) == 61) {
172 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) ||
173 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
174 world.setForeMap(x, y + 1, 60);
175 updateBlock(x, y + 2);
176 } else if (Items.isLava(world.getForeMap(x, y + 1))) {
177 if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4);
178 else world.setForeMap(x, y + 1, 68);
179 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
180 if (world.getForeMap(x + 1, y) == 0 ||
181 (!Items.BLOCKS.getValueAt(world.getForeMap(x + 1, y)).collision && !Items.isFluid(world.getForeMap(x + 1, y))) ||
182 (Items.isWater(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 62)) {
183 world.setForeMap(x + 1, y, 62);
184 updateBlock(x + 1, y + 1);
185 } else if (Items.isLava(world.getForeMap(x + 1, y))) {
186 if (world.getForeMap(x + 1, y) > 9) world.setForeMap(x + 1, y, 4);
187 else world.setForeMap(x + 1, y, 68);
190 if (world.getForeMap(x - 1, y) == 0 ||
191 (!Items.BLOCKS.getValueAt(world.getForeMap(x - 1, y)).collision && !Items.isFluid(world.getForeMap(x - 1, y))) ||
192 (Items.isWater(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 62)) {
193 world.setForeMap(x - 1, y, 62);
194 updateBlock(x - 1, y + 1);
195 } else if (Items.isLava(world.getForeMap(x - 1, y))) {
196 if (world.getForeMap(x - 1, y) > 9) world.setForeMap(x - 1, y, 4);
197 else world.setForeMap(x - 1, y, 68);
200 return;
202 if (world.getForeMap(x, y) == 62) {
203 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) ||
204 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
205 world.setForeMap(x, y + 1, 60);
206 updateBlock(x, y + 2);
207 } else if (Items.isLava(world.getForeMap(x, y + 1))) {
208 if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4);
209 else world.setForeMap(x, y + 1, 68);
210 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
211 if (world.getForeMap(x + 1, y) == 0 ||
212 (!Items.BLOCKS.getValueAt(world.getForeMap(x + 1, y)).collision && !Items.isFluid(world.getForeMap(x + 1, y)))) {
213 world.setForeMap(x + 1, y, 63);
214 updateBlock(x + 1, y + 1);
215 } else if (Items.isLava(world.getForeMap(x + 1, y))) {
216 if (world.getForeMap(x + 1, y) > 9) world.setForeMap(x + 1, y, 4);
217 else world.setForeMap(x + 1, y, 68);
220 if (world.getForeMap(x - 1, y) == 0 ||
221 (!Items.BLOCKS.getValueAt(world.getForeMap(x - 1, y)).collision && !Items.isFluid(world.getForeMap(x - 1, y)))) {
222 world.setForeMap(x - 1, y, 63);
223 updateBlock(x - 1, y + 1);
224 } else if (Items.isLava(world.getForeMap(x - 1, y))) {
225 if (world.getForeMap(x - 1, y) > 9) world.setForeMap(x - 1, y, 4);
226 else world.setForeMap(x - 1, y, 68);
229 return;
231 if (world.getForeMap(x, y) == 63) {
232 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 61 && world.getForeMap(x, y + 1) <= 63) ||
233 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
234 world.setForeMap(x, y + 1, 60);
235 updateBlock(x, y + 2);
236 } else if (Items.isLava(world.getForeMap(x, y + 1))) {
237 if (world.getForeMap(x, y + 1) > 9) world.setForeMap(x, y + 1, 4);
238 else world.setForeMap(x, y + 1, 68);
240 return;
243 if (Items.isLava(world.getForeMap(x, y)) && world.getForeMap(x, y) != 9) {
244 if (world.getForeMap(x, y) == 64) {
245 if (!Items.isLava(world.getForeMap(x, y - 1)))
246 world.setForeMap(x, y, world.getForeMap(x, y) + 1);
247 } else if ((!Items.isLava(world.getForeMap(x, y - 1))) &&
248 (!Items.isLava(world.getForeMap(x - 1, y)) ||
249 (Items.isLava(world.getForeMap(x, y)) && world.getForeMap(x - 1, y) >= world.getForeMap(x, y))) &&
250 (!Items.isLava(world.getForeMap(x + 1, y)) ||
251 (Items.isLava(world.getForeMap(x, y)) && world.getForeMap(x + 1, y) >= world.getForeMap(x, y)))) {
252 world.setForeMap(x, y, world.getForeMap(x, y) + 1);
254 if (world.getForeMap(x, y) > 67) world.setForeMap(x, y, 0);
257 if (world.getForeMap(x, y) == 9 || world.getForeMap(x, y) == 64) {
258 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) ||
259 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
260 world.setForeMap(x, y + 1, 64);
261 updateBlock(x, y + 2);
262 } else if (Items.isWater(world.getForeMap(x, y + 1))) {
263 world.setForeMap(x, y + 1, 1);
264 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
265 if (world.getForeMap(x + 1, y) == 0 ||
266 (!Items.BLOCKS.getValueAt(world.getForeMap(x + 1, y)).collision && !Items.isFluid(world.getForeMap(x + 1, y))) ||
267 (Items.isLava(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 65)) {
268 world.setForeMap(x + 1, y, 65);
269 updateBlock(x + 1, y + 1);
270 } else if (Items.isWater(world.getForeMap(x + 1, y))) {
271 world.setForeMap(x + 1, y, 1);
274 if (world.getForeMap(x - 1, y) == 0 ||
275 (!Items.BLOCKS.getValueAt(world.getForeMap(x - 1, y)).collision && !Items.isFluid(world.getForeMap(x - 1, y))) ||
276 (Items.isLava(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 65)) {
277 world.setForeMap(x - 1, y, 65);
278 updateBlock(x - 1, y + 1);
279 } else if (Items.isWater(world.getForeMap(x - 1, y))) {
280 world.setForeMap(x - 1, y, 1);
283 return;
285 if (world.getForeMap(x, y) == 65) {
286 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) ||
287 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
288 world.setForeMap(x, y + 1, 64);
289 updateBlock(x, y + 2);
290 } else if (Items.isWater(world.getForeMap(x, y + 1))) {
291 world.setForeMap(x, y + 1, 1);
292 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
293 if (world.getForeMap(x + 1, y) == 0 ||
294 (!Items.BLOCKS.getValueAt(world.getForeMap(x + 1, y)).collision && !Items.isFluid(world.getForeMap(x + 1, y))) ||
295 (Items.isLava(world.getForeMap(x + 1, y)) && world.getForeMap(x + 1, y) > 66)) {
296 world.setForeMap(x + 1, y, 66);
297 updateBlock(x + 1, y + 1);
298 } else if (Items.isWater(world.getForeMap(x + 1, y))) {
299 world.setForeMap(x + 1, y, 1);
302 if (world.getForeMap(x - 1, y) == 0 ||
303 (!Items.BLOCKS.getValueAt(world.getForeMap(x - 1, y)).collision && !Items.isFluid(world.getForeMap(x - 1, y))) ||
304 (Items.isLava(world.getForeMap(x - 1, y)) && world.getForeMap(x - 1, y) > 66)) {
305 world.setForeMap(x - 1, y, 66);
306 updateBlock(x - 1, y + 1);
307 } else if (Items.isWater(world.getForeMap(x - 1, y))) {
308 world.setForeMap(x - 1, y, 1);
311 return;
313 if (world.getForeMap(x, y) == 66) {
314 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) ||
315 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
316 world.setForeMap(x, y + 1, 64);
317 updateBlock(x, y + 2);
318 } else if (Items.isWater(world.getForeMap(x, y + 1))) {
319 world.setForeMap(x, y + 1, 1);
320 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
321 if (world.getForeMap(x + 1, y) == 0 ||
322 (!Items.BLOCKS.getValueAt(world.getForeMap(x + 1, y)).collision && !Items.isFluid(world.getForeMap(x + 1, y)))) {
323 world.setForeMap(x + 1, y, 67);
324 updateBlock(x + 1, y + 1);
325 } else if (Items.isWater(world.getForeMap(x + 1, y))) {
326 world.setForeMap(x + 1, y, 1);
329 if (world.getForeMap(x - 1, y) == 0 ||
330 (!Items.BLOCKS.getValueAt(world.getForeMap(x - 1, y)).collision && !Items.isFluid(world.getForeMap(x - 1, y)))) {
331 world.setForeMap(x - 1, y, 67);
332 updateBlock(x - 1, y + 1);
333 } else if (Items.isWater(world.getForeMap(x - 1, y))) {
334 world.setForeMap(x - 1, y, 1);
337 return;
339 if (world.getForeMap(x, y) == 67) {
340 if (world.getForeMap(x, y + 1) == 0 || (world.getForeMap(x, y + 1) >= 65 && world.getForeMap(x, y + 1) <= 67) ||
341 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision && !Items.isFluid(world.getForeMap(x, y + 1)))) {
342 world.setForeMap(x, y + 1, 64);
343 updateBlock(x, y + 2);
344 } else if (Items.isWater(world.getForeMap(x, y + 1))) {
345 world.setForeMap(x, y + 1, 1);
347 return;
351 private void updateBlock(int x, int y) {
352 if (world.getForeMap(x, y) == 10) {
353 if (world.getForeMap(x, y + 1) == 0 || !Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
354 world.setForeMap(x, y, 0);
355 mobs.add(new FallingSand(x * 16, y * 16));
356 updateBlock(x, y - 1);
360 if (world.getForeMap(x, y) == 11) {
361 if (world.getForeMap(x, y + 1) == 0 || !Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
362 world.setForeMap(x, y, 0);
363 mobs.add(new FallingGravel(x * 16, y * 16));
364 updateBlock(x, y - 1);
368 if (world.getForeMap(x, y) == 59) {
369 if (world.getForeMap(x, y + 1) == 0 || !Items.BLOCKS.getValueAt(world.getForeMap(x, y + 1)).collision) {
370 world.setForeMap(x, y, 0);
371 updateBlock(x, y - 1);
375 if (world.getForeMap(x, y) == 2) {
376 if (world.getForeMap(x, y - 1) > 0 && (Items.BLOCKS.getValueAt(world.getForeMap(x, y - 1)).collision ||
377 Items.isFluid(world.getForeMap(x, y - 1)))) {
378 world.setForeMap(x, y, 3);
383 public void useItem(int x, int y, int id, boolean bg) {
384 if (id > 0 && Items.ITEMS.get(id).getType() == 0) {
385 if (!bg) world.placeToForeground(x, y, Items.ITEMS.get(id).getBlock());
386 else world.placeToBackground(x, y, Items.ITEMS.get(id).getBlock());
390 public void update(float delta) {
391 RUN_TIME += delta;
393 if (DO_UPD) {
394 for (int y = UPD_Y; y < UPD_Y + 16; y++)
395 for (int x = UPD_X; x < UPD_X + 16; x++) {
396 updateBlock(x, y);
398 DO_UPD = false;
401 for (int y = 0; y < world.getHeight(); y++) {
402 for (int x = (int) renderer.camera.position.x / 16 - 1; x < (int) (renderer.camera.position.x + renderer.camera.viewportWidth) / 16 + 1; x++) {
403 updateFluids(x, y);
407 physics.update(delta);
408 moveCursor();
409 checkCursorBounds();
411 if (isTouchDown && touchDownButton == Input.Buttons.LEFT) {
412 if (world.getForeMap(cursorX, cursorY) > 0 &&
413 Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getHp() >= 0) {// || world.getBackMap(cursorX, cursorY) > 0) {
414 blockDmg++;
415 if (blockDmg >= Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getHp()) {
416 if (Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getDrop() > 0)
417 drops.add(new Drop(cursorX * 16 + 4, cursorY * 16 + 4, Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getDrop()));
418 world.placeToForeground(cursorX, cursorY, 0);
419 blockDmg = 0;
424 if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
425 if (touchDownButton == Input.Buttons.RIGHT) {
426 useItem(cursorX, cursorY, player.inventory[invSlot], true);
427 isTouchDown = false;
428 } else if (touchDownY < Assets.invBar.getRegionHeight() &&
429 touchDownX > renderer.camera.viewportWidth / 2 - Assets.invBar.getRegionWidth() / 2 &&
430 touchDownX < renderer.camera.viewportWidth / 2 + Assets.invBar.getRegionWidth() / 2) {
431 CaveGame.STATE = AppState.GAME_CREATIVE_INV;
432 isTouchDown = false;