DEADSOFTWARE

Add drop and block breaking
[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.Drop;
11 import ru.deadsoftware.cavecraft.game.objects.Player;
13 import java.util.Iterator;
15 public class GamePhysics {
17 public static final int PL_SPEED = 2;
19 private GameProc gameProc;
21 private Vector2 gravity;
23 public GamePhysics(GameProc gameProc) {
24 this.gameProc = gameProc;
25 gravity = new Vector2(0,.9f);
26 }
28 private boolean checkJump(Rectangle rect, int dir) {
29 int bl;
30 switch (dir) {
31 case 0:
32 bl = gameProc.world.getForeMap((int)((rect.x-8)/16),(int)((rect.y+rect.height-8)/16));
33 if (checkColl(new Rectangle(rect.x-8, rect.y-18, rect.width, rect.height))) bl=0;
34 break;
35 case 1:
36 bl = gameProc.world.getForeMap((int)((rect.x+rect.width+8)/16),(int)((rect.y+rect.height-8)/16));
37 if (checkColl(new Rectangle(rect.x+rect.width+8, rect.y-18, rect.width, rect.height))) bl=0;
38 break;
39 default:
40 bl=0;
41 }
42 return (bl>0 && Items.BLOCKS.getValueAt(bl).toJump() &&
43 (rect.y+rect.height)-Items.BLOCKS.getValueAt(bl).getRect((int)((rect.x-8)/16),(int)((rect.y+rect.height-8)/16)).y>8);
44 }
46 private boolean checkColl(Rectangle rect) {
47 int bl;
48 int minX = (int) ((rect.x+rect.width/2)/16)-4;
49 int minY = (int) ((rect.y+rect.height/2)/16)-4;
50 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
51 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
52 if (minY<0) minY=0;
53 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
54 for (int y=minY; y<maxY; y++) {
55 for (int x=minX; x<maxX; x++) {
56 bl = gameProc.world.getForeMap(x,y);
57 if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
58 if (Intersector.overlaps(rect, Items.BLOCKS.getValueAt(bl).getRect(x,y))){
59 return true;
60 }
61 }
62 }
63 }
64 return false;
65 }
67 private int getBlock(Rectangle rect) {
68 return gameProc.world.getForeMap((int)(rect.x+rect.width/2)/16, (int)(rect.y+rect.height/8*7)/16);
69 }
71 private void dropPhy(Drop drop) {
72 if (drop.move.y < 9) drop.move.y += gravity.y/4;
73 drop.position.add(drop.move);
74 drop.position.y = MathUtils.round(drop.position.y);
75 while (checkColl(drop.getRect())) {
76 drop.position.y--;
77 drop.move.y = 0;
78 }
79 }
81 private void playerPhy(Player pl) {
82 pl.position.add(pl.moveY);
83 if (checkColl(pl.getRect())) {
84 int d = -1;
85 if (pl.moveY.y<0) d=1;
86 if (d==-1) {
87 pl.flyMode = false;
88 pl.canJump = true;
89 }
90 pl.position.y = MathUtils.round(pl.position.y);
91 while (checkColl(pl.getRect())) pl.position.y+=d;
92 pl.moveY.setZero();
93 } else {
94 pl.canJump = false;
95 }
97 if (Items.isFluid(getBlock(pl.getRect()))) {
98 if (CaveGame.TOUCH && pl.moveX.x!=0 && !gameProc.swim && !pl.flyMode) gameProc.swim = true;
99 if (!gameProc.swim) {
100 if (!pl.flyMode && pl.moveY.y < 4.5f) pl.moveY.add(gravity.x / 4, gravity.y / 4);
101 if (!pl.flyMode && pl.moveY.y > 4.5f) pl.moveY.add(0, -1f);
102 } else {
103 pl.moveY.add(0, -.5f);
104 if (pl.moveY.y<-3) pl.moveY.y = -3;
106 } else {
107 if (!pl.flyMode && pl.moveY.y<18) pl.moveY.add(gravity);
110 pl.position.add(pl.moveX);
111 if (checkColl(pl.getRect())) {
112 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
113 if (checkColl(pl.getRect())) {
114 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
115 int d = 0;
116 if (pl.moveX.x < 0) d = 1;
117 else if (pl.moveX.x > 0) d = -1;
118 pl.position.x = MathUtils.round(pl.position.x);
119 while (checkColl(pl.getRect())) pl.position.x += d;
122 if (pl.position.x+pl.texWidth/2<0) pl.position.x+=gameProc.world.getWidth()*16;
123 if (pl.position.x+pl.texWidth/2>gameProc.world.getWidth()*16) pl.position.x-=gameProc.world.getWidth()*16;
124 if (pl.position.y > gameProc.world.getHeight()*16) {
125 pl.position = gameProc.world.getSpawnPoint().cpy();
127 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
128 pl.moveY.add(0, -8);
129 pl.canJump = false;
133 private void mobPhy(Mob mob) {
134 mob.position.add(mob.moveY);
135 if (checkColl(mob.getRect())) {
136 int d = -1;
137 if (mob.moveY.y<0) d=1;
138 if (d==-1) mob.canJump = true;
139 mob.position.y = MathUtils.round(mob.position.y);
140 while (checkColl(mob.getRect())) mob.position.y+=d;
141 mob.moveY.setZero();
142 if (mob.getType() > 0) {
143 gameProc.world.setForeMap((int)mob.position.x/16, (int)mob.position.y/16, mob.getType());
144 mob.position.y = -1;
145 mob.dead = true;
147 } else {
148 mob.canJump = false;
151 if (mob.getType()==0 && Items.isFluid(getBlock(mob.getRect()))) {
152 if (mob.moveY.y > 9) mob.moveY.add(0, -.9f);
153 mob.moveY.add(0, -.5f);
154 if (mob.moveY.y<-3) mob.moveY.y = -3;
155 } else if (mob.moveY.y<18) mob.moveY.add(gravity);
157 mob.position.add(mob.moveX);
158 if (checkColl(mob.getRect())) {
159 if (mob.canJump) {
160 mob.position.y-=8;
162 if (checkColl(mob.getRect())) {
163 if (mob.canJump) mob.position.y+=8;
164 int d = 0;
165 if (mob.moveX.x < 0) d = 1;
166 else if (mob.moveX.x > 0) d = -1;
167 mob.position.x = MathUtils.round(mob.position.x);
168 while (checkColl(mob.getRect())) mob.position.x += d;
169 if (mob.canJump) mob.changeDir();
172 if (mob.position.x+mob.width/2<0) mob.position.x+=gameProc.world.getWidth()*16;
173 if (mob.position.x+mob.width/2>gameProc.world.getWidth()*16) mob.position.x-=gameProc.world.getWidth()*16;
174 if (mob.position.y > gameProc.world.getHeight()*16) {
175 mob.position.y = 0;
177 if (checkJump(mob.getRect(), mob.dir) && mob.canJump && !mob.moveX.equals(Vector2.Zero)) {
178 mob.moveY.add(0, -8);
179 mob.canJump = false;
183 public void update(float delta) {
184 for (Drop drop : gameProc.drops) dropPhy(drop);
185 for (Mob mob : gameProc.mobs) {
186 mob.ai();
187 mobPhy(mob);
189 for (Iterator<Mob> it = gameProc.mobs.iterator(); it.hasNext();) {
190 Mob m = it.next();
191 if (m.dead)
192 it.remove();
194 playerPhy(gameProc.player);
196 gameProc.renderer.camera.position.set(
197 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
198 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
200 );