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,.9f);
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,
30 if (gameProc
.world
.getForeMap((int)((rect
.x
+(rect
.width
/2))/16)-1,(int)(rect
.y
/16))>0) bl
=0;
33 if ((int)((rect
.x
+(rect
.width
/2))/16) + 1<gameProc
.world
.getWidth())
34 bl
= gameProc
.world
.getForeMap(
35 (int)((rect
.x
+(rect
.width
/2))/16) + 1,
37 if (gameProc
.world
.getForeMap((int)((rect
.x
+(rect
.width
/2))/16)+1,(int)(rect
.y
/16))>0) bl
=0;
42 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
45 private boolean checkColl(Rectangle rect
) {
47 int minX
= (int) ((rect
.x
+rect
.width
/2)/16)-4;
48 int minY
= (int) ((rect
.y
+rect
.height
/2)/16)-4;
49 int maxX
= (int) ((rect
.x
+rect
.width
/2)/16)+4;
50 int maxY
= (int) ((rect
.y
+rect
.height
/2)/16)+4;
53 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
54 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
55 for (int y
=minY
; y
<maxY
; y
++) {
56 for (int x
=minX
; x
<maxX
; x
++) {
57 bl
= gameProc
.world
.getForeMap(x
,y
);
58 if (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
){
59 if (Intersector
.overlaps(rect
, Items
.BLOCKS
.getValueAt(bl
).getRect(x
,y
))){
68 private void playerPhy(Player pl
) {
69 pl
.position
.add(pl
.moveY
);
70 if (checkColl(pl
.getRect())) {
72 if (pl
.moveY
.y
<0) d
=1; else if (pl
.moveY
.y
>0) d
=-1;
77 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
78 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
83 if (!pl
.flyMode
&& pl
.moveY
.y
<18) pl
.moveY
.add(gravity
);
84 pl
.position
.add(pl
.moveX
);
85 if (pl
.position
.x
<0 ||
86 pl
.position
.x
+pl
.texWidth
>=gameProc
.world
.getWidth()*16)
87 pl
.position
.sub(pl
.moveX
);
88 if (checkColl(pl
.getRect())) {
89 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
-=8;
90 if (checkColl(pl
.getRect())) {
91 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
+=8;
93 if (pl
.moveX
.x
< 0) d
= 1;
94 else if (pl
.moveX
.x
> 0) d
= -1;
95 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
96 while (checkColl(pl
.getRect())) pl
.position
.x
+= d
;
99 if (pl
.position
.y
> gameProc
.world
.getHeight()*16) {
100 pl
.position
= gameProc
.world
.getSpawnPoint().cpy();
102 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
108 private void mobPhy(Mob mob
) {
109 mob
.position
.add(mob
.moveY
);
110 if (checkColl(mob
.getRect())) {
113 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
114 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
115 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
120 mob
.moveY
.add(gravity
);
121 mob
.position
.add(mob
.moveX
);
122 if (mob
.position
.x
<32 ||
123 mob
.position
.x
+mob
.width
>gameProc
.world
.getWidth()*16-32)
124 mob
.position
.sub(mob
.moveX
);
125 if (checkColl(mob
.getRect())) {
127 if (mob
.moveX
.x
<0) d
=1; else if (mob
.moveX
.x
>0) d
=-1;
128 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
129 while (checkColl(mob
.getRect())) mob
.position
.x
+=d
;
133 public void update(float delta
) {
134 for (Mob mob
: gameProc
.mobs
) {
138 playerPhy(gameProc
.player
);
140 gameProc
.renderer
.camera
.position
.set(
141 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
142 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,