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
.annotation
.CheckForNull
;
16 import javax
.inject
.Inject
;
17 import java
.util
.Iterator
;
21 public class GamePhysics
{
23 public static final float PL_JUMP_VELOCITY
= -133.332f;
24 public static final float PL_TERMINAL_VELOCITY
= 1254.4f;
26 private final Vector2 gravity
= new Vector2(0, 444.44f);
28 private final GameWorld mGameWorld
;
29 private final MainConfig mMainConfig
;
30 private final MobsController mMobsController
;
31 private final DropController mDropController
;
34 public GamePhysics(GameWorld gameWorld
,
35 MainConfig mainConfig
,
36 MobsController mobsController
,
37 DropController dropController
) {
38 mGameWorld
= gameWorld
;
39 mMainConfig
= mainConfig
;
40 mMobsController
= mobsController
;
41 mDropController
= dropController
;
45 * Checks if mob should jump
47 * @return true if mob should jump
49 private boolean checkJump(Mob mob
) {
50 int dir
= mob
.looksLeft() ?
0 : 1;
51 int blX
= (int) (mob
.getX() + mob
.getWidth() * dir
- 8 + 16 * dir
);
52 int blY
= (int) (mob
.getY() + mob
.getHeight() - 8);
53 int block
= mGameWorld
.getForeMap(blX
/ 16, blY
/ 16);
55 if (checkColl(new Rectangle(blX
, mob
.getY() - 18, mob
.getWidth(), mob
.getHeight()))) {
59 return (block
> 0 && GameItems
.getBlock(block
).toJump() &&
60 (mob
.getY() + mob
.getHeight()) - GameItems
.getBlock(block
).getRectangle(blX
/ 16, blY
/ 16).y
> 8);
63 private boolean checkColl(Rectangle rect
) {
64 int minX
= (int) ((rect
.x
+ rect
.width
/ 2) / 16) - 4;
65 int minY
= (int) ((rect
.y
+ rect
.height
/ 2) / 16) - 4;
66 int maxX
= (int) ((rect
.x
+ rect
.width
/ 2) / 16) + 4;
67 int maxY
= (int) ((rect
.y
+ rect
.height
/ 2) / 16) + 4;
73 if (maxY
> mGameWorld
.getHeight()) {
74 maxY
= mGameWorld
.getHeight();
78 for (int y
= minY
; y
< maxY
; y
++) {
79 for (int x
= minX
; x
< maxX
; x
++) {
80 block
= mGameWorld
.getForeMap(x
, y
);
81 if (block
> 0 && GameItems
.getBlock(block
).hasCollision()) {
82 if (Intersector
.overlaps(rect
, GameItems
.getBlock(block
).getRectangle(x
, y
))) {
92 private int getBlock(Rectangle rect
) {
93 return mGameWorld
.getForeMap((int) (rect
.x
+ rect
.width
/ 2) / 16,
94 (int) (rect
.y
+ rect
.height
/ 8 * 7) / 16);
97 private Rectangle
getShiftedPlayerRect(float shift
) {
98 final Player player
= mMobsController
.getPlayer();
99 return new Rectangle(player
.x
+ shift
, player
.y
, player
.width
, player
.height
);
103 * @return Rectangle representing magneting target for this drop
106 private Rectangle
getShiftedMagnetingPlayerRect(Drop drop
) {
107 final Player player
= mMobsController
.getPlayer();
109 if (drop
.canMagnetTo(player
)) {
110 return getShiftedPlayerRect(0);
113 final Rectangle shiftedLeft
= getShiftedPlayerRect(-mGameWorld
.getWidthPx());
114 if (drop
.canMagnetTo(shiftedLeft
)) {
118 final Rectangle shiftedRight
= getShiftedPlayerRect(mGameWorld
.getWidthPx());
119 if (drop
.canMagnetTo(shiftedRight
)) {
126 private void pickUpDropIfPossible(Rectangle shiftedPlayerTarget
, Drop drop
) {
127 final Player player
= mMobsController
.getPlayer();
129 if (Intersector
.overlaps(shiftedPlayerTarget
, drop
)) {
130 player
.pickUpDrop(drop
);
134 private void dropPhy(Drop drop
, float delta
) {
135 final Rectangle playerMagnetTarget
= getShiftedMagnetingPlayerRect(drop
);
136 final Vector2 dropVelocity
= drop
.getVelocity();
139 if (playerMagnetTarget
!= null) {
140 final Vector2 magnetVector
= new Vector2(playerMagnetTarget
.x
- drop
.x
,
141 playerMagnetTarget
.y
- drop
.y
);
142 magnetVector
.nor().scl(Drop
.MAGNET_VELOCITY
* delta
);
143 dropVelocity
.add(magnetVector
);
145 dropVelocity
.y
+= gravity
.y
* delta
;
148 dropVelocity
.x
= MathUtils
.clamp(dropVelocity
.x
, -Drop
.MAGNET_VELOCITY
, Drop
.MAGNET_VELOCITY
);
149 dropVelocity
.y
= MathUtils
.clamp(dropVelocity
.y
, -Drop
.MAGNET_VELOCITY
, Drop
.MAGNET_VELOCITY
);
151 drop
.x
+= dropVelocity
.x
* delta
;
152 drop
.y
+= dropVelocity
.y
* delta
;
154 if (checkColl(drop
)) {
155 dropVelocity
.setZero();
158 } while (checkColl(drop
));
161 if (playerMagnetTarget
!= null) {
162 pickUpDropIfPossible(playerMagnetTarget
, drop
);
166 private void mobXColl(Mob mob
) {
167 if (checkColl(mob
)) {
168 if (mob
.canJump() && !mob
.isFlyMode()) {
172 if (checkColl(mob
)) {
173 if (mob
.canJump() && !mob
.isFlyMode()) {
179 if (mob
.getVelocity().x
< 0) {
181 } else if (mob
.getVelocity().x
> 0) {
185 mob
.x
= MathUtils
.round(mob
.getX());
187 while (checkColl(mob
)) {
197 mob
.checkWorldBounds(mGameWorld
);
200 private void mobYColl(Mob mob
) {
201 if (checkColl(mob
)) {
204 if (mob
.getVelocity().y
< 0) {
209 mob
.setCanJump(true);
210 mob
.setFlyMode(false);
213 mob
.y
= MathUtils
.round(mob
.getY());
215 while (checkColl(mob
)) {
219 mob
.getVelocity().y
= 0;
225 // dmg = max(0, (h - 48) / 32) - half of blocks fallen starting from 3 blocks height
226 // int dmg = ((int)Math.max(0f, (((mob.getVelocity().y * mob.getVelocity().y) / (2 * gravity.y)) - 48f) / 16f));
227 // if (dmg > 0) System.out.println("Damage: " + dmg);
231 mob
.setCanJump(checkColl(mob
));
235 if (mob
.getY() > mGameWorld
.getHeightPx()) {
240 private void playerPhy(Player player
, float delta
) {
241 if (player
.isDead()) {
245 if (GameItems
.isFluid(getBlock(player
))) {
246 if (mMainConfig
.isTouch() && player
.getVelocity().x
!= 0 && !player
.swim
&& !player
.isFlyMode()) {
250 if (!player
.isFlyMode() && player
.getVelocity().y
< 32f) {
251 player
.getVelocity().y
+= gravity
.y
* delta
;
253 if (!player
.isFlyMode() && player
.getVelocity().y
> 32f) {
254 player
.getVelocity().y
-= player
.getVelocity().y
* 32f * delta
;
257 player
.getVelocity().y
+= PL_JUMP_VELOCITY
* delta
;
258 if (player
.getVelocity().y
< -player
.getSpeed()) {
259 player
.getVelocity().y
= -player
.getSpeed();
263 if (!player
.isFlyMode() && player
.getVelocity().y
< PL_TERMINAL_VELOCITY
) {
264 player
.getVelocity().y
+= gravity
.y
* delta
;
268 player
.y
+= player
.getVelocity().y
* delta
;
271 player
.x
+= player
.getVelocity().x
* (player
.isFlyMode() ?
1.5f : 1) *
272 (GameItems
.isFluid(getBlock(player
)) && !player
.isFlyMode() ?
.8f : 1) * delta
;
276 if (mMainConfig
.isTouch() && !player
.isFlyMode() && player
.canJump() && player
.getVelocity().x
!= 0 && checkJump(player
)) {
278 player
.setCanJump(false);
282 private void mobPhy(Mob mob
, float delta
) {
283 if (mob
.getType() == Mob
.Type
.MOB
&& GameItems
.isFluid(getBlock(mob
))) {
284 if (mob
.getVelocity().y
> 32f) {
285 mob
.getVelocity().y
-= mob
.getVelocity().y
* 32f * delta
;
288 mob
.getVelocity().y
+= PL_JUMP_VELOCITY
* delta
;
290 if (mob
.getVelocity().y
< -mob
.getSpeed()) {
291 mob
.getVelocity().y
= -mob
.getSpeed();
293 } else if (!mob
.isFlyMode() && mob
.getVelocity().y
< PL_TERMINAL_VELOCITY
) {
294 mob
.getVelocity().y
+= gravity
.y
* delta
;
297 mob
.y
+= mob
.getVelocity().y
* delta
;
304 mob
.x
+= mob
.getVelocity().x
* delta
;
307 if (mob
.canJump() && mob
.getVelocity().x
!= 0 && checkJump(mob
)) {
309 mob
.setCanJump(false);
313 void update(float delta
) {
314 Player player
= mMobsController
.getPlayer();
316 for (Iterator
<Drop
> it
= mDropController
.getIterator(); it
.hasNext(); ) {
317 Drop drop
= it
.next();
318 dropPhy(drop
, delta
);
319 if (drop
.getPickedUp()) {
324 for (Iterator
<Mob
> it
= mMobsController
.getIterator(); it
.hasNext(); ) {
326 mob
.ai(mGameWorld
, delta
);
333 playerPhy(player
, delta
);
334 if (player
.isDead()) {
335 player
.respawn(mGameWorld
);