DEADSOFTWARE

Even better collision
[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 bl;
44 int minX = (int) ((rect.x+rect.width/2)/16)-4;
45 int minY = (int) ((rect.y+rect.height/2)/16)-4;
46 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
47 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
48 if (minX<0) minX=0;
49 if (minY<0) minY=0;
50 if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth();
51 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
52 for (int y=minY; y<maxY; y++) {
53 for (int x=minX; x<maxX; x++) {
54 bl = gameProc.world.getForeMap(x,y);
55 if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
56 if (Intersector.overlaps(rect, Items.BLOCKS.getValueAt(bl).getRect(x,y))){
57 return true;
58 }
59 }
60 }
61 }
62 return false;
63 }
65 private void playerPhy(Player pl) {
66 pl.position.add(pl.moveY);
67 if (checkColl(pl.getRect())) {
68 pl.flyMode = false;
69 pl.canJump = true;
70 int d = -1;
71 if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
72 pl.position.y = MathUtils.round(pl.position.y);
73 while (checkColl(pl.getRect())) pl.position.y+=d;
74 pl.moveY.setZero();
75 } else {
76 pl.canJump = false;
77 }
78 if (!pl.flyMode) pl.moveY.add(gravity);
79 pl.position.add(pl.moveX);
80 if (pl.position.x<0 ||
81 pl.position.x+pl.texWidth>=gameProc.world.getWidth()*16)
82 pl.position.sub(pl.moveX);
83 if (checkColl(pl.getRect())) {
84 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
85 if (checkColl(pl.getRect())) {
86 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
87 int d = 0;
88 if (pl.moveX.x < 0) d = 1;
89 else if (pl.moveX.x > 0) d = -1;
90 pl.position.x = MathUtils.round(pl.position.x);
91 while (checkColl(pl.getRect())) pl.position.x += d;
92 }
93 }
95 /*if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
96 pl.moveY.add(0, -8);
97 pl.canJump = false;
98 }*/
99 }
101 private void mobPhy(Mob mob) {
102 mob.position.add(mob.moveY);
103 if (checkColl(mob.getRect())) {
104 mob.canJump = true;
105 int d = -1;
106 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
107 mob.position.y = MathUtils.round(mob.position.y);
108 while (checkColl(mob.getRect())) mob.position.y+=d;
109 mob.moveY.setZero();
110 } else {
111 mob.canJump = false;
113 mob.moveY.add(gravity);
114 mob.position.add(mob.moveX);
115 if (mob.position.x<32 ||
116 mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
117 mob.position.sub(mob.moveX);
118 if (checkColl(mob.getRect())) {
119 int d = 0;
120 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
121 mob.position.x = MathUtils.round(mob.position.x);
122 while (checkColl(mob.getRect())) mob.position.x+=d;
126 public void update(float delta) {
128 for (Mob mob : gameProc.mobs) {
129 mob.ai();
130 mobPhy(mob);
132 playerPhy(gameProc.player);
134 gameProc.renderer.camera.position.set(
135 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
136 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
138 );