490960b8986efffa5c4a24a81b1ef55136a0b65a
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 private void wasdPressed(int keyCode
) {
19 if (gameProc
.ctrlMode
==0) {
22 gameProc
.player
.moveX
.add(-GamePhysics
.PL_SPEED
, 0);
23 gameProc
.player
.dir
= 0;
26 gameProc
.player
.moveX
.add(GamePhysics
.PL_SPEED
, 0);
27 gameProc
.player
.dir
= 1;
45 if (gameProc
.cursorX
< 0)
47 if (gameProc
.cursorX
>= gameProc
.world
.getWidth())
48 gameProc
.cursorX
= gameProc
.world
.getWidth()-1;
49 if (gameProc
.cursorY
< 0)
51 if (gameProc
.cursorY
>= gameProc
.world
.getHeight())
52 gameProc
.cursorY
= gameProc
.world
.getHeight()-1;
56 public void keyDown(int keyCode
) {
57 if (keyCode
== Input
.Keys
.W
|| keyCode
== Input
.Keys
.A
||
58 keyCode
== Input
.Keys
.S
|| keyCode
== Input
.Keys
.D
) {
60 } else switch (keyCode
) {
61 case Input
.Keys
.ALT_LEFT
:
63 gameProc
.cursorX
= (int)(gameProc
.player
.position
.x
/16);
64 gameProc
.cursorY
= (int)(gameProc
.player
.position
.y
/16);
65 if (gameProc
.ctrlMode
> 1) gameProc
.ctrlMode
= 0;
68 case Input
.Keys
.SPACE
:
69 if (gameProc
.player
.canJump
) {
70 gameProc
.player
.moveY
.add(0, -7);
71 } else if (!gameProc
.player
.flyMode
) {
72 gameProc
.player
.flyMode
= true;
73 gameProc
.player
.moveY
.setZero();
75 gameProc
.player
.moveY
.y
= -GamePhysics
.PL_SPEED
;
79 case Input
.Keys
.CONTROL_LEFT
:
80 gameProc
.player
.moveY
.y
= GamePhysics
.PL_SPEED
;
84 gameProc
.renderer
.showCreative
= !gameProc
.renderer
.showCreative
;
89 public void keyUp(int keyCode
) {
91 case Input
.Keys
.A
: case Input
.Keys
.D
:
92 gameProc
.player
.moveX
.x
= 0;
95 case Input
.Keys
.SPACE
: case Input
.Keys
.CONTROL_LEFT
:
96 if (gameProc
.player
.flyMode
) gameProc
.player
.moveY
.setZero();
101 public void mouseMoved(int screenX
, int screenY
) {
104 public void touchDown(int screenX
, int screenY
, int button
) {
105 if (gameProc
.renderer
.showCreative
&&
106 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.creativeInv
.getRegionWidth()/2 &&
107 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.creativeInv
.getRegionWidth()/2 &&
108 screenY
>gameProc
.renderer
.camera
.viewportHeight
/2-Assets
.creativeInv
.getRegionHeight()/2 &&
109 screenY
<gameProc
.renderer
.camera
.viewportHeight
/2+Assets
.creativeInv
.getRegionHeight()/2) {
111 int ix
= (int) (screenX
- (gameProc
.renderer
.camera
.viewportWidth
/ 2 - Assets
.creativeInv
.getRegionWidth() / 2 + 8)) / 18;
112 int iy
= (int) (screenY
- (gameProc
.renderer
.camera
.viewportHeight
/ 2 - Assets
.creativeInv
.getRegionHeight() / 2 + 18)) / 18;
113 int item
= ix
+ iy
* 8;
114 for (int i
= 8; i
> 0; i
--) {
115 gameProc
.player
.inventory
[i
] = gameProc
.player
.inventory
[i
- 1];
117 if (item
>= 0 && item
< Items
.BLOCKS
.size
) gameProc
.player
.inventory
[0] = item
;
118 } catch (Exception e
) {
119 Gdx
.app
.error("GameInputHandler", e
.toString());
121 } else if (gameProc
.renderer
.showCreative
) {
122 gameProc
.renderer
.showCreative
= !gameProc
.renderer
.showCreative
;
124 gameProc
.touchDownX
= screenX
;
125 gameProc
.touchDownY
= screenY
;
126 gameProc
.touchDownTime
= TimeUtils
.millis();
127 gameProc
.isTouchDown
= true;
128 gameProc
.touchDownButton
= button
;
132 public void touchUp(int screenX
, int screenY
, int button
) {
133 if (gameProc
.isTouchDown
) {
134 if (button
== Input
.Buttons
.RIGHT
){
135 if (gameProc
.ctrlMode
==1) {
136 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
,
137 gameProc
.player
.inventory
[gameProc
.invSlot
]);
139 } else if (button
== Input
.Buttons
.LEFT
) {
140 if (gameProc
.ctrlMode
==1) {
141 if (gameProc
.world
.getForeMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
142 gameProc
.world
.placeToForeground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
143 } else if (gameProc
.world
.getBackMap(gameProc
.cursorX
, gameProc
.cursorY
) > 0) {
144 gameProc
.world
.placeToBackground(gameProc
.cursorX
, gameProc
.cursorY
, 0);
147 if (screenY
<Assets
.invBar
.getRegionHeight() &&
148 screenX
>gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
149 screenX
<gameProc
.renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
150 gameProc
.invSlot
= (int)((screenX
-(gameProc
.renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2))/20);
155 gameProc
.isTouchDown
= false;
158 public void touchDragged(int screenX
, int screenY
) {
161 public void scrolled(int amount
) {
162 gameProc
.invSlot
+= amount
;
163 if (gameProc
.invSlot
< 0) gameProc
.invSlot
= 8;
164 if (gameProc
.invSlot
> 8) gameProc
.invSlot
= 0;