DEADSOFTWARE

Ciclic worlds
[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.Items;
9 import ru.deadsoftware.cavecraft.game.mobs.Mob;
10 import ru.deadsoftware.cavecraft.game.objects.Player;
12 public class GamePhysics {
14 public static final int PL_SPEED = 2;
16 private GameProc gameProc;
18 private Vector2 gravity;
20 public GamePhysics(GameProc gameProc) {
21 this.gameProc = gameProc;
22 gravity = new Vector2(0,.9f);
23 }
25 private boolean checkJump(Rectangle rect, int dir) {
26 int bl = 0;
27 switch (dir) {
28 case 0:
29 bl = gameProc.world.getForeMap(
30 (int)((rect.x+(rect.width/2))/16) - 1,
31 (int)(rect.y/16)+1);
32 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16))>0) bl=0;
33 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)-1,(int)(rect.y/16)-1)>0) bl=0;
34 break;
35 case 1:
36 bl = gameProc.world.getForeMap(
37 (int)((rect.x+(rect.width/2))/16) + 1,
38 (int)(rect.y/16)+1);
39 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16))>0) bl=0;
40 if (gameProc.world.getForeMap((int)((rect.x+(rect.width/2))/16)+1,(int)(rect.y/16)-1)>0) bl=0;
41 break;
42 default:
43 bl=0;
44 }
45 return (bl>0 && Items.BLOCKS.getValueAt(bl).collision);
46 }
48 private boolean checkColl(Rectangle rect) {
49 int bl;
50 int minX = (int) ((rect.x+rect.width/2)/16)-4;
51 int minY = (int) ((rect.y+rect.height/2)/16)-4;
52 int maxX = (int) ((rect.x+rect.width/2)/16)+4;
53 int maxY = (int) ((rect.y+rect.height/2)/16)+4;
54 if (minY<0) minY=0;
55 if (maxY>gameProc.world.getHeight()) maxY = gameProc.world.getHeight();
56 for (int y=minY; y<maxY; y++) {
57 for (int x=minX; x<maxX; x++) {
58 bl = gameProc.world.getForeMap(x,y);
59 if (bl>0 && Items.BLOCKS.getValueAt(bl).collision){
60 if (Intersector.overlaps(rect, Items.BLOCKS.getValueAt(bl).getRect(x,y))){
61 return true;
62 }
63 }
64 }
65 }
66 return false;
67 }
69 private void playerPhy(Player pl) {
70 pl.position.add(pl.moveY);
71 if (checkColl(pl.getRect())) {
72 int d = -1;
73 if (pl.moveY.y<0) d=1; else if (pl.moveY.y>0) d=-1;
74 if (d==-1) {
75 pl.flyMode = false;
76 pl.canJump = true;
77 }
78 pl.position.y = MathUtils.round(pl.position.y);
79 while (checkColl(pl.getRect())) pl.position.y+=d;
80 pl.moveY.setZero();
81 } else {
82 pl.canJump = false;
83 }
84 if (!pl.flyMode && pl.moveY.y<18) pl.moveY.add(gravity);
85 pl.position.add(pl.moveX);
86 if (checkColl(pl.getRect())) {
87 if (pl.canJump && !pl.flyMode) pl.position.y-=8;
88 if (checkColl(pl.getRect())) {
89 if (pl.canJump && !pl.flyMode) pl.position.y+=8;
90 int d = 0;
91 if (pl.moveX.x < 0) d = 1;
92 else if (pl.moveX.x > 0) d = -1;
93 pl.position.x = MathUtils.round(pl.position.x);
94 while (checkColl(pl.getRect())) pl.position.x += d;
95 }
96 }
97 if (pl.position.y > gameProc.world.getHeight()*16) {
98 pl.position = gameProc.world.getSpawnPoint().cpy();
99 }
100 if (CaveGame.TOUCH && checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
101 pl.moveY.add(0, -8);
102 pl.canJump = false;
106 private void mobPhy(Mob mob) {
107 mob.position.add(mob.moveY);
108 if (checkColl(mob.getRect())) {
109 mob.canJump = true;
110 int d = -1;
111 if (mob.moveY.y<0) d=1; else if (mob.moveY.y>0) d=-1;
112 mob.position.y = MathUtils.round(mob.position.y);
113 while (checkColl(mob.getRect())) mob.position.y+=d;
114 mob.moveY.setZero();
115 } else {
116 mob.canJump = false;
118 mob.moveY.add(gravity);
119 mob.position.add(mob.moveX);
120 if (checkColl(mob.getRect())) {
121 int d = 0;
122 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
123 mob.position.x = MathUtils.round(mob.position.x);
124 while (checkColl(mob.getRect())) mob.position.x+=d;
128 public void update(float delta) {
129 for (Mob mob : gameProc.mobs) {
130 mob.ai();
131 mobPhy(mob);
133 playerPhy(gameProc.player);
135 gameProc.renderer.camera.position.set(
136 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
137 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
139 );