DEADSOFTWARE

Update input handling (pretty messy)
[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.CaveGame;
5 import ru.deadsoftware.cavecraft.Items;
6 import ru.deadsoftware.cavecraft.game.mobs.Mob;
7 import ru.deadsoftware.cavecraft.game.objects.Player;
9 public class GamePhysics {
11 public static final int PL_SPEED = 2;
13 private GameProc gameProc;
15 private Vector2 gravity;
17 public GamePhysics(GameProc gameProc) {
18 this.gameProc = gameProc;
19 gravity = new Vector2(0,1);
20 }
22 private boolean checkJump(Rectangle rect, int dir) {
23 int bl = 0;
24 switch (dir) {
25 case 0:
26 if ((int)((rect.x+(rect.width/2))/16) - 1>=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 if ((int)((rect.x+(rect.width/2))/16) + 1<gameProc.world.getWidth())
33 bl = gameProc.world.getForeMap(
34 (int)((rect.x+(rect.width/2))/16) + 1,
35 (int)(rect.y/16)+1);
36 break;
37 default:
38 bl=0;
39 }
40 return (bl>0 && Items.BLOCKS.getValueAt(bl).collision);
41 }
43 private boolean checkColl(Rectangle rect) {
44 int bl;
45 int minX = (int) ((rect.x+rect.width/2)/16)-4;
46 int minY = (int) ((rect.y+rect.height/2)/16)-4;
47 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
48 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
49 if (minX<0) minX=0;
50 if (minY<0) minY=0;
51 if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth();
52 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
53 for (int y=minY; y<maxY; y++) {
54 for (int x=minX; x<maxX; x++) {
55 bl = gameProc.world.getForeMap(x,y);
56 if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
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 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 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
86 if (checkColl(pl.getRect())) {
87 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
88 int d = 0;
89 if (pl.moveX.x < 0) d = 1;
90 else if (pl.moveX.x > 0) d = -1;
91 pl.position.x = MathUtils.round(pl.position.x);
92 while (checkColl(pl.getRect())) pl.position.x += d;
93 }
94 }
95 if (pl.position.y > gameProc.world.getHeight()*16) {
96 pl.position = gameProc.world.getSpawnPoint().cpy();
97 }
98 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
99 pl.moveY.add(0, -8);
100 pl.canJump = false;
104 private void mobPhy(Mob mob) {
105 mob.position.add(mob.moveY);
106 if (checkColl(mob.getRect())) {
107 mob.canJump = true;
108 int d = -1;
109 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
110 mob.position.y = MathUtils.round(mob.position.y);
111 while (checkColl(mob.getRect())) mob.position.y+=d;
112 mob.moveY.setZero();
113 } else {
114 mob.canJump = false;
116 mob.moveY.add(gravity);
117 mob.position.add(mob.moveX);
118 if (mob.position.x<32 ||
119 mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
120 mob.position.sub(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) {
131 for (Mob mob : gameProc.mobs) {
132 mob.ai();
133 mobPhy(mob);
135 playerPhy(gameProc.player);
137 gameProc.renderer.camera.position.set(
138 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
139 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
141 );