DEADSOFTWARE

Ciclic worlds
[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.Array;
6 import com.badlogic.gdx.utils.TimeUtils;
7 import ru.deadsoftware.cavecraft.*;
8 import ru.deadsoftware.cavecraft.game.mobs.Mob;
9 import ru.deadsoftware.cavecraft.game.mobs.Pig;
10 import ru.deadsoftware.cavecraft.game.objects.Player;
12 public class GameProc {
14 public static double RUN_TIME = 0;
16 public Player player;
18 public Array<Mob> mobs;
20 public GameWorld world;
21 public GameRenderer renderer;
22 public GamePhysics physics;
24 public int cursorX, cursorY;
25 public int invSlot;
26 public int ctrlMode;
28 public boolean isTouchDown, isKeyDown;
29 public int touchDownX, touchDownY, keyDownCode;
30 public int touchDownButton;
31 public long touchDownTime;
33 public GameProc() {
34 world = new GameWorld(1024,256);
35 renderer = new GameRenderer(this);
36 physics = new GamePhysics(this);
37 player = new Player(world.getSpawnPoint());
38 mobs = new Array<Mob>();
39 for (int i=0; i<world.getWidth(); i+=64) {
40 mobs.add(new Pig(i*16, 0, world));
41 }
42 if (!CaveGame.TOUCH) ctrlMode = 1;
43 }
45 public void resetRenderer() {
46 renderer = new GameRenderer(this);
47 }
49 private boolean isAutoselectable(int x, int y) {
50 return (world.getForeMap(x,y)>0 &&
51 Items.BLOCKS.getValueAt(world.getForeMap(x,y)).collision);
52 }
54 private void moveCursor() {
55 if (ctrlMode==0 && CaveGame.TOUCH) {
56 if (player.canJump) {
57 cursorX = (int) (player.position.x + player.texWidth / 2) / 16;
58 if (player.dir == 0) cursorX--;
59 else cursorX++;
60 cursorY = (int) (player.position.y + player.texWidth) / 16;
61 if (!isAutoselectable(cursorX, cursorY)) {
62 cursorY++;
63 }
64 if (!isAutoselectable(cursorX, cursorY)) {
65 cursorY++;
66 }
67 if (!isAutoselectable(cursorX, cursorY)) {
68 if (player.dir == 0) cursorX++;
69 else cursorX--;
70 }
71 } else {
72 cursorX = (int) (player.position.x + player.texWidth / 2) / 16;
73 cursorY = (int) (player.position.y + player.height+8)/16;
74 }
75 } else if (!CaveGame.TOUCH){
76 cursorX = (int)(Gdx.input.getX()*
77 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)/16;
78 cursorY = (int)(Gdx.input.getY()*
79 (renderer.camera.viewportHeight/GameScreen.getHeight())+renderer.camera.position.y)/16;
80 if ((Gdx.input.getX()*
81 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)<0)
82 cursorX--;
83 }
84 }
86 private void checkCursorBounds() {
87 if (cursorY < 0) cursorY = 0;
88 if (cursorY >= world.getHeight()) cursorY = world.getHeight()-1;
89 if (cursorX<(player.position.x+player.texWidth/2)/16)
90 player.dir=0;
91 if (cursorX>(player.position.x+player.texWidth/2)/16)
92 player.dir=1;
93 }
95 public void update(float delta) {
96 RUN_TIME += delta;
98 physics.update(delta);
99 moveCursor();
100 checkCursorBounds();
102 if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
103 if (touchDownButton== Input.Buttons.RIGHT) {
104 world.placeToBackground(cursorX, cursorY,
105 player.inventory[invSlot]);
106 } else if (touchDownButton==Input.Buttons.LEFT &&
107 touchDownY< Assets.invBar.getRegionHeight() &&
108 touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
109 touchDownX<renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
110 CaveGame.STATE = GameState.GAME_CREATIVE_INV;
112 isTouchDown = false;