DEADSOFTWARE

2d59d349118d0da781cc7d703aa9a2811768626a
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GamePhysics.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.math.MathUtils;
4 import com.badlogic.gdx.math.Rectangle;
5 import com.badlogic.gdx.math.Vector2;
6 import com.badlogic.gdx.math.Vector3;
7 import ru.deadsoftware.cavecraft.Items;
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,1);
22 }
24 private boolean checkJump(Rectangle rect, int dir) {
25 int bl = 0;
26 switch (dir) {
27 case 0:
28 if ((int)((rect.x+(rect.width/2))/16) - 1>=0)
29 bl = gameProc.world.getForeMap(
30 (int)((rect.x+(rect.width/2))/16) - 1,
31 (int)(rect.y/16)+1);
32 break;
33 case 1:
34 if ((int)((rect.x+(rect.width/2))/16) + 1<gameProc.world.getWidth())
35 bl = gameProc.world.getForeMap(
36 (int)((rect.x+(rect.width/2))/16) + 1,
37 (int)(rect.y/16)+1);
38 break;
39 default:
40 bl=0;
41 }
42 return (bl>0 && Items.BLOCKS.getValueAt(bl).collision);
43 }
45 private boolean checkColl(Rectangle rect) {
46 int[] bl = new int [6];
47 bl[0] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)rect.y/16));
48 bl[1] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)rect.y/16));
49 bl[2] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)(rect.y+rect.height/2)/16));
50 bl[3] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+rect.height/2)/16));
51 bl[4] = gameProc.world.getForeMap(((int)(rect.x)/16), ((int)(rect.y+rect.height-1)/16));
52 bl[5] = gameProc.world.getForeMap(((int)(rect.x+rect.width-1)/16), ((int)(rect.y+(rect.height-1))/16));
53 for (int b: bl) if (b>0 && Items.BLOCKS.getValueAt(b).collision) {
54 return true;
55 }
56 return false;
57 }
59 private void playerPhy(Player pl) {
60 pl.position.add(pl.moveY);
61 if (checkColl(pl.getRect())) {
62 pl.flyMode = false;
63 pl.canJump = true;
64 int d = -1;
65 if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
66 pl.position.y = MathUtils.round(pl.position.y);
67 while (checkColl(pl.getRect())) pl.position.y+=d;
68 pl.moveY.setZero();
69 } else {
70 pl.canJump = false;
71 }
72 if (!pl.flyMode) pl.moveY.add(gravity);
73 pl.position.add(pl.moveX);
74 if (pl.position.x<0 ||
75 pl.position.x+pl.texWidth>=gameProc.world.getWidth()*16)
76 pl.position.sub(pl.moveX);
77 if (checkColl(pl.getRect())) {
78 int d = 0;
79 if (pl.moveX.x<0) d=1; else if (pl.moveX.x>0) d=-1;
80 pl.position.x = MathUtils.round(pl.position.x);
81 while (checkColl(pl.getRect())) pl.position.x+=d;
82 }
84 if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
85 pl.moveY.add(0, -8);
86 pl.canJump = false;
87 }
88 }
90 private void mobPhy(Mob mob) {
91 mob.position.add(mob.moveY);
92 if (checkColl(mob.getRect())) {
93 mob.canJump = true;
94 int d = -1;
95 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
96 mob.position.y = MathUtils.round(mob.position.y);
97 while (checkColl(mob.getRect())) mob.position.y+=d;
98 mob.moveY.setZero();
99 } else {
100 mob.canJump = false;
102 mob.moveY.add(gravity);
103 mob.position.add(mob.moveX);
104 if (mob.position.x<32 ||
105 mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
106 mob.position.sub(mob.moveX);
107 if (checkColl(mob.getRect())) {
108 int d = 0;
109 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
110 mob.position.x = MathUtils.round(mob.position.x);
111 while (checkColl(mob.getRect())) mob.position.x+=d;
115 public void update(float delta) {
117 for (Mob mob : gameProc.mobs) {
118 mob.ai();
119 mobPhy(mob);
121 playerPhy(gameProc.player);
123 gameProc.renderer.camera.position.set(
124 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
125 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
127 );