DEADSOFTWARE

aec59d21387608383401b3182e42c75e79588fe9
[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 if (gameProc.player.canJump) {
54 gameProc.player.moveY.add(0, -8);
55 } else if (!gameProc.player.flyMode) {
56 gameProc.player.flyMode = true;
57 gameProc.player.moveY.setZero();
58 } else {
59 gameProc.player.moveY.y = -GamePhysics.PL_SPEED;
60 }
61 }
62 if (keyCode == Input.Keys.CONTROL_LEFT) {
63 gameProc.player.moveY.y = GamePhysics.PL_SPEED;
64 }
65 }
67 public void keyUp(int keyCode) {
68 if (keyCode == Input.Keys.A || keyCode == Input.Keys.D) {
69 gameProc.player.moveX.x = 0;
70 }
71 if (keyCode == Input.Keys.SPACE) {
72 if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
73 }
74 if (keyCode == Input.Keys.CONTROL_LEFT) {
75 if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
76 }
77 }
79 public void mouseMoved(int screenX, int screenY) {
80 }
82 public void touchDown(int screenX, int screenY, int button) {
83 gameProc.touchDownX = screenX;
84 gameProc.touchDownY = screenY;
85 gameProc.touchDownTime = TimeUtils.millis();
86 gameProc.isTouchDown = true;
87 }
89 public void touchUp(int screenX, int screenY, int button) {
90 if (gameProc.isTouchDown) {
91 if (button == Input.Buttons.RIGHT){
92 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY,
93 gameProc.player.inventory[gameProc.invSlot]);
94 } else if (button == Input.Buttons.LEFT) {
95 if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
96 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
97 } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
98 gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
99 }
102 gameProc.isTouchDown = false;
105 public void touchDragged(int screenX, int screenY) {
108 public void scrolled(int amount) {
109 gameProc.invSlot += amount;
110 if (gameProc.invSlot < 0) gameProc.invSlot = 8;
111 if (gameProc.invSlot > 8) gameProc.invSlot = 0;