DEADSOFTWARE

Better collision, disable auto jump
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GamePhysics.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.math.*;
4 import ru.deadsoftware.cavecraft.Items;
5 import ru.deadsoftware.cavecraft.game.mobs.Mob;
6 import ru.deadsoftware.cavecraft.game.objects.Player;
8 public class GamePhysics {
10 public static final int PL_SPEED = 2;
12 private GameProc gameProc;
14 private Vector2 gravity;
16 public GamePhysics(GameProc gameProc) {
17 this.gameProc = gameProc;
18 gravity = new Vector2(0,1);
19 }
21 private boolean checkJump(Rectangle rect, int dir) {
22 int bl = 0;
23 switch (dir) {
24 case 0:
25 if ((int)((rect.x+(rect.width/2))/16) - 1>=0)
26 bl = gameProc.world.getForeMap(
27 (int)((rect.x+(rect.width/2))/16) - 1,
28 (int)(rect.y/16)+1);
29 break;
30 case 1:
31 if ((int)((rect.x+(rect.width/2))/16) + 1<gameProc.world.getWidth())
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 && Items.BLOCKS.getValueAt(bl).collision);
40 }
42 private boolean checkColl(Rectangle rect) {
43 int[] ids = new int[6];
44 ids[0] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)rect.y/16));
45 ids[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)rect.y/16));
46 ids[2] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)(rect.y+rect.height/2)/16));
47 ids[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+rect.height/2)/16));
48 ids[4] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)(rect.y+rect.height-1)/16));
49 ids[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+(rect.height-1))/16));
50 Rectangle[] bl = new Rectangle[6];
51 if (ids[0]>0) bl[0] = Items.BLOCKS.getValueAt(ids[0]).getRect((int)(rect.x)/16,(int)rect.y/16);
52 if (ids[1]>0) bl[1] = Items.BLOCKS.getValueAt(ids[1]).getRect(((int)(rect.x+rect.width-1)/16), ((int)rect.y/16));
53 if (ids[2]>0) bl[2] = Items.BLOCKS.getValueAt(ids[2]).getRect(((int)(rect.x)/16), ((int)(rect.y+rect.height/2)/16));
54 if (ids[3]>0) bl[3] = Items.BLOCKS.getValueAt(ids[3]).getRect(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+rect.height/2)/16));
55 if (ids[4]>0) bl[4] = Items.BLOCKS.getValueAt(ids[4]).getRect(((int)(rect.x)/16), ((int)(rect.y+rect.height-1)/16));
56 if (ids[5]>0) bl[5] = Items.BLOCKS.getValueAt(ids[5]).getRect(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+(rect.height-1))/16));
57 for (int i=0; i<6; i++) {
58 if (ids[i]>0 &&
59 Items.BLOCKS.getValueAt(ids[i]).collision &&
60 Intersector.overlaps(rect,bl[i]))
61 return true;
62 }
63 return false;
64 }
66 private void playerPhy(Player pl) {
67 pl.position.add(pl.moveY);
68 if (checkColl(pl.getRect())) {
69 pl.flyMode = false;
70 pl.canJump = true;
71 int d = -1;
72 if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
73 pl.position.y = MathUtils.round(pl.position.y);
74 while (checkColl(pl.getRect())) pl.position.y+=d;
75 pl.moveY.setZero();
76 } else {
77 pl.canJump = false;
78 }
79 if (!pl.flyMode) pl.moveY.add(gravity);
80 pl.position.add(pl.moveX);
81 if (pl.position.x<0 ||
82 pl.position.x+pl.texWidth>=gameProc.world.getWidth()*16)
83 pl.position.sub(pl.moveX);
84 if (checkColl(pl.getRect())) {
85 int d = 0;
86 if (pl.moveX.x<0) d=1; else if (pl.moveX.x>0) d=-1;
87 pl.position.x = MathUtils.round(pl.position.x);
88 while (checkColl(pl.getRect())) pl.position.x+=d;
89 }
91 /*if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
92 pl.moveY.add(0, -8);
93 pl.canJump = false;
94 }*/
95 }
97 private void mobPhy(Mob mob) {
98 mob.position.add(mob.moveY);
99 if (checkColl(mob.getRect())) {
100 mob.canJump = true;
101 int d = -1;
102 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
103 mob.position.y = MathUtils.round(mob.position.y);
104 while (checkColl(mob.getRect())) mob.position.y+=d;
105 mob.moveY.setZero();
106 } else {
107 mob.canJump = false;
109 mob.moveY.add(gravity);
110 mob.position.add(mob.moveX);
111 if (mob.position.x<32 ||
112 mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
113 mob.position.sub(mob.moveX);
114 if (checkColl(mob.getRect())) {
115 int d = 0;
116 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
117 mob.position.x = MathUtils.round(mob.position.x);
118 while (checkColl(mob.getRect())) mob.position.x+=d;
122 public void update(float delta) {
124 for (Mob mob : gameProc.mobs) {
125 mob.ai();
126 mobPhy(mob);
128 playerPhy(gameProc.player);
130 gameProc.renderer.camera.position.set(
131 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
132 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
134 );