DEADSOFTWARE

25658c165421870f9d3868e08f16f4bd96a2e847
[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)) world.setForeMap(x+1, y, 8);
157 if (world.getForeMap(x-1, y)==0 ||
158 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ||
159 (Items.isWater(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>61)) {
160 world.setForeMap(x-1,y,61);
161 updateBlock(x-1, y+1);
162 } else if (Items.isLava(world.getForeMap(x-1, y))) {
163 if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4);
164 else world.setForeMap(x-1, y, 68);
165 } else if (world.getForeMap(x-1, y)==61 && (world.getForeMap(x-2, y)==8 || world.getForeMap(x-2, y)==60)) world.setForeMap(x-1, y, 8);
167 return;
169 if (world.getForeMap(x, y) == 61) {
170 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=61 && world.getForeMap(x, y+1)<=63) ||
171 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
172 world.setForeMap(x,y+1,60);
173 updateBlock(x, y+2);
174 } else if (Items.isLava(world.getForeMap(x, y+1))) {
175 if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4);
176 else world.setForeMap(x, y+1, 68);
177 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
178 if (world.getForeMap(x+1, y)==0 ||
179 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ||
180 (Items.isWater(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>62)){
181 world.setForeMap(x+1,y,62);
182 updateBlock(x+1, y+1);
183 } else if (Items.isLava(world.getForeMap(x+1, y))) {
184 if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4);
185 else world.setForeMap(x+1, y, 68);
188 if (world.getForeMap(x-1, y)==0 ||
189 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ||
190 (Items.isWater(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>62)){
191 world.setForeMap(x-1,y,62);
192 updateBlock(x-1, y+1);
193 } else if (Items.isLava(world.getForeMap(x-1, y))) {
194 if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4);
195 else world.setForeMap(x-1, y, 68);
198 return;
200 if (world.getForeMap(x, y) == 62) {
201 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=61 && world.getForeMap(x, y+1)<=63) ||
202 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
203 world.setForeMap(x,y+1,60);
204 updateBlock(x, y+2);
205 } else if (Items.isLava(world.getForeMap(x, y+1))) {
206 if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4);
207 else world.setForeMap(x, y+1, 68);
208 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
209 if (world.getForeMap(x+1, y)==0 ||
210 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ){
211 world.setForeMap(x+1,y,63);
212 updateBlock(x+1, y+1);
213 } else if (Items.isLava(world.getForeMap(x+1, y))) {
214 if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4);
215 else world.setForeMap(x+1, y, 68);
218 if (world.getForeMap(x-1, y)==0 ||
219 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ){
220 world.setForeMap(x-1,y,63);
221 updateBlock(x-1, y+1);
222 } else if (Items.isLava(world.getForeMap(x-1, y))) {
223 if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4);
224 else world.setForeMap(x-1, y, 68);
227 return;
229 if (world.getForeMap(x, y) == 63) {
230 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=61 && world.getForeMap(x, y+1)<=63) ||
231 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
232 world.setForeMap(x,y+1,60);
233 updateBlock(x, y+2);
234 } else if (Items.isLava(world.getForeMap(x, y+1))) {
235 if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4);
236 else world.setForeMap(x, y+1, 68);
238 return;
241 if (Items.isLava(world.getForeMap(x, y)) && world.getForeMap(x, y)!=9) {
242 if (world.getForeMap(x, y)==64) {
243 if (!Items.isLava(world.getForeMap(x, y - 1)))
244 world.setForeMap(x, y, world.getForeMap(x, y) + 1);
245 } else if ((!Items.isLava(world.getForeMap(x,y-1))) &&
246 (!Items.isLava(world.getForeMap(x-1,y)) ||
247 (Items.isLava(world.getForeMap(x,y)) && world.getForeMap(x-1, y)>=world.getForeMap(x, y))) &&
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 world.setForeMap(x, y, world.getForeMap(x, y)+1);
252 if (world.getForeMap(x, y)>67) world.setForeMap(x, y, 0);
255 if (world.getForeMap(x, y) == 9 || world.getForeMap(x, y) == 64) {
256 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) ||
257 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
258 world.setForeMap(x,y+1,64);
259 updateBlock(x, y+2);
260 } else if (Items.isWater(world.getForeMap(x, y+1))) {
261 world.setForeMap(x, y+1, 1);
262 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
263 if (world.getForeMap(x+1, y)==0 ||
264 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ||
265 (Items.isLava(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>65)) {
266 world.setForeMap(x+1,y,65);
267 updateBlock(x+1, y+1);
268 } else if (Items.isWater(world.getForeMap(x+1, y))) {
269 world.setForeMap(x+1, y, 1);
272 if (world.getForeMap(x-1, y)==0 ||
273 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ||
274 (Items.isLava(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>65)) {
275 world.setForeMap(x-1,y,65);
276 updateBlock(x-1, y+1);
277 } else if (Items.isWater(world.getForeMap(x-1, y))) {
278 world.setForeMap(x-1, y, 1);
281 return;
283 if (world.getForeMap(x, y) == 65) {
284 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) ||
285 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
286 world.setForeMap(x,y+1,64);
287 updateBlock(x, y+2);
288 } else if (Items.isWater(world.getForeMap(x, y+1))) {
289 world.setForeMap(x, y+1, 1);
290 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
291 if (world.getForeMap(x+1, y)==0 ||
292 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ||
293 (Items.isLava(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>66)){
294 world.setForeMap(x+1,y,66);
295 updateBlock(x+1, y+1);
296 } else if (Items.isWater(world.getForeMap(x+1, y))) {
297 world.setForeMap(x+1, y, 1);
300 if (world.getForeMap(x-1, y)==0 ||
301 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ||
302 (Items.isLava(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>66)){
303 world.setForeMap(x-1,y,66);
304 updateBlock(x-1, y+1);
305 } else if (Items.isWater(world.getForeMap(x-1, y))) {
306 world.setForeMap(x-1, y, 1);
309 return;
311 if (world.getForeMap(x, y) == 66) {
312 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) ||
313 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
314 world.setForeMap(x,y+1,64);
315 updateBlock(x, y+2);
316 } else if (Items.isWater(world.getForeMap(x, y+1))) {
317 world.setForeMap(x, y+1, 1);
318 } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
319 if (world.getForeMap(x+1, y)==0 ||
320 (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ){
321 world.setForeMap(x+1,y,67);
322 updateBlock(x+1, y+1);
323 } else if (Items.isWater(world.getForeMap(x+1, y))) {
324 world.setForeMap(x+1, y, 1);
327 if (world.getForeMap(x-1, y)==0 ||
328 (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ){
329 world.setForeMap(x-1,y,67);
330 updateBlock(x-1, y+1);
331 } else if (Items.isWater(world.getForeMap(x-1, y))) {
332 world.setForeMap(x-1, y, 1);
335 return;
337 if (world.getForeMap(x, y) == 67) {
338 if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) ||
339 (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) {
340 world.setForeMap(x,y+1,64);
341 updateBlock(x, y+2);
342 } else if (Items.isWater(world.getForeMap(x, y+1))) {
343 world.setForeMap(x, y+1, 1);
345 return;
349 private void updateBlock(int x, int y) {
350 if (world.getForeMap(x, y) == 10) {
351 if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x,y+1)).collision) {
352 world.setForeMap(x, y, 0);
353 mobs.add(new FallingSand(x*16, y*16));
354 updateBlock(x, y-1);
358 if (world.getForeMap(x, y) == 11) {
359 if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x,y+1)).collision) {
360 world.setForeMap(x, y, 0);
361 mobs.add(new FallingGravel(x*16, y*16));
362 updateBlock(x, y-1);
366 if (world.getForeMap(x, y) == 59) {
367 if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) {
368 world.setForeMap(x,y,0);
369 updateBlock(x, y-1);
373 if (world.getForeMap(x, y) == 2) {
374 if (world.getForeMap(x, y-1)>0 && (Items.BLOCKS.getValueAt(world.getForeMap(x, y-1)).collision ||
375 Items.isFluid(world.getForeMap(x, y-1)))) {
376 world.setForeMap(x, y, 3);
381 public void useItem(int x, int y, int id, boolean bg) {
382 if (id>0 && Items.ITEMS.get(id).getType()==0) {
383 if (!bg) world.placeToForeground(x, y, Items.ITEMS.get(id).getBlock());
384 else world.placeToBackground(x, y, Items.ITEMS.get(id).getBlock());
388 public void update(float delta) {
389 RUN_TIME += delta;
391 if (DO_UPD) {
392 for (int y=UPD_Y; y<UPD_Y+16; y++)
393 for (int x=UPD_X; x<UPD_X+16; x++) {
394 updateBlock(x, y);
396 DO_UPD = false;
399 for (int y=0; y<world.getHeight(); y++) {
400 for (int x=(int)renderer.camera.position.x/16-1; x<(int)(renderer.camera.position.x+renderer.camera.viewportWidth)/16+1; x++) {
401 updateFluids(x, y);
405 physics.update(delta);
406 moveCursor();
407 checkCursorBounds();
409 if (isTouchDown && touchDownButton==Input.Buttons.LEFT) {
410 if (world.getForeMap(cursorX, cursorY) > 0 &&
411 Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getHp()>=0){// || world.getBackMap(cursorX, cursorY) > 0) {
412 blockDmg++;
413 if (blockDmg>=Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getHp()) {
414 if (Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getDrop()>0)
415 drops.add(new Drop(cursorX*16+4, cursorY*16+4, Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getDrop()));
416 world.placeToForeground(cursorX, cursorY, 0);
417 blockDmg=0;
422 if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
423 if (touchDownButton== Input.Buttons.RIGHT) {
424 useItem(cursorX, cursorY, player.inventory[invSlot], true);
425 isTouchDown = false;
426 } else if (touchDownY< Assets.invBar.getRegionHeight() &&
427 touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
428 touchDownX<renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
429 CaveGame.STATE = AppState.GAME_CREATIVE_INV;
430 isTouchDown = false;