DEADSOFTWARE

Autoswim on touch screen
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GamePhysics.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.Gdx;
4 import com.badlogic.gdx.math.Intersector;
5 import com.badlogic.gdx.math.MathUtils;
6 import com.badlogic.gdx.math.Rectangle;
7 import com.badlogic.gdx.math.Vector2;
8 import ru.deadsoftware.cavecraft.CaveGame;
9 import ru.deadsoftware.cavecraft.game.mobs.Mob;
10 import ru.deadsoftware.cavecraft.game.objects.Player;
12 import java.util.Iterator;
14 public class GamePhysics {
16 public static final int PL_SPEED = 2;
18 private GameProc gameProc;
20 private Vector2 gravity;
22 public GamePhysics(GameProc gameProc) {
23 this.gameProc = gameProc;
24 gravity = new Vector2(0,.9f);
25 }
27 private boolean checkJump(Rectangle rect, int dir) {
28 int bl;
29 switch (dir) {
30 case 0:
31 bl = gameProc.world.getForeMap((int)((rect.x-8)/16),(int)((rect.y+rect.height-8)/16));
32 if (checkColl(new Rectangle(rect.x+rect.width/2, rect.y-18, rect.width, rect.height))) bl=0;
33 break;
34 case 1:
35 bl = gameProc.world.getForeMap((int)((rect.x+rect.width+8)/16),(int)((rect.y+rect.height-8)/16));
36 if (checkColl(new Rectangle(rect.x+rect.width/2, rect.y-18, rect.width, rect.height))) bl=0;
37 break;
38 default:
39 bl=0;
40 }
41 return (bl>0 && Items.BLOCKS.getValueAt(bl).toJump() &&
42 (rect.y+rect.height)-Items.BLOCKS.getValueAt(bl).getRect((int)((rect.x-8)/16),(int)((rect.y+rect.height-8)/16)).y>8);
43 }
45 private boolean checkColl(Rectangle rect) {
46 int bl;
47 int minX = (int) ((rect.x+rect.width/2)/16)-4;
48 int minY = (int) ((rect.y+rect.height/2)/16)-4;
49 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
50 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
51 if (minY<0) minY=0;
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 int getBlock(Rectangle rect) {
67 return gameProc.world.getForeMap((int)(rect.x+rect.width/2)/16, (int)(rect.y+rect.height/8*7)/16);
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;
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 }
86 if (Items.isFluid(getBlock(pl.getRect()))) {
87 if (CaveGame.TOUCH && pl.moveX.x!=0 && !gameProc.swim && !pl.flyMode) gameProc.swim = true;
88 if (!gameProc.swim) {
89 if (!pl.flyMode && pl.moveY.y < 9) pl.moveY.add(gravity.x / 2, gravity.y / 2);
90 if (!pl.flyMode && pl.moveY.y > 9) pl.moveY.add(0, -.9f);
91 } else {
92 pl.moveY.add(0, -.5f);
93 if (pl.moveY.y<-3) pl.moveY.y = -3;
94 }
95 } else {
96 if (!pl.flyMode && pl.moveY.y<18) pl.moveY.add(gravity);
97 if (CaveGame.TOUCH && gameProc.swim) gameProc.swim = false;
98 }
100 pl.position.add(pl.moveX);
101 if (checkColl(pl.getRect())) {
102 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
103 if (checkColl(pl.getRect())) {
104 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
105 int d = 0;
106 if (pl.moveX.x < 0) d = 1;
107 else if (pl.moveX.x > 0) d = -1;
108 pl.position.x = MathUtils.round(pl.position.x);
109 while (checkColl(pl.getRect())) pl.position.x += d;
112 if (pl.position.x+pl.texWidth/2<0) pl.position.x+=gameProc.world.getWidth()*16;
113 if (pl.position.x+pl.texWidth/2>gameProc.world.getWidth()*16) pl.position.x-=gameProc.world.getWidth()*16;
114 if (pl.position.y > gameProc.world.getHeight()*16) {
115 pl.position = gameProc.world.getSpawnPoint().cpy();
117 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
118 pl.moveY.add(0, -8);
119 pl.canJump = false;
123 private void mobPhy(Mob mob) {
124 mob.position.add(mob.moveY);
125 if (checkColl(mob.getRect())) {
126 int d = -1;
127 if (mob.moveY.y<0) d=1;
128 if (d==-1) mob.canJump = true;
129 mob.position.y = MathUtils.round(mob.position.y);
130 while (checkColl(mob.getRect())) mob.position.y+=d;
131 mob.moveY.setZero();
132 if (mob.getType() > 0) {
133 gameProc.world.setForeMap((int)mob.position.x/16, (int)mob.position.y/16, mob.getType());
134 mob.position.y = -1;
135 mob.dead = true;
137 } else {
138 mob.canJump = false;
141 if (mob.getType()==0 && Items.isFluid(getBlock(mob.getRect()))) {
142 if (mob.moveY.y > 9) mob.moveY.add(0, -.9f);
143 mob.moveY.add(0, -.5f);
144 if (mob.moveY.y<-3) mob.moveY.y = -3;
145 } else if (mob.moveY.y<18) mob.moveY.add(gravity);
147 mob.position.add(mob.moveX);
148 if (checkColl(mob.getRect())) {
149 if (mob.canJump) {
150 mob.position.y-=8;
152 if (checkColl(mob.getRect())) {
153 if (mob.canJump) mob.position.y+=8;
154 int d = 0;
155 if (mob.moveX.x < 0) d = 1;
156 else if (mob.moveX.x > 0) d = -1;
157 mob.position.x = MathUtils.round(mob.position.x);
158 while (checkColl(mob.getRect())) mob.position.x += d;
159 if (mob.canJump) mob.changeDir();
162 if (mob.position.x+mob.width/2<0) mob.position.x+=gameProc.world.getWidth()*16;
163 if (mob.position.x+mob.width/2>gameProc.world.getWidth()*16) mob.position.x-=gameProc.world.getWidth()*16;
164 if (mob.position.y > gameProc.world.getHeight()*16) {
165 mob.position.y = 0;
167 if (checkJump(mob.getRect(), mob.dir) && mob.canJump && !mob.moveX.equals(Vector2.Zero)) {
168 mob.moveY.add(0, -8);
169 mob.canJump = false;
173 public void update(float delta) {
174 for (Mob mob : gameProc.mobs) {
175 mob.ai();
176 mobPhy(mob);
178 for (Iterator<Mob> it = gameProc.mobs.iterator(); it.hasNext();) {
179 Mob m = it.next();
180 if (m.dead)
181 it.remove();
183 playerPhy(gameProc.player);
185 gameProc.renderer.camera.position.set(
186 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
187 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
189 );