1 package ru
.deadsoftware
.cavecraft
.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 com
.badlogic
.gdx
.math
.MathUtils
;
8 import ru
.deadsoftware
.cavecraft
.CaveGame
;
9 import ru
.deadsoftware
.cavecraft
.GameScreen
;
10 import ru
.deadsoftware
.cavecraft
.game
.mobs
.Mob
;
11 import ru
.deadsoftware
.cavecraft
.game
.objects
.Drop
;
12 import ru
.deadsoftware
.cavecraft
.game
.objects
.Player
;
13 import ru
.deadsoftware
.cavecraft
.misc
.Assets
;
14 import ru
.deadsoftware
.cavecraft
.misc
.Renderer
;
16 public class GameRenderer
extends Renderer
{
20 GameRenderer(GameProc gp
, float width
, float heigth
) {
22 Gdx
.gl
.glClearColor(0f, .6f, .6f, 1f);
26 private float drawX(int x
) {
27 return x
* 16 - getCamX();
30 private float drawY(int y
) {
31 return y
* 16 - getCamY();
34 private void drawWreck(int bl
) {
35 if (gp
.blockDmg
> 0) {
36 spriter
.draw(Assets
.wreck
[
38 GameItems
.getBlock(bl
).getHp()],
39 gp
.curX
* 16 - getCamX(),
40 gp
.curY
* 16 - getCamY());
44 private void drawWorldBackground() {
45 int minX
= (int) (getCamX() / 16) - 1;
46 int minY
= (int) (getCamY() / 16) - 1;
47 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
48 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
49 if (minY
< 0) minY
= 0;
50 if (maxY
> gp
.world
.getHeight()) maxY
= gp
.world
.getHeight();
51 for (int y
= minY
; y
< maxY
; y
++) {
52 for (int x
= minX
; x
< maxX
; x
++) {
53 if ((gp
.world
.getForeMap(x
, y
) == 0 || GameItems
.getBlock(gp
.world
.getForeMap(x
, y
)).isTransparent())
54 && gp
.world
.getBackMap(x
, y
) > 0) {
56 Assets
.blockTex
[GameItems
.getBlock(gp
.world
.getBackMap(x
, y
)).getTex()],
58 if (gp
.world
.getForeMap(x
, y
) == 0 && x
== gp
.curX
&& y
== gp
.curY
)
59 drawWreck(gp
.world
.getBackMap(gp
.curX
, gp
.curY
));
60 Assets
.shade
.setPosition(drawX(x
), drawY(y
));
61 Assets
.shade
.draw(spriter
);
63 if (gp
.world
.getForeMap(x
, y
) > 0 && GameItems
.getBlock(gp
.world
.getForeMap(x
, y
)).isBackground()) {
65 Assets
.blockTex
[GameItems
.getBlock(gp
.world
.getForeMap(x
, y
)).getTex()],
67 if (x
== gp
.curX
&& y
== gp
.curY
)
68 drawWreck(gp
.world
.getForeMap(gp
.curX
, gp
.curY
));
74 private void drawWorldForeground() {
75 int minX
= (int) (getCamX() / 16) - 1;
76 int minY
= (int) (getCamY() / 16) - 1;
77 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
78 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
79 if (minY
< 0) minY
= 0;
80 if (maxY
> gp
.world
.getHeight()) maxY
= gp
.world
.getHeight();
81 for (int y
= minY
; y
< maxY
; y
++) {
82 for (int x
= minX
; x
< maxX
; x
++) {
83 if (gp
.world
.getForeMap(x
, y
) > 0 && !GameItems
.getBlock(gp
.world
.getForeMap(x
, y
)).isBackground()) {
85 Assets
.blockTex
[GameItems
.getBlock(gp
.world
.getForeMap(x
, y
)).getTex()],
87 if (x
== gp
.curX
&& y
== gp
.curY
)
88 drawWreck(gp
.world
.getForeMap(gp
.curX
, gp
.curY
));
94 private void drawMob(Mob mob
) {
95 float mobDrawX
= mob
.pos
.x
- getCamX();
96 float mobDrawY
= mob
.pos
.y
- getCamY();
98 if (mobDrawX
+ mob
.getWidth() - gp
.world
.getWidthPx() >= 0 && mobDrawX
- gp
.world
.getWidthPx() <= getWidth())
99 mob
.draw(spriter
, mobDrawX
- gp
.world
.getWidthPx(), mobDrawY
);
101 if (mobDrawX
+ mob
.getWidth() >= 0 && mobDrawX
<= getWidth())
102 mob
.draw(spriter
, mobDrawX
, mobDrawY
);
104 if (mobDrawX
+ mob
.getWidth() + gp
.world
.getWidthPx() >= 0 && mobDrawX
+ gp
.world
.getWidthPx() <= getWidth())
105 mob
.draw(spriter
, mobDrawX
+ gp
.world
.getWidthPx(), mobDrawY
);
108 private void drawDrop(Drop drop
) {
109 switch (GameItems
.getItem(drop
.getId()).getType()) {
111 Assets
.blockTex
[GameItems
.getItem(drop
.getId()).getTex()].setPosition(
112 drop
.pos
.x
- getCamX() - gp
.world
.getWidthPx(),
113 drop
.pos
.y
- getCamY());
114 Assets
.blockTex
[GameItems
.getItem(drop
.getId()).getTex()].draw(spriter
);
115 Assets
.blockTex
[GameItems
.getItem(drop
.getId()).getTex()].setPosition(
116 drop
.pos
.x
- getCamX(),
117 drop
.pos
.y
- getCamY());
118 Assets
.blockTex
[GameItems
.getItem(drop
.getId()).getTex()].draw(spriter
);
119 Assets
.blockTex
[GameItems
.getItem(drop
.getId()).getTex()].setPosition(
120 drop
.pos
.x
- getCamX() + gp
.world
.getWidthPx(),
121 drop
.pos
.y
- getCamY());
122 Assets
.blockTex
[GameItems
.getItem(drop
.getId()).getTex()].draw(spriter
);
126 private void drawPlayer(Player pl
) {
128 float drawX
= pl
.pos
.x
- getCamX() - 2;
129 float drawY
= pl
.pos
.y
- getCamY();
131 if (pl
.mov
.x
!= 0 || Assets
.plSprite
[0][2].getRotation() != 0) {
132 Assets
.plSprite
[0][2].rotate(Player
.ANIM_SPEED
);
133 Assets
.plSprite
[1][2].rotate(-Player
.ANIM_SPEED
);
134 Assets
.plSprite
[0][3].rotate(-Player
.ANIM_SPEED
);
135 Assets
.plSprite
[1][3].rotate(Player
.ANIM_SPEED
);
137 Assets
.plSprite
[0][2].setRotation(0);
138 Assets
.plSprite
[1][2].setRotation(0);
139 Assets
.plSprite
[0][3].setRotation(0);
140 Assets
.plSprite
[1][3].setRotation(0);
142 if (Assets
.plSprite
[0][2].getRotation() >= 60 || Assets
.plSprite
[0][2].getRotation() <= -60)
143 Player
.ANIM_SPEED
= -Player
.ANIM_SPEED
;
146 Assets
.plSprite
[1][2].setPosition(drawX
- 6, drawY
);
147 Assets
.plSprite
[1][2].draw(spriter
);
149 Assets
.plSprite
[1][3].setPosition(drawX
- 6, drawY
+ 10);
150 Assets
.plSprite
[1][3].draw(spriter
);
152 Assets
.plSprite
[0][3].setPosition(drawX
- 6, drawY
+ 10);
153 Assets
.plSprite
[0][3].draw(spriter
);
155 spriter
.draw(Assets
.plSprite
[pl
.getDir()][0], drawX
- 2, drawY
- 2);
157 spriter
.draw(Assets
.plSprite
[pl
.getDir()][1], drawX
- 2, drawY
+ 8);
159 if (pl
.inv
[gp
.slot
] > 0) {
160 float handRotation
= MathUtils
.degRad
* Assets
.plSprite
[0][2].getRotation();
161 switch (GameItems
.getItem(pl
.inv
[gp
.slot
]).getType()) {
163 Assets
.blockTex
[GameItems
.getItem(pl
.inv
[gp
.slot
]).getTex()].setPosition(
164 drawX
- 8 * MathUtils
.sin(handRotation
),
165 drawY
+ 6 + 8 * MathUtils
.cos(handRotation
));
166 Assets
.blockTex
[GameItems
.getItem(pl
.inv
[gp
.slot
]).getTex()].draw(spriter
);
169 Assets
.itemTex
[GameItems
.getItem(pl
.inv
[gp
.slot
]).getTex()].flip((pl
.getDir() == 0), false);
170 Assets
.itemTex
[GameItems
.getItem(pl
.inv
[gp
.slot
]).getTex()].setRotation(
171 -45 + pl
.getDir() * 90 + Assets
.plSprite
[0][2].getRotation());
172 Assets
.itemTex
[GameItems
.getItem(pl
.inv
[gp
.slot
]).getTex()].setPosition(
173 drawX
- 10 + (12 * pl
.getDir()) - 8 * MathUtils
.sin(handRotation
),
174 drawY
+ 2 + 8 * MathUtils
.cos(handRotation
));
175 Assets
.itemTex
[GameItems
.getItem(pl
.inv
[gp
.slot
]).getTex()].draw(spriter
);
176 Assets
.itemTex
[GameItems
.getItem(pl
.inv
[gp
.slot
]).getTex()].flip((pl
.getDir() == 0), false);
181 Assets
.plSprite
[0][2].setPosition(drawX
- 6, drawY
);
182 Assets
.plSprite
[0][2].draw(spriter
);
185 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
186 private void drawCreative() {
187 float x
= getWidth() / 2 - (float) Assets
.creativeInv
.getRegionWidth() / 2;
188 float y
= getHeight() / 2 - (float) Assets
.creativeInv
.getRegionHeight() / 2;
189 spriter
.draw(Assets
.creativeInv
, x
, y
);
190 spriter
.draw(Assets
.creativeScr
, x
+ 156,
191 y
+ 18 + (gp
.creativeScroll
* (72f / gp
.maxCreativeScroll
)));
192 for (int i
= gp
.creativeScroll
* 8; i
< gp
.creativeScroll
* 8 + 40; i
++) {
193 if (i
> 0 && i
< GameItems
.getItemsSize())
194 switch (GameItems
.getItem(i
).getType()) {
196 spriter
.draw(Assets
.blockTex
[GameItems
.getItem(i
).getTex()],
197 x
+ 8 + ((i
- gp
.creativeScroll
* 8) % 8) * 18,
198 y
+ 18 + ((i
- gp
.creativeScroll
* 8) / 8) * 18);
201 spriter
.draw(Assets
.itemTex
[GameItems
.getItem(i
).getTex()],
202 x
+ 8 + ((i
- gp
.creativeScroll
* 8) % 8) * 18,
203 y
+ 18 + ((i
- gp
.creativeScroll
* 8) / 8) * 18);
207 for (int i
= 0; i
< 9; i
++) {
208 if (gp
.player
.inv
[i
] > 0)
209 switch (GameItems
.getItem(gp
.player
.inv
[i
]).getType()) {
211 spriter
.draw(Assets
.blockTex
[GameItems
.getItem(gp
.player
.inv
[i
]).getTex()],
212 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
215 spriter
.draw(Assets
.itemTex
[GameItems
.getItem(gp
.player
.inv
[i
]).getTex()],
216 x
+ 8 + i
* 18, y
+ Assets
.creativeInv
.getRegionHeight() - 24);
222 private void drawGUI() {
223 if (gp
.world
.getForeMap(gp
.curX
, gp
.curY
) > 0 ||
224 gp
.world
.getBackMap(gp
.curX
, gp
.curY
) > 0 ||
227 spriter
.draw(Assets
.guiCur
,
228 gp
.curX
* 16 - getCamX(),
229 gp
.curY
* 16 - getCamY());
230 spriter
.draw(Assets
.invBar
, getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2, 0);
231 for (int i
= 0; i
< 9; i
++) {
232 if (gp
.player
.inv
[i
] > 0) {
233 switch (GameItems
.getItem(gp
.player
.inv
[i
]).getType()) {
235 spriter
.draw(Assets
.blockTex
[GameItems
.getItem(gp
.player
.inv
[i
]).getTex()],
236 getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
240 spriter
.draw(Assets
.itemTex
[GameItems
.getItem(gp
.player
.inv
[i
]).getTex()],
241 getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 + 3 + i
* 20,
247 spriter
.draw(Assets
.invBarCur
,
248 getWidth() / 2 - (float) Assets
.invBar
.getRegionWidth() / 2 - 1 + 20 * gp
.slot
,
252 private void drawTouchGui() {
253 spriter
.draw(Assets
.touchArrows
[0], 26, getHeight() - 52);
254 spriter
.draw(Assets
.touchArrows
[1], 0, getHeight() - 26);
255 spriter
.draw(Assets
.touchArrows
[2], 26, getHeight() - 26);
256 spriter
.draw(Assets
.touchArrows
[3], 52, getHeight() - 26);
257 spriter
.draw(Assets
.touchLMB
, getWidth() - 52, getHeight() - 26);
258 spriter
.draw(Assets
.touchRMB
, getWidth() - 26, getHeight() - 26);
259 spriter
.draw(Assets
.touchMode
, 78, getHeight() - 26);
260 if (gp
.ctrlMode
== 1) {
261 Assets
.shade
.setPosition(83, getHeight() - 21);
262 Assets
.shade
.draw(spriter
);
266 private void drawGamePlay() {
267 drawWorldBackground();
268 drawPlayer(gp
.player
);
269 for (Mob mob
: gp
.mobs
) drawMob(mob
);
270 for (Drop drop
: gp
.drops
) drawDrop(drop
);
271 drawWorldForeground();
276 public void render() {
277 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
280 switch (CaveGame
.STATE
) {
284 case GAME_CREATIVE_INV
:
290 if (CaveGame
.TOUCH
) drawTouchGui();
292 if (GameScreen
.SHOW_DEBUG
) {
293 drawString("FPS: " + GameScreen
.FPS
, 0, 0);
294 drawString("X: " + (int) (gp
.player
.pos
.x
/ 16), 0, 10);
295 drawString("Y: " + (int) (gp
.player
.pos
.y
/ 16), 0, 20);
296 drawString("CurX: " + gp
.curX
, 0, 30);
297 drawString("CurY: " + gp
.curY
, 0, 40);
298 drawString("Mobs: " + gp
.mobs
.size(), 0, 50);
299 drawString("Drops: " + gp
.drops
.size(), 0, 60);
300 drawString("Block: " + GameItems
.getBlockKey(gp
.world
.getForeMap(gp
.curX
, gp
.curY
)), 0, 70);
301 drawString("Game mode: " + gp
.player
.gameMode
, 0, 80);