1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.math
.*;
4 import ru
.deadsoftware
.cavecraft
.CaveGame
;
5 import ru
.deadsoftware
.cavecraft
.Items
;
6 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
7 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
9 public class GamePhysics
{
11 public static final int PL_SPEED
= 2;
13 private GameProc gameProc
;
15 private Vector2 gravity
;
17 public GamePhysics(GameProc gameProc
) {
18 this.gameProc
= gameProc
;
19 gravity
= new Vector2(0,1);
22 private boolean checkJump(Rectangle rect
, int dir
) {
26 if ((int)((rect
.x
+(rect
.width
/2))/16) - 1>=0)
27 bl
= gameProc
.world
.getForeMap(
28 (int)((rect
.x
+(rect
.width
/2))/16) - 1,
32 if ((int)((rect
.x
+(rect
.width
/2))/16) + 1<gameProc
.world
.getWidth())
33 bl
= gameProc
.world
.getForeMap(
34 (int)((rect
.x
+(rect
.width
/2))/16) + 1,
40 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
43 private boolean checkColl(Rectangle rect
) {
45 int minX
= (int) ((rect
.x
+rect
.width
/2)/16)-4;
46 int minY
= (int) ((rect
.y
+rect
.height
/2)/16)-4;
47 int maxX
= (int) ((rect
.x
+rect
.width
/2)/16)+4;
48 int maxY
= (int) ((rect
.y
+rect
.height
/2)/16)+4;
51 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
52 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
53 for (int y
=minY
; y
<maxY
; y
++) {
54 for (int x
=minX
; x
<maxX
; x
++) {
55 bl
= gameProc
.world
.getForeMap(x
,y
);
56 if (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
){
57 if (Intersector
.overlaps(rect
, Items
.BLOCKS
.getValueAt(bl
).getRect(x
,y
))){
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())) {
85 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
-=8;
86 if (checkColl(pl
.getRect())) {
87 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
+=8;
89 if (pl
.moveX
.x
< 0) d
= 1;
90 else if (pl
.moveX
.x
> 0) d
= -1;
91 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
92 while (checkColl(pl
.getRect())) pl
.position
.x
+= d
;
95 if (pl
.position
.y
> gameProc
.world
.getHeight()*16) {
96 pl
.position
= gameProc
.world
.getSpawnPoint().cpy();
98 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
104 private void mobPhy(Mob mob
) {
105 mob
.position
.add(mob
.moveY
);
106 if (checkColl(mob
.getRect())) {
109 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
110 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
111 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
116 mob
.moveY
.add(gravity
);
117 mob
.position
.add(mob
.moveX
);
118 if (mob
.position
.x
<32 ||
119 mob
.position
.x
+mob
.width
>gameProc
.world
.getWidth()*16-32)
120 mob
.position
.sub(mob
.moveX
);
121 if (checkColl(mob
.getRect())) {
123 if (mob
.moveX
.x
<0) d
=1; else if (mob
.moveX
.x
>0) d
=-1;
124 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
125 while (checkColl(mob
.getRect())) mob
.position
.x
+=d
;
129 public void update(float delta
) {
131 for (Mob mob
: gameProc
.mobs
) {
135 playerPhy(gameProc
.player
);
137 gameProc
.renderer
.camera
.position
.set(
138 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
139 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,