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 ru
.deadsoftware
.cavedroid
.CaveGame
;
10 import ru
.deadsoftware
.cavedroid
.GameScreen
;
11 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
12 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
13 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
14 import ru
.deadsoftware
.cavedroid
.misc
.Renderer
;
16 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
17 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.guiMap
;
18 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.textureRegions
;
20 public class GameRenderer
extends Renderer
{
22 GameRenderer(float width
, float height
) {
24 Gdx
.gl
.glClearColor(0f, .6f, .6f, 1f);
27 private float drawX(int x
) {
28 return x
* 16 - getCamX();
31 private float drawY(int y
) {
32 return y
* 16 - getCamY();
35 private void drawWreck(int bl
) {
36 if (GP
.input
.getBlockDamage() > 0) {
37 int index
= 10 * GP
.input
.getBlockDamage() / GameItems
.getBlock(bl
).getHp();
38 String key
= "break_" + index
;
39 spriter
.draw(textureRegions
.get(key
), GP
.input
.getCurX() * 16 - getCamX(),
40 GP
.input
.getCurY() * 16 - getCamY());
44 private void drawBlock(int x
, int y
, boolean drawBG
) {
46 if ((!GP
.world
.hasForeAt(x
, y
) || GP
.world
.getForeMapBlock(x
, y
).isTransparent())
47 && GP
.world
.hasBackAt(x
, y
)) {
48 spriter
.draw(GP
.world
.getBackMapBlock(x
, y
).getTex(), drawX(x
), drawY(y
));
49 if (!GP
.world
.hasForeAt(x
, y
) && x
== GP
.input
.getCurX() && y
== GP
.input
.getCurY())
50 drawWreck(GP
.world
.getBackMap(GP
.input
.getCurX(), GP
.input
.getCurY()));
53 if (GP
.world
.hasForeAt(x
, y
) && GP
.world
.getForeMapBlock(x
, y
).isBackground() == drawBG
) {
54 spriter
.draw(GP
.world
.getForeMapBlock(x
, y
).getTex(), drawX(x
), drawY(y
));
55 if (x
== GP
.input
.getCurX() && y
== GP
.input
.getCurY())
56 drawWreck(GP
.world
.getForeMap(GP
.input
.getCurX(), GP
.input
.getCurY()));
60 private void drawWorld(boolean bg
) {
61 int minX
= (int) (getCamX() / 16) - 1;
62 int minY
= (int) (getCamY() / 16) - 1;
63 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
64 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
65 if (minY
< 0) minY
= 0;
66 if (maxY
> GP
.world
.getHeight()) maxY
= GP
.world
.getHeight();
67 for (int y
= minY
; y
< maxY
; y
++) {
68 for (int x
= minX
; x
< maxX
; x
++) {
74 Gdx
.gl
.glEnable(GL20
.GL_BLEND
);
75 Gdx
.gl
.glBlendFunc(GL20
.GL_SRC_ALPHA
, GL20
.GL_ONE_MINUS_SRC_ALPHA
);
76 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
77 shaper
.setColor(0f, 0f, 0f, .5f);
78 for (int y
= minY
; y
< maxY
; y
++) {
79 for (int x
= minX
; x
< maxX
; x
++) {
80 if ((!GP
.world
.hasForeAt(x
, y
) || GP
.world
.getForeMapBlock(x
, y
).isTransparent())
81 && GP
.world
.hasBackAt(x
, y
))
82 shaper
.rect(drawX(x
), drawY(y
), 16, 16);
86 Gdx
.gl
.glDisable(GL20
.GL_BLEND
);
91 private void drawMob(Mob mob
) {
92 float mobDrawX
= mob
.getX() - getCamX();
93 float mobDrawY
= mob
.getY() - getCamY();
95 if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
+ GP
.world
.getWidthPx() > 0) {
96 mobDrawX
+= GP
.world
.getWidthPx();
97 } else if (mobDrawX
> getWidth() && mobDrawX
+ mob
.getWidth() - GP
.world
.getWidthPx() > 0) {
98 mobDrawX
-= GP
.world
.getWidthPx();
99 } else if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
> getWidth()) {
103 mob
.draw(spriter
, mobDrawX
, mobDrawY
);
106 private void drawDrop(Drop drop
) {
109 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
110 private void drawCreative() {
111 TextureRegion creative
= textureRegions
.get("creative");
112 float x
= getWidth() / 2 - (float) creative
.getRegionWidth() / 2;
113 float y
= getHeight() / 2 - (float) creative
.getRegionHeight() / 2;
114 spriter
.draw(creative
, x
, y
);
115 spriter
.draw(textureRegions
.get("handle"), x
+ 156,
116 y
+ 18 + (GP
.input
.getCreativeScroll() * (72f / GameProc
.MAX_CREATIVE_SCROLL
)));
117 for (int i
= GP
.input
.getCreativeScroll() * 8; i
< GP
.input
.getCreativeScroll() * 8 + 40; i
++) {
118 if (i
> 0 && i
< GameItems
.getItemsSize())
119 if (GameItems
.getItem(i
).isBlock()) {
120 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(i
)).getTex(),
121 x
+ 8 + ((i
- GP
.input
.getCreativeScroll() * 8) % 8) * 18,
122 y
+ 18 + ((i
- GP
.input
.getCreativeScroll() * 8) / 8) * 18);
124 spriter
.draw(GameItems
.getItem(i
).getTex(),
125 x
+ 8 + ((i
- GP
.input
.getCreativeScroll() * 8) % 8) * 18,
126 y
+ 18 + ((i
- GP
.input
.getCreativeScroll() * 8) / 8) * 18);
129 for (int i
= 0; i
< 9; i
++) {
130 if (GP
.player
.inventory
[i
] > 0)
131 if (GameItems
.getItem(GP
.player
.inventory
[i
]).isBlock()) {
132 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(GP
.player
.inventory
[i
])).getTex(),
133 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
135 spriter
.draw(GameItems
.getItem(GP
.player
.inventory
[i
]).getTex(),
136 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
142 private void drawGUI() {
143 TextureRegion cursor
= textureRegions
.get("cursor");
144 TextureRegion hotbar
= textureRegions
.get("hotbar");
145 TextureRegion hotbarSelector
= textureRegions
.get("hotbar_selector");
147 if (GP
.world
.hasForeAt(GP
.input
.getCurX(), GP
.input
.getCurY()) ||
148 GP
.world
.hasBackAt(GP
.input
.getCurX(), GP
.input
.getCurY()) ||
149 GP
.controlMode
== ControlMode
.CURSOR
||
152 GP
.input
.getCurX() * 16 - getCamX(),
153 GP
.input
.getCurY() * 16 - getCamY());
154 spriter
.draw(hotbar
, getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2, 0);
155 for (int i
= 0; i
< 9; i
++) {
156 if (GP
.player
.inventory
[i
] > 0) {
157 if (GameItems
.getItem(GP
.player
.inventory
[i
]).isBlock()) {
158 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(GP
.player
.inventory
[i
])).getTex(),
159 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
162 spriter
.draw(GameItems
.getItem(GP
.player
.inventory
[i
]).getTex(),
163 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
168 spriter
.draw(hotbarSelector
,
169 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 - 1 + 20 * GP
.player
.slot
,
173 private void drawTouchGui() {
174 for (int i
= 0; i
< guiMap
.size
; i
++) {
175 Rectangle touchKey
= guiMap
.getValueAt(i
).getRect();
176 spriter
.draw(textureRegions
.get(guiMap
.getKeyAt(i
)),
177 touchKey
.x
, touchKey
.y
, touchKey
.width
, touchKey
.height
);
179 if (GP
.controlMode
== ControlMode
.CURSOR
) {
180 spriter
.draw(textureRegions
.get("shade"), 83, getHeight() - 21);
184 private void drawGamePlay() {
186 GP
.player
.draw(spriter
, GP
.player
.getX() - getCamX() - 2, GP
.player
.getY() - getCamY());
187 GP
.mobs
.forEach(this::drawMob
);
188 GP
.drops
.forEach(this::drawDrop
);
194 public void render() {
195 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
198 switch (CaveGame
.GAME_STATE
) {
208 if (CaveGame
.TOUCH
) drawTouchGui();
212 if (GameScreen
.SHOW_MAP
) {
214 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
215 shaper
.setColor(Color
.LIGHT_GRAY
);
216 shaper
.rect(0, 0, GP
.world
.getWidth(), 128);
217 for (int y
= 128; y
< 256; y
++) {
218 for (int x
= 0; x
< getWidth(); x
++) {
219 if (GP
.world
.hasForeAt(x
, y
) || GP
.world
.hasBackAt(x
, y
)) {
220 if (GameItems
.isWater(GP
.world
.getForeMap(x
, y
))) {
221 shaper
.setColor(Color
.BLUE
);
222 } else if (GameItems
.isLava(GP
.world
.getForeMap(x
, y
))) {
223 shaper
.setColor(Color
.RED
);
225 if (GP
.world
.hasForeAt(x
, y
)) {
226 shaper
.setColor(Color
.BLACK
);
228 shaper
.setColor(Color
.DARK_GRAY
);
231 shaper
.rect(x
, y
- 128, 1, 1);
235 shaper
.setColor(Color
.OLIVE
);
236 shaper
.rect(GP
.player
.getMapX(), GP
.player
.getUpperMapY() - 128, 1, 2);
241 if (GameScreen
.SHOW_DEBUG
) {
243 drawString("FPS: " + GameScreen
.FPS
, 0, 0);
244 drawString("X: " + GP
.player
.getMapX(), 0, 10);
245 drawString("Y: " + GP
.player
.getUpperMapY() / 16, 0, 20);
246 drawString("CurX: " + GP
.input
.getCurX(), 0, 30);
247 drawString("CurY: " + GP
.input
.getCurY(), 0, 40);
248 drawString("Mobs: " + GP
.mobs
.size(), 0, 50);
249 drawString("Drops: " + GP
.drops
.size(), 0, 60);
250 drawString("Block: " + GameItems
.getBlockKey(GP
.world
.getForeMap(GP
.input
.getCurX(), GP
.input
.getCurY())), 0, 70);
251 drawString("Hand: " + GameItems
.getItemKey(GP
.player
.inventory
[GP
.player
.slot
]), 0, 80);
252 drawString("Game mode: " + GP
.player
.gameMode
, 0, 90);