88791dc67a59d337235c8a79d55c9080cb117c04
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
) {
29 int blX
= (int) (rect
.x
+ rect
.width
* dir
- 8 + 16 * dir
);
30 int blY
= (int) (rect
.y
+ rect
.height
- 8);
32 bl
= gp
.world
.getForeMap(blX
/ 16, blY
/ 16);
33 if (checkColl(new Rectangle(blX
, rect
.y
- 18, rect
.width
, rect
.height
))) bl
= 0;
35 return (bl
> 0 && GameItems
.getBlock(bl
).toJump() &&
36 (rect
.y
+ rect
.height
) - GameItems
.getBlock(bl
).getRect(blX
/ 16, blY
/ 16).y
> 8);
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;
45 if (minY
< 0) minY
= 0;
46 if (maxY
> gp
.world
.getHeight()) maxY
= gp
.world
.getHeight();
47 for (int y
= minY
; y
< maxY
; y
++) {
48 for (int x
= minX
; x
< maxX
; x
++) {
49 bl
= gp
.world
.getForeMap(x
, y
);
50 if (bl
> 0 && GameItems
.getBlock(bl
).hasCollision()) {
51 if (Intersector
.overlaps(rect
, GameItems
.getBlock(bl
).getRect(x
, y
))) {
60 private int getBlock(Rectangle rect
) {
61 return gp
.world
.getForeMap((int) (rect
.x
+ rect
.width
/ 2) / 16, (int) (rect
.y
+ rect
.height
/ 8 * 7) / 16);
64 private void dropPhy(Drop drop
) {
65 if (drop
.closeToPlayer(gp
) > 0) {
66 drop
.moveToPlayer(gp
);
68 if (drop
.move
.x
>= .5f) drop
.move
.x
-= .5f;
69 else if (drop
.move
.x
<= -.5f) drop
.move
.x
+= .5f;
71 if (drop
.move
.y
< 9) drop
.move
.y
+= gravity
.y
/ 4;
73 drop
.pos
.add(drop
.move
);
74 if (drop
.pos
.x
+ 8 > gp
.world
.getWidthPx()) drop
.pos
.x
-= gp
.world
.getWidthPx();
75 else if (drop
.pos
.x
< 0) drop
.pos
.x
+= gp
.world
.getWidthPx();
76 drop
.pos
.y
= MathUtils
.round(drop
.pos
.y
);
77 while (checkColl(drop
.getRect())) {
83 private void mobXColl(Mob mob
) {
84 if (checkColl(mob
.getRect())) {
85 if (mob
.canJump
&& !mob
.flyMode
) {
88 if (checkColl(mob
.getRect())) {
89 if (mob
.canJump
&& !mob
.flyMode
) mob
.pos
.y
+= 8;
91 if (mob
.mov
.x
< 0) d
= 1;
92 else if (mob
.mov
.x
> 0) d
= -1;
93 mob
.pos
.x
= MathUtils
.round(mob
.pos
.x
);
94 while (checkColl(mob
.getRect())) mob
.pos
.x
+= d
;
95 if (mob
.canJump
) mob
.changeDir();
98 if (mob
.pos
.x
+ mob
.getWidth() / 2 < 0) mob
.pos
.x
+= gp
.world
.getWidthPx();
99 if (mob
.pos
.x
+ mob
.getWidth() / 2 > gp
.world
.getWidthPx()) mob
.pos
.x
-= gp
.world
.getWidthPx();
102 private void mobYColl(Mob mob
) {
103 if (checkColl(mob
.getRect())) {
105 if (mob
.mov
.y
< 0) d
= 1;
110 mob
.pos
.y
= MathUtils
.round(mob
.pos
.y
);
111 while (checkColl(mob
.getRect())) mob
.pos
.y
+= d
;
113 if (mob
.getType() > 0) {
114 gp
.world
.setForeMap(mob
.getMapX(), mob
.getMapY(), mob
.getType());
120 if (mob
.pos
.y
> gp
.world
.getHeightPx()) {
125 private void playerPhy(Player pl
) {
126 pl
.pos
.y
+= pl
.mov
.y
;
128 if (pl
.isDead()) return;
130 if (GameItems
.isFluid(getBlock(pl
.getRect()))) {
131 if (CaveGame
.TOUCH
&& pl
.mov
.x
!= 0 && !pl
.swim
&& !pl
.flyMode
) pl
.swim
= true;
133 if (!pl
.flyMode
&& pl
.mov
.y
< 4.5f) pl
.mov
.add(gravity
.x
/ 4, gravity
.y
/ 4);
134 if (!pl
.flyMode
&& pl
.mov
.y
> 4.5f) pl
.mov
.add(0, -1f);
137 if (pl
.mov
.y
< -3) pl
.mov
.y
= -3;
140 if (!pl
.flyMode
&& pl
.mov
.y
< 18) pl
.mov
.add(gravity
);
143 pl
.pos
.x
+= pl
.mov
.x
;
146 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.getDir()) && !pl
.flyMode
&& pl
.canJump
&& pl
.mov
.x
!= 0) {
152 private void mobPhy(Mob mob
) {
153 mob
.pos
.y
+= mob
.mov
.y
;
155 if (mob
.isDead()) return;
157 if (mob
.getType() == 0 && GameItems
.isFluid(getBlock(mob
.getRect()))) {
158 if (mob
.mov
.y
> 9) mob
.mov
.add(0, -.9f);
159 mob
.mov
.add(0, -.5f);
160 if (mob
.mov
.y
< -3) mob
.mov
.y
= -3;
161 } else if (!mob
.flyMode
&& mob
.mov
.y
< 18) mob
.mov
.add(gravity
);
163 mob
.pos
.x
+= mob
.mov
.x
;
166 if (checkJump(mob
.getRect(), mob
.getDir()) && mob
.canJump
&& mob
.mov
.x
!= 0) {
172 void update(float delta
) {
173 //TODO use delta time
174 for (Iterator
<Drop
> it
= gp
.drops
.iterator(); it
.hasNext(); ) {
175 Drop drop
= it
.next();
177 if (Intersector
.overlaps(drop
.getRect(), gp
.player
.getRect())) drop
.pickUpDrop(gp
.player
);
178 if (drop
.pickedUp
) it
.remove();
181 for (Iterator
<Mob
> it
= gp
.mobs
.iterator(); it
.hasNext(); ) {
185 if (mob
.isDead()) it
.remove();
188 playerPhy(gp
.player
);
189 if (gp
.player
.isDead()) gp
.player
.respawn(gp
.world
);
191 gp
.renderer
.setCamPos(
192 gp
.player
.pos
.x
+ gp
.player
.getWidth() / 2 - gp
.renderer
.getWidth() / 2,
193 gp
.player
.pos
.y
+ gp
.player
.getHeight() / 2 - gp
.renderer
.getHeight() / 2);