DEADSOFTWARE

8f3e8fe7309f8f679626b2fbf5665f12fb21a8a3
[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 switch (dir) {
30 case 0:
31 bl = gp.world.getForeMap((int) ((rect.x - 8) / 16), (int) ((rect.y + rect.height - 8) / 16));
32 if (checkColl(new Rectangle(rect.x - 8, rect.y - 18, rect.width, rect.height))) bl = 0;
33 break;
34 case 1:
35 bl = gp.world.getForeMap((int) ((rect.x + rect.width + 8) / 16), (int) ((rect.y + rect.height - 8) / 16));
36 if (checkColl(new Rectangle(rect.x + rect.width + 8, rect.y - 18, rect.width, rect.height))) bl = 0;
37 break;
38 default:
39 bl = 0;
40 }
41 return (bl > 0 && GameItems.getBlock(bl).toJump() &&
42 (rect.y + rect.height) - GameItems.getBlock(bl).getRect((int) ((rect.x - 8) / 16), (int) ((rect.y + rect.height - 8) / 16)).y > 8);
43 }
45 private boolean checkColl(Rectangle rect) {
46 int bl;
47 int minX = (int) ((rect.x + rect.width / 2) / 16) - 4;
48 int minY = (int) ((rect.y + rect.height / 2) / 16) - 4;
49 int maxX = (int) ((rect.x + rect.width / 2) / 16) + 4;
50 int maxY = (int) ((rect.y + rect.height / 2) / 16) + 4;
51 if (minY < 0) minY = 0;
52 if (maxY > gp.world.getHeight()) maxY = gp.world.getHeight();
53 for (int y = minY; y < maxY; y++) {
54 for (int x = minX; x < maxX; x++) {
55 bl = gp.world.getForeMap(x, y);
56 if (bl > 0 && GameItems.getBlock(bl).hasCollision()) {
57 if (Intersector.overlaps(rect, GameItems.getBlock(bl).getRect(x, y))) {
58 return true;
59 }
60 }
61 }
62 }
63 return false;
64 }
66 private int getBlock(Rectangle rect) {
67 return gp.world.getForeMap((int) (rect.x + rect.width / 2) / 16, (int) (rect.y + rect.height / 8 * 7) / 16);
68 }
70 private void dropPhy(Drop drop) {
71 if (drop.closeToPlayer(gp) > 0) {
72 drop.moveToPlayer(gp);
73 } else {
74 if (drop.move.x >= .5f) drop.move.x -= .5f;
75 else if (drop.move.x <= -.5f) drop.move.x += .5f;
76 else drop.move.x = 0;
77 if (drop.move.y < 9) drop.move.y += gravity.y / 4;
78 }
79 drop.pos.add(drop.move);
80 if (drop.pos.x + 8 > gp.world.getWidthPx()) drop.pos.x -= gp.world.getWidthPx();
81 else if (drop.pos.x < 0) drop.pos.x += gp.world.getWidthPx();
82 drop.pos.y = MathUtils.round(drop.pos.y);
83 while (checkColl(drop.getRect())) {
84 drop.pos.y--;
85 drop.move.y = 0;
86 }
87 }
89 private void playerPhy(Player pl) {
90 pl.pos.y += pl.mov.y;
91 if (checkColl(pl.getRect())) {
92 int d = -1;
93 if (pl.mov.y < 0) d = 1;
94 if (d == -1) {
95 pl.flyMode = false;
96 pl.canJump = true;
97 }
98 pl.pos.y = MathUtils.round(pl.pos.y);
99 while (checkColl(pl.getRect())) pl.pos.y += d;
100 pl.mov.y = 0;
101 } else {
102 pl.canJump = false;
105 if (GameItems.isFluid(getBlock(pl.getRect()))) {
106 if (CaveGame.TOUCH && pl.mov.x != 0 && !gp.swim && !pl.flyMode) gp.swim = true;
107 if (!gp.swim) {
108 if (!pl.flyMode && pl.mov.y < 4.5f) pl.mov.add(gravity.x / 4, gravity.y / 4);
109 if (!pl.flyMode && pl.mov.y > 4.5f) pl.mov.add(0, -1f);
110 } else {
111 pl.mov.add(0, -.5f);
112 if (pl.mov.y < -3) pl.mov.y = -3;
114 } else {
115 if (!pl.flyMode && pl.mov.y < 18) pl.mov.add(gravity);
118 pl.pos.x += pl.mov.x;
119 if (checkColl(pl.getRect())) {
120 if (pl.canJump && !pl.flyMode) pl.pos.y -= 8;
121 if (checkColl(pl.getRect())) {
122 if (pl.canJump && !pl.flyMode) pl.pos.y += 8;
123 int d = 0;
124 if (pl.mov.x < 0) d = 1;
125 else if (pl.mov.x > 0) d = -1;
126 pl.pos.x = MathUtils.round(pl.pos.x);
127 while (checkColl(pl.getRect())) pl.pos.x += d;
130 if (pl.pos.x + pl.getWidth() / 2 < 0) pl.pos.x += gp.world.getWidthPx();
131 if (pl.pos.x + pl.getWidth() / 2 > gp.world.getWidthPx()) pl.pos.x -= gp.world.getWidthPx();
132 if (pl.pos.y > gp.world.getHeightPx()) {
133 pl.respawn(gp.world);
135 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.getDir()) && !pl.flyMode && pl.canJump && pl.mov.x != 0) {
136 pl.mov.add(0, -8);
137 pl.canJump = false;
141 private void mobPhy(Mob mob) {
142 mob.pos.y += mob.mov.y;
143 if (checkColl(mob.getRect())) {
144 int d = -1;
145 if (mob.mov.y < 0) d = 1;
146 if (d == -1) mob.canJump = true;
147 mob.pos.y = MathUtils.round(mob.pos.y);
148 while (checkColl(mob.getRect())) mob.pos.y += d;
149 mob.mov.y = 0;
150 if (mob.getType() > 0) {
151 gp.world.setForeMap(mob.getMapX(), mob.getMapY(), mob.getType());
152 mob.kill();
154 } else {
155 mob.canJump = false;
158 if (mob.getType() == 0 && GameItems.isFluid(getBlock(mob.getRect()))) {
159 if (mob.mov.y > 9) mob.mov.add(0, -.9f);
160 mob.mov.add(0, -.5f);
161 if (mob.mov.y < -3) mob.mov.y = -3;
162 } else if (mob.mov.y < 18) mob.mov.add(gravity);
164 mob.pos.x += mob.mov.x;
165 if (checkColl(mob.getRect())) {
166 if (mob.canJump) {
167 mob.pos.y -= 8;
169 if (checkColl(mob.getRect())) {
170 if (mob.canJump) mob.pos.y += 8;
171 int d = 0;
172 if (mob.mov.x < 0) d = 1;
173 else if (mob.mov.x > 0) d = -1;
174 mob.pos.x = MathUtils.round(mob.pos.x);
175 while (checkColl(mob.getRect())) mob.pos.x += d;
176 if (mob.canJump) mob.changeDir();
179 if (mob.pos.x + mob.getWidth() / 2 < 0) mob.pos.x += gp.world.getWidthPx();
180 if (mob.pos.x + mob.getWidth() / 2 > gp.world.getWidthPx()) mob.pos.x -= gp.world.getWidthPx();
181 if (mob.pos.y > gp.world.getHeightPx()) {
182 mob.pos.y = 0;
184 if (checkJump(mob.getRect(), mob.getDir()) && mob.canJump && mob.mov.x != 0) {
185 mob.mov.add(0, -8);
186 mob.canJump = false;
190 void update(float delta) {
191 for (Iterator<Drop> it = gp.drops.iterator(); it.hasNext(); ) {
192 Drop drop = it.next();
193 dropPhy(drop);
194 if (Intersector.overlaps(drop.getRect(), gp.player.getRect())) drop.pickUpDrop(gp.player);
195 if (drop.pickedUp) it.remove();
198 for (Iterator<Mob> it = gp.mobs.iterator(); it.hasNext(); ) {
199 Mob mob = it.next();
200 mob.ai();
201 mobPhy(mob);
202 if (mob.isDead()) it.remove();
205 playerPhy(gp.player);
207 gp.renderer.setCamPos(
208 gp.player.pos.x + (float) gp.player.getWidth() / 2 - gp.renderer.getWidth() / 2,
209 gp.player.pos.y + (float) gp.player.getHeight() / 2 - gp.renderer.getHeight() / 2);