2d59d349118d0da781cc7d703aa9a2811768626a
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
.Items
;
8 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
9 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
11 public class GamePhysics
{
13 public static final int PL_SPEED
= 2;
15 private GameProc gameProc
;
17 private Vector2 gravity
;
19 public GamePhysics(GameProc gameProc
) {
20 this.gameProc
= gameProc
;
21 gravity
= new Vector2(0,1);
24 private boolean checkJump(Rectangle rect
, int dir
) {
28 if ((int)((rect
.x
+(rect
.width
/2))/16) - 1>=0)
29 bl
= gameProc
.world
.getForeMap(
30 (int)((rect
.x
+(rect
.width
/2))/16) - 1,
34 if ((int)((rect
.x
+(rect
.width
/2))/16) + 1<gameProc
.world
.getWidth())
35 bl
= gameProc
.world
.getForeMap(
36 (int)((rect
.x
+(rect
.width
/2))/16) + 1,
42 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
45 private boolean checkColl(Rectangle rect
) {
46 int[] bl
= new int [6];
47 bl
[0] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)rect
.y
/16));
48 bl
[1] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)rect
.y
/16));
49 bl
[2] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
/2)/16));
50 bl
[3] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+rect
.height
/2)/16));
51 bl
[4] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
-1)/16));
52 bl
[5] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+(rect
.height
-1))/16));
53 for (int b
: bl
) if (b
>0 && Items
.BLOCKS
.getValueAt(b
).collision
) {
59 private void playerPhy(Player pl
) {
60 pl
.position
.add(pl
.moveY
);
61 if (checkColl(pl
.getRect())) {
65 if (pl
.moveY
.y
<0) d
=1; else if (pl
.moveY
.y
>0) d
=-1;
66 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
67 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
72 if (!pl
.flyMode
) pl
.moveY
.add(gravity
);
73 pl
.position
.add(pl
.moveX
);
74 if (pl
.position
.x
<0 ||
75 pl
.position
.x
+pl
.texWidth
>=gameProc
.world
.getWidth()*16)
76 pl
.position
.sub(pl
.moveX
);
77 if (checkColl(pl
.getRect())) {
79 if (pl
.moveX
.x
<0) d
=1; else if (pl
.moveX
.x
>0) d
=-1;
80 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
81 while (checkColl(pl
.getRect())) pl
.position
.x
+=d
;
84 if (checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
90 private void mobPhy(Mob mob
) {
91 mob
.position
.add(mob
.moveY
);
92 if (checkColl(mob
.getRect())) {
95 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
96 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
97 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
102 mob
.moveY
.add(gravity
);
103 mob
.position
.add(mob
.moveX
);
104 if (mob
.position
.x
<32 ||
105 mob
.position
.x
+mob
.width
>gameProc
.world
.getWidth()*16-32)
106 mob
.position
.sub(mob
.moveX
);
107 if (checkColl(mob
.getRect())) {
109 if (mob
.moveX
.x
<0) d
=1; else if (mob
.moveX
.x
>0) d
=-1;
110 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
111 while (checkColl(mob
.getRect())) mob
.position
.x
+=d
;
115 public void update(float delta
) {
117 for (Mob mob
: gameProc
.mobs
) {
121 playerPhy(gameProc
.player
);
123 gameProc
.renderer
.camera
.position
.set(
124 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
125 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,