DEADSOFTWARE

Fix hitting animation
[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.mobs.Mob;
12 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
13 import ru.deadsoftware.cavedroid.game.mobs.Pig;
14 import ru.deadsoftware.cavedroid.game.mobs.Player;
15 import ru.deadsoftware.cavedroid.game.objects.DropController;
16 import ru.deadsoftware.cavedroid.game.world.GameWorld;
17 import ru.deadsoftware.cavedroid.misc.Assets;
18 import ru.deadsoftware.cavedroid.misc.ControlMode;
20 import javax.inject.Inject;
22 import static ru.deadsoftware.cavedroid.game.GameItems.*;
24 @GameScope
25 public class GameInput {
27 private final MainConfig mMainConfig;
28 private final GameWorld mGameWorld;
29 private final DropController mDropController;
30 private final MobsController mMobsController;
31 private final Player mPlayer;
33 private ControlMode mControlMode;
35 private boolean mKeyDown;
36 private boolean mTouchedDown;
37 private boolean mDragging;
39 private int mKeyDownCode;
40 private int mTouchDownBtn;
41 private float mTouchDownX;
42 private float mTouchDownY;
43 private long mTouchDownTime;
45 private int mCurX;
46 private int mCurY;
47 private int mCreativeScroll;
48 private int mBlockDamage;
50 @Inject
51 public GameInput(MainConfig mainConfig,
52 GameWorld gameWorld,
53 DropController dropController,
54 MobsController mobsController) {
55 mMainConfig = mainConfig;
56 mGameWorld = gameWorld;
57 mDropController = dropController;
58 mMobsController = mobsController;
60 mPlayer = mMobsController.getPlayer();
62 mControlMode = mMainConfig.isTouch() ? ControlMode.WALK : ControlMode.CURSOR;
63 }
65 private boolean checkSwim() {
66 return GameItems.isFluid(mGameWorld.getForeMap(mPlayer.getMapX(), mPlayer.getLowerMapY()));
67 }
69 private void goUpwards() {
70 if (checkSwim()) {
71 mPlayer.swim = true;
72 } else if (mPlayer.canJump()) {
73 mPlayer.jump();
74 } else if (!mPlayer.isFlyMode() && mPlayer.gameMode == 1) {
75 mPlayer.setFlyMode(true);
76 mPlayer.getVelocity().y = 0;
77 } else if (mPlayer.isFlyMode()) {
78 mPlayer.getVelocity().y = -mPlayer.getSpeed();
79 }
80 }
82 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
83 private boolean insideCreativeInv(float screenX, float screenY) {
84 TextureRegion creative = Assets.textureRegions.get("creative");
85 return (screenX > mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 &&
86 screenX < mMainConfig.getWidth() / 2 + creative.getRegionWidth() / 2 &&
87 screenY > mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 &&
88 screenY < mMainConfig.getHeight() / 2 + creative.getRegionHeight() / 2);
89 }
91 private void wasdPressed(int keycode) {
92 if (mControlMode == ControlMode.WALK || !mMainConfig.isTouch()) {
93 switch (keycode) {
94 case Input.Keys.A:
95 mPlayer.getVelocity().x = -mPlayer.getSpeed();
96 mPlayer.setDir(Mob.Direction.LEFT);
97 if (mMainConfig.isTouch() && checkSwim()) {
98 mPlayer.swim = true;
99 }
100 break;
101 case Input.Keys.D:
102 mPlayer.getVelocity().x = mPlayer.getSpeed();
103 mPlayer.setDir(Mob.Direction.RIGHT);
104 if (mMainConfig.isTouch() && checkSwim()) {
105 mPlayer.swim = true;
107 break;
108 case Input.Keys.W:
109 case Input.Keys.SPACE:
110 goUpwards();
111 break;
112 case Input.Keys.S:
113 case Input.Keys.CONTROL_LEFT:
114 mPlayer.getVelocity().y = mPlayer.getSpeed();
115 break;
117 } else {
118 switch (keycode) {
119 case Input.Keys.A:
120 mCurX--;
121 break;
122 case Input.Keys.D:
123 mCurX++;
124 break;
125 case Input.Keys.W:
126 mCurY--;
127 break;
128 case Input.Keys.S:
129 mCurY++;
130 break;
132 mBlockDamage = 0;
136 private boolean isNotAutoselectable(int x, int y) {
137 return (!mGameWorld.hasForeAt(x, y) || !mGameWorld.getForeMapBlock(x, y).hasCollision());
140 private void checkCursorBounds() {
141 if (mCurY < 0) {
142 mCurY = 0;
143 } else if (mCurY >= mGameWorld.getHeight()) {
144 mCurY = mGameWorld.getHeight() - 1;
147 if (mControlMode == ControlMode.CURSOR) {
148 if (mCurX * 16 + 8 < mPlayer.getX() + mPlayer.getWidth() / 2) {
149 mPlayer.setDir(Mob.Direction.LEFT);
150 } else {
151 mPlayer.setDir(Mob.Direction.RIGHT);
156 public void moveCursor(GameRenderer gameRenderer) {
157 int pastX = mCurX;
158 int pastY = mCurY;
160 if (mControlMode == ControlMode.WALK && mMainConfig.isTouch()) {
161 mCurX = mPlayer.getMapX() + (mPlayer.looksLeft() ? -1 : 1);
162 mCurY = mPlayer.getUpperMapY();
163 for (int i = 0; i < 2 && isNotAutoselectable(mCurX, mCurY); i++) {
164 mCurY++;
166 if (isNotAutoselectable(mCurX, mCurY)) {
167 mCurX += mPlayer.looksLeft() ? 1 : -1;
169 } else if (!mMainConfig.isTouch()) {
170 final int tmpX = (int) (Gdx.input.getX() * (mMainConfig.getWidth() /
171 Gdx.graphics.getWidth()) + gameRenderer.getCamX());
172 mCurX = tmpX / 16;
174 final int tmpY = (int) (Gdx.input.getY() * (mMainConfig.getHeight() /
175 Gdx.graphics.getHeight()) + gameRenderer.getCamY());
176 mCurY = tmpY / 16;
178 if (tmpX < 0) {
179 mCurX--;
182 final double a = tmpX - mPlayer.x;
183 final double b = tmpY - mPlayer.y;
185 mPlayer.headRotation = (float) Math.atan(b / a) * MathUtils.radDeg;
188 if (pastX != mCurX || pastY != mCurY) {
189 mBlockDamage = 0;
192 checkCursorBounds();
195 private void useItem(int x, int y, int id, boolean bg) {
196 mPlayer.startHitting();
197 String key = getItem(id).isBlock() ? getBlockKey(id) : getItemKey(id);
198 if (id > 0) {
199 if (getItem(id).isBlock()) {
200 if (!bg) {
201 mGameWorld.placeToForeground(x, y, getBlockIdByItemId(id));
202 } else {
203 mGameWorld.placeToBackground(x, y, getBlockIdByItemId(id));
205 } else {
206 switch (key) {
207 case "bucket_water":
208 mGameWorld.placeToForeground(x, y, getBlockId("water"));
209 mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
210 break;
211 case "bucket_lava":
212 mGameWorld.placeToForeground(x, y, getBlockId("lava"));
213 mPlayer.inventory[mPlayer.slot] = getItemId("bucket_empty");
214 break;
220 private void hitMobs() {
221 final Player player = mMobsController.getPlayer();
222 mMobsController.getMobs().forEach((mob) -> {
223 if (Intersector.overlaps(mob, player)) {
224 mob.damage(5);
225 mob.jump();
227 });
230 private void pressLMB() {
231 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
232 mPlayer.startHitting();
234 if ((mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.getForeMapBlock(mCurX, mCurY).getHp() >= 0) ||
235 (!mGameWorld.hasForeAt(mCurX, mCurY) && mGameWorld.hasBackAt(mCurX, mCurY) &&
236 mGameWorld.getBackMapBlock(mCurX, mCurY).getHp() >= 0)) {
237 if (mPlayer.gameMode == 0) {
238 mBlockDamage++;
239 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
240 if (mBlockDamage >= mGameWorld.getForeMapBlock(mCurX, mCurY).getHp()) {
241 mGameWorld.destroyForeMap(mCurX, mCurY);
242 mBlockDamage = 0;
244 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
245 if (mBlockDamage >= mGameWorld.getBackMapBlock(mCurX, mCurY).getHp()) {
246 mGameWorld.destroyBackMap(mCurX, mCurY);
247 mBlockDamage = 0;
250 } else {
251 if (mGameWorld.hasForeAt(mCurX, mCurY)) {
252 mGameWorld.placeToForeground(mCurX, mCurY, 0);
253 } else if (mGameWorld.hasBackAt(mCurX, mCurY)) {
254 mGameWorld.placeToBackground(mCurX, mCurY, 0);
256 mTouchedDown = false;
258 } else {
259 hitMobs();
260 mTouchedDown = false;
265 private boolean insideHotbar(float x, float y) {
266 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
267 return y < hotbar.getRegionHeight() &&
268 Range.open(mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2,
269 mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2).contains(x);
272 private void holdMB() {
273 if (mTouchDownBtn == Input.Buttons.RIGHT) {
274 useItem(mCurX, mCurY, mPlayer.inventory[mPlayer.slot], true);
275 mTouchedDown = false;
276 } else {
277 if (insideHotbar(mTouchDownX, mTouchDownY)) {
278 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
279 mTouchedDown = false;
284 public void keyDown(int keycode) {
285 mKeyDown = true;
286 mKeyDownCode = keycode;
287 switch (keycode) {
288 case Input.Keys.A:
289 case Input.Keys.D:
290 case Input.Keys.W:
291 case Input.Keys.S:
292 case Input.Keys.SPACE:
293 case Input.Keys.CONTROL_LEFT:
294 wasdPressed(keycode);
295 break;
297 case Input.Keys.ALT_LEFT:
298 if (mMainConfig.isTouch()) {
299 mControlMode = mControlMode == ControlMode.WALK ? ControlMode.CURSOR : ControlMode.WALK;
301 break;
303 case Input.Keys.E:
304 if (mMainConfig.checkGameUiWindow(GameUiWindow.NONE)) {
305 switch (mPlayer.gameMode) {
306 case 0:
307 //TODO survival inv
308 break;
309 case 1:
310 mMainConfig.setGameUiWindow(GameUiWindow.CREATIVE_INVENTORY);
311 break;
313 } else {
314 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
316 break;
318 case Input.Keys.G:
319 mMobsController.addMob(new Pig(mCurX * 16, mCurY * 16));
320 break;
322 case Input.Keys.Q:
323 mGameWorld.placeToForeground(mCurX, mCurY, 8);
324 break;
326 case Input.Keys.GRAVE:
327 mMobsController.getPlayer().gameMode = (mMobsController.getPlayer().gameMode + 1) % 2;
328 break;
330 case Input.Keys.ESCAPE:
331 case Input.Keys.BACK:
332 GameSaver.save(mMainConfig, mDropController, mMobsController, mGameWorld);
333 mMainConfig.getCaveGame().quitGame();
334 break;
336 case Input.Keys.F1:
337 mMainConfig.setShowInfo(!mMainConfig.isShowInfo());
338 break;
340 case Input.Keys.M:
341 mMainConfig.setShowMap(!mMainConfig.isShowMap());
342 break;
346 public void keyUp(int keycode) {
347 switch (keycode) {
348 case Input.Keys.A:
349 case Input.Keys.D:
350 mPlayer.getVelocity().x = 0;
351 if (mMainConfig.isTouch() && mPlayer.swim) {
352 mPlayer.swim = false;
354 break;
356 case Input.Keys.W:
357 case Input.Keys.S:
358 case Input.Keys.SPACE:
359 case Input.Keys.CONTROL_LEFT:
360 if (mPlayer.isFlyMode()) {
361 mPlayer.getVelocity().y = 0;
363 if (mPlayer.swim) {
364 mPlayer.swim = false;
366 break;
370 public void touchDown(float touchX, float touchY, int button) {
371 mTouchDownTime = TimeUtils.millis();
372 mTouchedDown = true;
373 mTouchDownBtn = button;
374 mTouchDownX = touchX;
375 mTouchDownY = touchY;
378 public void touchUp(float screenX, float screenY, int button) {
379 if (mDragging) {
380 mDragging = false;
381 return;
384 if (mMainConfig.isTouch() && mKeyDown) {
385 keyUp(mKeyDownCode);
386 mKeyDown = false;
388 TextureRegion hotbar = Assets.textureRegions.get("hotbar");
389 TextureRegion creative = Assets.textureRegions.get("creative");
390 if (mTouchedDown) {
391 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && insideCreativeInv(screenX, screenY)) {
392 int ix = (int) (screenX - (mMainConfig.getWidth() / 2 - creative.getRegionWidth() / 2 + 8)) / 18;
393 int iy = (int) (screenY - (mMainConfig.getHeight() / 2 - creative.getRegionHeight() / 2 + 18)) / 18;
394 int item = mCreativeScroll * 8 + (ix + iy * 8);
395 if (ix >= 8 || ix < 0 || iy < 0 || iy >= 5) {
396 item = -1;
398 if (item >= 0 && item < GameItems.getItemsSize()) {
399 System.arraycopy(mPlayer.inventory, 0, mPlayer.inventory, 1, 8);
400 mPlayer.inventory[0] = item;
402 } else if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY)) {
403 mMainConfig.setGameUiWindow(GameUiWindow.NONE);
404 } else if (screenY < hotbar.getRegionHeight() &&
405 screenX > mMainConfig.getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 &&
406 screenX < mMainConfig.getWidth() / 2 + (float) hotbar.getRegionWidth() / 2) {
407 mPlayer.slot = (int) ((screenX - (mMainConfig.getWidth() / 2 - hotbar.getRegionWidth() / 2)) / 20);
408 } else if (button == Input.Buttons.RIGHT) {
409 useItem(mCurX, mCurY,
410 mPlayer.inventory[mPlayer.slot], false);
411 } else if (button == Input.Buttons.LEFT) {
412 mBlockDamage = 0;
415 mTouchedDown = false;
418 public void touchDragged(float screenX, float screenY) {
419 if (Math.abs(screenX - mTouchDownX) < 16 && Math.abs(screenY - mTouchDownY) < 16) {
420 return;
423 mDragging = true;
424 if (mMainConfig.checkGameUiWindow(GameUiWindow.CREATIVE_INVENTORY) && Math.abs(screenY - mTouchDownY) > 16) {
425 if (insideCreativeInv(screenX, screenY)) {
426 mCreativeScroll -= (screenY - mTouchDownY) / 16;
427 mTouchDownX = screenX;
428 mTouchDownY = screenY;
429 if (mCreativeScroll < 0) {
430 mCreativeScroll = 0;
432 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
433 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
439 public void scrolled(float amountX, float amountY) {
440 switch (mMainConfig.getGameUiWindow()) {
441 case NONE:
442 mPlayer.slot += (int) amountY;
443 if (mPlayer.slot < 0) {
444 mPlayer.slot = 8;
446 if (mPlayer.slot > 8) {
447 mPlayer.slot = 0;
449 break;
450 case CREATIVE_INVENTORY:
451 mCreativeScroll += (int) amountY;
452 if (mCreativeScroll < 0) {
453 mCreativeScroll = 0;
455 if (mCreativeScroll > GameProc.MAX_CREATIVE_SCROLL) {
456 mCreativeScroll = GameProc.MAX_CREATIVE_SCROLL;
458 break;
462 public int getKeyDownCode() {
463 return mKeyDownCode;
466 public boolean isKeyDown() {
467 return mKeyDown;
470 int getBlockDamage() {
471 return mBlockDamage;
474 int getCurX() {
475 return mCurX;
478 int getCurY() {
479 return mCurY;
482 int getCreativeScroll() {
483 return mCreativeScroll;
486 public ControlMode getControlMode() {
487 return mControlMode;
490 public void setControlMode(ControlMode controlMode) {
491 mControlMode = controlMode;
494 void update() {
495 if (!mTouchedDown) {
496 mPlayer.stopHitting();
497 return;
500 if (mTouchDownBtn == Input.Buttons.LEFT) {
501 pressLMB();
504 if (mTouchedDown && TimeUtils.timeSinceMillis(mTouchDownTime) > 500) {
505 holdMB();