DEADSOFTWARE

70afcbfe5d3f1be77cdd772d61584b142022f7bd
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GamePhysics.java
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;
20 @GameScope
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;
33 @Inject
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;
42 }
44 /**
45 * Checks if mob should jump
46 *
47 * @return true if mob should jump
48 */
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()))) {
56 block = 0;
57 }
59 return (block > 0 && GameItems.getBlock(block).toJump() &&
60 (mob.getY() + mob.getHeight()) - GameItems.getBlock(block).getRectangle(blX / 16, blY / 16).y > 8);
61 }
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;
69 if (minY < 0) {
70 minY = 0;
71 }
73 if (maxY > mGameWorld.getHeight()) {
74 maxY = mGameWorld.getHeight();
75 }
77 int block;
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))) {
83 return true;
84 }
85 }
86 }
87 }
89 return false;
90 }
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);
95 }
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);
102 /**
103 * @return Rectangle representing magneting target for this drop
104 */
105 @CheckForNull
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)) {
115 return shiftedLeft;
118 final Rectangle shiftedRight = getShiftedPlayerRect(mGameWorld.getWidthPx());
119 if (drop.canMagnetTo(shiftedRight)) {
120 return shiftedRight;
123 return null;
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);
144 } else {
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();
156 do {
157 drop.y--;
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()) {
169 mob.y -= 8;
172 if (checkColl(mob)) {
173 if (mob.canJump() && !mob.isFlyMode()) {
174 mob.y += 8;
177 int d = 0;
179 if (mob.getVelocity().x < 0) {
180 d = 1;
181 } else if (mob.getVelocity().x > 0) {
182 d = -1;
185 mob.x = MathUtils.round(mob.getX());
187 while (checkColl(mob)) {
188 mob.x += d;
191 if (mob.canJump()) {
192 mob.changeDir();
197 mob.checkWorldBounds(mGameWorld);
200 private void mobYColl(Mob mob) {
201 if (checkColl(mob)) {
202 int d = -1;
204 if (mob.getVelocity().y < 0) {
205 d = 1;
208 if (d == -1) {
209 mob.setCanJump(true);
210 mob.setFlyMode(false);
213 mob.y = MathUtils.round(mob.getY());
215 while (checkColl(mob)) {
216 mob.y += d;
219 mob.getVelocity().y = 0;
223 //todo fall damage
224 // h = (v^2) / 2g
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);
229 } else {
230 mob.y += 1;
231 mob.setCanJump(checkColl(mob));
232 mob.y -= 1;
235 if (mob.getY() > mGameWorld.getHeightPx()) {
236 mob.kill();
240 private void playerPhy(Player player, float delta) {
241 if (player.isDead()) {
242 return;
245 if (GameItems.isFluid(getBlock(player))) {
246 if (mMainConfig.isTouch() && player.getVelocity().x != 0 && !player.swim && !player.isFlyMode()) {
247 player.swim = true;
249 if (!player.swim) {
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;
256 } else {
257 player.getVelocity().y += PL_JUMP_VELOCITY * delta;
258 if (player.getVelocity().y < -player.getSpeed()) {
259 player.getVelocity().y = -player.getSpeed();
262 } else {
263 if (!player.isFlyMode() && player.getVelocity().y < PL_TERMINAL_VELOCITY) {
264 player.getVelocity().y += gravity.y * delta;
268 player.y += player.getVelocity().y * delta;
269 mobYColl(player);
271 player.x += player.getVelocity().x * (player.isFlyMode() ? 1.5f : 1) *
272 (GameItems.isFluid(getBlock(player)) && !player.isFlyMode() ? .8f : 1) * delta;
274 mobXColl(player);
276 if (mMainConfig.isTouch() && !player.isFlyMode() && player.canJump() && player.getVelocity().x != 0 && checkJump(player)) {
277 player.jump();
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;
298 mobYColl(mob);
300 if (mob.isDead()) {
301 return;
304 mob.x += mob.getVelocity().x * delta;
305 mobXColl(mob);
307 if (mob.canJump() && mob.getVelocity().x != 0 && checkJump(mob)) {
308 mob.jump();
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()) {
320 it.remove();
324 for (Iterator<Mob> it = mMobsController.getIterator(); it.hasNext(); ) {
325 Mob mob = it.next();
326 mob.ai(mGameWorld, delta);
327 mobPhy(mob, delta);
328 if (mob.isDead()) {
329 it.remove();
333 playerPhy(player, delta);
334 if (player.isDead()) {
335 player.respawn(mGameWorld);