DEADSOFTWARE

Some refactor
[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 public class GamePhysics {
16 public static final int PL_SPEED = 2;
18 private GameProc gp;
20 private Vector2 gravity;
22 public 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 && Items.blocks.getValueAt(bl).toJump() &&
42 (rect.y + rect.height) - Items.blocks.getValueAt(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 && Items.blocks.getValueAt(bl).coll) {
57 if (Intersector.overlaps(rect, Items.blocks.getValueAt(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.move.y < 9) drop.move.y += gravity.y / 4;
72 drop.position.add(drop.move);
73 drop.position.y = MathUtils.round(drop.position.y);
74 while (checkColl(drop.getRect())) {
75 drop.position.y--;
76 drop.move.y = 0;
77 }
78 }
80 private void playerPhy(Player pl) {
81 pl.position.y += pl.move.y;
82 if (checkColl(pl.getRect())) {
83 int d = -1;
84 if (pl.move.y < 0) d = 1;
85 if (d == -1) {
86 pl.flyMode = false;
87 pl.canJump = true;
88 }
89 pl.position.y = MathUtils.round(pl.position.y);
90 while (checkColl(pl.getRect())) pl.position.y += d;
91 pl.move.y = 0;
92 } else {
93 pl.canJump = false;
94 }
96 if (Items.isFluid(getBlock(pl.getRect()))) {
97 if (CaveGame.TOUCH && pl.move.x != 0 && !gp.swim && !pl.flyMode) gp.swim = true;
98 if (!gp.swim) {
99 if (!pl.flyMode && pl.move.y < 4.5f) pl.move.add(gravity.x / 4, gravity.y / 4);
100 if (!pl.flyMode && pl.move.y > 4.5f) pl.move.add(0, -1f);
101 } else {
102 pl.move.add(0, -.5f);
103 if (pl.move.y < -3) pl.move.y = -3;
105 } else {
106 if (!pl.flyMode && pl.move.y < 18) pl.move.add(gravity);
109 pl.position.x += pl.move.x;
110 if (checkColl(pl.getRect())) {
111 if (pl.canJump && !pl.flyMode) pl.position.y -= 8;
112 if (checkColl(pl.getRect())) {
113 if (pl.canJump && !pl.flyMode) pl.position.y += 8;
114 int d = 0;
115 if (pl.move.x < 0) d = 1;
116 else if (pl.move.x > 0) d = -1;
117 pl.position.x = MathUtils.round(pl.position.x);
118 while (checkColl(pl.getRect())) pl.position.x += d;
121 if (pl.position.x + pl.texWidth / 2 < 0) pl.position.x += gp.world.getWidth() * 16;
122 if (pl.position.x + pl.texWidth / 2 > gp.world.getWidth() * 16)
123 pl.position.x -= gp.world.getWidth() * 16;
124 if (pl.position.y > gp.world.getHeight() * 16) {
125 pl.position = gp.world.getSpawnPoint().cpy();
127 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && pl.move.x != 0) {
128 pl.move.add(0, -8);
129 pl.canJump = false;
133 private void mobPhy(Mob mob) {
134 mob.position.y += mob.move.y;
135 if (checkColl(mob.getRect())) {
136 int d = -1;
137 if (mob.move.y < 0) d = 1;
138 if (d == -1) mob.canJump = true;
139 mob.position.y = MathUtils.round(mob.position.y);
140 while (checkColl(mob.getRect())) mob.position.y += d;
141 mob.move.y = 0;
142 if (mob.getType() > 0) {
143 gp.world.setForeMap((int) mob.position.x / 16, (int) mob.position.y / 16, mob.getType());
144 mob.position.y = -1;
145 mob.dead = true;
147 } else {
148 mob.canJump = false;
151 if (mob.getType() == 0 && Items.isFluid(getBlock(mob.getRect()))) {
152 if (mob.move.y > 9) mob.move.add(0, -.9f);
153 mob.move.add(0, -.5f);
154 if (mob.move.y < -3) mob.move.y = -3;
155 } else if (mob.move.y < 18) mob.move.add(gravity);
157 mob.position.x += mob.move.x;
158 if (checkColl(mob.getRect())) {
159 if (mob.canJump) {
160 mob.position.y -= 8;
162 if (checkColl(mob.getRect())) {
163 if (mob.canJump) mob.position.y += 8;
164 int d = 0;
165 if (mob.move.x < 0) d = 1;
166 else if (mob.move.x > 0) d = -1;
167 mob.position.x = MathUtils.round(mob.position.x);
168 while (checkColl(mob.getRect())) mob.position.x += d;
169 if (mob.canJump) mob.changeDir();
172 if (mob.position.x + mob.width / 2 < 0) mob.position.x += gp.world.getWidth() * 16;
173 if (mob.position.x + mob.width / 2 > gp.world.getWidth() * 16)
174 mob.position.x -= gp.world.getWidth() * 16;
175 if (mob.position.y > gp.world.getHeight() * 16) {
176 mob.position.y = 0;
178 if (checkJump(mob.getRect(), mob.dir) && mob.canJump && mob.move.x != 0) {
179 mob.move.add(0, -8);
180 mob.canJump = false;
184 public void update(float delta) {
185 for (Iterator<Drop> it = gp.drops.iterator(); it.hasNext(); ) {
186 Drop drop = it.next();
187 dropPhy(drop);
188 if (Intersector.overlaps(drop.getRect(), gp.player.getRect()))
189 drop.pickUpDrop(gp.player);
190 if (drop.pickedUp) it.remove();
193 for (Iterator<Mob> it = gp.mobs.iterator(); it.hasNext(); ) {
194 Mob mob = it.next();
195 mob.ai();
196 mobPhy(mob);
197 if (mob.dead)
198 it.remove();
201 playerPhy(gp.player);
203 gp.renderer.setCamPos(
204 gp.player.position.x + gp.player.texWidth / 2 - gp.renderer.getWidth() / 2,
205 gp.player.position.y + gp.player.height / 2 - gp.renderer.getHeight() / 2);