DEADSOFTWARE

5aecce295466dda979db1ed307f27f0cbead3706
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameWorld.java
1 package ru.deadsoftware.cavedroid.game;
3 import ru.deadsoftware.cavedroid.game.mobs.FallingGravel;
4 import ru.deadsoftware.cavedroid.game.mobs.FallingSand;
5 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
6 import ru.deadsoftware.cavedroid.game.objects.Block;
7 import ru.deadsoftware.cavedroid.game.objects.DropController;
9 import javax.inject.Inject;
11 @GameScope
12 public class GameWorld {
14 private static final int DEFAULT_WIDTH = 1024;
15 private static final int DEFAULT_HEIGHT = 256;
16 private static final int UPDATE_RANGE = 16;
18 private final DropController mDropController;
19 private final MobsController mMobsController;
20 private final GameFluidsThread mGameFluidsThread;
22 private final int mWidth;
23 private final int mHeight;
24 private final int[][] mForeMap;
25 private final int[][] mBackMap;
27 private boolean mShouldUpdate;
28 private int mUpdateX;
29 private int mUpdateY;
31 @Inject
32 GameWorld(DropController dropController,
33 MobsController mobsController) {
34 mDropController = dropController;
35 mMobsController = mobsController;
37 mWidth = DEFAULT_WIDTH;
38 mHeight = DEFAULT_HEIGHT;
39 WorldGen.genWorld(mWidth, mHeight);
40 mForeMap = WorldGen.getForeMap();
41 mBackMap = WorldGen.getBackMap();
42 WorldGen.clear();
44 mGameFluidsThread = new GameFluidsThread(this, mMobsController, Thread.currentThread());
45 }
47 // GameWorld(int[][] foreMap, int[][] backMap) {
48 // this.foreMap = foreMap.clone();
49 // this.backMap = backMap.clone();
50 // WIDTH = foreMap.length;
51 // HEIGHT = foreMap[0].length;
52 // }
54 public int getWidth() {
55 return mWidth;
56 }
58 public int getHeight() {
59 return mHeight;
60 }
62 public float getWidthPx() {
63 return mWidth * 16f;
64 }
66 public float getHeightPx() {
67 return mHeight * 16f;
68 }
70 int[][] getFullForeMap() {
71 return mForeMap;
72 }
74 int[][] getFullBackMap() {
75 return mBackMap;
76 }
78 private int transformX(int x) {
79 x = x % getWidth();
80 if (x < 0) {
81 x = getWidth() - Math.abs(x);
82 }
83 return x;
84 }
86 private int getMap(int x, int y, int layer) {
87 int map = 0;
88 try {
89 x = transformX(x);
90 map = (layer == 0) ? mForeMap[x][y] : mBackMap[x][y];
91 } catch (ArrayIndexOutOfBoundsException ignored) {
92 }
93 return map;
94 }
96 private void setMap(int x, int y, int layer, int value) {
97 try {
98 x = transformX(x);
99 if (layer == 0) {
100 mForeMap[x][y] = value;
101 } else {
102 mBackMap[x][y] = value;
104 } catch (ArrayIndexOutOfBoundsException ignored) {
108 public boolean hasForeAt(int x, int y) {
109 return getMap(x, y, 0) != 0;
112 public boolean hasBackAt(int x, int y) {
113 return getMap(x, y, 1) != 0;
116 public int getForeMap(int x, int y) {
117 return getMap(x, y, 0);
120 public Block getForeMapBlock(int x, int y) {
121 return GameItems.getBlock(getMap(x, y, 0));
124 public void setForeMap(int x, int y, int id) {
125 setMap(x, y, 0, id);
128 public int getBackMap(int x, int y) {
129 return getMap(x, y, 1);
132 public Block getBackMapBlock(int x, int y) {
133 return GameItems.getBlock(getMap(x, y, 1));
136 public void setBackMap(int x, int y, int id) {
137 setMap(x, y, 1, id);
140 private void placeSlab(int x, int y, int value) {
141 switch (value) {
142 case 51:
143 setForeMap(x, y, 52);
144 break;
145 case 53:
146 setForeMap(x, y, 21);
147 break;
148 case 54:
149 setForeMap(x, y, 5);
150 break;
151 case 55:
152 setForeMap(x, y, 4);
153 break;
154 case 56:
155 setForeMap(x, y, 28);
156 break;
157 case 58:
158 setForeMap(x, y, 57);
159 break;
163 public void placeToForeground(int x, int y, int value) {
164 if (!hasForeAt(x, y) || value == 0 || !GameItems.getBlock(getForeMap(x, y)).hasCollision()) {
165 setForeMap(x, y, value);
166 } else if (GameItems.isSlab(value) && getForeMap(x, y) == value) {
167 placeSlab(x, y, value);
169 mUpdateX = x - 8;
170 mUpdateY = y - 8;
171 mShouldUpdate = true;
174 public void placeToBackground(int x, int y, int value) {
175 if (value == 0 || (getBackMap(x, y) == 0 && GameItems.getBlock(value).hasCollision()) &&
176 (!GameItems.getBlock(value).isTransparent() || value == 18)) {
177 setBackMap(x, y, value);
181 public void destroyForeMap(int x, int y) {
182 Block block = GameItems.getBlock(getForeMap(x, y));
183 if (block.hasDrop()) {
184 mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop()));
186 placeToForeground(x, y, 0);
189 public void destroyBackMap(int x, int y) {
190 Block block = GameItems.getBlock(getBackMap(x, y));
191 if (block.hasDrop()) {
192 mDropController.addDrop(transformX(x) * 16 + 4, y * 16 + 4, GameItems.getItemId(block.getDrop()));
194 placeToBackground(x, y, 0);
197 private void updateBlock(int x, int y) {
198 if (getForeMap(x, y) == 10) {
199 if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) {
200 setForeMap(x, y, 0);
201 mMobsController.addMob(FallingSand.class, x * 16, y * 16);
202 updateBlock(x, y - 1);
206 if (getForeMap(x, y) == 11) {
207 if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) {
208 setForeMap(x, y, 0);
209 mMobsController.addMob(FallingGravel.class, x * 16, y * 16);
210 updateBlock(x, y - 1);
214 if (hasForeAt(x, y) && getForeMapBlock(x, y).requiresBlock()) {
215 if (!hasForeAt(x, y + 1) || !getForeMapBlock(x, y + 1).hasCollision()) {
216 destroyForeMap(x, y);
217 updateBlock(x, y - 1);
221 if (getForeMap(x, y) == 2) {
222 if (hasForeAt(x, y - 1) && (getForeMapBlock(x, y - 1).hasCollision() ||
223 GameItems.isFluid(getForeMap(x, y - 1)))) {
224 setForeMap(x, y, 3);
229 public void update() {
230 if (mShouldUpdate) {
231 for (int y = mUpdateY; y < mUpdateY + UPDATE_RANGE; y++) {
232 for (int x = mUpdateX; x < mUpdateX + UPDATE_RANGE; x++) {
233 updateBlock(x, y);
236 mShouldUpdate = false;
239 if (!mGameFluidsThread.isAlive()) {
240 mGameFluidsThread.start();
244 public void startFluidsThread() {
245 mGameFluidsThread.start();