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
.TextureRegion
;
7 import com
.badlogic
.gdx
.graphics
.glutils
.ShapeRenderer
;
8 import com
.badlogic
.gdx
.math
.Rectangle
;
9 import com
.badlogic
.gdx
.math
.Vector2
;
10 import com
.badlogic
.gdx
.scenes
.scene2d
.utils
.ScissorStack
;
11 import ru
.deadsoftware
.cavedroid
.MainConfig
;
12 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
13 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
14 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
15 import ru
.deadsoftware
.cavedroid
.game
.objects
.Block
;
16 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
17 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
18 import ru
.deadsoftware
.cavedroid
.game
.world
.GameWorld
;
19 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
20 import ru
.deadsoftware
.cavedroid
.misc
.Renderer
;
22 import javax
.annotation
.Nullable
;
23 import javax
.inject
.Inject
;
25 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.guiMap
;
26 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.textureRegions
;
29 public class GameRenderer
extends Renderer
{
31 private final MainConfig mMainConfig
;
32 private final GameInput mGameInput
;
33 private final GameWorld mGameWorld
;
34 private final MobsController mMobsController
;
35 private final DropController mDropController
;
38 GameRenderer(MainConfig mainConfig
,
41 MobsController mobsController
,
42 DropController dropController
) {
43 super(mainConfig
.getWidth(), mainConfig
.getHeight());
45 mMainConfig
= mainConfig
;
46 mGameInput
= gameInput
;
47 mGameWorld
= gameWorld
;
48 mMobsController
= mobsController
;
49 mDropController
= dropController
;
51 Gdx
.gl
.glClearColor(0f, .6f, .6f, 1f);
54 private float drawX(int x
) {
55 return x
* 16 - getCamX();
58 private float drawY(int y
) {
59 return y
* 16 - getCamY();
62 private void drawWreck(int bl
) {
63 if (mGameInput
.getBlockDamage() > 0) {
64 int index
= 10 * mGameInput
.getBlockDamage() / GameItems
.getBlock(bl
).getHp();
65 String key
= "break_" + index
;
66 spriter
.draw(textureRegions
.get(key
), mGameInput
.getCurX() * 16 - getCamX(),
67 mGameInput
.getCurY() * 16 - getCamY());
71 private void drawBlock(int x
, int y
, boolean drawBG
) {
73 if ((!mGameWorld
.hasForeAt(x
, y
) || mGameWorld
.getForeMapBlock(x
, y
).isTransparent())
74 && mGameWorld
.hasBackAt(x
, y
)) {
75 mGameWorld
.getBackMapBlock(x
, y
).draw(spriter
, drawX(x
), drawY(y
));
76 if (!mGameWorld
.hasForeAt(x
, y
) && x
== mGameInput
.getCurX() && y
== mGameInput
.getCurY()) {
77 drawWreck(mGameWorld
.getBackMap(mGameInput
.getCurX(), mGameInput
.getCurY()));
81 if (mGameWorld
.hasForeAt(x
, y
) && mGameWorld
.getForeMapBlock(x
, y
).isBackground() == drawBG
) {
82 mGameWorld
.getForeMapBlock(x
, y
).draw(spriter
, drawX(x
), drawY(y
));
83 if (x
== mGameInput
.getCurX() && y
== mGameInput
.getCurY()) {
84 drawWreck(mGameWorld
.getForeMap(mGameInput
.getCurX(), mGameInput
.getCurY()));
89 private void drawWorld(boolean bg
) {
90 int minX
= (int) (getCamX() / 16) - 1;
91 int minY
= (int) (getCamY() / 16) - 1;
92 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
93 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
97 if (maxY
> mGameWorld
.getHeight()) {
98 maxY
= mGameWorld
.getHeight();
100 for (int y
= minY
; y
< maxY
; y
++) {
101 for (int x
= minX
; x
< maxX
; x
++) {
107 Gdx
.gl
.glEnable(GL20
.GL_BLEND
);
108 Gdx
.gl
.glBlendFunc(GL20
.GL_SRC_ALPHA
, GL20
.GL_ONE_MINUS_SRC_ALPHA
);
109 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
110 shaper
.setColor(0f, 0f, 0f, .5f);
111 for (int y
= minY
; y
< maxY
; y
++) {
112 for (int x
= minX
; x
< maxX
; x
++) {
113 if ((!mGameWorld
.hasForeAt(x
, y
) || mGameWorld
.getForeMapBlock(x
, y
).isTransparent())
114 && mGameWorld
.hasBackAt(x
, y
)) {
115 shaper
.rect(drawX(x
), drawY(y
), 16, 16);
120 Gdx
.gl
.glDisable(GL20
.GL_BLEND
);
125 private void drawMob(Mob mob
, float delta
) {
126 float mobDrawX
= mob
.getX() - getCamX();
127 float mobDrawY
= mob
.getY() - getCamY();
129 if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
+ mGameWorld
.getWidthPx() > 0) {
130 mobDrawX
+= mGameWorld
.getWidthPx();
131 } else if (mobDrawX
> getWidth() && mobDrawX
+ mob
.getWidth() - mGameWorld
.getWidthPx() > 0) {
132 mobDrawX
-= mGameWorld
.getWidthPx();
133 } else if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
> getWidth()) {
137 mob
.draw(spriter
, mobDrawX
, mobDrawY
, delta
);
140 private void drawDrop(Drop drop
) {
143 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
144 private void drawCreative() {
145 TextureRegion creative
= textureRegions
.get("creative");
146 float x
= getWidth() / 2 - (float) creative
.getRegionWidth() / 2;
147 float y
= getHeight() / 2 - (float) creative
.getRegionHeight() / 2;
148 spriter
.draw(creative
, x
, y
);
149 spriter
.draw(textureRegions
.get("handle"), x
+ 156,
150 y
+ 18 + (mGameInput
.getCreativeScroll() * (72f / GameProc
.MAX_CREATIVE_SCROLL
)));
151 for (int i
= mGameInput
.getCreativeScroll() * 8; i
< mGameInput
.getCreativeScroll() * 8 + 40; i
++) {
152 if (i
> 0 && i
< GameItems
.getItemsSize()) {
153 if (GameItems
.getItem(i
).isBlock()) {
154 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(i
)).getTexture(),
155 x
+ 8 + ((i
- mGameInput
.getCreativeScroll() * 8) % 8) * 18,
156 y
+ 18 + ((i
- mGameInput
.getCreativeScroll() * 8) / 8) * 18);
158 spriter
.draw(GameItems
.getItem(i
).getTexture(),
159 x
+ 8 + ((i
- mGameInput
.getCreativeScroll() * 8) % 8) * 18,
160 y
+ 18 + ((i
- mGameInput
.getCreativeScroll() * 8) / 8) * 18);
164 for (int i
= 0; i
< 9; i
++) {
165 if (mMobsController
.getPlayer().inventory
[i
] > 0) {
166 if (GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).isBlock()) {
167 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(mMobsController
.getPlayer().inventory
[i
])).getTexture(),
168 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
170 spriter
.draw(GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).getTexture(),
171 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
178 private void drawGUI() {
179 TextureRegion cursor
= textureRegions
.get("cursor");
180 TextureRegion hotbar
= textureRegions
.get("hotbar");
181 TextureRegion hotbarSelector
= textureRegions
.get("hotbar_selector");
183 if (mGameWorld
.hasForeAt(mGameInput
.getCurX(), mGameInput
.getCurY()) ||
184 mGameWorld
.hasBackAt(mGameInput
.getCurX(), mGameInput
.getCurY()) ||
185 mGameInput
.getControlMode() == ControlMode
.CURSOR
|| mMainConfig
.isTouch()) {
186 spriter
.draw(cursor
, mGameInput
.getCurX() * 16 - getCamX(), mGameInput
.getCurY() * 16 - getCamY());
188 spriter
.draw(hotbar
, getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2, 0);
189 for (int i
= 0; i
< 9; i
++) {
190 if (mMobsController
.getPlayer().inventory
[i
] > 0) {
191 if (GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).isBlock()) {
192 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(mMobsController
.getPlayer().inventory
[i
])).getTexture(),
193 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
196 spriter
.draw(GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).getTexture(),
197 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
202 spriter
.draw(hotbarSelector
,
203 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 - 1 + 20 * mMobsController
.getPlayer().slot
,
207 private void drawTouchGui() {
208 for (int i
= 0; i
< guiMap
.size
; i
++) {
209 Rectangle touchKey
= guiMap
.getValueAt(i
).getRect();
210 spriter
.draw(textureRegions
.get(guiMap
.getKeyAt(i
)),
211 touchKey
.x
, touchKey
.y
, touchKey
.width
, touchKey
.height
);
213 if (mGameInput
.getControlMode() == ControlMode
.CURSOR
) {
214 spriter
.draw(textureRegions
.get("shade"), 83, getHeight() - 21);
218 private void drawGamePlay(float delta
) {
219 Player player
= mMobsController
.getPlayer();
222 player
.draw(spriter
, player
.getX() - getCamX() - player
.getWidth() / 2, player
.getY() - getCamY(), delta
);
223 mMobsController
.forEach( (mob
) -> { drawMob(mob
, delta
); });
224 mDropController
.forEach(this::drawDrop
);
229 private void updateCameraPosition() {
230 Player player
= mMobsController
.getPlayer();
231 setCamPos(player
.getX() + player
.getWidth() / 2 - getWidth() / 2,
232 player
.getY() + player
.getHeight() / 2 - getHeight() / 2);
236 private Color
getMinimapColor(int x
, int y
) {
237 @Nullable Color result
= null;
239 final boolean hasForeMap
= mGameWorld
.hasForeAt(x
, y
);
240 final boolean hasBackMap
= mGameWorld
.hasBackAt(x
, y
);
243 final Block block
= mGameWorld
.getForeMapBlock(x
, y
);
245 if (GameItems
.isWater(block
)) {
247 } else if (GameItems
.isLava(block
)) {
250 result
= Color
.BLACK
;
252 } else if (hasBackMap
) {
253 result
= Color
.DARK_GRAY
;
259 private void drawMiniMap(float miniMapX
, float miniMapY
, float size
) {
260 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
262 shaper
.setColor(Color
.LIGHT_GRAY
);
263 shaper
.rect(miniMapX
, miniMapY
, size
, size
);
265 for (int x
= 0; x
< size
; x
++) {
266 for (int y
= 0; y
< size
; y
++) {
268 final int worldX
= (int) (mMobsController
.getPlayer().getMapX() - size
/ 2 + x
);
269 final int worldY
= (int) (mMobsController
.getPlayer().getUpperMapY() - size
/ 2 + y
);
272 final Color color
= getMinimapColor(worldX
, worldY
);
275 shaper
.setColor(color
);
276 shaper
.rect(miniMapX
+ x
, miniMapY
+ y
, 1, 1);
281 shaper
.setColor(Color
.OLIVE
);
282 shaper
.rect(miniMapX
+ size
/ 2, miniMapY
+ size
/ 2, 1, 2);
287 public void render(float delta
) {
288 int fps
= (int) (1 / delta
);
289 updateCameraPosition();
290 mGameInput
.moveCursor(this);
292 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
298 switch (mMainConfig
.getGameUiWindow()) {
299 case CREATIVE_INVENTORY
:
302 //TODO draw other ui windows
306 if (mMainConfig
.isTouch()) {
312 if (mMainConfig
.isShowMap()) {
313 drawMiniMap(getWidth() - 64f - 24f, 24f, 64f);
316 if (mMainConfig
.isShowInfo()) {
318 Player player
= mMobsController
.getPlayer();
319 drawString("FPS: " + fps
, 0, 0);
320 drawString("X: " + player
.getMapX(), 0, 10);
321 drawString("Y: " + player
.getUpperMapY(), 0, 20);
322 drawString("CurX: " + mGameInput
.getCurX(), 0, 30);
323 drawString("CurY: " + mGameInput
.getCurY(), 0, 40);
324 drawString("Velocity: " + player
.getVelocity(), 0, 50);
325 drawString("Swim: " + player
.swim
, 0, 60);
326 drawString("Mobs: " + mMobsController
.getSize(), 0, 70);
327 drawString("Drops: " + mDropController
.getSize(), 0, 80);
328 drawString("Block: " + GameItems
.getBlockKey(mGameWorld
.getForeMap(mGameInput
.getCurX(), mGameInput
.getCurY())), 0, 90);
329 drawString("Hand: " + GameItems
.getItemKey(mMobsController
.getPlayer().inventory
[mMobsController
.getPlayer().slot
]), 0, 100);
330 drawString("Game mode: " + player
.gameMode
, 0, 110);
331 drawString("Check swim: " + GameItems
.isFluid(mGameWorld
.getForeMap(player
.getMapX(), player
.getLowerMapY())), 0, 120);