DEADSOFTWARE

Fix blocks at x=0 not selectable by mouse
[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.getVelocity().add(0, -180);
71 } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) {
72 mPlayer.setFlyMode(true);
73 mPlayer.getVelocity().y = 0;
74 } else if (mPlayer.isFlyMode()) {
75 mPlayer.getVelocity().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.getVelocity().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.getVelocity().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.getVelocity().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 final int tmpX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() /
168 Gdx.graphics.getWidth()) + gameRenderer.getCamX());
169 mCurX = tmpX / 16;
171 mCurY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
172 Gdx.graphics.getHeight()) + gameRenderer.getCamY()) / 16;
173 if (tmpX < 0) {
174 mCurX--;
178 if (pastX != mCurX || pastY != mCurY) {
179 mBlockDamage = 0;
182 checkCursorBounds();
185 private void useItem(int x, int y, int id, boolean bg) {
186 String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id);
187 if (id > 0) {
188 if (getItem(id).isBlock()) {
189 if (!bg) {
190 mGameWorld.placeToForeground(x, y, getBlockIdByItemId(id));
191 } else {
192 mGameWorld.placeToBackground(x, y, getBlockIdByItemId(id));
194 } else {
195 switch (key) {
196 case "bucket_water":
197 mGameWorld.placeToForeground(x, y, getBlockId("water"));
198 mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
199 break;
200 case "bucket_lava":
201 mGameWorld.placeToForeground(x, y, getBlockId("lava"));
202 mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
203 break;
209 private void pressLMB() {
210 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE) &&
211 ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMapBlock(mCurX, mCurY).getHp() >= 0) ||
212 (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
213 mGameWorld.getBackMapBlock(mCurX, mCurY).getHp() >= 0))) {
214 if (mPlayer.gameMode == 0) {
215 mBlockDamage++;
216 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
217 if (mBlockDamage >= mGameWorld.getForeMapBlock(mCurX, mCurY).getHp()) {
218 mGameWorld.destroyForeMap(mCurX, mCurY);
219 mBlockDamage = 0;
221 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
222 if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) {
223 mGameWorld.destroyBackMap(mCurX, mCurY);
224 mBlockDamage = 0;
227 } else {
228 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
229 mGameWorld.placeToForeground(mCurX, mCurY, 0);
230 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
231 mGameWorld.placeToBackground(mCurX, mCurY, 0);
233 mTouchedDown = false;
238 private boolean insideHotbar(float x, float y) {
239 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
240 return y < hotbar.getRegionHeight() &&
241 Range.open(mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
242 mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
245 private void holdMB() {
246 if (mTouchDownBtn == Input.Buttons.RIGHT) {
247 useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true);
248 mTouchedDown = false;
249 } else {
250 if (insideHotbar(mTouchDownX, mTouchDownY)) {
251 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
252 mTouchedDown = false;
257 public void keyDown(int keycode) {
258 mKeyDown = true;
259 mKeyDownCode = keycode;
260 switch (keycode) {
261 case Input.Keys.A:
262 case Input.Keys.D:
263 case Input.Keys.W:
264 case Input.Keys.S:
265 case Input.Keys.SPACE:
266 case Input.Keys.CONTROL_LEFT:
267 wasdPressed(keycode);
268 break;
270 case Input.Keys.ALT_LEFT:
271 if (mMainConfig.isTouch()) {
272 mControlMode = mControlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
274 break;
276 case Input.Keys.E:
277 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
278 switch (mPlayer.gameMode) {
279 case 0:
280 //TODO survival inv
281 break;
282 case 1:
283 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
284 break;
286 } else {
287 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
289 break;
291 case Input.Keys.G:
292 mMobsController.addMob(Pig.class, mCurX * 16, mCurY * 16);
293 break;
295 case Input.Keys.Q:
296 mGameWorld.placeToForeground(mCurX, mCurY, 8);
297 break;
299 case Input.Keys.ESCAPE:
300 case Input.Keys.BACK:
301 GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld);
302 mMainConfig.getCaveGame().quitGame();
303 break;
305 case Input.Keys.F1:
306 mMainConfig.setShowInfo(!mMainConfig.isShowInfo());
307 break;
309 case Input.Keys.M:
310 mMainConfig.setShowMap(!mMainConfig.isShowMap());
311 break;
315 public void keyUp(int keycode) {
316 switch (keycode) {
317 case Input.Keys.A:
318 case Input.Keys.D:
319 mPlayer.getVelocity().x = 0;
320 if (mMainConfig.isTouch() && mPlayer.swim) {
321 mPlayer.swim = false;
323 break;
325 case Input.Keys.W:
326 case Input.Keys.S:
327 case Input.Keys.SPACE:
328 case Input.Keys.CONTROL_LEFT:
329 if (mPlayer.isFlyMode()) {
330 mPlayer.getVelocity().y = 0;
332 if (mPlayer.swim) {
333 mPlayer.swim = false;
335 break;
339 public void touchDown(float touchX, float touchY, int button) {
340 mTouchDownTime = TimeUtils.millis();
341 mTouchedDown = true;
342 mTouchDownBtn = button;
343 mTouchDownX = touchX;
344 mTouchDownY = touchY;
347 public void touchUp(float screenX, float screenY, int button) {
348 if (mDragging) {
349 mDragging = false;
350 return;
353 if (mMainConfig.isTouch() && mKeyDown) {
354 keyUp(mKeyDownCode);
355 mKeyDown = false;
357 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
358 TextureRegion creative = Assets.textureRegions.get("creative");
359 if (mTouchedDown) {
360 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && insideCreativeInv(screenX, screenY)) {
361 int ix = (int) (screenX - (mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
362 int iy = (int) (screenY - (mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
363 int item = mCreativeScroll * 8 + (ix + iy * 8);
364 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) {
365 item = -1;
367 if (item >= 0 && item < GameItems.getItemsSize()) {
368 System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8);
369 mPlayer.inventory[0] = item;
371 } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
372 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
373 } else if (screenY < hotbar.getRegionHeight() &&
374 screenX > mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
375 screenX < mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) {
376 mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
377 } else if (button == Input.Buttons.RIGHT) {
378 useItem(mCurX, mCurY,
379 mPlayer.inventory[mPlayer.slot], false);
380 } else if (button == Input.Buttons.LEFT) {
381 mBlockDamage = 0;
384 mTouchedDown = false;
387 public void touchDragged(float screenX, float screenY) {
388 mDragging = true;
389 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) {
390 if (insideCreativeInv(screenX, screenY)) {
391 mCreativeScroll -= (screenY - mTouchDownY) / 16;
392 mTouchDownX = screenX;
393 mTouchDownY = screenY;
394 if (mCreativeScroll < 0) {
395 mCreativeScroll = 0;
397 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
398 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
404 public void scrolled(float amountX, float amountY) {
405 switch (mMainConfig.getGameUiWindow()) {
406 case NONE:
407 mPlayer.slot += (int) amountY;
408 if (mPlayer.slot < 0) {
409 mPlayer.slot = 8;
411 if (mPlayer.slot > 8) {
412 mPlayer.slot = 0;
414 break;
415 case CREATIVE_INVENTORY:
416 mCreativeScroll += (int) amountY;
417 if (mCreativeScroll < 0) {
418 mCreativeScroll = 0;
420 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
421 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
423 break;
427 public int getKeyDownCode() {
428 return mKeyDownCode;
431 public boolean isKeyDown() {
432 return mKeyDown;
435 int getBlockDamage() {
436 return mBlockDamage;
439 int getCurX() {
440 return mCurX;
443 int getCurY() {
444 return mCurY;
447 int getCreativeScroll() {
448 return mCreativeScroll;
451 public ControlMode getControlMode() {
452 return mControlMode;
455 public void setControlMode(ControlMode controlMode) {
456 mControlMode = controlMode;
459 void update() {
460 if (mTouchedDown && mTouchDownBtn == Input.Buttons.LEFT) {
461 pressLMB();
463 if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) {
464 holdMB();