DEADSOFTWARE

88791dc67a59d337235c8a79d55c9080cb117c04
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GamePhysics.java
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;
14 class GamePhysics {
16 static final int PL_SPEED = 2;
18 private GameProc gp;
20 private Vector2 gravity;
22 GamePhysics(GameProc gp) {
23 this.gp = gp;
24 gravity = new Vector2(0, .9f);
25 }
27 private boolean checkJump(Rectangle rect, int dir) {
28 int bl;
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);
37 }
39 private boolean checkColl(Rectangle rect) {
40 int bl;
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))) {
52 return true;
53 }
54 }
55 }
56 }
57 return false;
58 }
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);
62 }
64 private void dropPhy(Drop drop) {
65 if (drop.closeToPlayer(gp) > 0) {
66 drop.moveToPlayer(gp);
67 } else {
68 if (drop.move.x >= .5f) drop.move.x -= .5f;
69 else if (drop.move.x <= -.5f) drop.move.x += .5f;
70 else drop.move.x = 0;
71 if (drop.move.y < 9) drop.move.y += gravity.y / 4;
72 }
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())) {
78 drop.pos.y--;
79 drop.move.y = 0;
80 }
81 }
83 private void mobXColl(Mob mob) {
84 if (checkColl(mob.getRect())) {
85 if (mob.canJump && !mob.flyMode) {
86 mob.pos.y -= 8;
87 }
88 if (checkColl(mob.getRect())) {
89 if (mob.canJump && !mob.flyMode) mob.pos.y += 8;
90 int d = 0;
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();
96 }
97 }
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())) {
104 int d = -1;
105 if (mob.mov.y < 0) d = 1;
106 if (d == -1) {
107 mob.canJump = true;
108 mob.flyMode = false;
110 mob.pos.y = MathUtils.round(mob.pos.y);
111 while (checkColl(mob.getRect())) mob.pos.y += d;
112 mob.mov.y = 0;
113 if (mob.getType() > 0) {
114 gp.world.setForeMap(mob.getMapX(), mob.getMapY(), mob.getType());
115 mob.kill();
117 } else {
118 mob.canJump = false;
120 if (mob.pos.y > gp.world.getHeightPx()) {
121 mob.kill();
125 private void playerPhy(Player pl) {
126 pl.pos.y += pl.mov.y;
127 mobYColl(pl);
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;
132 if (!pl.swim) {
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);
135 } else {
136 pl.mov.add(0, -.5f);
137 if (pl.mov.y < -3) pl.mov.y = -3;
139 } else {
140 if (!pl.flyMode && pl.mov.y < 18) pl.mov.add(gravity);
143 pl.pos.x += pl.mov.x;
144 mobXColl(pl);
146 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDir()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) {
147 pl.mov.add(0, -8);
148 pl.canJump = false;
152 private void mobPhy(Mob mob) {
153 mob.pos.y += mob.mov.y;
154 mobYColl(mob);
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;
164 mobXColl(mob);
166 if (checkJump(mob.getRect(), mob.getDir()) && mob.canJump && mob.mov.x != 0) {
167 mob.mov.add(0, -8);
168 mob.canJump = false;
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();
176 dropPhy(drop);
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(); ) {
182 Mob mob = it.next();
183 mob.ai();
184 mobPhy(mob);
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);