c288afface0331516b5feb2969414f3fecfd1c46
1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.Input
;
5 import com
.badlogic
.gdx
.utils
.TimeUtils
;
6 import ru
.deadsoftware
.cavecraft
.Assets
;
7 import ru
.deadsoftware
.cavecraft
.GameScreen
;
8 import ru
.deadsoftware
.cavecraft
.Items
;
10 public class GameInputHandler
{
12 private GameProc gameProc
;
14 public GameInputHandler(GameProc gameProc
) {
15 this.gameProc
= gameProc
;
18 public void keyDown(int keyCode
) {
19 if (gameProc
.ctrlMode
==0) {
20 if (keyCode
== Input
.Keys
.A
) {
21 gameProc
.player
.moveX
.add(-GamePhysics
.PL_SPEED
, 0);
22 gameProc
.player
.dir
= 0;
24 if (keyCode
== Input
.Keys
.D
) {
25 gameProc
.player
.moveX
.add(GamePhysics
.PL_SPEED
, 0);
26 gameProc
.player
.dir
= 1;
29 if (keyCode
== Input
.Keys
.A
) {
32 if (keyCode
== Input
.Keys
.D
) {
35 if (keyCode
== Input
.Keys
.W
) {
38 if (keyCode
== Input
.Keys
.S
) {
41 if (gameProc
.cursorX
< 0)
43 if (gameProc
.cursorX
>= gameProc
.world
.getWidth())
44 gameProc
.cursorX
= gameProc
.world
.getWidth()-1;
45 if (gameProc
.cursorY
< 0)
47 if (gameProc
.cursorY
>= gameProc
.world
.getHeight())
48 gameProc
.cursorY
= gameProc
.world
.getHeight()-1;
50 if (keyCode
== Input
.Keys
.ALT_LEFT
) {
52 gameProc
.cursorX
= (int)(gameProc
.player
.position
.x
/16);
53 gameProc
.cursorY
= (int)(gameProc
.player
.position
.y
/16);
54 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
56 if (keyCode
== Input
.Keys
.SPACE
) {
57 if (gameProc
.player
.canJump
) {
58 gameProc
.player
.moveY
.add(0, -7);
59 } else if (!gameProc
.player
.flyMode
) {
60 gameProc
.player
.flyMode
= true;
61 gameProc
.player
.moveY
.setZero();
63 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
66 if (keyCode
== Input
.Keys
.CONTROL_LEFT
) {
67 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
69 if (keyCode
== Input
.Keys
.E
) {
70 gameProc
.renderer
.showCreative
= !gameProc
.renderer
.showCreative
;
74 public void keyUp(int keyCode
) {
75 if (keyCode
== Input
.Keys
.A
|| keyCode
== Input
.Keys
.D
) {
76 gameProc
.player
.moveX
.x
= 0;
78 if (keyCode
== Input
.Keys
.SPACE
) {
79 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
81 if (keyCode
== Input
.Keys
.CONTROL_LEFT
) {
82 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
86 public void mouseMoved(int screenX
, int screenY
) {
89 public void touchDown(int screenX
, int screenY
, int button
) {
90 gameProc
.touchDownX
= screenX
;
91 gameProc
.touchDownY
= screenY
;
92 gameProc
.touchDownTime
= TimeUtils
.millis();
93 gameProc
.isTouchDown
= true;
94 if (gameProc
.renderer
.showCreative
) {
96 int ix
= (int)(screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2+8))/18;
97 int iy
= (int)(screenY
-(gameProc
.renderer
.camera
.viewportHeight
/2 - Assets
.creativeInv
.getRegionHeight()/2+18))/18;
99 for (int i
=8; i
>0; i
--) {
100 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
-1];
102 if (item
>=0 && item
<Items
.BLOCKS
.size
) gameProc
.player
.inventory
[0] = item
;
103 } catch (Exception e
) {
104 Gdx
.app
.error("GameInputHandler", e
.toString());
109 public void touchUp(int screenX
, int screenY
, int button
) {
110 if (gameProc
.isTouchDown
) {
111 if (button
== Input
.Buttons
.RIGHT
){
112 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
113 gameProc
.player
.inventory
[gameProc
.invSlot
]);
114 } else if (button
== Input
.Buttons
.LEFT
) {
115 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
116 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
117 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
118 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
122 gameProc
.isTouchDown
= false;
125 public void touchDragged(int screenX
, int screenY
) {
128 public void scrolled(int amount
) {
129 gameProc
.invSlot
+= amount
;
130 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
131 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;