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
.CaveGame
;
7 import ru
.deadsoftware
.cavecraft
.GameScreen
;
8 import ru
.deadsoftware
.cavecraft
.game
.mobs
.FallingGravel
;
9 import ru
.deadsoftware
.cavecraft
.game
.mobs
.FallingSand
;
10 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
11 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Pig
;
12 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
13 import ru
.deadsoftware
.cavecraft
.misc
.AppState
;
14 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
16 import java
.io
.Serializable
;
17 import java
.util
.ArrayList
;
19 public class GameProc
implements Serializable
{
21 public static double RUN_TIME
= 0;
23 public static boolean DO_UPD
= false;
24 public static int UPD_X
= -1, UPD_Y
= -1;
25 public static int FUPD_X
, FUPD_Y
;
29 public ArrayList
<Mob
> mobs
;
31 public transient GameWorld world
;
32 public transient GameRenderer renderer
;
33 public transient GamePhysics physics
;
35 public int cursorX
, cursorY
;
38 public int creativeScroll
, maxCreativeScroll
;
40 public boolean isTouchDown
, isKeyDown
, swim
;
41 public int touchDownX
, touchDownY
, keyDownCode
;
42 public int touchDownButton
;
43 public long touchDownTime
;
46 world
= new GameWorld();
47 world
.generate(1024,256);
48 player
= new Player(world
.getSpawnPoint());
49 mobs
= new ArrayList
<Mob
>();
50 for (int i
=0; i
<16; i
++) {
51 mobs
.add(new Pig(i
*256, 196*16));
53 physics
= new GamePhysics(this);
55 renderer
= new GameRenderer(this,320,
56 320*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
59 renderer
= new GameRenderer(this,480,
60 480*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
62 maxCreativeScroll
= Items
.ITEMS
.size()/8;
66 public void resetRenderer() {
68 renderer
= new GameRenderer(this,320,
69 320*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
71 renderer
= new GameRenderer(this,480,
72 480*((float)GameScreen
.getHeight()/GameScreen
.getWidth()));
76 private boolean isAutoselectable(int x
, int y
) {
77 return (world
.getForeMap(x
,y
)>0 &&
78 Items
.BLOCKS
.getValueAt(world
.getForeMap(x
,y
)).collision
);
81 private void moveCursor() {
82 if (ctrlMode
== 0 && CaveGame
.TOUCH
) {
83 cursorX
= (int) (player
.position
.x
+ player
.texWidth
/ 2) / 16;
84 if (player
.dir
== 0) cursorX
--;
86 cursorY
= (int) (player
.position
.y
+ player
.texWidth
) / 16;
87 if (!isAutoselectable(cursorX
, cursorY
)) {
90 if (!isAutoselectable(cursorX
, cursorY
)) {
93 if (!isAutoselectable(cursorX
, cursorY
)) {
94 if (player
.dir
== 0) cursorX
++;
97 } else if (!CaveGame
.TOUCH
){
98 cursorX
= (int)(Gdx
.input
.getX()*
99 (renderer
.camera
.viewportWidth
/GameScreen
.getWidth())+renderer
.camera
.position
.x
)/16;
100 cursorY
= (int)(Gdx
.input
.getY()*
101 (renderer
.camera
.viewportHeight
/GameScreen
.getHeight())+renderer
.camera
.position
.y
)/16;
102 if ((Gdx
.input
.getX()*
103 (renderer
.camera
.viewportWidth
/GameScreen
.getWidth())+renderer
.camera
.position
.x
)<0)
108 private void checkCursorBounds() {
109 if (cursorY
< 0) cursorY
= 0;
110 if (cursorY
>= world
.getHeight()) cursorY
= world
.getHeight()-1;
112 if (cursorX
*16+8<player
.position
.x
+player
.texWidth
/2)
114 if (cursorX
*16+8>player
.position
.x
+player
.texWidth
/2)
119 private void updateFluids(int x
, int y
) {
120 if (Items
.isWater(world
.getForeMap(x
, y
)) && world
.getForeMap(x
, y
)!=8) {
121 if (world
.getForeMap(x
, y
)==60) {
122 if (!Items
.isWater(world
.getForeMap(x
, y
- 1)))
123 world
.setForeMap(x
, y
, world
.getForeMap(x
, y
) + 1);
124 } else if ((!Items
.isWater(world
.getForeMap(x
-1,y
)) ||
125 (Items
.isWater(world
.getForeMap(x
,y
)) && world
.getForeMap(x
-1, y
)>=world
.getForeMap(x
, y
))) &&
126 (!Items
.isWater(world
.getForeMap(x
+1,y
)) ||
127 (Items
.isWater(world
.getForeMap(x
,y
)) && world
.getForeMap(x
+1, y
)>=world
.getForeMap(x
, y
)))){
128 world
.setForeMap(x
, y
, world
.getForeMap(x
, y
)+1);
130 if (world
.getForeMap(x
, y
)>63) world
.setForeMap(x
, y
, 0);
133 if (world
.getForeMap(x
, y
) == 8 || world
.getForeMap(x
, y
) == 60) {
134 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=61 && world
.getForeMap(x
, y
+1)<=63) ||
135 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
136 world
.setForeMap(x
,y
+1,60);
138 } else if (Items
.isLava(world
.getForeMap(x
, y
+1))) {
139 if (world
.getForeMap(x
, y
+1)>9) world
.setForeMap(x
, y
+1, 4);
140 else world
.setForeMap(x
, y
+1, 68);
141 } else if (Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
) {
142 if (world
.getForeMap(x
+1, y
)==0 ||
143 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
+1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
+1, y
))) ||
144 (Items
.isWater(world
.getForeMap(x
+1, y
)) && world
.getForeMap(x
+1, y
)>61)) {
145 world
.setForeMap(x
+1,y
,61);
146 updateBlock(x
+1, y
+1);
147 } else if (Items
.isLava(world
.getForeMap(x
+1, y
))) {
148 if (world
.getForeMap(x
+1, y
)>9) world
.setForeMap(x
+1, y
, 4);
149 else world
.setForeMap(x
+1, y
, 68);
150 } else if (world
.getForeMap(x
+1, y
)==61 && (world
.getForeMap(x
+2, y
)==8 || world
.getForeMap(x
+2, y
)==60)) world
.setForeMap(x
+1, y
, 8);
152 if (world
.getForeMap(x
-1, y
)==0 ||
153 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
-1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
-1, y
))) ||
154 (Items
.isWater(world
.getForeMap(x
-1, y
)) && world
.getForeMap(x
-1, y
)>61)) {
155 world
.setForeMap(x
-1,y
,61);
156 updateBlock(x
-1, y
+1);
157 } else if (Items
.isLava(world
.getForeMap(x
-1, y
))) {
158 if (world
.getForeMap(x
-1, y
)>9) world
.setForeMap(x
-1, y
, 4);
159 else world
.setForeMap(x
-1, y
, 68);
160 } else if (world
.getForeMap(x
-1, y
)==61 && (world
.getForeMap(x
-2, y
)==8 || world
.getForeMap(x
-2, y
)==60)) world
.setForeMap(x
-1, y
, 8);
164 if (world
.getForeMap(x
, y
) == 61) {
165 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=61 && world
.getForeMap(x
, y
+1)<=63) ||
166 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
167 world
.setForeMap(x
,y
+1,60);
169 } else if (Items
.isLava(world
.getForeMap(x
, y
+1))) {
170 if (world
.getForeMap(x
, y
+1)>9) world
.setForeMap(x
, y
+1, 4);
171 else world
.setForeMap(x
, y
+1, 68);
172 } else if (Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
) {
173 if (world
.getForeMap(x
+1, y
)==0 ||
174 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
+1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
+1, y
))) ||
175 (Items
.isWater(world
.getForeMap(x
+1, y
)) && world
.getForeMap(x
+1, y
)>62)){
176 world
.setForeMap(x
+1,y
,62);
177 updateBlock(x
+1, y
+1);
178 } else if (Items
.isLava(world
.getForeMap(x
+1, y
))) {
179 if (world
.getForeMap(x
+1, y
)>9) world
.setForeMap(x
+1, y
, 4);
180 else world
.setForeMap(x
+1, y
, 68);
183 if (world
.getForeMap(x
-1, y
)==0 ||
184 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
-1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
-1, y
))) ||
185 (Items
.isWater(world
.getForeMap(x
-1, y
)) && world
.getForeMap(x
-1, y
)>62)){
186 world
.setForeMap(x
-1,y
,62);
187 updateBlock(x
-1, y
+1);
188 } else if (Items
.isLava(world
.getForeMap(x
-1, y
))) {
189 if (world
.getForeMap(x
-1, y
)>9) world
.setForeMap(x
-1, y
, 4);
190 else world
.setForeMap(x
-1, y
, 68);
195 if (world
.getForeMap(x
, y
) == 62) {
196 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=61 && world
.getForeMap(x
, y
+1)<=63) ||
197 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
198 world
.setForeMap(x
,y
+1,60);
200 } else if (Items
.isLava(world
.getForeMap(x
, y
+1))) {
201 if (world
.getForeMap(x
, y
+1)>9) world
.setForeMap(x
, y
+1, 4);
202 else world
.setForeMap(x
, y
+1, 68);
203 } else if (Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
) {
204 if (world
.getForeMap(x
+1, y
)==0 ||
205 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
+1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
+1, y
))) ){
206 world
.setForeMap(x
+1,y
,63);
207 updateBlock(x
+1, y
+1);
208 } else if (Items
.isLava(world
.getForeMap(x
+1, y
))) {
209 if (world
.getForeMap(x
+1, y
)>9) world
.setForeMap(x
+1, y
, 4);
210 else world
.setForeMap(x
+1, y
, 68);
213 if (world
.getForeMap(x
-1, y
)==0 ||
214 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
-1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
-1, y
))) ){
215 world
.setForeMap(x
-1,y
,63);
216 updateBlock(x
-1, y
+1);
217 } else if (Items
.isLava(world
.getForeMap(x
-1, y
))) {
218 if (world
.getForeMap(x
-1, y
)>9) world
.setForeMap(x
-1, y
, 4);
219 else world
.setForeMap(x
-1, y
, 68);
224 if (world
.getForeMap(x
, y
) == 63) {
225 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=61 && world
.getForeMap(x
, y
+1)<=63) ||
226 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
227 world
.setForeMap(x
,y
+1,60);
229 } else if (Items
.isLava(world
.getForeMap(x
, y
+1))) {
230 if (world
.getForeMap(x
, y
+1)>9) world
.setForeMap(x
, y
+1, 4);
231 else world
.setForeMap(x
, y
+1, 68);
236 if (Items
.isLava(world
.getForeMap(x
, y
)) && world
.getForeMap(x
, y
)!=9) {
237 if (world
.getForeMap(x
, y
)==64) {
238 if (!Items
.isLava(world
.getForeMap(x
, y
- 1)))
239 world
.setForeMap(x
, y
, world
.getForeMap(x
, y
) + 1);
240 } else if ((!Items
.isLava(world
.getForeMap(x
,y
-1))) &&
241 (!Items
.isLava(world
.getForeMap(x
-1,y
)) ||
242 (Items
.isLava(world
.getForeMap(x
,y
)) && world
.getForeMap(x
-1, y
)>=world
.getForeMap(x
, y
))) &&
243 (!Items
.isLava(world
.getForeMap(x
+1,y
)) ||
244 (Items
.isLava(world
.getForeMap(x
,y
)) && world
.getForeMap(x
+1, y
)>=world
.getForeMap(x
, y
)))){
245 world
.setForeMap(x
, y
, world
.getForeMap(x
, y
)+1);
247 if (world
.getForeMap(x
, y
)>67) world
.setForeMap(x
, y
, 0);
250 if (world
.getForeMap(x
, y
) == 9 || world
.getForeMap(x
, y
) == 64) {
251 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=65 && world
.getForeMap(x
, y
+1)<=67) ||
252 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
253 world
.setForeMap(x
,y
+1,64);
255 } else if (Items
.isWater(world
.getForeMap(x
, y
+1))) {
256 world
.setForeMap(x
, y
+1, 1);
257 } else if (Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
) {
258 if (world
.getForeMap(x
+1, y
)==0 ||
259 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
+1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
+1, y
))) ||
260 (Items
.isLava(world
.getForeMap(x
+1, y
)) && world
.getForeMap(x
+1, y
)>65)) {
261 world
.setForeMap(x
+1,y
,65);
262 updateBlock(x
+1, y
+1);
263 } else if (Items
.isWater(world
.getForeMap(x
+1, y
))) {
264 world
.setForeMap(x
+1, y
, 1);
267 if (world
.getForeMap(x
-1, y
)==0 ||
268 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
-1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
-1, y
))) ||
269 (Items
.isLava(world
.getForeMap(x
-1, y
)) && world
.getForeMap(x
-1, y
)>65)) {
270 world
.setForeMap(x
-1,y
,65);
271 updateBlock(x
-1, y
+1);
272 } else if (Items
.isWater(world
.getForeMap(x
-1, y
))) {
273 world
.setForeMap(x
-1, y
, 1);
278 if (world
.getForeMap(x
, y
) == 65) {
279 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=65 && world
.getForeMap(x
, y
+1)<=67) ||
280 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
281 world
.setForeMap(x
,y
+1,64);
283 } else if (Items
.isWater(world
.getForeMap(x
, y
+1))) {
284 world
.setForeMap(x
, y
+1, 1);
285 } else if (Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
) {
286 if (world
.getForeMap(x
+1, y
)==0 ||
287 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
+1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
+1, y
))) ||
288 (Items
.isLava(world
.getForeMap(x
+1, y
)) && world
.getForeMap(x
+1, y
)>66)){
289 world
.setForeMap(x
+1,y
,66);
290 updateBlock(x
+1, y
+1);
291 } else if (Items
.isWater(world
.getForeMap(x
+1, y
))) {
292 world
.setForeMap(x
+1, y
, 1);
295 if (world
.getForeMap(x
-1, y
)==0 ||
296 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
-1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
-1, y
))) ||
297 (Items
.isLava(world
.getForeMap(x
-1, y
)) && world
.getForeMap(x
-1, y
)>66)){
298 world
.setForeMap(x
-1,y
,66);
299 updateBlock(x
-1, y
+1);
300 } else if (Items
.isWater(world
.getForeMap(x
-1, y
))) {
301 world
.setForeMap(x
-1, y
, 1);
306 if (world
.getForeMap(x
, y
) == 66) {
307 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=65 && world
.getForeMap(x
, y
+1)<=67) ||
308 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
309 world
.setForeMap(x
,y
+1,64);
311 } else if (Items
.isWater(world
.getForeMap(x
, y
+1))) {
312 world
.setForeMap(x
, y
+1, 1);
313 } else if (Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
) {
314 if (world
.getForeMap(x
+1, y
)==0 ||
315 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
+1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
+1, y
))) ){
316 world
.setForeMap(x
+1,y
,67);
317 updateBlock(x
+1, y
+1);
318 } else if (Items
.isWater(world
.getForeMap(x
+1, y
))) {
319 world
.setForeMap(x
+1, y
, 1);
322 if (world
.getForeMap(x
-1, y
)==0 ||
323 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
-1, y
)).collision
&& !Items
.isFluid(world
.getForeMap(x
-1, y
))) ){
324 world
.setForeMap(x
-1,y
,67);
325 updateBlock(x
-1, y
+1);
326 } else if (Items
.isWater(world
.getForeMap(x
-1, y
))) {
327 world
.setForeMap(x
-1, y
, 1);
332 if (world
.getForeMap(x
, y
) == 67) {
333 if (world
.getForeMap(x
, y
+1)==0 || (world
.getForeMap(x
, y
+1)>=65 && world
.getForeMap(x
, y
+1)<=67) ||
334 (!Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
&& !Items
.isFluid(world
.getForeMap(x
, y
+1)))) {
335 world
.setForeMap(x
,y
+1,64);
337 } else if (Items
.isWater(world
.getForeMap(x
, y
+1))) {
338 world
.setForeMap(x
, y
+1, 1);
344 private void updateBlock(int x
, int y
) {
345 if (world
.getForeMap(x
, y
) == 10) {
346 if (world
.getForeMap(x
, y
+1)==0 || !Items
.BLOCKS
.getValueAt(world
.getForeMap(x
,y
+1)).collision
) {
347 world
.setForeMap(x
, y
, 0);
348 mobs
.add(new FallingSand(x
*16, y
*16));
353 if (world
.getForeMap(x
, y
) == 11) {
354 if (world
.getForeMap(x
, y
+1)==0 || !Items
.BLOCKS
.getValueAt(world
.getForeMap(x
,y
+1)).collision
) {
355 world
.setForeMap(x
, y
, 0);
356 mobs
.add(new FallingGravel(x
*16, y
*16));
361 if (world
.getForeMap(x
, y
) == 59) {
362 if (world
.getForeMap(x
, y
+1)==0 || !Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
+1)).collision
) {
363 world
.setForeMap(x
,y
,0);
368 if (world
.getForeMap(x
, y
) == 2) {
369 if (world
.getForeMap(x
, y
-1)>0 && (Items
.BLOCKS
.getValueAt(world
.getForeMap(x
, y
-1)).collision
||
370 Items
.isFluid(world
.getForeMap(x
, y
-1)))) {
371 world
.setForeMap(x
, y
, 3);
376 public void useItem(int x
, int y
, int id
, boolean bg
) {
377 if (Items
.ITEMS
.get(id
).getType()==0) {
378 if (!bg
) world
.placeToForeground(x
, y
, Items
.ITEMS
.get(id
).getBlock());
379 else world
.placeToBackground(x
, y
, Items
.ITEMS
.get(id
).getBlock());
383 public void update(float delta
) {
387 for (int y
=UPD_Y
; y
<UPD_Y
+16; y
++)
388 for (int x
=UPD_X
; x
<UPD_X
+16; x
++) {
394 for (int y
=0; y
<world
.getHeight(); y
++) {
395 for (int x
=(int)renderer
.camera
.position
.x
/16-1; x
<(int)(renderer
.camera
.position
.x
+renderer
.camera
.viewportWidth
)/16+1; x
++) {
400 updateFluids(FUPD_X
, FUPD_Y
);
402 if (FUPD_X
>=(int)(renderer
.camera
.position
.x
+renderer
.camera
.viewportWidth
)/16+1) {
403 FUPD_X
= (int) renderer
.camera
.position
.x
/ 16 - 1;
405 if (FUPD_Y
>=(int)(renderer
.camera
.position
.y
+renderer
.camera
.viewportHeight
)/16+1) {
406 FUPD_Y
= (int) renderer
.camera
.position
.y
/ 16 - 1;
410 physics
.update(delta
);
414 if (isTouchDown
&& TimeUtils
.timeSinceMillis(touchDownTime
) > 500) {
415 if (touchDownButton
== Input
.Buttons
.RIGHT
) {
416 useItem(cursorX
, cursorY
, player
.inventory
[invSlot
], true);
417 } else if (touchDownY
< Assets
.invBar
.getRegionHeight() &&
418 touchDownX
>renderer
.camera
.viewportWidth
/2-Assets
.invBar
.getRegionWidth()/2 &&
419 touchDownX
<renderer
.camera
.viewportWidth
/2+Assets
.invBar
.getRegionWidth()/2) {
420 CaveGame
.STATE
= AppState
.GAME_CREATIVE_INV
;