1ed455b75ce04cd450e7c6c5b407dfe31b205b8b
1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.math
.Rectangle
;
5 import com
.badlogic
.gdx
.math
.Vector2
;
6 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
8 public class GamePhysics
{
10 public static final int PL_SPEED
= 2;
12 private GameProc gameProc
;
14 private Vector2 gravity
;
16 public GamePhysics(GameProc gameProc
) {
17 this.gameProc
= gameProc
;
18 gravity
= new Vector2(0,2);
21 private boolean checkColl(Rectangle rect
) {
22 int[] bl
= new int [6];
23 bl
[0] = gameProc
.world
.getForeMap(((int)rect
.x
/16), ((int)rect
.y
/16));
24 bl
[1] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)rect
.y
/16));
25 bl
[2] = gameProc
.world
.getForeMap(((int)rect
.x
/16), ((int)(rect
.y
+rect
.height
/2)/16));
26 bl
[3] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+rect
.height
/2)/16));
27 bl
[4] = gameProc
.world
.getForeMap(((int)rect
.x
/16), ((int)(rect
.y
+rect
.height
-1)/16));
28 bl
[5] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+(rect
.height
-1))/16));
29 for (int b
: bl
) if (b
>0) {
35 private void movePlayer(Player pl
) {
36 pl
.position
.add(pl
.moveX
);
37 if (checkColl(pl
.getRect())) {
39 if (pl
.moveX
.x
<0) d
=1; else if (pl
.moveX
.x
>0) d
=-1;
40 while (checkColl(pl
.getRect())) pl
.position
.x
+=d
;
43 pl
.position
.add(pl
.moveY
);
44 if (checkColl(pl
.getRect())) {
46 if (pl
.moveY
.y
<0) d
=1; else if (pl
.moveY
.y
>0) d
=-1;
47 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
50 pl
.moveY
.add(gravity
);
53 gameProc
.renderer
.camTargetPos
.x
= pl
.position
.x
-gameProc
.renderer
.camera
.viewportWidth
+100;
56 gameProc
.renderer
.camTargetPos
.x
= pl
.position
.x
-100;
61 public void update(float delta
) {
62 movePlayer(gameProc
.player
);
63 if (gameProc
.renderer
.camera
.position
.x
- gameProc
.renderer
.camTargetPos
.x
<= 8 &&
64 gameProc
.renderer
.camera
.position
.x
- gameProc
.renderer
.camTargetPos
.x
>= -8) {
65 gameProc
.renderer
.camera
.position
.x
= gameProc
.renderer
.camTargetPos
.x
;
67 if (gameProc
.renderer
.camera
.position
.x
> gameProc
.renderer
.camTargetPos
.x
) {
68 gameProc
.renderer
.camera
.position
.sub(16,0,0);
70 if (gameProc
.renderer
.camera
.position
.x
< gameProc
.renderer
.camTargetPos
.x
) {
71 gameProc
.renderer
.camera
.position
.add(16,0,0);
73 gameProc
.renderer
.camera
.position
.y
= gameProc
.player
.position
.y
+gameProc
.player
.height
/2
74 -gameProc
.renderer
.camera
.viewportHeight
/2;