1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.math
.*;
4 import ru
.deadsoftware
.cavecraft
.Items
;
5 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
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,1);
21 private boolean checkJump(Rectangle rect
, int dir
) {
25 if ((int)((rect
.x
+(rect
.width
/2))/16) - 1>=0)
26 bl
= gameProc
.world
.getForeMap(
27 (int)((rect
.x
+(rect
.width
/2))/16) - 1,
31 if ((int)((rect
.x
+(rect
.width
/2))/16) + 1<gameProc
.world
.getWidth())
32 bl
= gameProc
.world
.getForeMap(
33 (int)((rect
.x
+(rect
.width
/2))/16) + 1,
39 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
42 private boolean checkColl(Rectangle rect
) {
43 int[] ids
= new int[6];
44 ids
[0] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)rect
.y
/16));
45 ids
[1] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)rect
.y
/16));
46 ids
[2] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
/2)/16));
47 ids
[3] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+rect
.height
/2)/16));
48 ids
[4] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
-1)/16));
49 ids
[5] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+(rect
.height
-1))/16));
50 Rectangle
[] bl
= new Rectangle
[6];
51 if (ids
[0]>0) bl
[0] = Items
.BLOCKS
.getValueAt(ids
[0]).getRect((int)(rect
.x
)/16,(int)rect
.y
/16);
52 if (ids
[1]>0) bl
[1] = Items
.BLOCKS
.getValueAt(ids
[1]).getRect(((int)(rect
.x
+rect
.width
-1)/16), ((int)rect
.y
/16));
53 if (ids
[2]>0) bl
[2] = Items
.BLOCKS
.getValueAt(ids
[2]).getRect(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
/2)/16));
54 if (ids
[3]>0) bl
[3] = Items
.BLOCKS
.getValueAt(ids
[3]).getRect(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+rect
.height
/2)/16));
55 if (ids
[4]>0) bl
[4] = Items
.BLOCKS
.getValueAt(ids
[4]).getRect(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
-1)/16));
56 if (ids
[5]>0) bl
[5] = Items
.BLOCKS
.getValueAt(ids
[5]).getRect(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+(rect
.height
-1))/16));
57 for (int i
=0; i
<6; i
++) {
59 Items
.BLOCKS
.getValueAt(ids
[i
]).collision
&&
60 Intersector
.overlaps(rect
,bl
[i
]))
66 private void playerPhy(Player pl
) {
67 pl
.position
.add(pl
.moveY
);
68 if (checkColl(pl
.getRect())) {
72 if (pl
.moveY
.y
<0) d
=1; else if (pl
.moveY
.y
>0) d
=-1;
73 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
74 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
79 if (!pl
.flyMode
) pl
.moveY
.add(gravity
);
80 pl
.position
.add(pl
.moveX
);
81 if (pl
.position
.x
<0 ||
82 pl
.position
.x
+pl
.texWidth
>=gameProc
.world
.getWidth()*16)
83 pl
.position
.sub(pl
.moveX
);
84 if (checkColl(pl
.getRect())) {
86 if (pl
.moveX
.x
<0) d
=1; else if (pl
.moveX
.x
>0) d
=-1;
87 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
88 while (checkColl(pl
.getRect())) pl
.position
.x
+=d
;
91 /*if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
97 private void mobPhy(Mob mob
) {
98 mob
.position
.add(mob
.moveY
);
99 if (checkColl(mob
.getRect())) {
102 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
103 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
104 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
109 mob
.moveY
.add(gravity
);
110 mob
.position
.add(mob
.moveX
);
111 if (mob
.position
.x
<32 ||
112 mob
.position
.x
+mob
.width
>gameProc
.world
.getWidth()*16-32)
113 mob
.position
.sub(mob
.moveX
);
114 if (checkColl(mob
.getRect())) {
116 if (mob
.moveX
.x
<0) d
=1; else if (mob
.moveX
.x
>0) d
=-1;
117 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
118 while (checkColl(mob
.getRect())) mob
.position
.x
+=d
;
122 public void update(float delta
) {
124 for (Mob mob
: gameProc
.mobs
) {
128 playerPhy(gameProc
.player
);
130 gameProc
.renderer
.camera
.position
.set(
131 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
132 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,