f8843f2c6851ba086a4c3a8e3fbb6aeccb61e529
1 package ru
.deadsoftware
.cavecraft
.game
;
3 import com
.badlogic
.gdx
.math
.MathUtils
;
4 import com
.badlogic
.gdx
.math
.Rectangle
;
5 import com
.badlogic
.gdx
.math
.Vector2
;
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,1);
22 private boolean checkColl(Rectangle rect
) {
23 int[] bl
= new int [6];
24 bl
[0] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)rect
.y
/16));
25 bl
[1] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)rect
.y
/16));
26 bl
[2] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
/2)/16));
27 bl
[3] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+rect
.height
/2)/16));
28 bl
[4] = gameProc
.world
.getForeMap(((int)(rect
.x
)/16), ((int)(rect
.y
+rect
.height
-1)/16));
29 bl
[5] = gameProc
.world
.getForeMap(((int)(rect
.x
+rect
.width
-1)/16), ((int)(rect
.y
+(rect
.height
-1))/16));
30 for (int b
: bl
) if (b
>0) {
36 private void playerPhy(Player pl
) {
37 pl
.position
.add(pl
.moveY
);
38 if (checkColl(pl
.getRect())) {
42 if (pl
.moveY
.y
<0) d
=1; else if (pl
.moveY
.y
>0) d
=-1;
43 pl
.position
.y
= MathUtils
.round(pl
.position
.y
);
44 while (checkColl(pl
.getRect())) pl
.position
.y
+=d
;
49 if (!pl
.flyMode
) pl
.moveY
.add(gravity
);
50 pl
.position
.add(pl
.moveX
);
51 if (pl
.position
.x
<0 ||
52 pl
.position
.x
+pl
.width
>gameProc
.world
.getWidth()*16)
53 pl
.position
.sub(pl
.moveX
);
54 if (checkColl(pl
.getRect())) {
56 if (pl
.moveX
.x
<0) d
=1; else if (pl
.moveX
.x
>0) d
=-1;
57 pl
.position
.x
= MathUtils
.round(pl
.position
.x
);
58 while (checkColl(pl
.getRect())) pl
.position
.x
+=d
;
62 gameProc
.renderer
.camTargetPos
.x
= pl
.position
.x
-gameProc
.renderer
.camera
.viewportWidth
+100;
65 gameProc
.renderer
.camTargetPos
.x
= pl
.position
.x
-100;
68 if (gameProc
.renderer
.camTargetPos
.x
< 0) gameProc
.renderer
.camTargetPos
.x
= 0;
69 if (gameProc
.renderer
.camTargetPos
.x
+ gameProc
.renderer
.camera
.viewportWidth
>
70 gameProc
.world
.getWidth()*16)
71 gameProc
.renderer
.camTargetPos
.x
= gameProc
.world
.getWidth()*16-gameProc
.renderer
.camera
.viewportWidth
;
74 private void mobPhy(Mob mob
) {
75 mob
.position
.add(mob
.moveY
);
76 if (checkColl(mob
.getRect())) {
79 if (mob
.moveY
.y
<0) d
=1; else if (mob
.moveY
.y
>0) d
=-1;
80 mob
.position
.y
= MathUtils
.round(mob
.position
.y
);
81 while (checkColl(mob
.getRect())) mob
.position
.y
+=d
;
86 mob
.moveY
.add(gravity
);
87 mob
.position
.add(mob
.moveX
);
88 if (mob
.position
.x
<32 ||
89 mob
.position
.x
+mob
.width
>gameProc
.world
.getWidth()*16-32)
90 mob
.position
.sub(mob
.moveX
);
91 if (checkColl(mob
.getRect())) {
93 if (mob
.moveX
.x
<0) d
=1; else if (mob
.moveX
.x
>0) d
=-1;
94 mob
.position
.x
= MathUtils
.round(mob
.position
.x
);
95 while (checkColl(mob
.getRect())) mob
.position
.x
+=d
;
99 public void update(float delta
) {
101 for (Mob mob
: gameProc
.mobs
) {
105 playerPhy(gameProc
.player
);
107 if (gameProc
.renderer
.camera
.position
.x
- gameProc
.renderer
.camTargetPos
.x
<= 8 &&
108 gameProc
.renderer
.camera
.position
.x
- gameProc
.renderer
.camTargetPos
.x
>= -8) {
109 gameProc
.renderer
.camera
.position
.x
= gameProc
.renderer
.camTargetPos
.x
;
111 if (gameProc
.renderer
.camera
.position
.x
> gameProc
.renderer
.camTargetPos
.x
) {
112 gameProc
.renderer
.camera
.position
.sub(16,0,0);
114 if (gameProc
.renderer
.camera
.position
.x
< gameProc
.renderer
.camTargetPos
.x
) {
115 gameProc
.renderer
.camera
.position
.add(16,0,0);
117 gameProc
.renderer
.camera
.position
.y
= gameProc
.player
.position
.y
+gameProc
.player
.height
/2
118 -gameProc
.renderer
.camera
.viewportHeight
/2;