DEADSOFTWARE

Add pigs
[cavedroid.git] / core / src / ru / deadsoftware / cavecraft / game / GamePhysics.java
1 package ru.deadsoftware.cavecraft.game;
3 import com.badlogic.gdx.math.*;
4 import ru.deadsoftware.cavecraft.CaveGame;
5 import ru.deadsoftware.cavecraft.Items;
6 import ru.deadsoftware.cavecraft.game.mobs.Mob;
7 import ru.deadsoftware.cavecraft.game.objects.Player;
9 public class GamePhysics {
11 public static final int PL_SPEED = 2;
13 private GameProc gameProc;
15 private Vector2 gravity;
17 public GamePhysics(GameProc gameProc) {
18 this.gameProc = gameProc;
19 gravity = new Vector2(0,.9f);
20 }
22 private boolean checkJump(Rectangle rect, int dir) {
23 int bl = 0;
24 switch (dir) {
25 case 0:
26 if ((int)((rect.x+(rect.width/2))/16) - 1>=0)
27 bl = gameProc.world.getForeMap(
28 (int)((rect.x+(rect.width/2))/16) - 1,
29 (int)(rect.y/16)+1);
30 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16))>0) bl=0;
31 break;
32 case 1:
33 if ((int)((rect.x+(rect.width/2))/16) + 1<gameProc.world.getWidth())
34 bl = gameProc.world.getForeMap(
35 (int)((rect.x+(rect.width/2))/16) + 1,
36 (int)(rect.y/16)+1);
37 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16))>0) bl=0;
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;
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 (minX<0) minX=0;
52 if (minY<0) minY=0;
53 if (maxX>gameProc.world.getWidth()) maxX = gameProc.world.getWidth();
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 (pl.position.x<0 ||
86 pl.position.x+pl.texWidth>=gameProc.world.getWidth()*16)
87 pl.position.sub(pl.moveX);
88 if (checkColl(pl.getRect())) {
89 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
90 if (checkColl(pl.getRect())) {
91 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
92 int d = 0;
93 if (pl.moveX.x < 0) d = 1;
94 else if (pl.moveX.x > 0) d = -1;
95 pl.position.x = MathUtils.round(pl.position.x);
96 while (checkColl(pl.getRect())) pl.position.x += d;
97 }
98 }
99 if (pl.position.y > gameProc.world.getHeight()*16) {
100 pl.position = gameProc.world.getSpawnPoint().cpy();
102 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
103 pl.moveY.add(0, -8);
104 pl.canJump = false;
108 private void mobPhy(Mob mob) {
109 mob.position.add(mob.moveY);
110 if (checkColl(mob.getRect())) {
111 mob.canJump = true;
112 int d = -1;
113 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
114 mob.position.y = MathUtils.round(mob.position.y);
115 while (checkColl(mob.getRect())) mob.position.y+=d;
116 mob.moveY.setZero();
117 } else {
118 mob.canJump = false;
120 mob.moveY.add(gravity);
121 mob.position.add(mob.moveX);
122 if (mob.position.x<32 ||
123 mob.position.x+mob.width>gameProc.world.getWidth()*16-32)
124 mob.position.sub(mob.moveX);
125 if (checkColl(mob.getRect())) {
126 int d = 0;
127 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
128 mob.position.x = MathUtils.round(mob.position.x);
129 while (checkColl(mob.getRect())) mob.position.x+=d;
133 public void update(float delta) {
134 for (Mob mob : gameProc.mobs) {
135 mob.ai();
136 mobPhy(mob);
138 playerPhy(gameProc.player);
140 gameProc.renderer.camera.position.set(
141 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
142 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
144 );