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
.Drop
;
10 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
12 import java
.util
.Iterator
;
14 public class GamePhysics
{
16 public static final int PL_SPEED
= 2;
18 private GameProc gameProc
;
20 private Vector2 gravity
;
22 public GamePhysics(GameProc gameProc
) {
23 this.gameProc
= gameProc
;
24 gravity
= new Vector2(0,.9f);
27 private boolean checkJump(Rectangle rect
, int dir
) {
31 bl
= gameProc
.world
.getForeMap((int)((rect
.x
-8)/16),(int)((rect
.y
+rect
.height
-8)/16));
32 if (checkColl(new Rectangle(rect
.x
-8, rect
.y
-18, rect
.width
, rect
.height
))) bl
=0;
35 bl
= gameProc
.world
.getForeMap((int)((rect
.x
+rect
.width
+8)/16),(int)((rect
.y
+rect
.height
-8)/16));
36 if (checkColl(new Rectangle(rect
.x
+rect
.width
+8, rect
.y
-18, rect
.width
, rect
.height
))) bl
=0;
41 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).toJump() &&
42 (rect
.y
+rect
.height
)-Items
.BLOCKS
.getValueAt(bl
).getRect((int)((rect
.x
-8)/16),(int)((rect
.y
+rect
.height
-8)/16)).y
>8);
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;
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 int getBlock(Rectangle rect
) {
67 return gameProc
.world
.getForeMap((int)(rect
.x
+rect
.width
/2)/16, (int)(rect
.y
+rect
.height
/8*7)/16);
70 private void dropPhy(Drop drop
) {
71 if (drop
.move
.y
< 9) drop
.move
.y
+= gravity
.y
/4;
72 drop
.position
.add(drop
.move
);
73 drop
.position
.y
= MathUtils
.round(drop
.position
.y
);
74 while (checkColl(drop
.getRect())) {
80 private void playerPhy(Player pl
) {
81 pl
.position
.add(pl
.moveY
);
82 if (checkColl(pl
.getRect())) {
84 if (pl
.moveY
.y
<0) d
=1;
89 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
90 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
96 if (Items
.isFluid(getBlock(pl
.getRect()))) {
97 if (CaveGame
.TOUCH
&& pl
.moveX
.x
!=0 && !gameProc
.swim
&& !pl
.flyMode
) gameProc
.swim
= true;
99 if (!pl
.flyMode
&& pl
.moveY
.y
< 4.5f) pl
.moveY
.add(gravity
.x
/ 4, gravity
.y
/ 4);
100 if (!pl
.flyMode
&& pl
.moveY
.y
> 4.5f) pl
.moveY
.add(0, -1f);
102 pl
.moveY
.add(0, -.5f);
103 if (pl
.moveY
.y
<-3) pl
.moveY
.y
= -3;
106 if (!pl
.flyMode
&& pl
.moveY
.y
<18) pl
.moveY
.add(gravity
);
109 pl
.position
.add(pl
.moveX
);
110 if (checkColl(pl
.getRect())) {
111 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
-=8;
112 if (checkColl(pl
.getRect())) {
113 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
+=8;
115 if (pl
.moveX
.x
< 0) d
= 1;
116 else if (pl
.moveX
.x
> 0) d
= -1;
117 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
118 while (checkColl(pl
.getRect())) pl
.position
.x
+= d
;
121 if (pl
.position
.x
+pl
.texWidth
/2<0) pl
.position
.x
+=gameProc
.world
.getWidth()*16;
122 if (pl
.position
.x
+pl
.texWidth
/2>gameProc
.world
.getWidth()*16) pl
.position
.x
-=gameProc
.world
.getWidth()*16;
123 if (pl
.position
.y
> gameProc
.world
.getHeight()*16) {
124 pl
.position
= gameProc
.world
.getSpawnPoint().cpy();
126 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& !pl
.moveX
.equals(Vector2
.Zero
)) {
132 private void mobPhy(Mob mob
) {
133 mob
.position
.add(mob
.moveY
);
134 if (checkColl(mob
.getRect())) {
136 if (mob
.moveY
.y
<0) d
=1;
137 if (d
==-1) mob
.canJump
= true;
138 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
139 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
141 if (mob
.getType() > 0) {
142 gameProc
.world
.setForeMap((int)mob
.position
.x
/16, (int)mob
.position
.y
/16, mob
.getType());
150 if (mob
.getType()==0 && Items
.isFluid(getBlock(mob
.getRect()))) {
151 if (mob
.moveY
.y
> 9) mob
.moveY
.add(0, -.9f);
152 mob
.moveY
.add(0, -.5f);
153 if (mob
.moveY
.y
<-3) mob
.moveY
.y
= -3;
154 } else if (mob
.moveY
.y
<18) mob
.moveY
.add(gravity
);
156 mob
.position
.add(mob
.moveX
);
157 if (checkColl(mob
.getRect())) {
161 if (checkColl(mob
.getRect())) {
162 if (mob
.canJump
) mob
.position
.y
+=8;
164 if (mob
.moveX
.x
< 0) d
= 1;
165 else if (mob
.moveX
.x
> 0) d
= -1;
166 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
167 while (checkColl(mob
.getRect())) mob
.position
.x
+= d
;
168 if (mob
.canJump
) mob
.changeDir();
171 if (mob
.position
.x
+mob
.width
/2<0) mob
.position
.x
+=gameProc
.world
.getWidth()*16;
172 if (mob
.position
.x
+mob
.width
/2>gameProc
.world
.getWidth()*16) mob
.position
.x
-=gameProc
.world
.getWidth()*16;
173 if (mob
.position
.y
> gameProc
.world
.getHeight()*16) {
176 if (checkJump(mob
.getRect(), mob
.dir
) && mob
.canJump
&& !mob
.moveX
.equals(Vector2
.Zero
)) {
177 mob
.moveY
.add(0, -8);
182 public void update(float delta
) {
183 for (Iterator
<Drop
> it
= gameProc
.drops
.iterator(); it
.hasNext(); ) {
184 Drop drop
= it
.next();
186 if (Intersector
.overlaps(drop
.getRect(), gameProc
.player
.getRect()))
187 drop
.pickUpDrop(gameProc
.player
);
188 if (drop
.pickedUp
) it
.remove();
191 for (Iterator
<Mob
> it
= gameProc
.mobs
.iterator(); it
.hasNext();) {
199 playerPhy(gameProc
.player
);
201 gameProc
.renderer
.camera
.position
.set(
202 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
203 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,