1 package ru
.deadsoftware
.cavedroid
.game
;
3 import com
.badlogic
.gdx
.Gdx
;
4 import com
.badlogic
.gdx
.graphics
.Color
;
5 import com
.badlogic
.gdx
.graphics
.GL20
;
6 import com
.badlogic
.gdx
.graphics
.g2d
.Sprite
;
7 import com
.badlogic
.gdx
.graphics
.g2d
.TextureRegion
;
8 import com
.badlogic
.gdx
.graphics
.glutils
.ShapeRenderer
;
9 import com
.badlogic
.gdx
.math
.Intersector
;
10 import com
.badlogic
.gdx
.math
.MathUtils
;
11 import com
.badlogic
.gdx
.math
.Rectangle
;
12 import com
.badlogic
.gdx
.math
.Vector2
;
13 import com
.badlogic
.gdx
.scenes
.scene2d
.utils
.ScissorStack
;
14 import ru
.deadsoftware
.cavedroid
.MainConfig
;
15 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
16 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
17 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
18 import ru
.deadsoftware
.cavedroid
.game
.model
.block
.Block
;
19 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
20 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
21 import ru
.deadsoftware
.cavedroid
.game
.objects
.Item
;
22 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
23 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
24 import ru
.deadsoftware
.cavedroid
.misc
.Renderer
;
25 import ru
.deadsoftware
.cavedroid
.misc
.utils
.SpriteUtilsKt
;
27 import javax
.annotation
.CheckForNull
;
28 import javax
.annotation
.Nullable
;
29 import javax
.inject
.Inject
;
31 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.*;
34 public class GameRenderer
extends Renderer
{
36 private static final String TAG
= "GameRenderer";
38 private final MainConfig mMainConfig
;
39 private final GameInput mGameInput
;
40 private final GameWorld mGameWorld
;
41 private final MobsController mMobsController
;
42 private final DropController mDropController
;
45 GameRenderer(MainConfig mainConfig
,
48 MobsController mobsController
,
49 DropController dropController
) {
50 super(mainConfig
.getWidth(), mainConfig
.getHeight());
52 mMainConfig
= mainConfig
;
53 mGameInput
= gameInput
;
54 mGameWorld
= gameWorld
;
55 mMobsController
= mobsController
;
56 mDropController
= dropController
;
58 Gdx
.gl
.glClearColor(0f, .6f, .6f, 1f);
61 private float drawX(int x
) {
62 return x
* 16 - getCamX();
65 private float drawY(int y
) {
66 return y
* 16 - getCamY();
69 private void drawWreck(int bl
) {
70 if (mGameInput
.getBlockDamage() > 0) {
71 int index
= 10 * mGameInput
.getBlockDamage() / GameItems
.getBlock(bl
).getHp();
72 String key
= "break_" + index
;
74 if (index
> 10 || index
< 0) {
78 spriter
.draw(textureRegions
.get(key
), mGameInput
.getCurX() * 16 - getCamX(),
79 mGameInput
.getCurY() * 16 - getCamY());
83 private void drawBlock(int x
, int y
, boolean drawBG
) {
85 if ((!mGameWorld
.hasForeAt(x
, y
) || mGameWorld
.getForeMapBlock(x
, y
).isTransparent())
86 && mGameWorld
.hasBackAt(x
, y
)) {
87 mGameWorld
.getBackMapBlock(x
, y
).draw(spriter
, drawX(x
), drawY(y
));
88 if (!mGameWorld
.hasForeAt(x
, y
) && x
== mGameInput
.getCurX() && y
== mGameInput
.getCurY()) {
89 drawWreck(mGameWorld
.getBackMap(mGameInput
.getCurX(), mGameInput
.getCurY()));
93 if (mGameWorld
.hasForeAt(x
, y
) && mGameWorld
.getForeMapBlock(x
, y
).isBackground() == drawBG
) {
94 mGameWorld
.getForeMapBlock(x
, y
).draw(spriter
, drawX(x
), drawY(y
));
95 if (x
== mGameInput
.getCurX() && y
== mGameInput
.getCurY()) {
96 drawWreck(mGameWorld
.getForeMap(mGameInput
.getCurX(), mGameInput
.getCurY()));
101 private void drawWorld(boolean bg
) {
102 int minX
= (int) (getCamX() / 16) - 1;
103 int minY
= (int) (getCamY() / 16) - 1;
104 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
105 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
109 if (maxY
> mGameWorld
.getHeight()) {
110 maxY
= mGameWorld
.getHeight();
112 for (int y
= minY
; y
< maxY
; y
++) {
113 for (int x
= minX
; x
< maxX
; x
++) {
119 Gdx
.gl
.glEnable(GL20
.GL_BLEND
);
120 Gdx
.gl
.glBlendFunc(GL20
.GL_SRC_ALPHA
, GL20
.GL_ONE_MINUS_SRC_ALPHA
);
121 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
122 shaper
.setColor(0f, 0f, 0f, .5f);
123 for (int y
= minY
; y
< maxY
; y
++) {
124 for (int x
= minX
; x
< maxX
; x
++) {
125 if ((!mGameWorld
.hasForeAt(x
, y
) || mGameWorld
.getForeMapBlock(x
, y
).isTransparent())
126 && mGameWorld
.hasBackAt(x
, y
)) {
127 shaper
.rect(drawX(x
), drawY(y
), 16, 16);
132 Gdx
.gl
.glDisable(GL20
.GL_BLEND
);
137 private Rectangle
getShiftedRectRespectfulToViewPort(final Rectangle rect
, final float shift
) {
138 return new Rectangle(rect
.x
+ shift
- getCamX(), rect
.y
- getCamY(), rect
.width
, rect
.height
);
142 private Rectangle
getDrawingRectIfInsideViewport(final Rectangle rectangle
) {
143 final Rectangle viewportRect
= new Rectangle(0, 0, getWidth(), getHeight());
145 final Rectangle notShifted
= getShiftedRectRespectfulToViewPort(rectangle
, 0);
146 if (Intersector
.overlaps(viewportRect
, notShifted
)) {
150 final Rectangle shiftedLeft
= getShiftedRectRespectfulToViewPort(rectangle
, -mGameWorld
.getWidthPx());
151 if (Intersector
.overlaps(viewportRect
, shiftedLeft
)) {
155 final Rectangle shiftedRight
= getShiftedRectRespectfulToViewPort(rectangle
, mGameWorld
.getWidthPx());
156 if (Intersector
.overlaps(viewportRect
, shiftedRight
)) {
163 private void drawMob(Mob mob
, float delta
) {
164 float mobDrawX
= mob
.getX() - getCamX();
165 float mobDrawY
= mob
.getY() - getCamY();
167 if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
+ mGameWorld
.getWidthPx() > 0) {
168 mobDrawX
+= mGameWorld
.getWidthPx();
169 } else if (mobDrawX
> getWidth() && mobDrawX
+ mob
.getWidth() - mGameWorld
.getWidthPx() > 0) {
170 mobDrawX
-= mGameWorld
.getWidthPx();
171 } else if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
> getWidth()) {
175 mob
.draw(spriter
, mobDrawX
, mobDrawY
, delta
);
178 private void drawDrop(Drop drop
) {
179 if (drop
.getId() <= 0) {
183 @CheckForNull final Rectangle drawingRect
= getDrawingRectIfInsideViewport(drop
);
185 if (drawingRect
== null) {
189 final Item item
= GameItems
.getItem(drop
.getId());
190 @CheckForNull final Block block
= GameItems
.getBlock(GameItems
.getItemKey(drop
.getId()));
191 @CheckForNull final Sprite sprite
= item
.isBlock()
195 if (sprite
== null) {
199 sprite
.setPosition(drawingRect
.x
, drawingRect
.y
);
200 sprite
.setSize(drawingRect
.width
, drawingRect
.height
);
201 sprite
.draw(spriter
);
204 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
205 private void drawCreative() {
206 TextureRegion creative
= textureRegions
.get("creative");
207 float x
= getWidth() / 2 - (float) creative
.getRegionWidth() / 2;
208 float y
= getHeight() / 2 - (float) creative
.getRegionHeight() / 2;
209 spriter
.draw(creative
, x
, y
);
210 spriter
.draw(textureRegions
.get("handle"), x
+ 156,
211 y
+ 18 + (mGameInput
.getCreativeScroll() * (72f / GameProc
.MAX_CREATIVE_SCROLL
)));
212 for (int i
= mGameInput
.getCreativeScroll() * 8; i
< mGameInput
.getCreativeScroll() * 8 + 40; i
++) {
213 if (i
> 0 && i
< GameItems
.getItemsSize()) {
214 if (GameItems
.getItem(i
).isBlock()) {
215 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(i
)).getTexture(),
216 x
+ 8 + ((i
- mGameInput
.getCreativeScroll() * 8) % 8) * 18,
217 y
+ 18 + ((i
- mGameInput
.getCreativeScroll() * 8) / 8) * 18);
219 spriter
.draw(GameItems
.getItem(i
).getTexture(),
220 x
+ 8 + ((i
- mGameInput
.getCreativeScroll() * 8) % 8) * 18,
221 y
+ 18 + ((i
- mGameInput
.getCreativeScroll() * 8) / 8) * 18);
225 for (int i
= 0; i
< 9; i
++) {
226 if (mMobsController
.getPlayer().inventory
[i
] > 0) {
227 if (GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).isBlock()) {
228 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(mMobsController
.getPlayer().inventory
[i
])).getTexture(),
229 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
231 spriter
.draw(GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).getTexture(),
232 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
239 private void drawHealth(float x
, float y
) {
240 Player player
= mMobsController
.getPlayer();
242 if (player
.gameMode
== 1) {
246 TextureRegion wholeHeart
= textureRegions
.get("heart_whole");
247 TextureRegion halfHeart
= textureRegions
.get("heart_half");
249 int wholeHearts
= player
.getHealth() / 2;
251 for (int i
= 0; i
< wholeHearts
; i
++) {
252 spriter
.draw(wholeHeart
, x
+ i
* wholeHeart
.getRegionWidth(), y
);
255 if (player
.getHealth() % 2 == 1) {
256 spriter
.draw(halfHeart
, x
+ wholeHearts
* wholeHeart
.getRegionWidth(), y
);
260 private void drawGUI() {
261 TextureRegion cursor
= textureRegions
.get("cursor");
262 TextureRegion hotbar
= textureRegions
.get("hotbar");
263 TextureRegion hotbarSelector
= textureRegions
.get("hotbar_selector");
265 if (mGameWorld
.hasForeAt(mGameInput
.getCurX(), mGameInput
.getCurY()) ||
266 mGameWorld
.hasBackAt(mGameInput
.getCurX(), mGameInput
.getCurY()) ||
267 mGameInput
.getControlMode() == ControlMode
.CURSOR
|| mMainConfig
.isTouch()) {
268 spriter
.draw(cursor
, mGameInput
.getCurX() * 16 - getCamX(), mGameInput
.getCurY() * 16 - getCamY());
271 float hotbarX
= getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2;
272 spriter
.draw(hotbar
, hotbarX
, 0);
273 drawHealth(hotbarX
, hotbar
.getRegionHeight());
275 for (int i
= 0; i
< 9; i
++) {
276 if (mMobsController
.getPlayer().inventory
[i
] > 0) {
277 if (GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).isBlock()) {
278 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(mMobsController
.getPlayer().inventory
[i
])).getTexture(),
279 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
282 spriter
.draw(GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).getTexture(),
283 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
288 spriter
.draw(hotbarSelector
,
289 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 - 1 + 20 * mMobsController
.getPlayer().slot
,
293 private void drawTouchGui() {
294 for (int i
= 0; i
< guiMap
.size
; i
++) {
295 Rectangle touchKey
= guiMap
.getValueAt(i
).getRect();
296 spriter
.draw(textureRegions
.get(guiMap
.getKeyAt(i
)),
297 touchKey
.x
, touchKey
.y
, touchKey
.width
, touchKey
.height
);
299 if (mGameInput
.getControlMode() == ControlMode
.CURSOR
) {
300 spriter
.draw(textureRegions
.get("shade"), 83, getHeight() - 21);
304 private void drawGamePlay(float delta
) {
305 Player player
= mMobsController
.getPlayer();
308 player
.draw(spriter
, player
.getX() - getCamX() - player
.getWidth() / 2, player
.getY() - getCamY(), delta
);
309 mMobsController
.getMobs().forEach( (mob
) -> { drawMob(mob
, delta
); });
310 mDropController
.forEach(this::drawDrop
);
315 private void updateCameraPosition() {
316 Player player
= mMobsController
.getPlayer();
317 setCamPos(player
.getX() + player
.getWidth() / 2 - getWidth() / 2,
318 player
.getY() + player
.getHeight() / 2 - getHeight() / 2);
322 private Color
getMinimapColor(int x
, int y
) {
323 @Nullable Color result
= null;
325 final boolean hasForeMap
= mGameWorld
.hasForeAt(x
, y
);
326 final boolean hasBackMap
= mGameWorld
.hasBackAt(x
, y
);
329 final Block block
= mGameWorld
.getForeMapBlock(x
, y
);
331 if (GameItems
.isWater(block
)) {
333 } else if (GameItems
.isLava(block
)) {
336 result
= Color
.BLACK
;
338 } else if (hasBackMap
) {
339 result
= Color
.DARK_GRAY
;
345 private void drawMiniMap(float miniMapX
, float miniMapY
, float size
) {
346 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
348 shaper
.setColor(Color
.LIGHT_GRAY
);
349 shaper
.rect(miniMapX
, miniMapY
, size
, size
);
351 for (int x
= 0; x
< size
; x
++) {
352 for (int y
= 0; y
< size
; y
++) {
354 final int worldX
= (int) (mMobsController
.getPlayer().getMapX() - size
/ 2 + x
);
355 final int worldY
= (int) (mMobsController
.getPlayer().getUpperMapY() - size
/ 2 + y
);
358 final Color color
= getMinimapColor(worldX
, worldY
);
361 shaper
.setColor(color
);
362 shaper
.rect(miniMapX
+ x
, miniMapY
+ y
, 1, 1);
367 shaper
.setColor(Color
.OLIVE
);
368 shaper
.rect(miniMapX
+ size
/ 2, miniMapY
+ size
/ 2, 1, 2);
373 public void render(float delta
) {
374 int fps
= MathUtils
.ceil(1 / delta
);
375 updateCameraPosition();
376 mGameInput
.moveCursor(this);
378 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
384 switch (mMainConfig
.getGameUiWindow()) {
385 case CREATIVE_INVENTORY
:
388 //TODO draw other ui windows
392 if (mMainConfig
.isTouch()) {
398 if (mMainConfig
.isShowMap()) {
399 drawMiniMap(getWidth() - 64f - 24f, 24f, 64f);
402 if (mMainConfig
.isShowInfo()) {
404 Player player
= mMobsController
.getPlayer();
405 drawString("FPS: " + fps
, 0, 0);
406 drawString("X: " + player
.getMapX(), 0, 10);
407 drawString("Y: " + player
.getUpperMapY(), 0, 20);
408 drawString("CurX: " + mGameInput
.getCurX(), 0, 30);
409 drawString("CurY: " + mGameInput
.getCurY(), 0, 40);
410 drawString("Velocity: " + player
.getVelocity(), 0, 50);
411 drawString("Swim: " + player
.swim
, 0, 60);
412 drawString("Mobs: " + mMobsController
.getMobs().size(), 0, 70);
413 drawString("Drops: " + mDropController
.getSize(), 0, 80);
414 drawString("Block: " + GameItems
.getBlockKey(mGameWorld
.getForeMap(mGameInput
.getCurX(), mGameInput
.getCurY())), 0, 90);
415 drawString("Hand: " + GameItems
.getItemKey(mMobsController
.getPlayer().inventory
[mMobsController
.getPlayer().slot
]), 0, 100);
416 drawString("Game mode: " + player
.gameMode
, 0, 110);
417 drawString("Check swim: " + GameItems
.isFluid(mGameWorld
.getForeMap(player
.getMapX(), player
.getLowerMapY())), 0, 120);