DEADSOFTWARE

bded5818b071d57a703cba3a9dc7a4af1fec2bba
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GameInput.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.misc.AppState;
6 import ru.deadsoftware.cavecraft.misc.Assets;
7 import ru.deadsoftware.cavecraft.CaveGame;
9 public class GameInput {
11 private GameProc gameProc;
13 public GameInput(GameProc gameProc) {
14 this.gameProc = gameProc;
15 }
17 private void wasdPressed(int keycode) {
18 if (gameProc.ctrlMode==0 || !CaveGame.TOUCH) {
19 switch (keycode) {
20 case Input.Keys.A:
21 gameProc.player.moveX.x = -GamePhysics.PL_SPEED;
22 gameProc.player.dir = 0;
23 break;
24 case Input.Keys.D:
25 gameProc.player.moveX.x = GamePhysics.PL_SPEED;
26 gameProc.player.dir = 1;
27 break;
28 }
29 } else if (CaveGame.TOUCH){
30 switch (keycode) {
31 case Input.Keys.A:
32 gameProc.cursorX--;
33 break;
34 case Input.Keys.D:
35 gameProc.cursorX++;
36 break;
37 case Input.Keys.W:
38 gameProc.cursorY--;
39 break;
40 case Input.Keys.S:
41 gameProc.cursorY++;
42 break;
43 }
44 }
45 }
47 public void keyDown(int keycode) {
48 gameProc.isKeyDown = true;
49 gameProc.keyDownCode = keycode;
50 if (keycode == Input.Keys.W || keycode == Input.Keys.A ||
51 keycode == Input.Keys.S || keycode == Input.Keys.D) {
52 wasdPressed(keycode);
53 } else switch (keycode) {
54 case Input.Keys.ALT_LEFT:
55 if (CaveGame.TOUCH) {
56 gameProc.ctrlMode++;
57 if (gameProc.ctrlMode > 1) gameProc.ctrlMode = 0;
58 }
59 break;
61 case Input.Keys.SPACE:
62 if (gameProc.player.canJump) {
63 gameProc.player.moveY.add(0, -7);
64 } else if (!gameProc.player.flyMode) {
65 gameProc.player.flyMode = true;
66 gameProc.player.moveY.setZero();
67 } else {
68 gameProc.player.moveY.y = -GamePhysics.PL_SPEED;
69 }
70 break;
72 case Input.Keys.CONTROL_LEFT:
73 gameProc.player.moveY.y = GamePhysics.PL_SPEED;
74 break;
76 case Input.Keys.E:
77 if (CaveGame.STATE == AppState.GAME_PLAY) CaveGame.STATE = AppState.GAME_CREATIVE_INV;
78 else CaveGame.STATE = AppState.GAME_PLAY;
79 break;
81 case Input.Keys.ESCAPE: case Input.Keys.BACK:
82 CaveGame.STATE = AppState.GOTO_MENU;
83 break;
84 }
85 }
87 public void keyUp(int keycode) {
88 switch (keycode) {
89 case Input.Keys.A: case Input.Keys.D:
90 gameProc.player.moveX.x = 0;
91 break;
93 case Input.Keys.SPACE: case Input.Keys.CONTROL_LEFT:
94 if (gameProc.player.flyMode) gameProc.player.moveY.setZero();
95 break;
96 }
97 }
99 public void mouseMoved(int screenX, int screenY) {
102 public void touchDown(int screenX, int screenY, int button) {
103 if (CaveGame.STATE == AppState.GAME_CREATIVE_INV &&
104 screenX>gameProc.renderer.camera.viewportWidth/2-Assets.creativeInv.getRegionWidth()/2 &&
105 screenX<gameProc.renderer.camera.viewportWidth/2+Assets.creativeInv.getRegionWidth()/2 &&
106 screenY>gameProc.renderer.camera.viewportHeight/2-Assets.creativeInv.getRegionHeight()/2 &&
107 screenY<gameProc.renderer.camera.viewportHeight/2+Assets.creativeInv.getRegionHeight()/2) {
108 int ix = (int) (screenX - (gameProc.renderer.camera.viewportWidth / 2 - Assets.creativeInv.getRegionWidth() / 2 + 8)) / 18;
109 int iy = (int) (screenY - (gameProc.renderer.camera.viewportHeight / 2 - Assets.creativeInv.getRegionHeight() / 2 + 18)) / 18;
110 int item = ix + iy * 8;
111 if (item >= 0 && item < Items.BLOCKS.size) {
112 for (int i = 8; i > 0; i--) {
113 gameProc.player.inventory[i] = gameProc.player.inventory[i - 1];
115 gameProc.player.inventory[0] = item;
117 } else if (CaveGame.STATE == AppState.GAME_CREATIVE_INV) {
118 CaveGame.STATE = AppState.GAME_PLAY;
119 } else {
120 gameProc.touchDownX = screenX;
121 gameProc.touchDownY = screenY;
122 gameProc.touchDownTime = TimeUtils.millis();
123 gameProc.isTouchDown = true;
124 gameProc.touchDownButton = button;
128 public void touchUp(int screenX, int screenY, int button) {
129 if (gameProc.isKeyDown) {
130 keyUp(gameProc.keyDownCode);
131 gameProc.isKeyDown = false;
133 if (gameProc.isTouchDown) {
134 if (button == Input.Buttons.RIGHT){
135 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY,
136 gameProc.player.inventory[gameProc.invSlot]);
137 } else if (button == Input.Buttons.LEFT) {
138 if (screenY<Assets.invBar.getRegionHeight() &&
139 screenX>gameProc.renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2 &&
140 screenX<gameProc.renderer.camera.viewportWidth/2+Assets.invBar.getRegionWidth()/2) {
141 gameProc.invSlot = (int)((screenX-(gameProc.renderer.camera.viewportWidth/2-Assets.invBar.getRegionWidth()/2))/20);
142 } else if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
143 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
144 } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
145 gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
150 gameProc.isTouchDown = false;
153 public void touchDragged(int screenX, int screenY) {
156 public void scrolled(int amount) {
157 gameProc.invSlot += amount;
158 if (gameProc.invSlot < 0) gameProc.invSlot = 8;
159 if (gameProc.invSlot > 8) gameProc.invSlot = 0;