DEADSOFTWARE

Add player and physics
[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 (keyCode == Input.Keys.LEFT) {
16 gameProc.player.moveX.add(-GamePhysics.PL_SPEED,0);
17 gameProc.player.dir = 0;
18 }
19 if (keyCode == Input.Keys.RIGHT) {
20 gameProc.player.moveX.add(GamePhysics.PL_SPEED,0);
21 gameProc.player.dir = 1;
22 }
23 if (keyCode == Input.Keys.UP) gameProc.player.moveY.add(0,-12);
24 }
26 public void keyUp(int keyCode) {
27 if (keyCode == Input.Keys.RIGHT || keyCode == Input.Keys.LEFT) {
28 gameProc.player.moveX.x = 0;
29 }
30 }
32 public void mouseMoved(int screenX, int screenY) {
33 gameProc.cursorX = (int)((screenX+gameProc.renderer.camera.position.x)/16);
34 gameProc.cursorY = (int)((screenY+gameProc.renderer.camera.position.y)/16);
35 if (gameProc.cursorX < 0)
36 gameProc.cursorX = 0;
37 if (gameProc.cursorX >= gameProc.world.getWidth())
38 gameProc.cursorX = gameProc.world.getWidth()-1;
39 if (gameProc.cursorY < 0)
40 gameProc.cursorY = 0;
41 if (gameProc.cursorY >= gameProc.world.getHeight())
42 gameProc.cursorY = gameProc.world.getHeight()-1;
44 }
46 public void touchDown(int screenX, int screenY, int button) {
47 gameProc.touchDownX = screenX;
48 gameProc.touchDownY = screenY;
49 gameProc.touchDownTime = TimeUtils.millis();
50 gameProc.isTouchDown = true;
51 }
53 public void touchUp(int screenX, int screenY, int button) {
54 if (gameProc.isTouchDown) {
55 if (button == Input.Buttons.RIGHT){
56 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 1);
57 } else if (button == Input.Buttons.LEFT) {
58 if (gameProc.world.getForeMap(gameProc.cursorX, gameProc.cursorY) > 0) {
59 gameProc.world.placeToForeground(gameProc.cursorX, gameProc.cursorY, 0);
60 } else if (gameProc.world.getBackMap(gameProc.cursorX, gameProc.cursorY) > 0) {
61 gameProc.world.placeToBackground(gameProc.cursorX, gameProc.cursorY, 0);
62 }
63 }
64 }
65 gameProc.isTouchDown = false;
66 }
68 public void touchDragged(int screenX, int screenY) {
69 gameProc.renderer.camera.position.x += (gameProc.touchDownX-screenX);
70 gameProc.renderer.camera.position.y += (gameProc.touchDownY-screenY);
71 gameProc.touchDownX = screenX;
72 gameProc.touchDownY = screenY;
73 gameProc.isTouchDown = false;
74 }
76 }