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));
29 if (checkColl(new Rectangle(rect
.x
-16, rect
.y
-18, rect
.width
, rect
.height
))) bl
=0;
32 bl
= gameProc
.world
.getForeMap((int)((rect
.x
+rect
.width
+8)/16),(int)((rect
.y
+rect
.height
-8)/16));
33 if (checkColl(new Rectangle(rect
.x
+16, rect
.y
-18, rect
.width
, rect
.height
))) bl
=0;
38 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
41 private boolean checkColl(Rectangle rect
) {
43 int minX
= (int) ((rect
.x
+rect
.width
/2)/16)-4;
44 int minY
= (int) ((rect
.y
+rect
.height
/2)/16)-4;
45 int maxX
= (int) ((rect
.x
+rect
.width
/2)/16)+4;
46 int maxY
= (int) ((rect
.y
+rect
.height
/2)/16)+4;
48 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
49 for (int y
=minY
; y
<maxY
; y
++) {
50 for (int x
=minX
; x
<maxX
; x
++) {
51 bl
= gameProc
.world
.getForeMap(x
,y
);
52 if (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
){
53 if (Intersector
.overlaps(rect
, Items
.BLOCKS
.getValueAt(bl
).getRect(x
,y
))){
62 private void playerPhy(Player pl
) {
63 pl
.position
.add(pl
.moveY
);
64 if (checkColl(pl
.getRect())) {
66 if (pl
.moveY
.y
<0) d
=1;
71 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
72 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
77 if (!pl
.flyMode
&& pl
.moveY
.y
<18) pl
.moveY
.add(gravity
);
78 pl
.position
.add(pl
.moveX
);
79 if (checkColl(pl
.getRect())) {
80 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
-=8;
81 if (checkColl(pl
.getRect())) {
82 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
+=8;
84 if (pl
.moveX
.x
< 0) d
= 1;
85 else if (pl
.moveX
.x
> 0) d
= -1;
86 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
87 while (checkColl(pl
.getRect())) pl
.position
.x
+= d
;
90 if (pl
.position
.x
+pl
.texWidth
/2<0) pl
.position
.x
+=gameProc
.world
.getWidth()*16;
91 if (pl
.position
.x
+pl
.texWidth
/2>gameProc
.world
.getWidth()*16) pl
.position
.x
-=gameProc
.world
.getWidth()*16;
92 if (pl
.position
.y
> gameProc
.world
.getHeight()*16) {
93 pl
.position
= gameProc
.world
.getSpawnPoint().cpy();
95 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
101 private void mobPhy(Mob mob
) {
102 mob
.position
.add(mob
.moveY
);
103 if (checkColl(mob
.getRect())) {
105 if (mob
.moveY
.y
<0) d
=1;
106 if (d
==-1) mob
.canJump
= true;
107 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
108 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
113 if (mob
.moveY
.y
<18) mob
.moveY
.add(gravity
);
114 mob
.position
.add(mob
.moveX
);
115 if (checkColl(mob
.getRect())) {
119 if (checkColl(mob
.getRect())) {
120 if (mob
.canJump
) mob
.position
.y
+=8;
122 if (mob
.moveX
.x
< 0) d
= 1;
123 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
;
126 if (mob
.canJump
) mob
.changeDir();
129 if (mob
.position
.x
+mob
.width
/2<0) mob
.position
.x
+=gameProc
.world
.getWidth()*16;
130 if (mob
.position
.x
+mob
.width
/2>gameProc
.world
.getWidth()*16) mob
.position
.x
-=gameProc
.world
.getWidth()*16;
131 if (mob
.position
.y
> gameProc
.world
.getHeight()*16) {
134 if (checkJump(mob
.getRect(), mob
.dir
) && mob
.canJump
&& !mob
.moveX
.equals(Vector2
.Zero
)) {
135 mob
.moveY
.add(0, -8);
140 public void update(float delta
) {
141 for (Mob mob
: gameProc
.mobs
) {
145 playerPhy(gameProc
.player
);
147 gameProc
.renderer
.camera
.position
.set(
148 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
149 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,