DEADSOFTWARE

Fix controls on Android
[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.Items;
9 import ru.deadsoftware.cavecraft.game.mobs.Mob;
10 import ru.deadsoftware.cavecraft.game.objects.Player;
12 public class GamePhysics {
14 public static final int PL_SPEED = 2;
16 private GameProc gameProc;
18 private Vector2 gravity;
20 public GamePhysics(GameProc gameProc) {
21 this.gameProc = gameProc;
22 gravity = new Vector2(0,.9f);
23 }
25 private boolean checkJump(Rectangle rect, int dir) {
26 int bl = 0;
27 if (rect.x<0) rect.x-=16;
28 switch (dir) {
29 case 0:
30 bl = gameProc.world.getForeMap(
31 (int)((rect.x+(rect.width/2))/16) - 1,
32 (int)(rect.y/16)+1);
33 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16))>0) bl=0;
34 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16)-1)>0) bl=0;
35 break;
36 case 1:
37 bl = gameProc.world.getForeMap(
38 (int)((rect.x+(rect.width/2))/16) + 1,
39 (int)(rect.y/16)+1);
40 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16))>0) bl=0;
41 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16)-1)>0) bl=0;
42 break;
43 default:
44 bl=0;
45 }
46 return (bl>0 && Items.BLOCKS.getValueAt(bl).collision);
47 }
49 private boolean checkColl(Rectangle rect) {
50 int bl;
51 int minX = (int) ((rect.x+rect.width/2)/16)-4;
52 int minY = (int) ((rect.y+rect.height/2)/16)-4;
53 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
54 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
55 if (minY<0) minY=0;
56 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
57 for (int y=minY; y<maxY; y++) {
58 for (int x=minX; x<maxX; x++) {
59 bl = gameProc.world.getForeMap(x,y);
60 if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
61 if (Intersector.overlaps(rect, Items.BLOCKS.getValueAt(bl).getRect(x,y))){
62 return true;
63 }
64 }
65 }
66 }
67 return false;
68 }
70 private void playerPhy(Player pl) {
71 pl.position.add(pl.moveY);
72 if (checkColl(pl.getRect())) {
73 int d = -1;
74 if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
75 if (d==-1) {
76 pl.flyMode = false;
77 pl.canJump = true;
78 }
79 pl.position.y = MathUtils.round(pl.position.y);
80 while (checkColl(pl.getRect())) pl.position.y+=d;
81 pl.moveY.setZero();
82 } else {
83 pl.canJump = false;
84 }
85 if (!pl.flyMode && pl.moveY.y<18) pl.moveY.add(gravity);
86 pl.position.add(pl.moveX);
87 if (checkColl(pl.getRect())) {
88 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
89 if (checkColl(pl.getRect())) {
90 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
91 int d = 0;
92 if (pl.moveX.x < 0) d = 1;
93 else if (pl.moveX.x > 0) d = -1;
94 pl.position.x = MathUtils.round(pl.position.x);
95 while (checkColl(pl.getRect())) pl.position.x += d;
96 }
97 }
98 if (pl.position.y > gameProc.world.getHeight()*16) {
99 pl.position = gameProc.world.getSpawnPoint().cpy();
101 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
102 pl.moveY.add(0, -8);
103 pl.canJump = false;
107 private void mobPhy(Mob mob) {
108 mob.position.add(mob.moveY);
109 if (checkColl(mob.getRect())) {
110 mob.canJump = true;
111 int d = -1;
112 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
113 mob.position.y = MathUtils.round(mob.position.y);
114 while (checkColl(mob.getRect())) mob.position.y+=d;
115 mob.moveY.setZero();
116 } else {
117 mob.canJump = false;
119 mob.moveY.add(gravity);
120 mob.position.add(mob.moveX);
121 if (checkColl(mob.getRect())) {
122 int d = 0;
123 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
124 mob.position.x = MathUtils.round(mob.position.x);
125 while (checkColl(mob.getRect())) mob.position.x+=d;
129 public void update(float delta) {
130 for (Mob mob : gameProc.mobs) {
131 mob.ai();
132 mobPhy(mob);
134 playerPhy(gameProc.player);
136 gameProc.renderer.camera.position.set(
137 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
138 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
140 );