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
.glutils
.ShapeRenderer
;
7 import ru
.deadsoftware
.cavedroid
.CaveGame
;
8 import ru
.deadsoftware
.cavedroid
.GameScreen
;
9 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
10 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
11 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
12 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
13 import ru
.deadsoftware
.cavedroid
.misc
.Renderer
;
15 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
17 public class GameRenderer
extends Renderer
{
19 GameRenderer(float width
, float height
) {
21 Gdx
.gl
.glClearColor(0f, .6f, .6f, 1f);
24 private float drawX(int x
) {
25 return x
* 16 - getCamX();
28 private float drawY(int y
) {
29 return y
* 16 - getCamY();
32 private void drawWreck(int bl
) {
33 if (GP
.blockDmg
> 0) {
34 spriter
.draw(Assets
.wreck
[
36 GameItems
.getBlock(bl
).getHp()],
37 GP
.curX
* 16 - getCamX(),
38 GP
.curY
* 16 - getCamY());
42 private void drawBlock(int x
, int y
, boolean drawBG
) {
44 if ((!GP
.world
.hasForeAt(x
, y
) || GP
.world
.getForeMapBlock(x
, y
).isTransparent())
45 && GP
.world
.hasBackAt(x
, y
)) {
46 spriter
.draw(GP
.world
.getBackMapBlock(x
, y
).getTex(), drawX(x
), drawY(y
));
47 if (!GP
.world
.hasForeAt(x
, y
) && x
== GP
.curX
&& y
== GP
.curY
)
48 drawWreck(GP
.world
.getBackMap(GP
.curX
, GP
.curY
));
51 if (GP
.world
.hasForeAt(x
, y
) && GP
.world
.getForeMapBlock(x
, y
).isBackground() == drawBG
) {
52 spriter
.draw(GP
.world
.getForeMapBlock(x
, y
).getTex(), drawX(x
), drawY(y
));
53 if (x
== GP
.curX
&& y
== GP
.curY
)
54 drawWreck(GP
.world
.getForeMap(GP
.curX
, GP
.curY
));
58 private void drawWorld(boolean bg
) {
59 int minX
= (int) (getCamX() / 16) - 1;
60 int minY
= (int) (getCamY() / 16) - 1;
61 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
62 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
63 if (minY
< 0) minY
= 0;
64 if (maxY
> GP
.world
.getHeight()) maxY
= GP
.world
.getHeight();
65 for (int y
= minY
; y
< maxY
; y
++) {
66 for (int x
= minX
; x
< maxX
; x
++) {
72 Gdx
.gl
.glEnable(GL20
.GL_BLEND
);
73 Gdx
.gl
.glBlendFunc(GL20
.GL_SRC_ALPHA
, GL20
.GL_ONE_MINUS_SRC_ALPHA
);
74 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
75 shaper
.setColor(0f, 0f, 0f, .5f);
76 for (int y
= minY
; y
< maxY
; y
++) {
77 for (int x
= minX
; x
< maxX
; x
++) {
78 if ((!GP
.world
.hasForeAt(x
, y
) || GP
.world
.getForeMapBlock(x
, y
).isTransparent())
79 && GP
.world
.hasBackAt(x
, y
))
80 shaper
.rect(drawX(x
), drawY(y
), 16, 16);
84 Gdx
.gl
.glDisable(GL20
.GL_BLEND
);
89 private void drawMob(Mob mob
) {
90 float mobDrawX
= mob
.pos
.x
- getCamX();
91 float mobDrawY
= mob
.pos
.y
- getCamY();
93 if (mobDrawX
+ mob
.getWidth() - GP
.world
.getWidthPx() >= 0 && mobDrawX
- GP
.world
.getWidthPx() <= getWidth())
94 mob
.draw(spriter
, mobDrawX
- GP
.world
.getWidthPx(), mobDrawY
);
96 if (mobDrawX
+ mob
.getWidth() >= 0 && mobDrawX
<= getWidth())
97 mob
.draw(spriter
, mobDrawX
, mobDrawY
);
99 if (mobDrawX
+ mob
.getWidth() + GP
.world
.getWidthPx() >= 0 && mobDrawX
+ GP
.world
.getWidthPx() <= getWidth())
100 mob
.draw(spriter
, mobDrawX
+ GP
.world
.getWidthPx(), mobDrawY
);
103 private void drawDrop(Drop drop
) {
106 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
107 private void drawCreative() {
108 float x
= getWidth() / 2 - (float) Assets
.creativeInv
.getRegionWidth() / 2;
109 float y
= getHeight() / 2 - (float) Assets
.creativeInv
.getRegionHeight() / 2;
110 spriter
.draw(Assets
.creativeInv
, x
, y
);
111 spriter
.draw(Assets
.creativeScr
, x
+ 156,
112 y
+ 18 + (GP
.creativeScroll
* (72f / GameProc
.MAX_CREATIVE_SCROLL
)));
113 for (int i
= GP
.creativeScroll
* 8; i
< GP
.creativeScroll
* 8 + 40; i
++) {
114 if (i
> 0 && i
< GameItems
.getItemsSize())
115 if (GameItems
.getItem(i
).isBlock()) {
116 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(i
)).getTex(),
117 x
+ 8 + ((i
- GP
.creativeScroll
* 8) % 8) * 18,
118 y
+ 18 + ((i
- GP
.creativeScroll
* 8) / 8) * 18);
120 spriter
.draw(GameItems
.getItem(i
).getTex(),
121 x
+ 8 + ((i
- GP
.creativeScroll
* 8) % 8) * 18,
122 y
+ 18 + ((i
- GP
.creativeScroll
* 8) / 8) * 18);
125 for (int i
= 0; i
< 9; i
++) {
126 if (GP
.player
.inventory
[i
] > 0)
127 if (GameItems
.getItem(GP
.player
.inventory
[i
]).isBlock()) {
128 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(GP
.player
.inventory
[i
])).getTex(),
129 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
131 spriter
.draw(GameItems
.getItem(GP
.player
.inventory
[i
]).getTex(),
132 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
138 private void drawGUI() {
139 if (GP
.world
.hasForeAt(GP
.curX
, GP
.curY
) ||
140 GP
.world
.hasBackAt(GP
.curX
, GP
.curY
) ||
141 GP
.controlMode
== ControlMode
.CURSOR
||
143 spriter
.draw(Assets
.guiCur
,
144 GP
.curX
* 16 - getCamX(),
145 GP
.curY
* 16 - getCamY());
146 spriter
.draw(Assets
.invBar
, getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2, 0);
147 for (int i
= 0; i
< 9; i
++) {
148 if (GP
.player
.inventory
[i
] > 0) {
149 if (GameItems
.getItem(GP
.player
.inventory
[i
]).isBlock()) {
150 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(GP
.player
.inventory
[i
])).getTex(),
151 getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
154 spriter
.draw(GameItems
.getItem(GP
.player
.inventory
[i
]).getTex(),
155 getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
160 spriter
.draw(Assets
.invBarCur
,
161 getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 - 1 + 20 * GP
.player
.slot
,
165 private void drawTouchGui() {
166 spriter
.draw(Assets
.touchArrows
[0], 26, getHeight() - 52);
167 spriter
.draw(Assets
.touchArrows
[1], 0, getHeight() - 26);
168 spriter
.draw(Assets
.touchArrows
[2], 26, getHeight() - 26);
169 spriter
.draw(Assets
.touchArrows
[3], 52, getHeight() - 26);
170 spriter
.draw(Assets
.touchLMB
, getWidth() - 52, getHeight() - 26);
171 spriter
.draw(Assets
.touchRMB
, getWidth() - 26, getHeight() - 26);
172 spriter
.draw(Assets
.touchMode
, 78, getHeight() - 26);
173 if (GP
.controlMode
== ControlMode
.CURSOR
) {
174 spriter
.draw(Assets
.shade
, 83, getHeight() - 21);
178 private void drawGamePlay() {
180 GP
.player
.draw(spriter
, GP
.player
.pos
.x
- getCamX() - 2, GP
.player
.pos
.y
- getCamY());
181 for (Mob mob
: GP
.mobs
) drawMob(mob
);
182 for (Drop drop
: GP
.drops
) drawDrop(drop
);
188 public void render() {
189 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
192 switch (CaveGame
.GAME_STATE
) {
202 if (CaveGame
.TOUCH
) drawTouchGui();
206 if (GameScreen
.SHOW_MAP
) {
208 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
209 shaper
.setColor(Color
.LIGHT_GRAY
);
210 shaper
.rect(0, 0, GP
.world
.getWidth(), 128);
211 for (int y
= 128; y
< 256; y
++) {
212 for (int x
= 0; x
< getWidth(); x
++) {
213 if (GP
.world
.hasForeAt(x
, y
) || GP
.world
.hasBackAt(x
, y
)) {
214 if (GameItems
.isWater(GP
.world
.getForeMap(x
, y
))) {
215 shaper
.setColor(Color
.BLUE
);
216 } else if (GameItems
.isLava(GP
.world
.getForeMap(x
, y
))) {
217 shaper
.setColor(Color
.RED
);
219 if (GP
.world
.hasForeAt(x
, y
)) {
220 shaper
.setColor(Color
.BLACK
);
222 shaper
.setColor(Color
.DARK_GRAY
);
225 shaper
.rect(x
, y
- 128, 1, 1);
229 shaper
.setColor(Color
.OLIVE
);
230 shaper
.rect(GP
.player
.pos
.x
/ 16, GP
.player
.pos
.y
/ 16 - 128, 1, 2);
236 if (GameScreen
.SHOW_DEBUG
) {
237 drawString("FPS: " + GameScreen
.FPS
, 0, 0);
238 drawString("X: " + (int) (GP
.player
.pos
.x
/ 16), 0, 10);
239 drawString("Y: " + (int) (GP
.player
.pos
.y
/ 16), 0, 20);
240 drawString("CurX: " + GP
.curX
, 0, 30);
241 drawString("CurY: " + GP
.curY
, 0, 40);
242 drawString("Mobs: " + GP
.mobs
.size(), 0, 50);
243 drawString("Drops: " + GP
.drops
.size(), 0, 60);
244 drawString("Block: " + GameItems
.getBlockKey(GP
.world
.getForeMap(GP
.curX
, GP
.curY
)), 0, 70);
245 drawString("Hand: " + GameItems
.getItemKey(GP
.player
.inventory
[GP
.player
.slot
]), 0, 80);
246 drawString("Game mode: " + GP
.player
.gameMode
, 0, 90);