1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.math
.Intersector
;
4 import com
.badlogic
.gdx
.math
.MathUtils
;
5 import com
.badlogic
.gdx
.math
.Rectangle
;
6 import com
.badlogic
.gdx
.math
.Vector2
;
7 import ru
.deadsoftware
.cavecraft
.CaveGame
;
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,.9f);
24 private boolean checkJump(Rectangle rect
, int dir
) {
28 bl
= gameProc
.world
.getForeMap(
29 (int)((rect
.x
+(rect
.width
/2))/16) - 1,
31 if (gameProc
.world
.getForeMap((int)((rect
.x
+(rect
.width
/2))/16)-1,(int)(rect
.y
/16))>0) bl
=0;
32 if (gameProc
.world
.getForeMap((int)((rect
.x
+(rect
.width
/2))/16)-1,(int)(rect
.y
/16)-1)>0) bl
=0;
35 bl
= gameProc
.world
.getForeMap(
36 (int)((rect
.x
+(rect
.width
/2))/16) + 1,
38 if (gameProc
.world
.getForeMap((int)((rect
.x
+(rect
.width
/2))/16)+1,(int)(rect
.y
/16))>0) bl
=0;
39 if (gameProc
.world
.getForeMap((int)((rect
.x
+(rect
.width
/2))/16)+1,(int)(rect
.y
/16)-1)>0) bl
=0;
44 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
47 private boolean checkColl(Rectangle rect
) {
49 int minX
= (int) ((rect
.x
+rect
.width
/2)/16)-4;
50 int minY
= (int) ((rect
.y
+rect
.height
/2)/16)-4;
51 int maxX
= (int) ((rect
.x
+rect
.width
/2)/16)+4;
52 int maxY
= (int) ((rect
.y
+rect
.height
/2)/16)+4;
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 (checkColl(pl
.getRect())) {
86 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
-=8;
87 if (checkColl(pl
.getRect())) {
88 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
+=8;
90 if (pl
.moveX
.x
< 0) d
= 1;
91 else if (pl
.moveX
.x
> 0) d
= -1;
92 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
93 while (checkColl(pl
.getRect())) pl
.position
.x
+= d
;
96 if (pl
.position
.x
+pl
.texWidth
/2<0) pl
.position
.x
+=gameProc
.world
.getWidth()*16;
97 if (pl
.position
.x
+pl
.texWidth
/2>gameProc
.world
.getWidth()*16) pl
.position
.x
-=gameProc
.world
.getWidth()*16;
98 if (pl
.position
.y
> gameProc
.world
.getHeight()*16) {
99 pl
.position
= gameProc
.world
.getSpawnPoint().cpy();
101 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
107 private void mobPhy(Mob mob
) {
108 mob
.position
.add(mob
.moveY
);
109 if (checkColl(mob
.getRect())) {
112 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
113 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
114 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
119 mob
.moveY
.add(gravity
);
120 mob
.position
.add(mob
.moveX
);
121 if (mob
.position
.x
+mob
.width
/2<0) mob
.position
.x
+=gameProc
.world
.getWidth()*16;
122 if (mob
.position
.x
+mob
.width
/2>gameProc
.world
.getWidth()*16) mob
.position
.x
-=gameProc
.world
.getWidth()*16;
123 if (checkColl(mob
.getRect())) {
125 if (mob
.moveX
.x
<0) d
=1; else if (mob
.moveX
.x
>0) d
=-1;
126 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
127 while (checkColl(mob
.getRect())) mob
.position
.x
+=d
;
131 public void update(float delta
) {
132 for (Mob mob
: gameProc
.mobs
) {
136 playerPhy(gameProc
.player
);
138 gameProc
.renderer
.camera
.position
.set(
139 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
140 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,