DEADSOFTWARE

Rename position -> pos
[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) {
77 gp.player.flyMode = true;
78 gp.player.move.y = 0;
79 } else {
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) CaveGame.STATE = AppState.GAME_CREATIVE_INV;
90 else CaveGame.STATE = AppState.GAME_PLAY;
91 break;
93 case Input.Keys.G:
94 gp.mobs.add(new Pig(gp.curX * 16, gp.curY * 16));
95 break;
97 case Input.Keys.ESCAPE:
98 case Input.Keys.BACK:
99 CaveGame.STATE = AppState.GOTO_MENU;
100 break;
102 case Input.Keys.F1:
103 GameScreen.SHOW_DEBUG = !GameScreen.SHOW_DEBUG;
104 break;
108 public void keyUp(int keycode) {
109 switch (keycode) {
110 case Input.Keys.A:
111 case Input.Keys.D:
112 gp.player.move.x = 0;
113 if (CaveGame.TOUCH && gp.swim) gp.swim = false;
114 break;
116 case Input.Keys.SPACE:
117 case Input.Keys.CONTROL_LEFT:
118 if (gp.player.flyMode) gp.player.move.y = 0;
119 if (gp.swim) gp.swim = false;
120 break;
124 public void mouseMoved(int screenX, int screenY) {
127 public void touchDown(int screenX, int screenY, int button) {
128 gp.touchDownTime = TimeUtils.millis();
129 gp.isTouchDown = true;
130 gp.touchDownBtn = button;
131 gp.touchDownX = screenX;
132 gp.touchDownY = screenY;
135 public void touchUp(int screenX, int screenY, int button) {
136 if (CaveGame.TOUCH && gp.isKeyDown) {
137 keyUp(gp.keyDownCode);
138 gp.isKeyDown = false;
140 if (gp.isTouchDown) {
141 if (CaveGame.STATE == AppState.GAME_CREATIVE_INV &&
142 screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 &&
143 screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 &&
144 screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 &&
145 screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) {
146 int ix = (int) (screenX - (gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18;
147 int iy = (int) (screenY - (gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18;
148 int item = gp.creativeScroll * 8 + (ix + iy * 8);
149 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) item = -1;
150 if (item >= 0 && item < GameItems.getItemsSize()) {
151 for (int i = 8; i > 0; i--) {
152 gp.player.inv[i] = gp.player.inv[i - 1];
154 gp.player.inv[0] = item;
156 } else if (CaveGame.STATE == AppState.GAME_CREATIVE_INV) {
157 CaveGame.STATE = AppState.GAME_PLAY;
158 } else if (screenY < Assets.invBar.getRegionHeight() &&
159 screenX > gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2 &&
160 screenX < gp.renderer.getWidth() / 2 + Assets.invBar.getRegionWidth() / 2) {
161 gp.slot = (int) ((screenX - (gp.renderer.getWidth() / 2 - Assets.invBar.getRegionWidth() / 2)) / 20);
162 } else if (button == Input.Buttons.RIGHT) {
163 gp.useItem(gp.curX, gp.curY,
164 gp.player.inv[gp.slot], false);
165 } else if (button == Input.Buttons.LEFT) {
166 gp.blockDmg = 0;
169 gp.isTouchDown = false;
172 public void touchDragged(int screenX, int screenY) {
173 if (CaveGame.STATE == AppState.GAME_CREATIVE_INV && Math.abs(screenY - gp.touchDownY) > 16) {
174 if (screenX > gp.renderer.getWidth() / 2 - Assets.creativeInv.getRegionWidth() / 2 &&
175 screenX < gp.renderer.getWidth() / 2 + Assets.creativeInv.getRegionWidth() / 2 &&
176 screenY > gp.renderer.getHeight() / 2 - Assets.creativeInv.getRegionHeight() / 2 &&
177 screenY < gp.renderer.getHeight() / 2 + Assets.creativeInv.getRegionHeight() / 2) {
178 gp.creativeScroll -= (screenY - gp.touchDownY) / 16;
179 gp.touchDownX = screenX;
180 gp.touchDownY = screenY;
181 if (gp.creativeScroll < 0) gp.creativeScroll = 0;
182 if (gp.creativeScroll > gp.maxCreativeScroll)
183 gp.creativeScroll = gp.maxCreativeScroll;
188 public void scrolled(int amount) {
189 switch (CaveGame.STATE) {
190 case GAME_PLAY:
191 gp.slot += amount;
192 if (gp.slot < 0) gp.slot = 8;
193 if (gp.slot > 8) gp.slot = 0;
194 break;
195 case GAME_CREATIVE_INV:
196 gp.creativeScroll += amount;
197 if (gp.creativeScroll < 0) gp.creativeScroll = 0;
198 if (gp.creativeScroll > gp.maxCreativeScroll)
199 gp.creativeScroll = gp.maxCreativeScroll;
200 break;