1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.math
.MathUtils
;
4 import com
.badlogic
.gdx
.math
.Rectangle
;
5 import com
.badlogic
.gdx
.math
.Vector2
;
6 import com
.badlogic
.gdx
.math
.Vector3
;
7 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
8 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
10 public class GamePhysics
{
12 public static final int PL_SPEED
= 2;
14 private GameProc gameProc
;
16 private Vector2 gravity
;
18 public GamePhysics(GameProc gameProc
) {
19 this.gameProc
= gameProc
;
20 gravity
= new Vector2(0,1);
23 private boolean checkJump(Rectangle rect
, int dir
) {
27 bl
= gameProc
.world
.getForeMap(
28 (int)((rect
.x
+(rect
.width
/2))/16) - 1,
32 bl
= gameProc
.world
.getForeMap(
33 (int)((rect
.x
+(rect
.width
/2))/16) + 1,
42 private boolean checkColl(Rectangle rect
) {
43 int[] bl
= new int [6];
44 bl
[0] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)rect
.y
/16));
45 bl
[1] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)rect
.y
/16));
46 bl
[2] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
/2)/16));
47 bl
[3] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+rect
.height
/2)/16));
48 bl
[4] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
-1)/16));
49 bl
[5] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+(rect
.height
-1))/16));
50 for (int b
: bl
) if (b
>0) {
56 private void playerPhy(Player pl
) {
57 pl
.position
.add(pl
.moveY
);
58 if (checkColl(pl
.getRect())) {
62 if (pl
.moveY
.y
<0) d
=1; else if (pl
.moveY
.y
>0) d
=-1;
63 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
64 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
69 if (!pl
.flyMode
) pl
.moveY
.add(gravity
);
70 pl
.position
.add(pl
.moveX
);
71 if (pl
.position
.x
<0 ||
72 pl
.position
.x
+pl
.texWidth
>=gameProc
.world
.getWidth()*16)
73 pl
.position
.sub(pl
.moveX
);
74 if (checkColl(pl
.getRect())) {
76 if (pl
.moveX
.x
<0) d
=1; else if (pl
.moveX
.x
>0) d
=-1;
77 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
78 while (checkColl(pl
.getRect())) pl
.position
.x
+=d
;
81 if (checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
87 private void mobPhy(Mob mob
) {
88 mob
.position
.add(mob
.moveY
);
89 if (checkColl(mob
.getRect())) {
92 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
93 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
94 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
99 mob
.moveY
.add(gravity
);
100 mob
.position
.add(mob
.moveX
);
101 if (mob
.position
.x
<32 ||
102 mob
.position
.x
+mob
.width
>gameProc
.world
.getWidth()*16-32)
103 mob
.position
.sub(mob
.moveX
);
104 if (checkColl(mob
.getRect())) {
106 if (mob
.moveX
.x
<0) d
=1; else if (mob
.moveX
.x
>0) d
=-1;
107 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
108 while (checkColl(mob
.getRect())) mob
.position
.x
+=d
;
112 public void update(float delta
) {
114 for (Mob mob
: gameProc
.mobs
) {
118 playerPhy(gameProc
.player
);
120 gameProc
.renderer
.camera
.position
.set(
121 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
122 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,