DEADSOFTWARE

Add touch controls
[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;
6 public class GameInputHandler {
8 private GameProc gameProc;
10 public GameInputHandler(GameProc gameProc) {
11 this.gameProc = gameProc;
12 }
14 public void keyDown(int keyCode) {
15 if (gameProc.ctrlMode==0) {
16 if (keyCode == Input.Keys.A) {
17 gameProc.player.moveX.add(-GamePhysics.PL_SPEED, 0);
18 gameProc.player.dir = 0;
19 }
20 if (keyCode == Input.Keys.D) {
21 gameProc.player.moveX.add(GamePhysics.PL_SPEED, 0);
22 gameProc.player.dir = 1;
23 }
24 } else {
25 if (keyCode == Input.Keys.A) {
26 gameProc.cursorX--;
27 }
28 if (keyCode == Input.Keys.D) {
29 gameProc.cursorX++;
30 }
31 if (keyCode == Input.Keys.W) {
32 gameProc.cursorY--;
33 }
34 if (keyCode == Input.Keys.S) {
35 gameProc.cursorY++;
36 }
37 if (gameProc.cursorX < 0)
38 gameProc.cursorX = 0;
39 if (gameProc.cursorX >= gameProc.world.getWidth())
40 gameProc.cursorX = gameProc.world.getWidth()-1;
41 if (gameProc.cursorY < 0)
42 gameProc.cursorY = 0;
43 if (gameProc.cursorY >= gameProc.world.getHeight())
44 gameProc.cursorY = gameProc.world.getHeight()-1;
45 }
46 if (keyCode == Input.Keys.ALT_LEFT) {
47 gameProc.ctrlMode++;
48 gameProc.cursorX = (int)(gameProc.player.position.x/16);
49 gameProc.cursorY = (int)(gameProc.player.position.y/16);
50 if (gameProc.ctrlMode > 1) gameProc.ctrlMode = 0;
51 }
52 if (keyCode == Input.Keys.SPACE &&
53 gameProc.player.canJump) gameProc.player.moveY.add(0, -8);
54 }
56 public void keyUp(int keyCode) {
57 if (keyCode == Input.Keys.A || keyCode == Input.Keys.D) {
58 gameProc.player.moveX.x = 0;
59 }
60 }
62 public void mouseMoved(int screenX, int screenY) {
63 }
65 public void touchDown(int screenX, int screenY, int button) {
66 gameProc.touchDownX = screenX;
67 gameProc.touchDownY = screenY;
68 gameProc.touchDownTime = TimeUtils.millis();
69 gameProc.isTouchDown = true;
70 }
72 public void touchUp(int screenX, int screenY, int button) {
73 if (gameProc.isTouchDown) {
74 if (button == Input.Buttons.RIGHT){
75 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY,
76 gameProc.player.inventory[gameProc.invSlot]);
77 } else if (button == Input.Buttons.LEFT) {
78 if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
79 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
80 } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
81 gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
82 }
83 }
84 }
85 gameProc.isTouchDown = false;
86 }
88 public void touchDragged(int screenX, int screenY) {
89 }
91 public void scrolled(int amount) {
92 gameProc.invSlot += amount;
93 if (gameProc.invSlot < 0) gameProc.invSlot = 8;
94 if (gameProc.invSlot > 8) gameProc.invSlot = 0;
95 }
97 }