94f6f36d9dd6aa37e0b4ffc4a4f2caf7d3d338e2
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
;
16 static final int PL_SPEED
= 2;
20 private Vector2 gravity
;
22 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
).hasCollision()) {
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
.x
>= .5f) drop
.move
.x
-= .5f;
75 else if (drop
.move
.x
<= -.5f) drop
.move
.x
+= .5f;
77 if (drop
.move
.y
< 9) drop
.move
.y
+= gravity
.y
/ 4;
79 drop
.pos
.add(drop
.move
);
80 if (drop
.pos
.x
+ 8 > gp
.world
.getWidthPx()) drop
.pos
.x
-= gp
.world
.getWidthPx();
81 else if (drop
.pos
.x
< 0) drop
.pos
.x
+= gp
.world
.getWidthPx();
82 drop
.pos
.y
= MathUtils
.round(drop
.pos
.y
);
83 while (checkColl(drop
.getRect())) {
89 private void mobXColl(Mob mob
) {
90 if (checkColl(mob
.getRect())) {
91 if (mob
.canJump
&& !mob
.flyMode
) {
94 if (checkColl(mob
.getRect())) {
95 if (mob
.canJump
&& !mob
.flyMode
) mob
.pos
.y
+= 8;
97 if (mob
.mov
.x
< 0) d
= 1;
98 else if (mob
.mov
.x
> 0) d
= -1;
99 mob
.pos
.x
= MathUtils
.round(mob
.pos
.x
);
100 while (checkColl(mob
.getRect())) mob
.pos
.x
+= d
;
101 if (mob
.canJump
) mob
.changeDir();
104 if (mob
.pos
.x
+ mob
.getWidth() / 2 < 0) mob
.pos
.x
+= gp
.world
.getWidthPx();
105 if (mob
.pos
.x
+ mob
.getWidth() / 2 > gp
.world
.getWidthPx()) mob
.pos
.x
-= gp
.world
.getWidthPx();
108 private void mobYColl(Mob mob
) {
109 if (checkColl(mob
.getRect())) {
111 if (mob
.mov
.y
< 0) d
= 1;
116 mob
.pos
.y
= MathUtils
.round(mob
.pos
.y
);
117 while (checkColl(mob
.getRect())) mob
.pos
.y
+= d
;
119 if (mob
.getType() > 0) {
120 gp
.world
.setForeMap(mob
.getMapX(), mob
.getMapY(), mob
.getType());
126 if (mob
.pos
.y
> gp
.world
.getHeightPx()) {
131 private void playerPhy(Player pl
) {
132 pl
.pos
.y
+= pl
.mov
.y
;
134 if (pl
.isDead()) return;
136 if (GameItems
.isFluid(getBlock(pl
.getRect()))) {
137 if (CaveGame
.TOUCH
&& pl
.mov
.x
!= 0 && !pl
.swim
&& !pl
.flyMode
) pl
.swim
= true;
139 if (!pl
.flyMode
&& pl
.mov
.y
< 4.5f) pl
.mov
.add(gravity
.x
/ 4, gravity
.y
/ 4);
140 if (!pl
.flyMode
&& pl
.mov
.y
> 4.5f) pl
.mov
.add(0, -1f);
143 if (pl
.mov
.y
< -3) pl
.mov
.y
= -3;
146 if (!pl
.flyMode
&& pl
.mov
.y
< 18) pl
.mov
.add(gravity
);
149 pl
.pos
.x
+= pl
.mov
.x
;
152 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.getDir()) && !pl
.flyMode
&& pl
.canJump
&& pl
.mov
.x
!= 0) {
158 private void mobPhy(Mob mob
) {
159 mob
.pos
.y
+= mob
.mov
.y
;
161 if (mob
.isDead()) return;
163 if (mob
.getType() == 0 && GameItems
.isFluid(getBlock(mob
.getRect()))) {
164 if (mob
.mov
.y
> 9) mob
.mov
.add(0, -.9f);
165 mob
.mov
.add(0, -.5f);
166 if (mob
.mov
.y
< -3) mob
.mov
.y
= -3;
167 } else if (!mob
.flyMode
&& mob
.mov
.y
< 18) mob
.mov
.add(gravity
);
169 mob
.pos
.x
+= mob
.mov
.x
;
172 if (checkJump(mob
.getRect(), mob
.getDir()) && mob
.canJump
&& mob
.mov
.x
!= 0) {
178 void update(float delta
) {
179 //TODO use delta time
180 for (Iterator
<Drop
> it
= gp
.drops
.iterator(); it
.hasNext(); ) {
181 Drop drop
= it
.next();
183 if (Intersector
.overlaps(drop
.getRect(), gp
.player
.getRect())) drop
.pickUpDrop(gp
.player
);
184 if (drop
.pickedUp
) it
.remove();
187 for (Iterator
<Mob
> it
= gp
.mobs
.iterator(); it
.hasNext(); ) {
191 if (mob
.isDead()) it
.remove();
194 playerPhy(gp
.player
);
195 if (gp
.player
.isDead()) gp
.player
.respawn(gp
.world
);
197 gp
.renderer
.setCamPos(
198 gp
.player
.pos
.x
+ gp
.player
.getWidth() / 2 - gp
.renderer
.getWidth() / 2,
199 gp
.player
.pos
.y
+ gp
.player
.getHeight() / 2 - gp
.renderer
.getHeight() / 2);