DEADSOFTWARE

0b714ddd43eda90fa210a60108f592d79ece325e
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameInputHandler.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.GameScreen;
7 public class GameInputHandler {
9 private GameProc gameProc;
11 public GameInputHandler(GameProc gameProc) {
12 this.gameProc = gameProc;
13 }
15 public void keyDown(int keyCode) {
16 if (gameProc.ctrlMode==0) {
17 if (keyCode == Input.Keys.A) {
18 gameProc.player.moveX.add(-GamePhysics.PL_SPEED, 0);
19 gameProc.player.dir = 0;
20 }
21 if (keyCode == Input.Keys.D) {
22 gameProc.player.moveX.add(GamePhysics.PL_SPEED, 0);
23 gameProc.player.dir = 1;
24 }
25 } else {
26 if (keyCode == Input.Keys.A) {
27 gameProc.cursorX--;
28 }
29 if (keyCode == Input.Keys.D) {
30 gameProc.cursorX++;
31 }
32 if (keyCode == Input.Keys.W) {
33 gameProc.cursorY--;
34 }
35 if (keyCode == Input.Keys.S) {
36 gameProc.cursorY++;
37 }
38 if (gameProc.cursorX < 0)
39 gameProc.cursorX = 0;
40 if (gameProc.cursorX >= gameProc.world.getWidth())
41 gameProc.cursorX = gameProc.world.getWidth()-1;
42 if (gameProc.cursorY < 0)
43 gameProc.cursorY = 0;
44 if (gameProc.cursorY >= gameProc.world.getHeight())
45 gameProc.cursorY = gameProc.world.getHeight()-1;
46 }
47 if (keyCode == Input.Keys.ALT_LEFT) {
48 gameProc.ctrlMode++;
49 gameProc.cursorX = (int)(gameProc.player.position.x/16);
50 gameProc.cursorY = (int)(gameProc.player.position.y/16);
51 if (gameProc.ctrlMode > 1) gameProc.ctrlMode = 0;
52 }
53 if (keyCode == Input.Keys.SPACE) {
54 if (gameProc.player.canJump) {
55 gameProc.player.moveY.add(0, -7);
56 } else if (!gameProc.player.flyMode) {
57 gameProc.player.flyMode = true;
58 gameProc.player.moveY.setZero();
59 } else {
60 gameProc.player.moveY.y = -GamePhysics.PL_SPEED;
61 }
62 }
63 if (keyCode == Input.Keys.CONTROL_LEFT) {
64 gameProc.player.moveY.y = GamePhysics.PL_SPEED;
65 }
66 }
68 public void keyUp(int keyCode) {
69 if (keyCode == Input.Keys.A || keyCode == Input.Keys.D) {
70 gameProc.player.moveX.x = 0;
71 }
72 if (keyCode == Input.Keys.SPACE) {
73 if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
74 }
75 if (keyCode == Input.Keys.CONTROL_LEFT) {
76 if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
77 }
78 }
80 public void mouseMoved(int screenX, int screenY) {
81 }
83 public void touchDown(int screenX, int screenY, int button) {
84 gameProc.touchDownX = screenX;
85 gameProc.touchDownY = screenY;
86 gameProc.touchDownTime = TimeUtils.millis();
87 gameProc.isTouchDown = true;
88 }
90 public void touchUp(int screenX, int screenY, int button) {
91 if (gameProc.isTouchDown) {
92 if (button == Input.Buttons.RIGHT){
93 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY,
94 gameProc.player.inventory[gameProc.invSlot]);
95 } else if (button == Input.Buttons.LEFT) {
96 if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
97 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
98 } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
99 gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
103 gameProc.isTouchDown = false;
106 public void touchDragged(int screenX, int screenY) {
109 public void scrolled(int amount) {
110 gameProc.invSlot += amount;
111 if (gameProc.invSlot < 0) gameProc.invSlot = 8;
112 if (gameProc.invSlot > 8) gameProc.invSlot = 0;