e461a9399381d38c59b31dfcf518b6cba66176da
1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.math
.*;
4 import ru
.deadsoftware
.cavecraft
.Items
;
5 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
6 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
8 public class GamePhysics
{
10 public static final int PL_SPEED
= 2;
12 private GameProc gameProc
;
14 private Vector2 gravity
;
16 public GamePhysics(GameProc gameProc
) {
17 this.gameProc
= gameProc
;
18 gravity
= new Vector2(0,1);
21 private boolean checkJump(Rectangle rect
, int dir
) {
25 if ((int)((rect
.x
+(rect
.width
/2))/16) - 1>=0)
26 bl
= gameProc
.world
.getForeMap(
27 (int)((rect
.x
+(rect
.width
/2))/16) - 1,
31 if ((int)((rect
.x
+(rect
.width
/2))/16) + 1<gameProc
.world
.getWidth())
32 bl
= gameProc
.world
.getForeMap(
33 (int)((rect
.x
+(rect
.width
/2))/16) + 1,
39 return (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
);
42 private boolean checkColl(Rectangle rect
) {
44 int minX
= (int) ((rect
.x
+rect
.width
/2)/16)-4;
45 int minY
= (int) ((rect
.y
+rect
.height
/2)/16)-4;
46 int maxX
= (int) ((rect
.x
+rect
.width
/2)/16)+4;
47 int maxY
= (int) ((rect
.y
+rect
.height
/2)/16)+4;
50 if (maxX
>gameProc
.world
.getWidth()) maxX
= gameProc
.world
.getWidth();
51 if (maxY
>gameProc
.world
.getHeight()) maxY
= gameProc
.world
.getHeight();
52 for (int y
=minY
; y
<maxY
; y
++) {
53 for (int x
=minX
; x
<maxX
; x
++) {
54 bl
= gameProc
.world
.getForeMap(x
,y
);
55 if (bl
>0 && Items
.BLOCKS
.getValueAt(bl
).collision
){
56 if (Intersector
.overlaps(rect
, Items
.BLOCKS
.getValueAt(bl
).getRect(x
,y
))){
65 private void playerPhy(Player pl
) {
66 pl
.position
.add(pl
.moveY
);
67 if (checkColl(pl
.getRect())) {
71 if (pl
.moveY
.y
<0) d
=1; else if (pl
.moveY
.y
>0) d
=-1;
72 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
73 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
78 if (!pl
.flyMode
) pl
.moveY
.add(gravity
);
79 pl
.position
.add(pl
.moveX
);
80 if (pl
.position
.x
<0 ||
81 pl
.position
.x
+pl
.texWidth
>=gameProc
.world
.getWidth()*16)
82 pl
.position
.sub(pl
.moveX
);
83 if (checkColl(pl
.getRect())) {
84 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
-=8;
85 if (checkColl(pl
.getRect())) {
86 if (pl
.canJump
&& !pl
.flyMode
) pl
.position
.y
+=8;
88 if (pl
.moveX
.x
< 0) d
= 1;
89 else if (pl
.moveX
.x
> 0) d
= -1;
90 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
91 while (checkColl(pl
.getRect())) pl
.position
.x
+= d
;
94 if (pl
.position
.y
> gameProc
.world
.getHeight()*16) {
95 pl
.position
= gameProc
.world
.getSpawnPoint().cpy();
97 /*if (checkJump(pl.getRect(), pl.dir) && !pl.flyMode && pl.canJump && !pl.moveX.equals(Vector2.Zero)) {
103 private void mobPhy(Mob mob
) {
104 mob
.position
.add(mob
.moveY
);
105 if (checkColl(mob
.getRect())) {
108 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
109 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
110 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
115 mob
.moveY
.add(gravity
);
116 mob
.position
.add(mob
.moveX
);
117 if (mob
.position
.x
<32 ||
118 mob
.position
.x
+mob
.width
>gameProc
.world
.getWidth()*16-32)
119 mob
.position
.sub(mob
.moveX
);
120 if (checkColl(mob
.getRect())) {
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
) {
130 for (Mob mob
: gameProc
.mobs
) {
134 playerPhy(gameProc
.player
);
136 gameProc
.renderer
.camera
.position
.set(
137 gameProc
.player
.position
.x
+gameProc
.player
.texWidth
/2 - gameProc
.renderer
.camera
.viewportWidth
/2,
138 gameProc
.player
.position
.y
+gameProc
.player
.height
/2-gameProc
.renderer
.camera
.viewportHeight
/2,