DEADSOFTWARE

Minor enhancements
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GamePhysics.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.math.MathUtils;
4 import com.badlogic.gdx.math.Rectangle;
5 import com.badlogic.gdx.math.Vector2;
6 import com.badlogic.gdx.math.Vector3;
7 import ru.deadsoftware.cavecraft.game.mobs.Mob;
8 import ru.deadsoftware.cavecraft.game.objects.Player;
10 public class GamePhysics {
12 public static final int PL_SPEED = 2;
14 private GameProc gameProc;
16 private Vector2 gravity;
18 public GamePhysics(GameProc gameProc) {
19 this.gameProc = gameProc;
20 gravity = new Vector2(0,1);
21 }
23 private boolean checkJump(Rectangle rect, int dir) {
24 int bl;
25 switch (dir) {
26 case 0:
27 bl = gameProc.world.getForeMap(
28 (int)((rect.x+(rect.width/2))/16) - 1,
29 (int)(rect.y/16)+1);
30 break;
31 case 1:
32 bl = gameProc.world.getForeMap(
33 (int)((rect.x+(rect.width/2))/16) + 1,
34 (int)(rect.y/16)+1);
35 break;
36 default:
37 bl=0;
38 }
39 return (bl!=0);
40 }
42 private boolean checkColl(Rectangle rect) {
43 int[] bl = new int [6];
44 bl[0] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)rect.y/16));
45 bl[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)rect.y/16));
46 bl[2] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)(rect.y+rect.height/2)/16));
47 bl[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+rect.height/2)/16));
48 bl[4] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)(rect.y+rect.height-1)/16));
49 bl[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+(rect.height-1))/16));
50 for (int b: bl) if (b>0) {
51 return true;
52 }
53 return false;
54 }
56 private void playerPhy(Player pl) {
57 pl.position.add(pl.moveY);
58 if (checkColl(pl.getRect())) {
59 pl.flyMode = false;
60 pl.canJump = true;
61 int d = -1;
62 if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
63 pl.position.y = MathUtils.round(pl.position.y);
64 while (checkColl(pl.getRect())) pl.position.y+=d;
65 pl.moveY.setZero();
66 } else {
67 pl.canJump = false;
68 }
69 if (!pl.flyMode) pl.moveY.add(gravity);
70 pl.position.add(pl.moveX);
71 if (pl.position.x<0 ||
72 pl.position.x+pl.texWidth>=gameProc.world.getWidth()*16)
73 pl.position.sub(pl.moveX);
74 if (checkColl(pl.getRect())) {
75 int d = 0;
76 if (pl.moveX.x<0) d=1; else if (pl.moveX.x>0) d=-1;
77 pl.position.x = MathUtils.round(pl.position.x);
78 while (checkColl(pl.getRect())) pl.position.x+=d;
79 }
81 if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
82 pl.moveY.add(0, -8);
83 pl.canJump = false;
84 }
85 }
87 private void mobPhy(Mob mob) {
88 mob.position.add(mob.moveY);
89 if (checkColl(mob.getRect())) {
90 mob.canJump = true;
91 int d = -1;
92 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
93 mob.position.y = MathUtils.round(mob.position.y);
94 while (checkColl(mob.getRect())) mob.position.y+=d;
95 mob.moveY.setZero();
96 } else {
97 mob.canJump = false;
98 }
99 mob.moveY.add(gravity);
100 mob.position.add(mob.moveX);
101 if (mob.position.x<32 ||
102 mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
103 mob.position.sub(mob.moveX);
104 if (checkColl(mob.getRect())) {
105 int d = 0;
106 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
107 mob.position.x = MathUtils.round(mob.position.x);
108 while (checkColl(mob.getRect())) mob.position.x+=d;
112 public void update(float delta) {
114 for (Mob mob : gameProc.mobs) {
115 mob.ai();
116 mobPhy(mob);
118 playerPhy(gameProc.player);
120 gameProc.renderer.camera.position.set(
121 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
122 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
124 );