DEADSOFTWARE

Add logic to some blocks and fluids
[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.Player;
13 import ru.deadsoftware.cavecraft.misc.AppState;
14 import ru.deadsoftware.cavecraft.misc.Assets;
16 import java.io.Serializable;
17 import java.util.ArrayList;
19 public class GameProc implements Serializable{
21 public static double RUN_TIME = 0;
23 public static boolean DO_UPD = false;
24 public static int UPD_X = -1, UPD_Y = -1;
25 public static int FUPD_X, FUPD_Y;
27 public Player player;
29 public ArrayList<Mob> mobs;
31 public transient GameWorld world;
32 public transient GameRenderer renderer;
33 public transient GamePhysics physics;
35 public int cursorX, cursorY;
36 public int invSlot;
37 public int ctrlMode;
38 public int creativeScroll, maxCreativeScroll;
40 public boolean isTouchDown, isKeyDown, swim;
41 public int touchDownX, touchDownY, keyDownCode;
42 public int touchDownButton;
43 public long touchDownTime;
45 public GameProc() {
46 world = new GameWorld();
47 world.generate(1024,256);
48 player = new Player(world.getSpawnPoint());
49 mobs = new ArrayList<Mob>();
50 for (int i=0; i<16; i++) {
51 mobs.add(new Pig(i*256, 196*16));
52 }
53 physics = new GamePhysics(this);
54 if (CaveGame.TOUCH) {
55 renderer = new GameRenderer(this,320,
56 320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
57 } else {
58 ctrlMode = 1;
59 renderer = new GameRenderer(this,480,
60 480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
61 }
62 maxCreativeScroll = Items.BLOCKS.size/8;
63 GameSaver.save(this);
64 }
66 public void resetRenderer() {
67 if (CaveGame.TOUCH) {
68 renderer = new GameRenderer(this,320,
69 320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
70 } else {
71 renderer = new GameRenderer(this,480,
72 480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
73 }
74 }
76 private boolean isAutoselectable(int x, int y) {
77 return (world.getForeMap(x,y)>0 &&
78 Items.BLOCKS.getValueAt(world.getForeMap(x,y)).collision);
79 }
81 private void moveCursor() {
82 if (ctrlMode == 0 && CaveGame.TOUCH) {
83 cursorX = (int) (player.position.x + player.texWidth / 2) / 16;
84 if (player.dir == 0) cursorX--;
85 else cursorX++;
86 cursorY = (int) (player.position.y + player.texWidth) / 16;
87 if (!isAutoselectable(cursorX, cursorY)) {
88 cursorY++;
89 }
90 if (!isAutoselectable(cursorX, cursorY)) {
91 cursorY++;
92 }
93 if (!isAutoselectable(cursorX, cursorY)) {
94 if (player.dir == 0) cursorX++;
95 else cursorX--;
96 }
97 } else if (!CaveGame.TOUCH){
98 cursorX = (int)(Gdx.input.getX()*
99 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)/16;
100 cursorY = (int)(Gdx.input.getY()*
101 (renderer.camera.viewportHeight/GameScreen.getHeight())+renderer.camera.position.y)/16;
102 if ((Gdx.input.getX()*
103 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)<0)
104 cursorX--;
108 private void checkCursorBounds() {
109 if (cursorY < 0) cursorY = 0;
110 if (cursorY >= world.getHeight()) cursorY = world.getHeight()-1;
111 if (ctrlMode==1) {
112 if (cursorX*16+8<player.position.x+player.texWidth/2)
113 player.dir=0;
114 if (cursorX*16+8>player.position.x+player.texWidth/2)
115 player.dir=1;
119 private void updateFluids(int x, int y) {
120 if (Items.isWater(world.getForeMap(x, y)) && world.getForeMap(x, y)!=8) {
121 if ((!Items.isWater(world.getForeMap(x-1,y)) ||
122 (Items.isWater(world.getForeMap(x,y)) && world.getForeMap(x-1, y)>=world.getForeMap(x, y))) &&
123 (!Items.isWater(world.getForeMap(x+1,y)) ||
124 (Items.isWater(world.getForeMap(x,y)) && world.getForeMap(x+1, y)>=world.getForeMap(x, y)))){
125 world.setForeMap(x, y, world.getForeMap(x, y)+1);
126 if (world.getForeMap(x, y)>62) world.setForeMap(x, y, 0);
130 if (world.getForeMap(x, y) == 8) {
131 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=60 && world.getForeMap(x, y+1)<=62) ||
132 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
133 world.setForeMap(x,y+1,8);
134 updateBlock(x, y+2);
135 } else if (Items.isLava(world.getForeMap(x, y+1))) {
136 if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4);
137 else world.setForeMap(x, y+1, 66);
138 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
139 if (world.getForeMap(x+1, y)==0 ||
140 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ||
141 (Items.isWater(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>60)) {
142 world.setForeMap(x+1,y,60);
143 updateBlock(x+1, y+1);
144 } else if (Items.isLava(world.getForeMap(x+1, y))) {
145 if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4);
146 else world.setForeMap(x+1, y, 66);
147 } else if (world.getForeMap(x+1, y)==60 && world.getForeMap(x+2, y)==8) world.setForeMap(x+1, y, 8);
149 if (world.getForeMap(x-1, y)==0 ||
150 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ||
151 (Items.isWater(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>60)) {
152 world.setForeMap(x-1,y,60);
153 updateBlock(x-1, y+1);
154 } else if (Items.isLava(world.getForeMap(x-1, y))) {
155 if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4);
156 else world.setForeMap(x-1, y, 66);
157 } else if (world.getForeMap(x-1, y)==60 && world.getForeMap(x-2, y)==8) world.setForeMap(x-1, y, 8);
159 return;
161 if (world.getForeMap(x, y) == 60) {
162 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=60 && world.getForeMap(x, y+1)<=62) ||
163 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
164 world.setForeMap(x,y+1,8);
165 updateBlock(x, y+2);
166 } else if (Items.isLava(world.getForeMap(x, y+1))) {
167 if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4);
168 else world.setForeMap(x, y+1, 66);
169 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
170 if (world.getForeMap(x+1, y)==0 ||
171 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ||
172 (Items.isWater(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>61)){
173 world.setForeMap(x+1,y,61);
174 updateBlock(x+1, y+1);
175 } else if (Items.isLava(world.getForeMap(x+1, y))) {
176 if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4);
177 else world.setForeMap(x+1, y, 66);
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)>61)){
183 world.setForeMap(x-1,y,61);
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, 66);
190 return;
192 if (world.getForeMap(x, y) == 61) {
193 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=60 && world.getForeMap(x, y+1)<=62) ||
194 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
195 world.setForeMap(x,y+1,8);
196 updateBlock(x, y+2);
197 } else if (Items.isLava(world.getForeMap(x, y+1))) {
198 if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4);
199 else world.setForeMap(x, y+1, 66);
200 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
201 if (world.getForeMap(x+1, y)==0 ||
202 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ){
203 world.setForeMap(x+1,y,62);
204 updateBlock(x+1, y+1);
205 } else if (Items.isLava(world.getForeMap(x+1, y))) {
206 if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4);
207 else world.setForeMap(x+1, y, 66);
210 if (world.getForeMap(x-1, y)==0 ||
211 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ){
212 world.setForeMap(x-1,y,62);
213 updateBlock(x-1, y+1);
214 } else if (Items.isLava(world.getForeMap(x-1, y))) {
215 if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4);
216 else world.setForeMap(x-1, y, 66);
219 return;
221 if (world.getForeMap(x, y) == 62) {
222 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=60 && world.getForeMap(x, y+1)<=62) ||
223 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
224 world.setForeMap(x,y+1,8);
225 updateBlock(x, y+2);
226 } else if (Items.isLava(world.getForeMap(x, y+1))) {
227 if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4);
228 else world.setForeMap(x, y+1, 66);
230 return;
233 if (Items.isLava(world.getForeMap(x, y)) && world.getForeMap(x, y)!=9) {
234 if ((!Items.isLava(world.getForeMap(x-1,y)) ||
235 (Items.isLava(world.getForeMap(x,y)) && world.getForeMap(x-1, y)>=world.getForeMap(x, y))) &&
236 (!Items.isLava(world.getForeMap(x+1,y)) ||
237 (Items.isLava(world.getForeMap(x,y)) && world.getForeMap(x+1, y)>=world.getForeMap(x, y)))){
238 world.setForeMap(x, y, world.getForeMap(x, y)+1);
239 if (world.getForeMap(x, y)>65) world.setForeMap(x, y, 0);
243 if (world.getForeMap(x, y) == 9) {
244 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=63 && world.getForeMap(x, y+1)<=65) ||
245 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
246 world.setForeMap(x,y+1,9);
247 updateBlock(x, y+2);
248 } else if (Items.isWater(world.getForeMap(x, y+1))) {
249 world.setForeMap(x, y+1, 1);
250 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
251 if (world.getForeMap(x+1, y)==0 ||
252 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ||
253 (Items.isLava(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>63)) {
254 world.setForeMap(x+1,y,63);
255 updateBlock(x+1, y+1);
256 } else if (Items.isWater(world.getForeMap(x+1, y))) {
257 world.setForeMap(x+1, y, 1);
260 if (world.getForeMap(x-1, y)==0 ||
261 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ||
262 (Items.isLava(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>63)) {
263 world.setForeMap(x-1,y,63);
264 updateBlock(x-1, y+1);
265 } else if (Items.isWater(world.getForeMap(x-1, y))) {
266 world.setForeMap(x-1, y, 1);
269 return;
271 if (world.getForeMap(x, y) == 63) {
272 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=63 && world.getForeMap(x, y+1)<=65) ||
273 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
274 world.setForeMap(x,y+1,9);
275 updateBlock(x, y+2);
276 } else if (Items.isWater(world.getForeMap(x, y+1))) {
277 world.setForeMap(x, y+1, 1);
278 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
279 if (world.getForeMap(x+1, y)==0 ||
280 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ||
281 (Items.isLava(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>64)){
282 world.setForeMap(x+1,y,64);
283 updateBlock(x+1, y+1);
284 } else if (Items.isWater(world.getForeMap(x+1, y))) {
285 world.setForeMap(x+1, y, 1);
288 if (world.getForeMap(x-1, y)==0 ||
289 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ||
290 (Items.isLava(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>64)){
291 world.setForeMap(x-1,y,64);
292 updateBlock(x-1, y+1);
293 } else if (Items.isWater(world.getForeMap(x-1, y))) {
294 world.setForeMap(x-1, y, 1);
297 return;
299 if (world.getForeMap(x, y) == 64) {
300 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=63 && world.getForeMap(x, y+1)<=65) ||
301 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
302 world.setForeMap(x,y+1,9);
303 updateBlock(x, y+2);
304 } else if (Items.isWater(world.getForeMap(x, y+1))) {
305 world.setForeMap(x, y+1, 1);
306 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
307 if (world.getForeMap(x+1, y)==0 ||
308 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ){
309 world.setForeMap(x+1,y,65);
310 updateBlock(x+1, y+1);
311 } else if (Items.isWater(world.getForeMap(x+1, y))) {
312 world.setForeMap(x+1, y, 1);
315 if (world.getForeMap(x-1, y)==0 ||
316 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ){
317 world.setForeMap(x-1,y,65);
318 updateBlock(x-1, y+1);
319 } else if (Items.isWater(world.getForeMap(x-1, y))) {
320 world.setForeMap(x-1, y, 1);
323 return;
325 if (world.getForeMap(x, y) == 65) {
326 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=63 && world.getForeMap(x, y+1)<=65) ||
327 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
328 world.setForeMap(x,y+1,9);
329 updateBlock(x, y+2);
330 } else if (Items.isWater(world.getForeMap(x, y+1))) {
331 world.setForeMap(x, y+1, 1);
333 return;
337 private void updateBlock(int x, int y) {
338 if (world.getForeMap(x, y) == 10) {
339 if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x,y+1)).collision) {
340 world.setForeMap(x, y, 0);
341 mobs.add(new FallingSand(x*16, y*16));
342 updateBlock(x, y-1);
346 if (world.getForeMap(x, y) == 11) {
347 if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x,y+1)).collision) {
348 world.setForeMap(x, y, 0);
349 mobs.add(new FallingGravel(x*16, y*16));
350 updateBlock(x, y-1);
354 if (world.getForeMap(x, y) == 59) {
355 if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
356 world.setForeMap(x,y,0);
357 updateBlock(x, y-1);
361 if (world.getForeMap(x, y) == 2) {
362 if (world.getForeMap(x, y-1)>0 && (Items.BLOCKS.getValueAt(world.getForeMap(x, y-1)).collision ||
363 Items.isFluid(world.getForeMap(x, y-1)))) {
364 world.setForeMap(x, y, 3);
369 public void update(float delta) {
370 RUN_TIME += delta;
372 if (DO_UPD) {
373 for (int y=UPD_Y; y<UPD_Y+16; y++)
374 for (int x=UPD_X; x<UPD_X+16; x++) {
375 updateBlock(x, y);
377 DO_UPD = false;
380 for (int y=(int)renderer.camera.position.y/16-1; y<(int)(renderer.camera.position.y+renderer.camera.viewportHeight)/16+1; y++) {
381 for (int x=(int)renderer.camera.position.x/16-1; x<(int)(renderer.camera.position.x+renderer.camera.viewportWidth)/16+1; x++) {
382 updateFluids(x, y);
386 updateFluids(FUPD_X, FUPD_Y);
387 FUPD_X++;
388 if (FUPD_X>=(int)(renderer.camera.position.x+renderer.camera.viewportWidth)/16+1) {
389 FUPD_X = (int) renderer.camera.position.x / 16 - 1;
390 FUPD_Y++;
391 if (FUPD_Y>=(int)(renderer.camera.position.y+renderer.camera.viewportHeight)/16+1) {
392 FUPD_Y = (int) renderer.camera.position.y / 16 - 1;
396 physics.update(delta);
397 moveCursor();
398 checkCursorBounds();
400 if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
401 if (touchDownButton== Input.Buttons.RIGHT) {
402 world.placeToBackground(cursorX, cursorY,
403 player.inventory[invSlot]);
404 } else if (touchDownY< Assets.invBar.getRegionHeight() &&
405 touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
406 touchDownX<renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
407 CaveGame.STATE = AppState.GAME_CREATIVE_INV;
409 isTouchDown = false;