DEADSOFTWARE

Store block references intead of ids
[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.math.Intersector;
7 import com.badlogic.gdx.math.MathUtils;
8 import com.badlogic.gdx.utils.TimeUtils;
9 import com.google.common.collect.Range;
10 import ru.deadsoftware.cavedroid.MainConfig;
11 import ru.deadsoftware.cavedroid.game.actions.CommonBlockActionUtilsKt;
12 import ru.deadsoftware.cavedroid.game.actions.placeblock.IPlaceBlockAction;
13 import ru.deadsoftware.cavedroid.game.actions.useitem.IUseItemAction;
14 import ru.deadsoftware.cavedroid.game.mobs.Mob;
15 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
16 import ru.deadsoftware.cavedroid.game.mobs.Pig;
17 import ru.deadsoftware.cavedroid.game.mobs.Player;
18 import ru.deadsoftware.cavedroid.game.model.item.Item;
19 import ru.deadsoftware.cavedroid.game.objects.DropController;
20 import ru.deadsoftware.cavedroid.game.world.GameWorld;
21 import ru.deadsoftware.cavedroid.misc.Assets;
22 import ru.deadsoftware.cavedroid.misc.ControlMode;
24 import javax.inject.Inject;
26 import java.util.Map;
28 import static ru.deadsoftware.cavedroid.game.GameItems.*;
30 @GameScope
31 public class GameInput {
33 private static final String TAG = "GameInput";
35 private final MainConfig mMainConfig;
36 private final GameWorld mGameWorld;
37 private final DropController mDropController;
38 private final MobsController mMobsController;
39 private final GameItemsHolder mGameItemsHolder;
40 private final Map<String, IUseItemAction> mUseItemActionMap;
41 private final Map<String, IPlaceBlockAction> mPlaceBlockActionMap;
43 private final Player mPlayer;
45 private ControlMode mControlMode;
47 private boolean mKeyDown;
48 private boolean mTouchedDown;
49 private boolean mDragging;
51 private int mKeyDownCode;
52 private int mTouchDownBtn;
53 private float mTouchDownX;
54 private float mTouchDownY;
55 private long mTouchDownTime;
57 private int mCurX;
58 private int mCurY;
59 private int mCreativeScroll;
60 private int mBlockDamage;
62 @Inject
63 public GameInput(MainConfig mainConfig,
64 GameWorld gameWorld,
65 DropController dropController,
66 MobsController mobsController,
67 GameItemsHolder gameItemsHolder,
68 Map<String, IUseItemAction> useItemActionMap,
69 Map<String, IPlaceBlockAction> placeBlockActionMap) {
70 mMainConfig = mainConfig;
71 mGameWorld = gameWorld;
72 mDropController = dropController;
73 mMobsController = mobsController;
74 mGameItemsHolder = gameItemsHolder;
75 mUseItemActionMap = useItemActionMap;
76 mPlaceBlockActionMap = placeBlockActionMap;
78 mPlayer = mMobsController.getPlayer();
80 mControlMode = mMainConfig.isTouch() ? ControlMode.WALK : ControlMode.CURSOR;
81 }
83 private boolean checkSwim() {
84 return mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()).isFluid();
85 }
87 private void goUpwards() {
88 if (checkSwim()) {
89 mPlayer.swim = true;
90 } else if (mPlayer.canJump()) {
91 mPlayer.jump();
92 } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) {
93 mPlayer.setFlyMode(true);
94 mPlayer.getVelocity().y = 0;
95 } else if (mPlayer.isFlyMode()) {
96 mPlayer.getVelocity().y = -mPlayer.getSpeed();
97 }
98 }
100 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
101 private boolean insideCreativeInv(float screenX, float screenY) {
102 TextureRegion creative = Assets.textureRegions.get("creative");
103 return (screenX > mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 &&
104 screenX < mMainConfig.getWidth() / 2 + creative.getRegionWidth() / 2 &&
105 screenY > mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 &&
106 screenY < mMainConfig.getHeight() / 2 + creative.getRegionHeight() / 2);
109 private void wasdPressed(int keycode) {
110 if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) {
111 switch (keycode) {
112 case Input.Keys.A:
113 mPlayer.getVelocity().x = -mPlayer.getSpeed();
114 mPlayer.setDir(Mob.Direction.LEFT);
115 if (mMainConfig.isTouch() && checkSwim()) {
116 mPlayer.swim = true;
118 break;
119 case Input.Keys.D:
120 mPlayer.getVelocity().x = mPlayer.getSpeed();
121 mPlayer.setDir(Mob.Direction.RIGHT);
122 if (mMainConfig.isTouch() && checkSwim()) {
123 mPlayer.swim = true;
125 break;
126 case Input.Keys.W:
127 case Input.Keys.SPACE:
128 goUpwards();
129 break;
130 case Input.Keys.S:
131 case Input.Keys.CONTROL_LEFT:
132 mPlayer.getVelocity().y = mPlayer.getSpeed();
133 break;
135 } else {
136 switch (keycode) {
137 case Input.Keys.A:
138 mCurX--;
139 break;
140 case Input.Keys.D:
141 mCurX++;
142 break;
143 case Input.Keys.W:
144 mCurY--;
145 break;
146 case Input.Keys.S:
147 mCurY++;
148 break;
150 mBlockDamage = 0;
154 private boolean isNotAutoselectable(int x, int y) {
155 return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMap(x, y).hasCollision());
158 private void checkCursorBounds() {
159 if (mCurY < 0) {
160 mCurY = 0;
161 } else if (mCurY >= mGameWorld.getHeight()) {
162 mCurY = mGameWorld.getHeight() - 1;
165 if (mControlMode == ControlMode.CURSOR) {
166 if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) {
167 mPlayer.setDir(Mob.Direction.LEFT);
168 } else {
169 mPlayer.setDir(Mob.Direction.RIGHT);
174 public void moveCursor(GameRenderer gameRenderer) {
175 int pastX = mCurX;
176 int pastY = mCurY;
178 if (mControlMode == ControlMode.WALK && mMainConfig.isTouch()) {
179 mCurX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1);
180 mCurY = mPlayer.getUpperMapY();
181 for (int i = 0; i < 2 && isNotAutoselectable(mCurX, mCurY); i++) {
182 mCurY++;
184 if (isNotAutoselectable(mCurX, mCurY)) {
185 mCurX += mPlayer.looksLeft() ? 1 : -1;
187 } else if (!mMainConfig.isTouch()) {
188 final int tmpX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() /
189 Gdx.graphics.getWidth()) + gameRenderer.getCamX());
190 mCurX = tmpX / 16;
192 final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
193 Gdx.graphics.getHeight()) + gameRenderer.getCamY());
194 mCurY = tmpY / 16;
196 if (tmpX < 0) {
197 mCurX--;
200 final double a = tmpX - mPlayer.x;
201 final double b = tmpY - mPlayer.y;
203 mPlayer.headRotation = (float) Math.atan(b / a) * MathUtils.radDeg;
206 if (pastX != mCurX || pastY != mCurY) {
207 mBlockDamage = 0;
210 checkCursorBounds();
213 private void useItem(int x, int y, int id, boolean bg) {
214 mPlayer.startHitting();
216 if (id > 0) {
217 final Item item = getItem(id);
219 if (item instanceof Item.Placeable) {
220 if (!bg) {
221 CommonBlockActionUtilsKt.placeToForegroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y);
222 } else {
223 CommonBlockActionUtilsKt.placeToBackgroundAction(mPlaceBlockActionMap, (Item.Placeable) item, x, y);
225 } else if (item instanceof Item.Usable) {
226 final String actionKey = ((Item.Usable)item).getUseActionKey();
227 final IUseItemAction useItemAction = mUseItemActionMap.get(actionKey);
229 if (useItemAction != null) {
230 useItemAction.perform((Item.Usable) item, x, y);
231 } else {
232 Gdx.app.error(TAG, "use item action " + actionKey + " not found");
238 private void hitMobs() {
239 final Player player = mMobsController.getPlayer();
240 mMobsController.getMobs().forEach((mob) -> {
241 if (Intersector.overlaps(mob, player)) {
242 mob.damage(5);
243 mob.jump();
245 });
248 private void pressLMB() {
249 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
250 mPlayer.startHitting();
252 if ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMap(mCurX, mCurY).getHp() >= 0) ||
253 (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
254 mGameWorld.getBackMap(mCurX, mCurY).getHp() >= 0)) {
255 if (mPlayer.gameMode == 0) {
256 mBlockDamage++;
257 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
258 if (mBlockDamage >= mGameWorld.getForeMap(mCurX, mCurY).getHp()) {
259 mGameWorld.destroyForeMap(mCurX, mCurY);
260 mBlockDamage = 0;
262 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
263 if (mBlockDamage >= mGameWorld.getBackMap(mCurX, mCurY).getHp()) {
264 mGameWorld.destroyBackMap(mCurX, mCurY);
265 mBlockDamage = 0;
268 } else {
269 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
270 mGameWorld.placeToForeground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
271 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
272 mGameWorld.placeToBackground(mCurX, mCurY, mGameItemsHolder.getFallbackBlock());
274 mTouchedDown = false;
276 } else {
277 hitMobs();
278 mTouchedDown = false;
283 private boolean insideHotbar(float x, float y) {
284 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
285 return y < hotbar.getRegionHeight() &&
286 Range.open(mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
287 mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
290 private void holdMB() {
291 if (mTouchDownBtn == Input.Buttons.RIGHT) {
292 useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true);
293 mTouchedDown = false;
294 } else {
295 if (insideHotbar(mTouchDownX, mTouchDownY)) {
296 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
297 mTouchedDown = false;
302 public void keyDown(int keycode) {
303 mKeyDown = true;
304 mKeyDownCode = keycode;
305 switch (keycode) {
306 case Input.Keys.A:
307 case Input.Keys.D:
308 case Input.Keys.W:
309 case Input.Keys.S:
310 case Input.Keys.SPACE:
311 case Input.Keys.CONTROL_LEFT:
312 wasdPressed(keycode);
313 break;
315 case Input.Keys.ALT_LEFT:
316 if (mMainConfig.isTouch()) {
317 mControlMode = mControlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
319 break;
321 case Input.Keys.E:
322 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
323 switch (mPlayer.gameMode) {
324 case 0:
325 //TODO survival inv
326 break;
327 case 1:
328 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
329 break;
331 } else {
332 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
334 break;
336 case Input.Keys.G:
337 final Mob pig = new Pig(mCurX * 16, mCurY * 16);
338 pig.attachToController(mMobsController);
339 break;
341 case Input.Keys.GRAVE:
342 mMobsController.getPlayer().gameMode = (mMobsController.getPlayer().gameMode + 1) % 2;
343 break;
345 case Input.Keys.ESCAPE:
346 case Input.Keys.BACK:
347 GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld);
348 mMainConfig.getCaveGame().quitGame();
349 break;
351 case Input.Keys.F1:
352 mMainConfig.setShowInfo(!mMainConfig.isShowInfo());
353 break;
355 case Input.Keys.M:
356 mMainConfig.setShowMap(!mMainConfig.isShowMap());
357 break;
361 public void keyUp(int keycode) {
362 switch (keycode) {
363 case Input.Keys.A:
364 case Input.Keys.D:
365 mPlayer.getVelocity().x = 0;
366 if (mMainConfig.isTouch() && mPlayer.swim) {
367 mPlayer.swim = false;
369 break;
371 case Input.Keys.W:
372 case Input.Keys.S:
373 case Input.Keys.SPACE:
374 case Input.Keys.CONTROL_LEFT:
375 if (mPlayer.isFlyMode()) {
376 mPlayer.getVelocity().y = 0;
378 if (mPlayer.swim) {
379 mPlayer.swim = false;
381 break;
385 public void touchDown(float touchX, float touchY, int button) {
386 mTouchDownTime = TimeUtils.millis();
387 mTouchedDown = true;
388 mTouchDownBtn = button;
389 mTouchDownX = touchX;
390 mTouchDownY = touchY;
393 public void touchUp(float screenX, float screenY, int button) {
394 if (mDragging) {
395 mDragging = false;
396 return;
399 if (mMainConfig.isTouch() && mKeyDown) {
400 keyUp(mKeyDownCode);
401 mKeyDown = false;
403 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
404 TextureRegion creative = Assets.textureRegions.get("creative");
405 if (mTouchedDown) {
406 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && insideCreativeInv(screenX, screenY)) {
407 int ix = (int) (screenX - (mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
408 int iy = (int) (screenY - (mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
409 int item = mCreativeScroll * 8 + (ix + iy * 8);
410 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) {
411 item = -1;
413 if (item >= 0 && item < GameItems.getItemsSize()) {
414 System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8);
415 mPlayer.inventory[0] = item;
417 } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
418 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
419 } else if (screenY < hotbar.getRegionHeight() &&
420 screenX > mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
421 screenX < mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) {
422 mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
423 } else if (button == Input.Buttons.RIGHT) {
424 useItem(mCurX, mCurY,
425 mPlayer.inventory[mPlayer.slot], false);
426 } else if (button == Input.Buttons.LEFT) {
427 mBlockDamage = 0;
430 mTouchedDown = false;
433 public void touchDragged(float screenX, float screenY) {
434 if (Math.abs(screenX - mTouchDownX) < 16 && Math.abs(screenY - mTouchDownY) < 16) {
435 return;
438 mDragging = true;
439 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) {
440 if (insideCreativeInv(screenX, screenY)) {
441 mCreativeScroll -= (screenY - mTouchDownY) / 16;
442 mTouchDownX = screenX;
443 mTouchDownY = screenY;
444 if (mCreativeScroll < 0) {
445 mCreativeScroll = 0;
447 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
448 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
454 public void scrolled(float amountX, float amountY) {
455 switch (mMainConfig.getGameUiWindow()) {
456 case NONE:
457 mPlayer.slot += (int) amountY;
458 if (mPlayer.slot < 0) {
459 mPlayer.slot = 8;
461 if (mPlayer.slot > 8) {
462 mPlayer.slot = 0;
464 break;
465 case CREATIVE_INVENTORY:
466 mCreativeScroll += (int) amountY;
467 if (mCreativeScroll < 0) {
468 mCreativeScroll = 0;
470 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
471 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
473 break;
477 public int getKeyDownCode() {
478 return mKeyDownCode;
481 public boolean isKeyDown() {
482 return mKeyDown;
485 int getBlockDamage() {
486 return mBlockDamage;
489 int getCurX() {
490 return mCurX;
493 int getCurY() {
494 return mCurY;
497 int getCreativeScroll() {
498 return mCreativeScroll;
501 public ControlMode getControlMode() {
502 return mControlMode;
505 public void setControlMode(ControlMode controlMode) {
506 mControlMode = controlMode;
509 void update() {
510 if (!mTouchedDown) {
511 mPlayer.stopHitting();
512 return;
515 if (mTouchDownBtn == Input.Buttons.LEFT) {
516 pressLMB();
519 if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) {
520 holdMB();