1 package ru
.deadsoftware
.cavedroid
.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
.cavedroid
.MainConfig
;
8 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
9 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
10 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
11 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
12 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
13 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
15 import javax
.inject
.Inject
;
16 import java
.util
.Iterator
;
20 public class GamePhysics
{
22 public static final int PL_SPEED
= 120;
24 private final Vector2 gravity
= new Vector2(0, .09f);
26 private final GameWorld mGameWorld
;
27 private final MainConfig mMainConfig
;
28 private final MobsController mMobsController
;
29 private final DropController mDropController
;
32 public GamePhysics(GameWorld gameWorld
,
33 MainConfig mainConfig
,
34 MobsController mobsController
,
35 DropController dropController
) {
36 mGameWorld
= gameWorld
;
37 mMainConfig
= mainConfig
;
38 mMobsController
= mobsController
;
39 mDropController
= dropController
;
43 * Checks if mob should jump
45 * @return true if mob should jump
47 private boolean checkJump(Mob mob
) {
48 int dir
= mob
.looksLeft() ?
0 : 1;
49 int blX
= (int) (mob
.getX() + mob
.getWidth() * dir
- 8 + 16 * dir
);
50 int blY
= (int) (mob
.getY() + mob
.getHeight() - 8);
51 int block
= mGameWorld
.getForeMap(blX
/ 16, blY
/ 16);
53 if (checkColl(new Rectangle(blX
, mob
.getY() - 18, mob
.getWidth(), mob
.getHeight()))) {
57 return (block
> 0 && GameItems
.getBlock(block
).toJump() &&
58 (mob
.getY() + mob
.getHeight()) - GameItems
.getBlock(block
).getRectangle(blX
/ 16, blY
/ 16).y
> 8);
61 private boolean checkColl(Rectangle rect
) {
62 int minX
= (int) ((rect
.x
+ rect
.width
/ 2) / 16) - 4;
63 int minY
= (int) ((rect
.y
+ rect
.height
/ 2) / 16) - 4;
64 int maxX
= (int) ((rect
.x
+ rect
.width
/ 2) / 16) + 4;
65 int maxY
= (int) ((rect
.y
+ rect
.height
/ 2) / 16) + 4;
71 if (maxY
> mGameWorld
.getHeight()) {
72 maxY
= mGameWorld
.getHeight();
76 for (int y
= minY
; y
< maxY
; y
++) {
77 for (int x
= minX
; x
< maxX
; x
++) {
78 block
= mGameWorld
.getForeMap(x
, y
);
79 if (block
> 0 && GameItems
.getBlock(block
).hasCollision()) {
80 if (Intersector
.overlaps(rect
, GameItems
.getBlock(block
).getRectangle(x
, y
))) {
90 private int getBlock(Rectangle rect
) {
91 return mGameWorld
.getForeMap((int) (rect
.x
+ rect
.width
/ 2) / 16,
92 (int) (rect
.y
+ rect
.height
/ 8 * 7) / 16);
95 private void dropPhy(Drop drop
, float delta
) {
96 int dropToPlayer
= drop
.closeToPlayer(mGameWorld
, mMobsController
.getPlayer());
97 if (dropToPlayer
> 0) {
98 drop
.moveToPlayer(mGameWorld
, mMobsController
.getPlayer(), dropToPlayer
);
100 if (drop
.getVelocity().x
>= .5f) {
101 drop
.getVelocity().x
-= .5f;
102 } else if (drop
.getVelocity().x
<= -.5f) {
103 drop
.getVelocity().x
+= .5f;
105 drop
.getVelocity().x
= 0;
107 if (drop
.getVelocity().y
< 9) {
108 drop
.getVelocity().y
+= gravity
.y
/ 4;
114 if (checkColl(drop
)) {
115 drop
.getVelocity().set(0, -1);
118 } while (checkColl(drop
));
119 drop
.getVelocity().setZero();
123 private void mobXColl(Mob mob
) {
124 if (checkColl(mob
)) {
125 if (mob
.canJump() && !mob
.isFlyMode()) {
129 if (checkColl(mob
)) {
130 if (mob
.canJump() && !mob
.isFlyMode()) {
136 if (mob
.getVelocity().x
< 0) {
138 } else if (mob
.getVelocity().x
> 0) {
142 mob
.x
= MathUtils
.round(mob
.getX());
144 while (checkColl(mob
)) {
154 mob
.checkWorldBounds(mGameWorld
);
157 private void mobYColl(Mob mob
) {
158 if (checkColl(mob
)) {
161 if (mob
.getVelocity().y
< 0) {
166 mob
.setCanJump(true);
167 mob
.setFlyMode(false);
170 mob
.y
= MathUtils
.round(mob
.getY());
172 while (checkColl(mob
)) {
176 mob
.getVelocity().y
= 0;
180 mob
.setCanJump(checkColl(mob
));
184 if (mob
.getY() > mGameWorld
.getHeightPx()) {
189 private void playerPhy(Player player
, float delta
) {
190 if (player
.isDead()) {
194 if (GameItems
.isFluid(getBlock(player
))) {
195 if (mMainConfig
.isTouch() && player
.getVelocity().x
!= 0 && !player
.swim
&& !player
.isFlyMode()) {
199 if (!player
.isFlyMode() && player
.getVelocity().y
< 270f) {
200 player
.getVelocity().x
+= gravity
.y
/ 4;
202 if (!player
.isFlyMode() && player
.getVelocity().y
> 270f) {
203 player
.getVelocity().add(0, -60f);
206 player
.getVelocity().add(0, -30f);
207 if (player
.getVelocity().y
< -180) {
208 player
.getVelocity().y
= -180;
212 if (!player
.isFlyMode() && player
.getVelocity().y
< 1080) {
213 player
.getVelocity().add(gravity
);
217 player
.y
+= player
.getVelocity().y
* delta
;
220 player
.x
+= player
.getVelocity().x
* (player
.isFlyMode() ?
1.5f : 1) *
221 (GameItems
.isFluid(getBlock(player
)) && !player
.isFlyMode() ?
.8f : 1) * delta
;
225 if (mMainConfig
.isTouch() && !player
.isFlyMode() && player
.canJump() && player
.getVelocity().x
!= 0 && checkJump(player
)) {
226 player
.getVelocity().add(0, -480);
227 player
.setCanJump(false);
231 private void mobPhy(Mob mob
, float delta
) {
232 if (mob
.getType() == Mob
.Type
.MOB
&& GameItems
.isFluid(getBlock(mob
))) {
233 if (mob
.getVelocity().y
> 540) {
234 mob
.getVelocity().add(0, -5.4f);
237 mob
.getVelocity().add(0, -30f);
239 if (mob
.getVelocity().y
< -180) {
240 mob
.getVelocity().y
= -180;
242 } else if (!mob
.isFlyMode() && mob
.getVelocity().y
< 1080) {
243 mob
.getVelocity().add(gravity
);
246 mob
.y
+= mob
.getVelocity().y
* delta
;
253 mob
.x
+= mob
.getVelocity().x
* delta
;
256 if (mob
.canJump() && mob
.getVelocity().x
!= 0 && checkJump(mob
)) {
257 mob
.getVelocity().add(0, -480);
258 mob
.setCanJump(false);
262 void update(float delta
) {
263 Player player
= mMobsController
.getPlayer();
265 for (Iterator
<Drop
> it
= mDropController
.getIterator(); it
.hasNext(); ) {
266 Drop drop
= it
.next();
267 dropPhy(drop
, delta
);
268 if (Intersector
.overlaps(drop
, player
)) {
269 drop
.pickUpDrop(player
);
271 if (drop
.isPickedUp()) {
276 for (Iterator
<Mob
> it
= mMobsController
.getIterator(); it
.hasNext(); ) {
278 mob
.ai(mGameWorld
, delta
);
285 playerPhy(player
, delta
);
286 if (player
.isDead()) {
287 player
.respawn(mGameWorld
);