DEADSOFTWARE

687c6a9e5e6d919f96ab41b7dc0ee5ffbfd1bd06
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameInput.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Input;
4 import com.badlogic.gdx.utils.TimeUtils;
5 import ru.deadsoftware.cavecraft.CaveGame;
6 import ru.deadsoftware.cavecraft.GameScreen;
7 import ru.deadsoftware.cavecraft.game.mobs.Pig;
8 import ru.deadsoftware.cavecraft.misc.AppState;
9 import ru.deadsoftware.cavecraft.misc.Assets;
11 public class GameInput {
13 private GameProc gp;
15 public GameInput(GameProc gp) {
16 this.gp = gp;
17 }
19 private boolean checkSwim() {
20 return (GameItems.isFluid(gp.world.getForeMap((int) (gp.player.pos.x + gp.player.width / 2) / 16,
21 (int) (gp.player.pos.y + gp.player.height / 4 * 3) / 16)));
22 }
24 private void wasdPressed(int keycode) {
25 if (gp.ctrlMode == 0 || !CaveGame.TOUCH) {
26 switch (keycode) {
27 case Input.Keys.A:
28 gp.player.move.x = -GamePhysics.PL_SPEED;
29 gp.player.dir = 0;
30 if (CaveGame.TOUCH && checkSwim()) gp.swim = true;
31 break;
32 case Input.Keys.D:
33 gp.player.move.x = GamePhysics.PL_SPEED;
34 gp.player.dir = 1;
35 if (CaveGame.TOUCH && checkSwim()) gp.swim = true;
36 break;
37 }
38 } else if (CaveGame.TOUCH) {
39 switch (keycode) {
40 case Input.Keys.A:
41 gp.curX--;
42 break;
43 case Input.Keys.D:
44 gp.curX++;
45 break;
46 case Input.Keys.W:
47 gp.curY--;
48 break;
49 case Input.Keys.S:
50 gp.curY++;
51 break;
52 }
53 gp.blockDmg = 0;
54 }
55 }
57 public void keyDown(int keycode) {
58 gp.isKeyDown = true;
59 gp.keyDownCode = keycode;
60 if (keycode == Input.Keys.W || keycode == Input.Keys.A ||
61 keycode == Input.Keys.S || keycode == Input.Keys.D) {
62 wasdPressed(keycode);
63 } else switch (keycode) {
64 case Input.Keys.ALT_LEFT:
65 if (CaveGame.TOUCH) {
66 gp.ctrlMode++;
67 if (gp.ctrlMode > 1) gp.ctrlMode = 0;
68 }
69 break;
71 case Input.Keys.SPACE:
72 if (checkSwim()) {
73 gp.swim = true;
74 } else if (gp.player.canJump) {
75 gp.player.move.add(0, -7);
76 } else if (!gp.player.flyMode && gp.player.gameMode == 1) {
77 gp.player.flyMode = true;
78 gp.player.move.y = 0;
79 } else if (gp.player.flyMode) {
80 gp.player.move.y = -GamePhysics.PL_SPEED;
81 }
82 break;
84 case Input.Keys.CONTROL_LEFT:
85 gp.player.move.y = GamePhysics.PL_SPEED;
86 break;
88 case Input.Keys.E:
89 if (CaveGame.STATE == AppState.GAME_PLAY) switch (gp.player.gameMode) {
90 case 0:
91 //TODO survival inv
92 break;
93 case 1:
94 CaveGame.STATE = AppState.GAME_CREATIVE_INV;
95 break;
96 }
97 else CaveGame.STATE = AppState.GAME_PLAY;
98 break;
100 case Input.Keys.G:
101 gp.mobs.add(new Pig(gp.curX * 16, gp.curY * 16));
102 break;
104 case Input.Keys.ESCAPE:
105 case Input.Keys.BACK:
106 CaveGame.STATE = AppState.GOTO_MENU;
107 break;
109 case Input.Keys.F1:
110 GameScreen.SHOW_DEBUG = !GameScreen.SHOW_DEBUG;
111 break;
115 public void keyUp(int keycode) {
116 switch (keycode) {
117 case Input.Keys.A:
118 case Input.Keys.D:
119 gp.player.move.x = 0;
120 if (CaveGame.TOUCH && gp.swim) gp.swim = false;
121 break;
123 case Input.Keys.SPACE:
124 case Input.Keys.CONTROL_LEFT:
125 if (gp.player.flyMode) gp.player.move.y = 0;
126 if (gp.swim) gp.swim = false;
127 break;
131 public void mouseMoved(int screenX, int screenY) {
134 public void touchDown(int screenX, int screenY, int button) {
135 gp.touchDownTime = TimeUtils.millis();
136 gp.isTouchDown = true;
137 gp.touchDownBtn = button;
138 gp.touchDownX = screenX;
139 gp.touchDownY = screenY;
142 public void touchUp(int screenX, int screenY, int button) {
143 if (CaveGame.TOUCH && gp.isKeyDown) {
144 keyUp(gp.keyDownCode);
145 gp.isKeyDown = false;
147 if (gp.isTouchDown) {
148 if (CaveGame.STATE == AppState.GAME_CREATIVE_INV &&
149 screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 &&
150 screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 &&
151 screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 &&
152 screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) {
153 int ix = (int) (screenX - (gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18;
154 int iy = (int) (screenY - (gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18;
155 int item = gp.creativeScroll * 8 + (ix + iy * 8);
156 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) item = -1;
157 if (item >= 0 && item < GameItems.getItemsSize()) {
158 for (int i = 8; i > 0; i--) {
159 gp.player.inv[i] = gp.player.inv[i - 1];
161 gp.player.inv[0] = item;
163 } else if (CaveGame.STATE == AppState.GAME_CREATIVE_INV) {
164 CaveGame.STATE = AppState.GAME_PLAY;
165 } else if (screenY < Assets.invBar.getRegionHeight() &&
166 screenX > gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 &&
167 screenX < gp.renderer.getWidth() / 2 + Assets.invBar.getRegionWidth() / 2) {
168 gp.slot = (int) ((screenX - (gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20);
169 } else if (button == Input.Buttons.RIGHT) {
170 gp.useItem(gp.curX, gp.curY,
171 gp.player.inv[gp.slot], false);
172 } else if (button == Input.Buttons.LEFT) {
173 gp.blockDmg = 0;
176 gp.isTouchDown = false;
179 public void touchDragged(int screenX, int screenY) {
180 if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && Math.abs(screenY - gp.touchDownY) > 16) {
181 if (screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 &&
182 screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 &&
183 screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 &&
184 screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) {
185 gp.creativeScroll -= (screenY - gp.touchDownY) / 16;
186 gp.touchDownX = screenX;
187 gp.touchDownY = screenY;
188 if (gp.creativeScroll < 0) gp.creativeScroll = 0;
189 if (gp.creativeScroll > gp.maxCreativeScroll)
190 gp.creativeScroll = gp.maxCreativeScroll;
195 public void scrolled(int amount) {
196 switch (CaveGame.STATE) {
197 case GAME_PLAY:
198 gp.slot += amount;
199 if (gp.slot < 0) gp.slot = 8;
200 if (gp.slot > 8) gp.slot = 0;
201 break;
202 case GAME_CREATIVE_INV:
203 gp.creativeScroll += amount;
204 if (gp.creativeScroll < 0) gp.creativeScroll = 0;
205 if (gp.creativeScroll > gp.maxCreativeScroll)
206 gp.creativeScroll = gp.maxCreativeScroll;
207 break;