1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.Input
;
5 import com
.badlogic
.gdx
.utils
.Disposable
;
6 import com
.badlogic
.gdx
.utils
.TimeUtils
;
7 import ru
.deadsoftware
.cavedroid
.CaveGame
;
8 import ru
.deadsoftware
.cavedroid
.GameScreen
;
9 import ru
.deadsoftware
.cavedroid
.game
.mobs
.FallingGravel
;
10 import ru
.deadsoftware
.cavedroid
.game
.mobs
.FallingSand
;
11 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
12 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Pig
;
13 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
14 import ru
.deadsoftware
.cavedroid
.game
.objects
.Player
;
15 import ru
.deadsoftware
.cavedroid
.misc
.AppState
;
16 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
18 import java
.io
.Serializable
;
19 import java
.util
.ArrayList
;
21 public class GameProc
implements Serializable
, Disposable
{
22 static boolean DO_UPD
= false;
23 static int UPD_X
= -1, UPD_Y
= -1;
25 private transient GameFluidsThread fluidThread
;
27 public transient GameWorld world
;
28 public transient GameRenderer renderer
;
29 public transient GamePhysics physics
;
32 public ArrayList
<Mob
> mobs
;
33 public ArrayList
<Drop
> drops
;
35 public boolean isTouchDown
, isKeyDown
;
36 public int ctrlMode
, touchDownX
, touchDownY
, touchDownBtn
, keyDownCode
;
37 public long touchDownTime
;
40 int creativeScroll
, maxCreativeScroll
;
43 public void initGame(int gameMode
) {
44 world
= new GameWorld();
45 world
.generate(1024, 256);
46 player
= new Player(gameMode
);
47 drops
= new ArrayList
<>();
48 mobs
= new ArrayList
<>();
49 for (int i
= 0; i
< 16; i
++) {
50 mobs
.add(new Pig(i
* 256, 196 * 16));
52 physics
= new GamePhysics();
54 int scale
= CaveGame
.TOUCH ?
320 : 480;
55 if (CaveGame
.TOUCH
) ctrlMode
= 1;
56 renderer
= new GameRenderer(scale
, scale
* GameScreen
.getHeight() / GameScreen
.getWidth());
58 maxCreativeScroll
= GameItems
.getItemsSize() / 8;
66 private void createFluidThread() {
67 fluidThread
= new GameFluidsThread();
70 public void resetRenderer() {
72 renderer
= new GameRenderer(320,
73 320 * ((float) GameScreen
.getHeight() / GameScreen
.getWidth()));
75 renderer
= new GameRenderer(480,
76 480 * ((float) GameScreen
.getHeight() / GameScreen
.getWidth()));
80 private boolean isAutoselectable(int x
, int y
) {
81 return (world
.getForeMap(x
, y
) > 0 &&
82 GameItems
.getBlock(world
.getForeMap(x
, y
)).hasCollision());
85 private void moveCursor() {
86 int pastX
= curX
, pastY
= curY
;
87 if (ctrlMode
== 0 && CaveGame
.TOUCH
) {
88 curX
= player
.getMapX();
89 if (player
.getDir() == 0) curX
--;
91 curY
= (int) (player
.pos
.y
+ player
.getWidth()) / 16;
92 if (!isAutoselectable(curX
, curY
)) {
95 if (!isAutoselectable(curX
, curY
)) {
98 if (!isAutoselectable(curX
, curY
)) {
99 if (player
.getDir() == 0) curX
++;
102 } else if (!CaveGame
.TOUCH
) {
103 curX
= (int) (Gdx
.input
.getX() *
104 (renderer
.getWidth() / GameScreen
.getWidth()) + renderer
.getCamX()) / 16;
105 curY
= (int) (Gdx
.input
.getY() *
106 (renderer
.getHeight() / GameScreen
.getHeight()) + renderer
.getCamY()) / 16;
107 if ((Gdx
.input
.getX() *
108 (renderer
.getWidth() / GameScreen
.getWidth()) + renderer
.getCamX()) < 0)
111 if (pastX
!= curX
|| pastY
!= curY
) blockDmg
= 0;
114 private void checkCursorBounds() {
115 if (curY
< 0) curY
= 0;
116 if (curY
>= world
.getHeight()) curY
= world
.getHeight() - 1;
118 if (curX
* 16 + 8 < player
.pos
.x
+ player
.getWidth() / 2)
120 if (curX
* 16 + 8 > player
.pos
.x
+ player
.getWidth() / 2)
125 private void updateBlock(int x
, int y
) {
126 if (world
.getForeMap(x
, y
) == 10) {
127 if (world
.getForeMap(x
, y
+ 1) == 0 || !GameItems
.getBlock(world
.getForeMap(x
, y
+ 1)).hasCollision()) {
128 world
.setForeMap(x
, y
, 0);
129 mobs
.add(new FallingSand(x
* 16, y
* 16));
130 updateBlock(x
, y
- 1);
134 if (world
.getForeMap(x
, y
) == 11) {
135 if (world
.getForeMap(x
, y
+ 1) == 0 || !GameItems
.getBlock(world
.getForeMap(x
, y
+ 1)).hasCollision()) {
136 world
.setForeMap(x
, y
, 0);
137 mobs
.add(new FallingGravel(x
* 16, y
* 16));
138 updateBlock(x
, y
- 1);
142 if (world
.getForeMap(x
, y
) > 0 && GameItems
.getBlock(world
.getForeMap(x
, y
)).requiresBlock()) {
143 if (world
.getForeMap(x
, y
+ 1) == 0 || !GameItems
.getBlock(world
.getForeMap(x
, y
+ 1)).hasCollision()) {
144 world
.destroyForeMap(x
, y
);
145 updateBlock(x
, y
- 1);
149 if (world
.getForeMap(x
, y
) == 2) {
150 if (world
.getForeMap(x
, y
- 1) > 0 && (GameItems
.getBlock(world
.getForeMap(x
, y
- 1)).hasCollision() ||
151 GameItems
.isFluid(world
.getForeMap(x
, y
- 1)))) {
152 world
.setForeMap(x
, y
, 3);
157 void useItem(int x
, int y
, int id
, boolean bg
) {
159 if (GameItems
.getItem(id
).isBlock()) {
160 if (!bg
) world
.placeToForeground(x
, y
, GameItems
.getBlockIdByItemId(id
));
161 else world
.placeToBackground(x
, y
, GameItems
.getBlockIdByItemId(id
));
165 world
.placeToForeground(x
, y
, 8);
166 player
.inv
[player
.invSlot
] = 64;
169 world
.placeToForeground(x
, y
, 9);
170 player
.inv
[player
.invSlot
] = 64;
177 public void update(float delta
) {
179 for (int y
= UPD_Y
; y
< UPD_Y
+ 16; y
++)
180 for (int x
= UPD_X
; x
< UPD_X
+ 16; x
++) {
186 physics
.update(delta
);
190 if (isTouchDown
&& touchDownBtn
== Input
.Buttons
.LEFT
) {
191 if ((world
.getForeMap(curX
, curY
) > 0 && GameItems
.getBlock(world
.getForeMap(curX
, curY
)).getHp() >= 0) ||
192 (world
.getForeMap(curX
, curY
) == 0 &&
193 world
.getBackMap(curX
, curY
) > 0 &&
194 GameItems
.getBlock(world
.getBackMap(curX
, curY
)).getHp() >= 0)) {
195 if (player
.gameMode
== 0) {
197 if (world
.getForeMap(curX
, curY
) > 0) {
198 if (blockDmg
>= GameItems
.getBlock(world
.getForeMap(curX
, curY
)).getHp()) {
199 world
.destroyForeMap(curX
, curY
);
202 } else if (world
.getBackMap(curX
, curY
) > 0) {
203 if (blockDmg
>= GameItems
.getBlock(world
.getBackMap(curX
, curY
)).getHp()) {
204 world
.destroyBackMap(curX
, curY
);
209 if (world
.getForeMap(curX
, curY
) > 0) world
.placeToForeground(curX
, curY
, 0);
210 else if (world
.getBackMap(curX
, curY
) > 0) world
.placeToBackground(curX
, curY
, 0);
216 if (isTouchDown
&& TimeUtils
.timeSinceMillis(touchDownTime
) > 500) {
217 if (touchDownBtn
== Input
.Buttons
.RIGHT
) {
218 useItem(curX
, curY
, player
.inv
[player
.invSlot
], true);
220 } else if (touchDownY
< Assets
.invBar
.getRegionHeight() &&
221 touchDownX
> renderer
.getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 &&
222 touchDownX
< renderer
.getWidth() / 2 + (float) Assets
.invBar
.getRegionWidth() / 2) {
223 CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;
228 if (fluidThread
== null || !fluidThread
.isAlive()) {
235 public void dispose() {
236 fluidThread
.interrupt();