1 package ru
.deadsoftware
.cavedroid
.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
.cavedroid
.CaveGame
;
8 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
9 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
10 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
12 import java
.util
.Iterator
;
14 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
18 static final int PL_SPEED
= 2;
20 private final Vector2 gravity
= new Vector2(0, .9f);
22 private boolean checkJump(Rectangle rect
, int dir
) {
24 int blX
= (int) (rect
.x
+ rect
.width
* dir
- 8 + 16 * dir
);
25 int blY
= (int) (rect
.y
+ rect
.height
- 8);
27 bl
= GP
.world
.getForeMap(blX
/ 16, blY
/ 16);
28 if (checkColl(new Rectangle(blX
, rect
.y
- 18, rect
.width
, rect
.height
))) bl
= 0;
30 return (bl
> 0 && GameItems
.getBlock(bl
).toJump() &&
31 (rect
.y
+ rect
.height
) - GameItems
.getBlock(bl
).getRect(blX
/ 16, blY
/ 16).y
> 8);
34 private boolean checkColl(Rectangle rect
) {
36 int minX
= (int) ((rect
.x
+ rect
.width
/ 2) / 16) - 4;
37 int minY
= (int) ((rect
.y
+ rect
.height
/ 2) / 16) - 4;
38 int maxX
= (int) ((rect
.x
+ rect
.width
/ 2) / 16) + 4;
39 int maxY
= (int) ((rect
.y
+ rect
.height
/ 2) / 16) + 4;
40 if (minY
< 0) minY
= 0;
41 if (maxY
> GP
.world
.getHeight()) maxY
= GP
.world
.getHeight();
42 for (int y
= minY
; y
< maxY
; y
++) {
43 for (int x
= minX
; x
< maxX
; x
++) {
44 bl
= GP
.world
.getForeMap(x
, y
);
45 if (bl
> 0 && GameItems
.getBlock(bl
).hasCollision()) {
46 if (Intersector
.overlaps(rect
, GameItems
.getBlock(bl
).getRect(x
, y
))) {
55 private int getBlock(Rectangle rect
) {
56 return GP
.world
.getForeMap((int) (rect
.x
+ rect
.width
/ 2) / 16, (int) (rect
.y
+ rect
.height
/ 8 * 7) / 16);
59 private void dropPhy(Drop drop
) {
60 if (drop
.closeToPlayer() > 0) {
63 if (drop
.move
.x
>= .5f) drop
.move
.x
-= .5f;
64 else if (drop
.move
.x
<= -.5f) drop
.move
.x
+= .5f;
66 if (drop
.move
.y
< 9) drop
.move
.y
+= gravity
.y
/ 4;
68 drop
.pos
.add(drop
.move
);
69 if (drop
.pos
.x
+ 8 > GP
.world
.getWidthPx()) drop
.pos
.x
-= GP
.world
.getWidthPx();
70 else if (drop
.pos
.x
< 0) drop
.pos
.x
+= GP
.world
.getWidthPx();
71 drop
.pos
.y
= MathUtils
.round(drop
.pos
.y
);
72 while (checkColl(drop
.getRect())) {
78 private void mobXColl(Mob mob
) {
79 if (checkColl(mob
.getRect())) {
80 if (mob
.canJump() && !mob
.isFlyMode()) {
83 if (checkColl(mob
.getRect())) {
84 if (mob
.canJump() && !mob
.isFlyMode()) mob
.getPos().y
+= 8;
86 if (mob
.getMov().x
< 0) d
= 1;
87 else if (mob
.getMov().x
> 0) d
= -1;
88 mob
.getPos().x
= MathUtils
.round(mob
.getX());
89 while (checkColl(mob
.getRect())) mob
.getPos().x
+= d
;
90 if (mob
.canJump()) mob
.changeDir();
93 if (mob
.getX() + mob
.getWidth() / 2 < 0) mob
.getPos().x
+= GP
.world
.getWidthPx();
94 if (mob
.getX() + mob
.getWidth() / 2 > GP
.world
.getWidthPx()) mob
.getPos().x
-= GP
.world
.getWidthPx();
97 private void mobYColl(Mob mob
) {
98 if (checkColl(mob
.getRect())) {
100 if (mob
.getMov().y
< 0) d
= 1;
102 mob
.setCanJump(true);
103 mob
.setFlyMode(false);
105 mob
.getPos().y
= MathUtils
.round(mob
.getY());
106 while (checkColl(mob
.getRect())) mob
.getPos().y
+= d
;
108 if (mob
.getType() > 0) {
109 GP
.world
.setForeMap(mob
.getMapX(), mob
.getMiddleMapY(), mob
.getType());
113 mob
.setCanJump(false);
115 if (mob
.getY() > GP
.world
.getHeightPx()) {
120 private void playerPhy(Player pl
) {
121 pl
.getPos().y
+= pl
.getMov().y
;
123 if (pl
.isDead()) return;
125 if (GameItems
.isFluid(getBlock(pl
.getRect()))) {
126 if (CaveGame
.TOUCH
&& pl
.getMov().x
!= 0 && !pl
.swim
&& !pl
.isFlyMode()) pl
.swim
= true;
128 if (!pl
.isFlyMode() && pl
.getMov().y
< 4.5f) pl
.getMov().add(gravity
.x
/ 4, gravity
.y
/ 4);
129 if (!pl
.isFlyMode() && pl
.getMov().y
> 4.5f) pl
.getMov().add(0, -1f);
131 pl
.getMov().add(0, -.5f);
132 if (pl
.getMov().y
< -3) pl
.getMov().y
= -3;
135 if (!pl
.isFlyMode() && pl
.getMov().y
< 18) pl
.getMov().add(gravity
);
138 pl
.getPos().x
+= pl
.getMov().x
* (pl
.isFlyMode() ?
1.5f : 1) * (GameItems
.isFluid(getBlock(pl
.getRect())) && !pl
.isFlyMode() ?
.8f : 1);
141 if (CaveGame
.TOUCH
&& checkJump(pl
.getRect(), pl
.getDirection()) && !pl
.isFlyMode() && pl
.canJump() && pl
.getMov().x
!= 0) {
142 pl
.getMov().add(0, -8);
143 pl
.setCanJump(false);
147 private void mobPhy(Mob mob
) {
148 mob
.getPos().y
+= mob
.getMov().y
;
150 if (mob
.isDead()) return;
152 if (mob
.getType() == 0 && GameItems
.isFluid(getBlock(mob
.getRect()))) {
153 if (mob
.getMov().y
> 9) mob
.getMov().add(0, -.9f);
154 mob
.getMov().add(0, -.5f);
155 if (mob
.getMov().y
< -3) mob
.getMov().y
= -3;
156 } else if (!mob
.isFlyMode() && mob
.getMov().y
< 18) mob
.getMov().add(gravity
);
158 mob
.getPos().x
+= mob
.getMov().x
;
161 if (checkJump(mob
.getRect(), mob
.getDirection()) && mob
.canJump() && mob
.getMov().x
!= 0) {
162 mob
.getMov().add(0, -8);
163 mob
.setCanJump(false);
168 for (Iterator
<Drop
> it
= GP
.drops
.iterator(); it
.hasNext(); ) {
169 Drop drop
= it
.next();
171 if (Intersector
.overlaps(drop
.getRect(), GP
.player
.getRect())) drop
.pickUpDrop(GP
.player
);
172 if (drop
.pickedUp
) it
.remove();
175 for (Iterator
<Mob
> it
= GP
.mobs
.iterator(); it
.hasNext(); ) {
179 if (mob
.isDead()) it
.remove();
182 playerPhy(GP
.player
);
183 if (GP
.player
.isDead()) GP
.player
.respawn();
185 GP
.renderer
.setCamPos(
186 GP
.player
.getPos().x
+ GP
.player
.getWidth() / 2 - GP
.renderer
.getWidth() / 2,
187 GP
.player
.getPos().y
+ GP
.player
.getHeight() / 2 - GP
.renderer
.getHeight() / 2);