DEADSOFTWARE

34c4df111304a96557ee12ec0f767b3e8bfef40f
[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;
26 switch (dir) {
27 case 0:
28 bl = gameProc.world.getForeMap((int)((rect.x-8)/16),(int)((rect.y+rect.height-8)/16));
29 break;
30 case 1:
31 bl = gameProc.world.getForeMap((int)((rect.x+rect.width+8)/16),(int)((rect.y+rect.height-8)/16));
32 break;
33 default:
34 bl=0;
35 }
36 return (bl>0 && Items.BLOCKS.getValueAt(bl).collision);
37 }
39 private boolean checkColl(Rectangle rect) {
40 int bl;
41 int minX = (int) ((rect.x+rect.width/2)/16)-4;
42 int minY = (int) ((rect.y+rect.height/2)/16)-4;
43 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
44 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
45 if (minY<0) minY=0;
46 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
47 for (int y=minY; y<maxY; y++) {
48 for (int x=minX; x<maxX; x++) {
49 bl = gameProc.world.getForeMap(x,y);
50 if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
51 if (Intersector.overlaps(rect, Items.BLOCKS.getValueAt(bl).getRect(x,y))){
52 return true;
53 }
54 }
55 }
56 }
57 return false;
58 }
60 private void playerPhy(Player pl) {
61 pl.position.add(pl.moveY);
62 if (checkColl(pl.getRect())) {
63 int d = -1;
64 if (pl.moveY.y<0) d=1;
65 if (d==-1) {
66 pl.flyMode = false;
67 pl.canJump = true;
68 }
69 pl.position.y = MathUtils.round(pl.position.y);
70 while (checkColl(pl.getRect())) pl.position.y+=d;
71 pl.moveY.setZero();
72 } else {
73 pl.canJump = false;
74 }
75 if (!pl.flyMode && pl.moveY.y<18) pl.moveY.add(gravity);
76 pl.position.add(pl.moveX);
77 if (checkColl(pl.getRect())) {
78 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
79 if (checkColl(pl.getRect())) {
80 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
81 int d = 0;
82 if (pl.moveX.x < 0) d = 1;
83 else if (pl.moveX.x > 0) d = -1;
84 pl.position.x = MathUtils.round(pl.position.x);
85 while (checkColl(pl.getRect())) pl.position.x += d;
86 }
87 }
88 if (pl.position.x+pl.texWidth/2<0) pl.position.x+=gameProc.world.getWidth()*16;
89 if (pl.position.x+pl.texWidth/2>gameProc.world.getWidth()*16) pl.position.x-=gameProc.world.getWidth()*16;
90 if (pl.position.y > gameProc.world.getHeight()*16) {
91 pl.position = gameProc.world.getSpawnPoint().cpy();
92 }
93 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
94 pl.moveY.add(0, -8);
95 pl.canJump = false;
96 }
97 }
99 private void mobPhy(Mob mob) {
100 mob.position.add(mob.moveY);
101 if (checkColl(mob.getRect())) {
102 int d = -1;
103 if (mob.moveY.y<0) d=1;
104 if (d==-1) mob.canJump = true;
105 mob.position.y = MathUtils.round(mob.position.y);
106 while (checkColl(mob.getRect())) mob.position.y+=d;
107 mob.moveY.setZero();
108 } else {
109 mob.canJump = false;
111 if (mob.moveY.y<18) mob.moveY.add(gravity);
112 mob.position.add(mob.moveX);
113 if (checkColl(mob.getRect())) {
114 if (mob.canJump) {
115 mob.position.y-=8;
117 if (checkColl(mob.getRect())) {
118 if (mob.canJump) mob.position.y+=8;
119 int d = 0;
120 if (mob.moveX.x < 0) d = 1;
121 else if (mob.moveX.x > 0) d = -1;
122 mob.position.x = MathUtils.round(mob.position.x);
123 while (checkColl(mob.getRect())) mob.position.x += d;
126 if (mob.position.x+mob.width/2<0) mob.position.x+=gameProc.world.getWidth()*16;
127 if (mob.position.x+mob.width/2>gameProc.world.getWidth()*16) mob.position.x-=gameProc.world.getWidth()*16;
128 if (mob.position.y > gameProc.world.getHeight()*16) {
129 mob.position.y = 0;
131 if (checkJump(mob.getRect(), mob.dir) && mob.canJump && !mob.moveX.equals(Vector2.Zero)) {
132 mob.moveY.add(0, -8);
133 mob.canJump = false;
137 public void update(float delta) {
138 for (Mob mob : gameProc.mobs) {
139 mob.ai();
140 mobPhy(mob);
142 playerPhy(gameProc.player);
144 gameProc.renderer.camera.position.set(
145 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
146 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
148 );