DEADSOFTWARE

TP player when crossing world's edge
[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.x+pl.texWidth/2<0) pl.position.x+=gameProc.world.getWidth()*16;
98 if (pl.position.x+pl.texWidth/2>gameProc.world.getWidth()*16) pl.position.x-=gameProc.world.getWidth()*16;
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+mob.width/2<0) mob.position.x+=gameProc.world.getWidth()*16;
123 if (mob.position.x+mob.width/2>gameProc.world.getWidth()*16) mob.position.x-=gameProc.world.getWidth()*16;
124 if (checkColl(mob.getRect())) {
125 int d = 0;
126 if (mob.moveX.x<0) d=1; else if (mob.moveX.x>0) d=-1;
127 mob.position.x = MathUtils.round(mob.position.x);
128 while (checkColl(mob.getRect())) mob.position.x+=d;
132 public void update(float delta) {
133 for (Mob mob : gameProc.mobs) {
134 mob.ai();
135 mobPhy(mob);
137 playerPhy(gameProc.player);
139 gameProc.renderer.camera.position.set(
140 gameProc.player.position.x+gameProc.player.texWidth/2 - gameProc.renderer.camera.viewportWidth/2,
141 gameProc.player.position.y+gameProc.player.height/2-gameProc.renderer.camera.viewportHeight/2,
143 );