DEADSOFTWARE

Update version
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameInput.java
1 package ru.deadsoftware.cavedroid.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.Input;
5 import com.badlogic.gdx.graphics.g2d.TextureRegion;
6 import com.badlogic.gdx.utils.TimeUtils;
7 import com.google.common.collect.Range;
8 import ru.deadsoftware.cavedroid.MainConfig;
9 import ru.deadsoftware.cavedroid.game.mobs.Mob;
10 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
11 import ru.deadsoftware.cavedroid.game.mobs.Pig;
12 import ru.deadsoftware.cavedroid.game.mobs.Player;
13 import ru.deadsoftware.cavedroid.game.objects.DropController;
14 import ru.deadsoftware.cavedroid.misc.Assets;
15 import ru.deadsoftware.cavedroid.misc.ControlMode;
17 import javax.inject.Inject;
19 import static ru.deadsoftware.cavedroid.game.GameItems.*;
21 @GameScope
22 public class GameInput {
24 private final MainConfig mMainConfig;
25 private final GameWorld mGameWorld;
26 private final DropController mDropController;
27 private final MobsController mMobsController;
28 private final Player mPlayer;
30 private ControlMode mControlMode;
32 private boolean mKeyDown;
33 private boolean mTouchedDown;
34 private boolean mDragging;
36 private int mKeyDownCode;
37 private int mTouchDownBtn;
38 private float mTouchDownX;
39 private float mTouchDownY;
40 private long mTouchDownTime;
42 private int mCurX;
43 private int mCurY;
44 private int mCreativeScroll;
45 private int mBlockDamage;
47 @Inject
48 public GameInput(MainConfig mainConfig,
49 GameWorld gameWorld,
50 DropController dropController,
51 MobsController mobsController) {
52 mMainConfig = mainConfig;
53 mGameWorld = gameWorld;
54 mDropController = dropController;
55 mMobsController = mobsController;
57 mPlayer = mMobsController.getPlayer();
59 mControlMode = mMainConfig.isTouch() ? ControlMode.WALK : ControlMode.CURSOR;
60 }
62 private boolean checkSwim() {
63 return GameItems.isFluid(mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()));
64 }
66 private void goUpwards() {
67 if (checkSwim()) {
68 mPlayer.swim = true;
69 } else if (mPlayer.canJump()) {
70 mPlayer.getMove().add(0, -7);
71 } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) {
72 mPlayer.setFlyMode(true);
73 mPlayer.getMove().y = 0;
74 } else if (mPlayer.isFlyMode()) {
75 mPlayer.getMove().y = -GamePhysics.PL_SPEED;
76 }
77 }
79 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
80 private boolean insideCreativeInv(float screenX, float screenY) {
81 TextureRegion creative = Assets.textureRegions.get("creative");
82 return (screenX > mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 &&
83 screenX < mMainConfig.getWidth() / 2 + creative.getRegionWidth() / 2 &&
84 screenY > mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 &&
85 screenY < mMainConfig.getHeight() / 2 + creative.getRegionHeight() / 2);
86 }
88 private void wasdPressed(int keycode) {
89 if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) {
90 switch (keycode) {
91 case Input.Keys.A:
92 mPlayer.getMove().x = -GamePhysics.PL_SPEED;
93 mPlayer.setDir(Mob.Direction.LEFT);
94 if (mMainConfig.isTouch() && checkSwim()) {
95 mPlayer.swim = true;
96 }
97 break;
98 case Input.Keys.D:
99 mPlayer.getMove().x = GamePhysics.PL_SPEED;
100 mPlayer.setDir(Mob.Direction.RIGHT);
101 if (mMainConfig.isTouch() && checkSwim()) {
102 mPlayer.swim = true;
104 break;
105 case Input.Keys.W:
106 case Input.Keys.SPACE:
107 goUpwards();
108 break;
109 case Input.Keys.S:
110 case Input.Keys.CONTROL_LEFT:
111 mPlayer.getMove().y = GamePhysics.PL_SPEED;
112 break;
114 } else {
115 switch (keycode) {
116 case Input.Keys.A:
117 mCurX--;
118 break;
119 case Input.Keys.D:
120 mCurX++;
121 break;
122 case Input.Keys.W:
123 mCurY--;
124 break;
125 case Input.Keys.S:
126 mCurY++;
127 break;
129 mBlockDamage = 0;
133 private boolean isNotAutoselectable(int x, int y) {
134 return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMapBlock(x, y).hasCollision());
137 private void checkCursorBounds() {
138 if (mCurY < 0) {
139 mCurY = 0;
140 } else if (mCurY >= mGameWorld.getHeight()) {
141 mCurY = mGameWorld.getHeight() - 1;
144 if (mControlMode == ControlMode.CURSOR) {
145 if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) {
146 mPlayer.setDir(Mob.Direction.LEFT);
147 } else {
148 mPlayer.setDir(Mob.Direction.RIGHT);
153 public void moveCursor(GameRenderer gameRenderer) {
154 int pastX = mCurX;
155 int pastY = mCurY;
157 if (mControlMode == ControlMode.WALK && mMainConfig.isTouch()) {
158 mCurX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1);
159 mCurY = mPlayer.getUpperMapY();
160 for (int i = 0; i < 2 && isNotAutoselectable(mCurX, mCurY); i++) {
161 mCurY++;
163 if (isNotAutoselectable(mCurX, mCurY)) {
164 mCurX += mPlayer.looksLeft() ? 1 : -1;
166 } else if (!mMainConfig.isTouch()) {
167 mCurX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() /
168 Gdx.graphics.getWidth()) + gameRenderer.getCamX()) / 16;
170 mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
171 Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16;
172 if (mCurX < 0) {
173 mCurX--;
177 if (pastX != mCurX || pastY != mCurY) {
178 mBlockDamage = 0;
181 checkCursorBounds();
184 private void useItem(int x, int y, int id, boolean bg) {
185 String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id);
186 if (id > 0) {
187 if (getItem(id).isBlock()) {
188 if (!bg) {
189 mGameWorld.placeToForeground(x, y, getBlockIdByItemId(id));
190 } else {
191 mGameWorld.placeToBackground(x, y, getBlockIdByItemId(id));
193 } else {
194 switch (key) {
195 case "bucket_water":
196 mGameWorld.placeToForeground(x, y, getBlockId("water"));
197 mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
198 break;
199 case "bucket_lava":
200 mGameWorld.placeToForeground(x, y, getBlockId("lava"));
201 mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
202 break;
208 private void pressLMB() {
209 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE) &&
210 ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMapBlock(mCurX, mCurY).getHp() >= 0) ||
211 (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
212 mGameWorld.getBackMapBlock(mCurX, mCurY).getHp() >= 0))) {
213 if (mPlayer.gameMode == 0) {
214 mBlockDamage++;
215 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
216 if (mBlockDamage >= mGameWorld.getForeMapBlock(mCurX, mCurY).getHp()) {
217 mGameWorld.destroyForeMap(mCurX, mCurY);
218 mBlockDamage = 0;
220 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
221 if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) {
222 mGameWorld.destroyBackMap(mCurX, mCurY);
223 mBlockDamage = 0;
226 } else {
227 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
228 mGameWorld.placeToForeground(mCurX, mCurY, 0);
229 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
230 mGameWorld.placeToBackground(mCurX, mCurY, 0);
232 mTouchedDown = false;
237 private boolean insideHotbar(float x, float y) {
238 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
239 return y < hotbar.getRegionHeight() &&
240 Range.open(mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
241 mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
244 private void holdMB() {
245 if (mTouchDownBtn == Input.Buttons.RIGHT) {
246 useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true);
247 mTouchedDown = false;
248 } else {
249 if (insideHotbar(mTouchDownX, mTouchDownY)) {
250 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
251 mTouchedDown = false;
256 public void keyDown(int keycode) {
257 mKeyDown = true;
258 mKeyDownCode = keycode;
259 switch (keycode) {
260 case Input.Keys.A:
261 case Input.Keys.D:
262 case Input.Keys.W:
263 case Input.Keys.S:
264 case Input.Keys.SPACE:
265 case Input.Keys.CONTROL_LEFT:
266 wasdPressed(keycode);
267 break;
269 case Input.Keys.ALT_LEFT:
270 if (mMainConfig.isTouch()) {
271 mControlMode = mControlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
273 break;
275 case Input.Keys.E:
276 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
277 switch (mPlayer.gameMode) {
278 case 0:
279 //TODO survival inv
280 break;
281 case 1:
282 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
283 break;
285 } else {
286 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
288 break;
290 case Input.Keys.G:
291 mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16);
292 break;
294 case Input.Keys.Q:
295 mGameWorld.placeToForeground(mCurX, mCurY, 8);
296 break;
298 case Input.Keys.ESCAPE:
299 case Input.Keys.BACK:
300 GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld);
301 mMainConfig.getCaveGame().quitGame();
302 break;
304 case Input.Keys.F1:
305 mMainConfig.setShowInfo(!mMainConfig.isShowInfo());
306 break;
308 case Input.Keys.M:
309 mMainConfig.setShowMap(!mMainConfig.isShowMap());
310 break;
314 public void keyUp(int keycode) {
315 switch (keycode) {
316 case Input.Keys.A:
317 case Input.Keys.D:
318 mPlayer.getMove().x = 0;
319 if (mMainConfig.isTouch() && mPlayer.swim) {
320 mPlayer.swim = false;
322 break;
324 case Input.Keys.W:
325 case Input.Keys.S:
326 case Input.Keys.SPACE:
327 case Input.Keys.CONTROL_LEFT:
328 if (mPlayer.isFlyMode()) {
329 mPlayer.getMove().y = 0;
331 if (mPlayer.swim) {
332 mPlayer.swim = false;
334 break;
338 public void touchDown(float touchX, float touchY, int button) {
339 mTouchDownTime = TimeUtils.millis();
340 mTouchedDown = true;
341 mTouchDownBtn = button;
342 mTouchDownX = touchX;
343 mTouchDownY = touchY;
346 public void touchUp(float screenX, float screenY, int button) {
347 if (mDragging) {
348 mDragging = false;
349 return;
352 if (mMainConfig.isTouch() && mKeyDown) {
353 keyUp(mKeyDownCode);
354 mKeyDown = false;
356 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
357 TextureRegion creative = Assets.textureRegions.get("creative");
358 if (mTouchedDown) {
359 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && insideCreativeInv(screenX, screenY)) {
360 int ix = (int) (screenX - (mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
361 int iy = (int) (screenY - (mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
362 int item = mCreativeScroll * 8 + (ix + iy * 8);
363 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) {
364 item = -1;
366 if (item >= 0 && item < GameItems.getItemsSize()) {
367 System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8);
368 mPlayer.inventory[0] = item;
370 } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
371 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
372 } else if (screenY < hotbar.getRegionHeight() &&
373 screenX > mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
374 screenX < mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) {
375 mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
376 } else if (button == Input.Buttons.RIGHT) {
377 useItem(mCurX, mCurY,
378 mPlayer.inventory[mPlayer.slot], false);
379 } else if (button == Input.Buttons.LEFT) {
380 mBlockDamage = 0;
383 mTouchedDown = false;
386 public void touchDragged(float screenX, float screenY) {
387 mDragging = true;
388 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) {
389 if (insideCreativeInv(screenX, screenY)) {
390 mCreativeScroll -= (screenY - mTouchDownY) / 16;
391 mTouchDownX = screenX;
392 mTouchDownY = screenY;
393 if (mCreativeScroll < 0) {
394 mCreativeScroll = 0;
396 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
397 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
403 public void scrolled(float amountX, float amountY) {
404 switch (mMainConfig.getGameUiWindow()) {
405 case NONE:
406 mPlayer.slot += (int) amountY;
407 if (mPlayer.slot < 0) {
408 mPlayer.slot = 8;
410 if (mPlayer.slot > 8) {
411 mPlayer.slot = 0;
413 break;
414 case CREATIVE_INVENTORY:
415 mCreativeScroll += (int) amountY;
416 if (mCreativeScroll < 0) {
417 mCreativeScroll = 0;
419 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
420 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
422 break;
426 public int getKeyDownCode() {
427 return mKeyDownCode;
430 public boolean isKeyDown() {
431 return mKeyDown;
434 int getBlockDamage() {
435 return mBlockDamage;
438 int getCurX() {
439 return mCurX;
442 int getCurY() {
443 return mCurY;
446 int getCreativeScroll() {
447 return mCreativeScroll;
450 public ControlMode getControlMode() {
451 return mControlMode;
454 public void setControlMode(ControlMode controlMode) {
455 mControlMode = controlMode;
458 void update() {
459 if (mTouchedDown && mTouchDownBtn == Input.Buttons.LEFT) {
460 pressLMB();
462 if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) {
463 holdMB();