X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=core%2Fsrc%2Fru%2Fdeadsoftware%2Fcavecraft%2Fgame%2FGameProc.java;h=25658c165421870f9d3868e08f16f4bd96a2e847;hb=0b8922bfbb2bbec067b532ecb7912f6afe02c4ef;hp=2cdc302291cac2ec1c3faf961c5a8285cd261b13;hpb=e0aa1520178df544807f3e803b4b6169346b07fb;p=cavedroid.git diff --git a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java index 2cdc302..25658c1 100644 --- a/core/src/ru/deadsoftware/cavecraft/game/GameProc.java +++ b/core/src/ru/deadsoftware/cavecraft/game/GameProc.java @@ -2,48 +2,78 @@ package ru.deadsoftware.cavecraft.game; import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Input; -import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.TimeUtils; -import ru.deadsoftware.cavecraft.*; +import ru.deadsoftware.cavecraft.CaveGame; +import ru.deadsoftware.cavecraft.GameScreen; +import ru.deadsoftware.cavecraft.game.mobs.FallingGravel; +import ru.deadsoftware.cavecraft.game.mobs.FallingSand; import ru.deadsoftware.cavecraft.game.mobs.Mob; import ru.deadsoftware.cavecraft.game.mobs.Pig; +import ru.deadsoftware.cavecraft.game.objects.Drop; import ru.deadsoftware.cavecraft.game.objects.Player; +import ru.deadsoftware.cavecraft.misc.AppState; +import ru.deadsoftware.cavecraft.misc.Assets; -public class GameProc { +import java.io.Serializable; +import java.util.ArrayList; + +public class GameProc implements Serializable{ public static double RUN_TIME = 0; + public static boolean DO_UPD = false; + public static int UPD_X = -1, UPD_Y = -1; + public Player player; - public Array mobs; + public ArrayList mobs; + public ArrayList drops; - public GameWorld world; - public GameRenderer renderer; - public GamePhysics physics; + public transient GameWorld world; + public transient GameRenderer renderer; + public transient GamePhysics physics; public int cursorX, cursorY; public int invSlot; public int ctrlMode; + public int creativeScroll, maxCreativeScroll; + public int blockDmg = 0; - public boolean isTouchDown, isKeyDown; + public boolean isTouchDown, isKeyDown, swim; public int touchDownX, touchDownY, keyDownCode; public int touchDownButton; public long touchDownTime; public GameProc() { - world = new GameWorld(1024,256); - renderer = new GameRenderer(this); - physics = new GamePhysics(this); + world = new GameWorld(); + world.generate(1024,256); player = new Player(world.getSpawnPoint()); - mobs = new Array(); - for (int i=0; i(); + mobs = new ArrayList(); + for (int i=0; i<16; i++) { + mobs.add(new Pig(i*256, 196*16)); + } + physics = new GamePhysics(this); + if (CaveGame.TOUCH) { + renderer = new GameRenderer(this,320, + 320*((float)GameScreen.getHeight()/GameScreen.getWidth())); + } else { + ctrlMode = 1; + renderer = new GameRenderer(this,480, + 480*((float)GameScreen.getHeight()/GameScreen.getWidth())); } - if (!CaveGame.TOUCH) ctrlMode = 1; + maxCreativeScroll = Items.ITEMS.size()/8; + GameSaver.save(this); } public void resetRenderer() { - renderer = new GameRenderer(this); + if (CaveGame.TOUCH) { + renderer = new GameRenderer(this,320, + 320*((float)GameScreen.getHeight()/GameScreen.getWidth())); + } else { + renderer = new GameRenderer(this,480, + 480*((float)GameScreen.getHeight()/GameScreen.getWidth())); + } } private boolean isAutoselectable(int x, int y) { @@ -52,6 +82,7 @@ public class GameProc { } private void moveCursor() { + int pastX = cursorX, pastY = cursorY; if (ctrlMode == 0 && CaveGame.TOUCH) { cursorX = (int) (player.position.x + player.texWidth / 2) / 16; if (player.dir == 0) cursorX--; @@ -76,6 +107,7 @@ public class GameProc { (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)<0) cursorX--; } + if (pastX!=cursorX || pastY!=cursorY) blockDmg = 0; } private void checkCursorBounds() { @@ -89,24 +121,314 @@ public class GameProc { } } + private void updateFluids(int x, int y) { + if (Items.isWater(world.getForeMap(x, y)) && world.getForeMap(x, y)!=8) { + if (world.getForeMap(x, y)==60) { + if (!Items.isWater(world.getForeMap(x, y - 1))) + world.setForeMap(x, y, world.getForeMap(x, y) + 1); + } else if ((!Items.isWater(world.getForeMap(x-1,y)) || + (Items.isWater(world.getForeMap(x,y)) && world.getForeMap(x-1, y)>=world.getForeMap(x, y))) && + (!Items.isWater(world.getForeMap(x+1,y)) || + (Items.isWater(world.getForeMap(x,y)) && world.getForeMap(x+1, y)>=world.getForeMap(x, y)))){ + world.setForeMap(x, y, world.getForeMap(x, y)+1); + } + if (world.getForeMap(x, y)>63) world.setForeMap(x, y, 0); + } + + if (world.getForeMap(x, y) == 8 || world.getForeMap(x, y) == 60) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=61 && world.getForeMap(x, y+1)<=63) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,60); + updateBlock(x, y+2); + } else if (Items.isLava(world.getForeMap(x, y+1))) { + if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4); + else world.setForeMap(x, y+1, 68); + } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) { + if (world.getForeMap(x+1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) || + (Items.isWater(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>61)) { + world.setForeMap(x+1,y,61); + updateBlock(x+1, y+1); + } else if (Items.isLava(world.getForeMap(x+1, y))) { + if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4); + else world.setForeMap(x+1, y, 68); + } 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); + + if (world.getForeMap(x-1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) || + (Items.isWater(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>61)) { + world.setForeMap(x-1,y,61); + updateBlock(x-1, y+1); + } else if (Items.isLava(world.getForeMap(x-1, y))) { + if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4); + else world.setForeMap(x-1, y, 68); + } 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); + } + return; + } + if (world.getForeMap(x, y) == 61) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=61 && world.getForeMap(x, y+1)<=63) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,60); + updateBlock(x, y+2); + } else if (Items.isLava(world.getForeMap(x, y+1))) { + if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4); + else world.setForeMap(x, y+1, 68); + } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) { + if (world.getForeMap(x+1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) || + (Items.isWater(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>62)){ + world.setForeMap(x+1,y,62); + updateBlock(x+1, y+1); + } else if (Items.isLava(world.getForeMap(x+1, y))) { + if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4); + else world.setForeMap(x+1, y, 68); + } + + if (world.getForeMap(x-1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) || + (Items.isWater(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>62)){ + world.setForeMap(x-1,y,62); + updateBlock(x-1, y+1); + } else if (Items.isLava(world.getForeMap(x-1, y))) { + if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4); + else world.setForeMap(x-1, y, 68); + } + } + return; + } + if (world.getForeMap(x, y) == 62) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=61 && world.getForeMap(x, y+1)<=63) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,60); + updateBlock(x, y+2); + } else if (Items.isLava(world.getForeMap(x, y+1))) { + if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4); + else world.setForeMap(x, y+1, 68); + } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) { + if (world.getForeMap(x+1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ){ + world.setForeMap(x+1,y,63); + updateBlock(x+1, y+1); + } else if (Items.isLava(world.getForeMap(x+1, y))) { + if (world.getForeMap(x+1, y)>9) world.setForeMap(x+1, y, 4); + else world.setForeMap(x+1, y, 68); + } + + if (world.getForeMap(x-1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ){ + world.setForeMap(x-1,y,63); + updateBlock(x-1, y+1); + } else if (Items.isLava(world.getForeMap(x-1, y))) { + if (world.getForeMap(x-1, y)>9) world.setForeMap(x-1, y, 4); + else world.setForeMap(x-1, y, 68); + } + } + return; + } + if (world.getForeMap(x, y) == 63) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=61 && world.getForeMap(x, y+1)<=63) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,60); + updateBlock(x, y+2); + } else if (Items.isLava(world.getForeMap(x, y+1))) { + if (world.getForeMap(x, y+1)>9) world.setForeMap(x, y+1, 4); + else world.setForeMap(x, y+1, 68); + } + return; + } + + if (Items.isLava(world.getForeMap(x, y)) && world.getForeMap(x, y)!=9) { + if (world.getForeMap(x, y)==64) { + if (!Items.isLava(world.getForeMap(x, y - 1))) + world.setForeMap(x, y, world.getForeMap(x, y) + 1); + } else if ((!Items.isLava(world.getForeMap(x,y-1))) && + (!Items.isLava(world.getForeMap(x-1,y)) || + (Items.isLava(world.getForeMap(x,y)) && world.getForeMap(x-1, y)>=world.getForeMap(x, y))) && + (!Items.isLava(world.getForeMap(x+1,y)) || + (Items.isLava(world.getForeMap(x,y)) && world.getForeMap(x+1, y)>=world.getForeMap(x, y)))){ + world.setForeMap(x, y, world.getForeMap(x, y)+1); + } + if (world.getForeMap(x, y)>67) world.setForeMap(x, y, 0); + } + + if (world.getForeMap(x, y) == 9 || world.getForeMap(x, y) == 64) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,64); + updateBlock(x, y+2); + } else if (Items.isWater(world.getForeMap(x, y+1))) { + world.setForeMap(x, y+1, 1); + } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) { + if (world.getForeMap(x+1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) || + (Items.isLava(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>65)) { + world.setForeMap(x+1,y,65); + updateBlock(x+1, y+1); + } else if (Items.isWater(world.getForeMap(x+1, y))) { + world.setForeMap(x+1, y, 1); + } + + if (world.getForeMap(x-1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) || + (Items.isLava(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>65)) { + world.setForeMap(x-1,y,65); + updateBlock(x-1, y+1); + } else if (Items.isWater(world.getForeMap(x-1, y))) { + world.setForeMap(x-1, y, 1); + } + } + return; + } + if (world.getForeMap(x, y) == 65) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,64); + updateBlock(x, y+2); + } else if (Items.isWater(world.getForeMap(x, y+1))) { + world.setForeMap(x, y+1, 1); + } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) { + if (world.getForeMap(x+1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) || + (Items.isLava(world.getForeMap(x+1, y)) && world.getForeMap(x+1, y)>66)){ + world.setForeMap(x+1,y,66); + updateBlock(x+1, y+1); + } else if (Items.isWater(world.getForeMap(x+1, y))) { + world.setForeMap(x+1, y, 1); + } + + if (world.getForeMap(x-1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) || + (Items.isLava(world.getForeMap(x-1, y)) && world.getForeMap(x-1, y)>66)){ + world.setForeMap(x-1,y,66); + updateBlock(x-1, y+1); + } else if (Items.isWater(world.getForeMap(x-1, y))) { + world.setForeMap(x-1, y, 1); + } + } + return; + } + if (world.getForeMap(x, y) == 66) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,64); + updateBlock(x, y+2); + } else if (Items.isWater(world.getForeMap(x, y+1))) { + world.setForeMap(x, y+1, 1); + } else if (Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) { + if (world.getForeMap(x+1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x+1, y)).collision && !Items.isFluid(world.getForeMap(x+1, y))) ){ + world.setForeMap(x+1,y,67); + updateBlock(x+1, y+1); + } else if (Items.isWater(world.getForeMap(x+1, y))) { + world.setForeMap(x+1, y, 1); + } + + if (world.getForeMap(x-1, y)==0 || + (!Items.BLOCKS.getValueAt(world.getForeMap(x-1, y)).collision && !Items.isFluid(world.getForeMap(x-1, y))) ){ + world.setForeMap(x-1,y,67); + updateBlock(x-1, y+1); + } else if (Items.isWater(world.getForeMap(x-1, y))) { + world.setForeMap(x-1, y, 1); + } + } + return; + } + if (world.getForeMap(x, y) == 67) { + if (world.getForeMap(x, y+1)==0 || (world.getForeMap(x, y+1)>=65 && world.getForeMap(x, y+1)<=67) || + (!Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision && !Items.isFluid(world.getForeMap(x, y+1)))) { + world.setForeMap(x,y+1,64); + updateBlock(x, y+2); + } else if (Items.isWater(world.getForeMap(x, y+1))) { + world.setForeMap(x, y+1, 1); + } + return; + } + } + + private void updateBlock(int x, int y) { + if (world.getForeMap(x, y) == 10) { + if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x,y+1)).collision) { + world.setForeMap(x, y, 0); + mobs.add(new FallingSand(x*16, y*16)); + updateBlock(x, y-1); + } + } + + if (world.getForeMap(x, y) == 11) { + if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x,y+1)).collision) { + world.setForeMap(x, y, 0); + mobs.add(new FallingGravel(x*16, y*16)); + updateBlock(x, y-1); + } + } + + if (world.getForeMap(x, y) == 59) { + if (world.getForeMap(x, y+1)==0 || !Items.BLOCKS.getValueAt(world.getForeMap(x, y+1)).collision) { + world.setForeMap(x,y,0); + updateBlock(x, y-1); + } + } + + if (world.getForeMap(x, y) == 2) { + if (world.getForeMap(x, y-1)>0 && (Items.BLOCKS.getValueAt(world.getForeMap(x, y-1)).collision || + Items.isFluid(world.getForeMap(x, y-1)))) { + world.setForeMap(x, y, 3); + } + } + } + + public void useItem(int x, int y, int id, boolean bg) { + if (id>0 && Items.ITEMS.get(id).getType()==0) { + if (!bg) world.placeToForeground(x, y, Items.ITEMS.get(id).getBlock()); + else world.placeToBackground(x, y, Items.ITEMS.get(id).getBlock()); + } + } + public void update(float delta) { RUN_TIME += delta; + if (DO_UPD) { + for (int y=UPD_Y; y 0 && + Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getHp()>=0){// || world.getBackMap(cursorX, cursorY) > 0) { + blockDmg++; + if (blockDmg>=Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getHp()) { + if (Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getDrop()>0) + drops.add(new Drop(cursorX*16+4, cursorY*16+4, Items.BLOCKS.getValueAt(world.getForeMap(cursorX, cursorY)).getDrop())); + world.placeToForeground(cursorX, cursorY, 0); + blockDmg=0; + } + } + } + if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) { if (touchDownButton== Input.Buttons.RIGHT) { - world.placeToBackground(cursorX, cursorY, - player.inventory[invSlot]); - } else if (touchDownButton==Input.Buttons.LEFT && - touchDownY< Assets.invBar.getRegionHeight() && + useItem(cursorX, cursorY, player.inventory[invSlot], true); + isTouchDown = false; + } else if (touchDownY< Assets.invBar.getRegionHeight() && touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 && touchDownX