DEADSOFTWARE

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