DEADSOFTWARE

4b841bd47af0fd7103b3ac70b50f9cd73e67adc4
[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.Mob;
9 import ru.deadsoftware.cavecraft.game.mobs.Pig;
10 import ru.deadsoftware.cavecraft.game.objects.Player;
11 import ru.deadsoftware.cavecraft.misc.AppState;
12 import ru.deadsoftware.cavecraft.misc.Assets;
14 import java.io.Serializable;
15 import java.util.ArrayList;
17 public class GameProc implements Serializable{
19 public static double RUN_TIME = 0;
21 public Player player;
23 public ArrayList<Mob> mobs;
25 public transient GameWorld world;
26 public transient GameRenderer renderer;
27 public transient GamePhysics physics;
29 public int cursorX, cursorY;
30 public int invSlot;
31 public int ctrlMode;
32 public int creativeScroll, maxCreativeScroll;
34 public boolean isTouchDown, isKeyDown;
35 public int touchDownX, touchDownY, keyDownCode;
36 public int touchDownButton;
37 public long touchDownTime;
39 public GameProc() {
40 world = new GameWorld();
41 world.generate(1024,256);
42 player = new Player(world.getSpawnPoint());
43 mobs = new ArrayList<Mob>();
44 for (int i=0; i<16; i++) {
45 mobs.add(new Pig(i*256, 196*16));
46 }
47 physics = new GamePhysics(this);
48 if (CaveGame.TOUCH) {
49 renderer = new GameRenderer(this,320,
50 320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
51 } else {
52 ctrlMode = 1;
53 renderer = new GameRenderer(this,480,
54 480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
55 }
56 maxCreativeScroll = Items.BLOCKS.size/8;
57 GameSaver.save(this);
58 }
60 public void resetRenderer() {
61 if (CaveGame.TOUCH) {
62 renderer = new GameRenderer(this,320,
63 320*((float)GameScreen.getHeight()/GameScreen.getWidth()));
64 } else {
65 renderer = new GameRenderer(this,480,
66 480*((float)GameScreen.getHeight()/GameScreen.getWidth()));
67 }
68 }
70 private boolean isAutoselectable(int x, int y) {
71 return (world.getForeMap(x,y)>0 &&
72 Items.BLOCKS.getValueAt(world.getForeMap(x,y)).collision);
73 }
75 private void moveCursor() {
76 if (ctrlMode == 0 && CaveGame.TOUCH) {
77 cursorX = (int) (player.position.x + player.texWidth / 2) / 16;
78 if (player.dir == 0) cursorX--;
79 else cursorX++;
80 cursorY = (int) (player.position.y + player.texWidth) / 16;
81 if (!isAutoselectable(cursorX, cursorY)) {
82 cursorY++;
83 }
84 if (!isAutoselectable(cursorX, cursorY)) {
85 cursorY++;
86 }
87 if (!isAutoselectable(cursorX, cursorY)) {
88 if (player.dir == 0) cursorX++;
89 else cursorX--;
90 }
91 } else if (!CaveGame.TOUCH){
92 cursorX = (int)(Gdx.input.getX()*
93 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)/16;
94 cursorY = (int)(Gdx.input.getY()*
95 (renderer.camera.viewportHeight/GameScreen.getHeight())+renderer.camera.position.y)/16;
96 if ((Gdx.input.getX()*
97 (renderer.camera.viewportWidth/GameScreen.getWidth())+renderer.camera.position.x)<0)
98 cursorX--;
99 }
102 private void checkCursorBounds() {
103 if (cursorY < 0) cursorY = 0;
104 if (cursorY >= world.getHeight()) cursorY = world.getHeight()-1;
105 if (ctrlMode==1) {
106 if (cursorX*16+8<player.position.x+player.texWidth/2)
107 player.dir=0;
108 if (cursorX*16+8>player.position.x+player.texWidth/2)
109 player.dir=1;
113 public void update(float delta) {
114 RUN_TIME += delta;
116 physics.update(delta);
117 moveCursor();
118 checkCursorBounds();
120 if (isTouchDown && TimeUtils.timeSinceMillis(touchDownTime) > 500) {
121 if (touchDownButton== Input.Buttons.RIGHT) {
122 world.placeToBackground(cursorX, cursorY,
123 player.inventory[invSlot]);
124 } else if (touchDownY< Assets.invBar.getRegionHeight() &&
125 touchDownX>renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
126 touchDownX<renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
127 CaveGame.STATE = AppState.GAME_CREATIVE_INV;
129 isTouchDown = false;