DEADSOFTWARE

Move misc classes
[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.game.mobs.Mob;
9 import ru.deadsoftware.cavecraft.game.objects.Player;
11 public class GamePhysics {
13 public static final int PL_SPEED = 2;
15 private GameProc gameProc;
17 private Vector2 gravity;
19 public GamePhysics(GameProc gameProc) {
20 this.gameProc = gameProc;
21 gravity = new Vector2(0,.9f);
22 }
24 private boolean checkJump(Rectangle rect, int dir) {
25 int bl = 0;
26 switch (dir) {
27 case 0:
28 bl = gameProc.world.getForeMap(
29 (int)((rect.x+(rect.width/2))/16) - 1,
30 (int)(rect.y/16)+1);
31 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16))>0) bl=0;
32 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16)-1)>0) bl=0;
33 break;
34 case 1:
35 bl = gameProc.world.getForeMap(
36 (int)((rect.x+(rect.width/2))/16) + 1,
37 (int)(rect.y/16)+1);
38 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16))>0) bl=0;
39 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16)-1)>0) bl=0;
40 break;
41 default:
42 bl=0;
43 }
44 return (bl>0 && Items.BLOCKS.getValueAt(bl).collision);
45 }
47 private boolean checkColl(Rectangle rect) {
48 int bl;
49 int minX = (int) ((rect.x+rect.width/2)/16)-4;
50 int minY = (int) ((rect.y+rect.height/2)/16)-4;
51 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
52 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
53 if (minY<0) minY=0;
54 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
55 for (int y=minY; y<maxY; y++) {
56 for (int x=minX; x<maxX; x++) {
57 bl = gameProc.world.getForeMap(x,y);
58 if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
59 if (Intersector.overlaps(rect, Items.BLOCKS.getValueAt(bl).getRect(x,y))){
60 return true;
61 }
62 }
63 }
64 }
65 return false;
66 }
68 private void playerPhy(Player pl) {
69 pl.position.add(pl.moveY);
70 if (checkColl(pl.getRect())) {
71 int d = -1;
72 if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
73 if (d==-1) {
74 pl.flyMode = false;
75 pl.canJump = true;
76 }
77 pl.position.y = MathUtils.round(pl.position.y);
78 while (checkColl(pl.getRect())) pl.position.y+=d;
79 pl.moveY.setZero();
80 } else {
81 pl.canJump = false;
82 }
83 if (!pl.flyMode && pl.moveY.y<18) pl.moveY.add(gravity);
84 pl.position.add(pl.moveX);
85 if (checkColl(pl.getRect())) {
86 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
87 if (checkColl(pl.getRect())) {
88 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
89 int d = 0;
90 if (pl.moveX.x < 0) d = 1;
91 else if (pl.moveX.x > 0) d = -1;
92 pl.position.x = MathUtils.round(pl.position.x);
93 while (checkColl(pl.getRect())) pl.position.x += d;
94 }
95 }
96 if (pl.position.x+pl.texWidth/2<0) pl.position.x+=gameProc.world.getWidth()*16;
97 if (pl.position.x+pl.texWidth/2>gameProc.world.getWidth()*16) pl.position.x-=gameProc.world.getWidth()*16;
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 (mob.position.x+mob.width/2<0) mob.position.x+=gameProc.world.getWidth()*16;
122 if (mob.position.x+mob.width/2>gameProc.world.getWidth()*16) mob.position.x-=gameProc.world.getWidth()*16;
123 if (checkColl(mob.getRect())) {
124 int d = 0;
125 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
126 mob.position.x = MathUtils.round(mob.position.x);
127 while (checkColl(mob.getRect())) mob.position.x+=d;
131 public void update(float delta) {
132 for (Mob mob : gameProc.mobs) {
133 mob.ai();
134 mobPhy(mob);
136 playerPhy(gameProc.player);
138 gameProc.renderer.camera.position.set(
139 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
140 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
142 );