34c4df111304a96557ee12ec0f767b3e8bfef40f
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((int)((rect
.x
-8)/16),(int)((rect
.y
+rect
.height
-8)/16));
31 bl
= gameProc
.world
.getForeMap((int)((rect
.x
+rect
.width
+8)/16),(int)((rect
.y
+rect
.height
-8)/16));
36 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
39 private boolean checkColl(Rectangle rect
) {
41 int minX
= (int) ((rect
.x
+rect
.width
/2)/16)-4;
42 int minY
= (int) ((rect
.y
+rect
.height
/2)/16)-4;
43 int maxX
= (int) ((rect
.x
+rect
.width
/2)/16)+4;
44 int maxY
= (int) ((rect
.y
+rect
.height
/2)/16)+4;
46 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
47 for (int y
=minY
; y
<maxY
; y
++) {
48 for (int x
=minX
; x
<maxX
; x
++) {
49 bl
= gameProc
.world
.getForeMap(x
,y
);
50 if (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
){
51 if (Intersector
.overlaps(rect
, Items
.BLOCKS
.getValueAt(bl
).getRect(x
,y
))){
60 private void playerPhy(Player pl
) {
61 pl
.position
.add(pl
.moveY
);
62 if (checkColl(pl
.getRect())) {
64 if (pl
.moveY
.y
<0) d
=1;
69 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
70 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
75 if (!pl
.flyMode
&& pl
.moveY
.y
<18) pl
.moveY
.add(gravity
);
76 pl
.position
.add(pl
.moveX
);
77 if (checkColl(pl
.getRect())) {
78 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
-=8;
79 if (checkColl(pl
.getRect())) {
80 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
+=8;
82 if (pl
.moveX
.x
< 0) d
= 1;
83 else if (pl
.moveX
.x
> 0) d
= -1;
84 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
85 while (checkColl(pl
.getRect())) pl
.position
.x
+= d
;
88 if (pl
.position
.x
+pl
.texWidth
/2<0) pl
.position
.x
+=gameProc
.world
.getWidth()*16;
89 if (pl
.position
.x
+pl
.texWidth
/2>gameProc
.world
.getWidth()*16) pl
.position
.x
-=gameProc
.world
.getWidth()*16;
90 if (pl
.position
.y
> gameProc
.world
.getHeight()*16) {
91 pl
.position
= gameProc
.world
.getSpawnPoint().cpy();
93 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
99 private void mobPhy(Mob mob
) {
100 mob
.position
.add(mob
.moveY
);
101 if (checkColl(mob
.getRect())) {
103 if (mob
.moveY
.y
<0) d
=1;
104 if (d
==-1) mob
.canJump
= true;
105 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
106 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
111 if (mob
.moveY
.y
<18) mob
.moveY
.add(gravity
);
112 mob
.position
.add(mob
.moveX
);
113 if (checkColl(mob
.getRect())) {
117 if (checkColl(mob
.getRect())) {
118 if (mob
.canJump
) mob
.position
.y
+=8;
120 if (mob
.moveX
.x
< 0) d
= 1;
121 else if (mob
.moveX
.x
> 0) d
= -1;
122 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
123 while (checkColl(mob
.getRect())) mob
.position
.x
+= d
;
126 if (mob
.position
.x
+mob
.width
/2<0) mob
.position
.x
+=gameProc
.world
.getWidth()*16;
127 if (mob
.position
.x
+mob
.width
/2>gameProc
.world
.getWidth()*16) mob
.position
.x
-=gameProc
.world
.getWidth()*16;
128 if (mob
.position
.y
> gameProc
.world
.getHeight()*16) {
131 if (checkJump(mob
.getRect(), mob
.dir
) && mob
.canJump
&& !mob
.moveX
.equals(Vector2
.Zero
)) {
132 mob
.moveY
.add(0, -8);
137 public void update(float delta
) {
138 for (Mob mob
: gameProc
.mobs
) {
142 playerPhy(gameProc
.player
);
144 gameProc
.renderer
.camera
.position
.set(
145 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
146 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,