DEADSOFTWARE

Reimplement mobs
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameProc.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.utils.Disposable;
4 import ru.deadsoftware.cavedroid.CaveGame;
5 import ru.deadsoftware.cavedroid.GameScreen;
6 import ru.deadsoftware.cavedroid.game.mobs.FallingGravel;
7 import ru.deadsoftware.cavedroid.game.mobs.FallingSand;
8 import ru.deadsoftware.cavedroid.game.mobs.Mob;
9 import ru.deadsoftware.cavedroid.game.objects.Drop;
10 import ru.deadsoftware.cavedroid.game.mobs.Player;
11 import ru.deadsoftware.cavedroid.misc.ControlMode;
13 import java.io.Serializable;
14 import java.util.ArrayList;
16 public class GameProc implements Serializable, Disposable {
18 static final int MAX_CREATIVE_SCROLL = GameItems.getItemsSize() / 8;
20 private static final int WORLD_WIDTH = 1024;
21 private static final int WORLD_HEIGHT = 256;
22 private static final int UPD_RANGE = 16;
24 static boolean DO_UPD = false;
25 static int UPD_X = -1, UPD_Y = -1;
27 private transient GameFluidsThread fluidThread;
28 public transient GameWorld world;
29 public transient GameRenderer renderer;
30 public transient GameInput input;
31 transient GamePhysics physics;
33 public ControlMode controlMode;
34 public final Player player;
35 public final ArrayList<Mob> mobs;
36 final ArrayList<Drop> drops;
38 public void resetRenderer() {
39 int scale = CaveGame.TOUCH ? 320 : 480;
40 renderer = new GameRenderer(scale, scale * GameScreen.getHeight() / GameScreen.getWidth());
41 }
43 public GameProc(int gameMode) {
44 world = new GameWorld(WORLD_WIDTH, WORLD_HEIGHT);
45 player = new Player(gameMode);
46 drops = new ArrayList<>();
47 mobs = new ArrayList<>();
48 physics = new GamePhysics();
49 input = new GameInput();
50 controlMode = CaveGame.TOUCH ? ControlMode.WALK : ControlMode.CURSOR;
51 resetRenderer();
52 startFluidThread();
53 }
55 private void startFluidThread() {
56 fluidThread = new GameFluidsThread();
57 fluidThread.start();
58 }
60 private void updateBlock(int x, int y) {
61 if (world.getForeMap(x, y) == 10) {
62 if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) {
63 world.setForeMap(x, y, 0);
64 mobs.add(new FallingSand(x * 16, y * 16));
65 updateBlock(x, y - 1);
66 }
67 }
69 if (world.getForeMap(x, y) == 11) {
70 if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) {
71 world.setForeMap(x, y, 0);
72 mobs.add(new FallingGravel(x * 16, y * 16));
73 updateBlock(x, y - 1);
74 }
75 }
77 if (world.hasForeAt(x, y) && world.getForeMapBlock(x, y).requiresBlock()) {
78 if (!world.hasForeAt(x, y + 1) || !world.getForeMapBlock(x, y + 1).hasCollision()) {
79 world.destroyForeMap(x, y);
80 updateBlock(x, y - 1);
81 }
82 }
84 if (world.getForeMap(x, y) == 2) {
85 if (world.hasForeAt(x, y - 1) && (world.getForeMapBlock(x, y - 1).hasCollision() ||
86 GameItems.isFluid(world.getForeMap(x, y - 1)))) {
87 world.setForeMap(x, y, 3);
88 }
89 }
90 }
92 private void blockUpdater() {
93 if (DO_UPD) {
94 for (int y = UPD_Y; y < UPD_Y + UPD_RANGE; y++) {
95 for (int x = UPD_X; x < UPD_X + UPD_RANGE; x++) {
96 updateBlock(x, y);
97 }
98 }
99 DO_UPD = false;
103 public void update() {
104 physics.update();
105 input.update();
106 blockUpdater();
107 if (fluidThread == null || !fluidThread.isAlive()) startFluidThread();
110 @Override
111 public void dispose() {
112 fluidThread.interrupt();