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;
20 private Vector2 gravity
;
22 public GamePhysics(GameProc gp
) {
24 gravity
= new Vector2(0, .9f);
27 private boolean checkJump(Rectangle rect
, int dir
) {
31 bl
= gp
.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
= gp
.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 && GameItems
.getBlock(bl
).toJump() &&
42 (rect
.y
+ rect
.height
) - GameItems
.getBlock(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;
51 if (minY
< 0) minY
= 0;
52 if (maxY
> gp
.world
.getHeight()) maxY
= gp
.world
.getHeight();
53 for (int y
= minY
; y
< maxY
; y
++) {
54 for (int x
= minX
; x
< maxX
; x
++) {
55 bl
= gp
.world
.getForeMap(x
, y
);
56 if (bl
> 0 && GameItems
.getBlock(bl
).coll
) {
57 if (Intersector
.overlaps(rect
, GameItems
.getBlock(bl
).getRect(x
, y
))) {
66 private int getBlock(Rectangle rect
) {
67 return gp
.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
.closeToPlayer(gp
) > 0) {
72 drop
.moveToPlayer(gp
);
74 if (drop
.move
.y
< 9) drop
.move
.y
+= gravity
.y
/ 4;
76 drop
.pos
.add(drop
.move
);
77 if (drop
.pos
.x
+ 8 > gp
.world
.getWidth() * 16) drop
.pos
.x
-= gp
.world
.getWidth() * 16;
78 else if (drop
.pos
.x
< 0) drop
.pos
.x
+= gp
.world
.getWidth() * 16;
79 drop
.pos
.y
= MathUtils
.round(drop
.pos
.y
);
80 while (checkColl(drop
.getRect())) {
86 private void playerPhy(Player pl
) {
87 pl
.pos
.y
+= pl
.move
.y
;
88 if (checkColl(pl
.getRect())) {
90 if (pl
.move
.y
< 0) d
= 1;
95 pl
.pos
.y
= MathUtils
.round(pl
.pos
.y
);
96 while (checkColl(pl
.getRect())) pl
.pos
.y
+= d
;
102 if (GameItems
.isFluid(getBlock(pl
.getRect()))) {
103 if (CaveGame
.TOUCH
&& pl
.move
.x
!= 0 && !gp
.swim
&& !pl
.flyMode
) gp
.swim
= true;
105 if (!pl
.flyMode
&& pl
.move
.y
< 4.5f) pl
.move
.add(gravity
.x
/ 4, gravity
.y
/ 4);
106 if (!pl
.flyMode
&& pl
.move
.y
> 4.5f) pl
.move
.add(0, -1f);
108 pl
.move
.add(0, -.5f);
109 if (pl
.move
.y
< -3) pl
.move
.y
= -3;
112 if (!pl
.flyMode
&& pl
.move
.y
< 18) pl
.move
.add(gravity
);
115 pl
.pos
.x
+= pl
.move
.x
;
116 if (checkColl(pl
.getRect())) {
117 if (pl
.canJump
&& !pl
.flyMode
) pl
.pos
.y
-= 8;
118 if (checkColl(pl
.getRect())) {
119 if (pl
.canJump
&& !pl
.flyMode
) pl
.pos
.y
+= 8;
121 if (pl
.move
.x
< 0) d
= 1;
122 else if (pl
.move
.x
> 0) d
= -1;
123 pl
.pos
.x
= MathUtils
.round(pl
.pos
.x
);
124 while (checkColl(pl
.getRect())) pl
.pos
.x
+= d
;
127 if (pl
.pos
.x
+ pl
.texWidth
/ 2 < 0) pl
.pos
.x
+= gp
.world
.getWidth() * 16;
128 if (pl
.pos
.x
+ pl
.texWidth
/ 2 > gp
.world
.getWidth() * 16)
129 pl
.pos
.x
-= gp
.world
.getWidth() * 16;
130 if (pl
.pos
.y
> gp
.world
.getHeight() * 16) {
131 pl
.pos
= gp
.world
.getSpawnPoint().cpy();
133 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.dir
) && !pl
.flyMode
&& pl
.canJump
&& pl
.move
.x
!= 0) {
139 private void mobPhy(Mob mob
) {
140 mob
.pos
.y
+= mob
.move
.y
;
141 if (checkColl(mob
.getRect())) {
143 if (mob
.move
.y
< 0) d
= 1;
144 if (d
== -1) mob
.canJump
= true;
145 mob
.pos
.y
= MathUtils
.round(mob
.pos
.y
);
146 while (checkColl(mob
.getRect())) mob
.pos
.y
+= d
;
148 if (mob
.getType() > 0) {
149 gp
.world
.setForeMap((int) mob
.pos
.x
/ 16, (int) mob
.pos
.y
/ 16, mob
.getType());
157 if (mob
.getType() == 0 && GameItems
.isFluid(getBlock(mob
.getRect()))) {
158 if (mob
.move
.y
> 9) mob
.move
.add(0, -.9f);
159 mob
.move
.add(0, -.5f);
160 if (mob
.move
.y
< -3) mob
.move
.y
= -3;
161 } else if (mob
.move
.y
< 18) mob
.move
.add(gravity
);
163 mob
.pos
.x
+= mob
.move
.x
;
164 if (checkColl(mob
.getRect())) {
168 if (checkColl(mob
.getRect())) {
169 if (mob
.canJump
) mob
.pos
.y
+= 8;
171 if (mob
.move
.x
< 0) d
= 1;
172 else if (mob
.move
.x
> 0) d
= -1;
173 mob
.pos
.x
= MathUtils
.round(mob
.pos
.x
);
174 while (checkColl(mob
.getRect())) mob
.pos
.x
+= d
;
175 if (mob
.canJump
) mob
.changeDir();
178 if (mob
.pos
.x
+ mob
.width
/ 2 < 0) mob
.pos
.x
+= gp
.world
.getWidth() * 16;
179 if (mob
.pos
.x
+ mob
.width
/ 2 > gp
.world
.getWidth() * 16)
180 mob
.pos
.x
-= gp
.world
.getWidth() * 16;
181 if (mob
.pos
.y
> gp
.world
.getHeight() * 16) {
184 if (checkJump(mob
.getRect(), mob
.dir
) && mob
.canJump
&& mob
.move
.x
!= 0) {
190 public void update(float delta
) {
191 for (Iterator
<Drop
> it
= gp
.drops
.iterator(); it
.hasNext(); ) {
192 Drop drop
= it
.next();
194 if (Intersector
.overlaps(drop
.getRect(), gp
.player
.getRect())) drop
.pickUpDrop(gp
.player
);
195 if (drop
.pickedUp
) it
.remove();
198 for (Iterator
<Mob
> it
= gp
.mobs
.iterator(); it
.hasNext(); ) {
206 playerPhy(gp
.player
);
208 gp
.renderer
.setCamPos(
209 gp
.player
.pos
.x
+ gp
.player
.texWidth
/ 2 - gp
.renderer
.getWidth() / 2,
210 gp
.player
.pos
.y
+ gp
.player
.height
/ 2 - gp
.renderer
.getHeight() / 2);