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
.mobs
.Pig
;
13 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
14 import ru
.deadsoftware
.cavedroid
.misc
.Assets
;
15 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
16 import ru
.deadsoftware
.cavedroid
.misc
.Renderer
;
18 import static ru
.deadsoftware
.cavedroid
.GameScreen
.GP
;
19 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.*;
21 public class GameRenderer
extends Renderer
{
23 GameRenderer(float width
, float height
) {
25 Gdx
.gl
.glClearColor(0f, .6f, .6f, 1f);
28 private float drawX(int x
) {
29 return x
* 16 - getCamX();
32 private float drawY(int y
) {
33 return y
* 16 - getCamY();
36 private void drawWreck(int bl
) {
37 if (GP
.input
.getBlockDamage() > 0) {
38 int index
= 10 * GP
.input
.getBlockDamage() / GameItems
.getBlock(bl
).getHp();
39 String key
= "break_" + index
;
40 spriter
.draw(textureRegions
.get(key
), GP
.input
.getCurX() * 16 - getCamX(),
41 GP
.input
.getCurY() * 16 - getCamY());
45 private void drawBlock(int x
, int y
, boolean drawBG
) {
47 if ((!GP
.world
.hasForeAt(x
, y
) || GP
.world
.getForeMapBlock(x
, y
).isTransparent())
48 && GP
.world
.hasBackAt(x
, y
)) {
49 spriter
.draw(GP
.world
.getBackMapBlock(x
, y
).getTex(), drawX(x
), drawY(y
));
50 if (!GP
.world
.hasForeAt(x
, y
) && x
== GP
.input
.getCurX() && y
== GP
.input
.getCurY())
51 drawWreck(GP
.world
.getBackMap(GP
.input
.getCurX(), GP
.input
.getCurY()));
54 if (GP
.world
.hasForeAt(x
, y
) && GP
.world
.getForeMapBlock(x
, y
).isBackground() == drawBG
) {
55 spriter
.draw(GP
.world
.getForeMapBlock(x
, y
).getTex(), drawX(x
), drawY(y
));
56 if (x
== GP
.input
.getCurX() && y
== GP
.input
.getCurY())
57 drawWreck(GP
.world
.getForeMap(GP
.input
.getCurX(), GP
.input
.getCurY()));
61 private void drawWorld(boolean bg
) {
62 int minX
= (int) (getCamX() / 16) - 1;
63 int minY
= (int) (getCamY() / 16) - 1;
64 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
65 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
66 if (minY
< 0) minY
= 0;
67 if (maxY
> GP
.world
.getHeight()) maxY
= GP
.world
.getHeight();
68 for (int y
= minY
; y
< maxY
; y
++) {
69 for (int x
= minX
; x
< maxX
; x
++) {
75 Gdx
.gl
.glEnable(GL20
.GL_BLEND
);
76 Gdx
.gl
.glBlendFunc(GL20
.GL_SRC_ALPHA
, GL20
.GL_ONE_MINUS_SRC_ALPHA
);
77 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
78 shaper
.setColor(0f, 0f, 0f, .5f);
79 for (int y
= minY
; y
< maxY
; y
++) {
80 for (int x
= minX
; x
< maxX
; x
++) {
81 if ((!GP
.world
.hasForeAt(x
, y
) || GP
.world
.getForeMapBlock(x
, y
).isTransparent())
82 && GP
.world
.hasBackAt(x
, y
))
83 shaper
.rect(drawX(x
), drawY(y
), 16, 16);
87 Gdx
.gl
.glDisable(GL20
.GL_BLEND
);
92 private void drawMob(Mob mob
) {
93 float mobDrawX
= mob
.getX() - getCamX();
94 float mobDrawY
= mob
.getY() - getCamY();
95 mob
.draw(spriter
, mobDrawX
, mobDrawY
);
98 private void drawDrop(Drop drop
) {
101 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
102 private void drawCreative() {
103 TextureRegion creative
= textureRegions
.get("creative");
104 float x
= getWidth() / 2 - (float) creative
.getRegionWidth() / 2;
105 float y
= getHeight() / 2 - (float) creative
.getRegionHeight() / 2;
106 spriter
.draw(creative
, x
, y
);
107 spriter
.draw(textureRegions
.get("handle"), x
+ 156,
108 y
+ 18 + (GP
.input
.getCreativeScroll() * (72f / GameProc
.MAX_CREATIVE_SCROLL
)));
109 for (int i
= GP
.input
.getCreativeScroll() * 8; i
< GP
.input
.getCreativeScroll() * 8 + 40; i
++) {
110 if (i
> 0 && i
< GameItems
.getItemsSize())
111 if (GameItems
.getItem(i
).isBlock()) {
112 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(i
)).getTex(),
113 x
+ 8 + ((i
- GP
.input
.getCreativeScroll() * 8) % 8) * 18,
114 y
+ 18 + ((i
- GP
.input
.getCreativeScroll() * 8) / 8) * 18);
116 spriter
.draw(GameItems
.getItem(i
).getTex(),
117 x
+ 8 + ((i
- GP
.input
.getCreativeScroll() * 8) % 8) * 18,
118 y
+ 18 + ((i
- GP
.input
.getCreativeScroll() * 8) / 8) * 18);
121 for (int i
= 0; i
< 9; i
++) {
122 if (GP
.player
.inventory
[i
] > 0)
123 if (GameItems
.getItem(GP
.player
.inventory
[i
]).isBlock()) {
124 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(GP
.player
.inventory
[i
])).getTex(),
125 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
127 spriter
.draw(GameItems
.getItem(GP
.player
.inventory
[i
]).getTex(),
128 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
134 private void drawGUI() {
135 TextureRegion cursor
= textureRegions
.get("cursor");
136 TextureRegion hotbar
= textureRegions
.get("hotbar");
137 TextureRegion hotbarSelector
= textureRegions
.get("hotbar_selector");
139 if (GP
.world
.hasForeAt(GP
.input
.getCurX(), GP
.input
.getCurY()) ||
140 GP
.world
.hasBackAt(GP
.input
.getCurX(), GP
.input
.getCurY()) ||
141 GP
.controlMode
== ControlMode
.CURSOR
||
144 GP
.input
.getCurX() * 16 - getCamX(),
145 GP
.input
.getCurY() * 16 - getCamY());
146 spriter
.draw(hotbar
, getWidth() / 2 - (float) hotbar
.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) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
154 spriter
.draw(GameItems
.getItem(GP
.player
.inventory
[i
]).getTex(),
155 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
160 spriter
.draw(hotbarSelector
,
161 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 - 1 + 20 * GP
.player
.slot
,
165 private void drawTouchGui() {
166 for (int i
= 0; i
< guiMap
.size
; i
++) {
167 Rectangle touchKey
= guiMap
.getValueAt(i
).getRect();
168 spriter
.draw(textureRegions
.get(guiMap
.getKeyAt(i
)),
169 touchKey
.x
, touchKey
.y
, touchKey
.width
, touchKey
.height
);
171 if (GP
.controlMode
== ControlMode
.CURSOR
) {
172 spriter
.draw(textureRegions
.get("shade"), 83, getHeight() - 21);
176 private void drawGamePlay() {
178 GP
.player
.draw(spriter
, GP
.player
.getX() - getCamX() - 2, GP
.player
.getY() - getCamY());
179 GP
.mobs
.forEach(this::drawMob
);
180 GP
.drops
.forEach(this::drawDrop
);
186 public void render() {
187 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
190 switch (CaveGame
.GAME_STATE
) {
200 if (CaveGame
.TOUCH
) drawTouchGui();
204 if (GameScreen
.SHOW_MAP
) {
206 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
207 shaper
.setColor(Color
.LIGHT_GRAY
);
208 shaper
.rect(0, 0, GP
.world
.getWidth(), 128);
209 for (int y
= 128; y
< 256; y
++) {
210 for (int x
= 0; x
< getWidth(); x
++) {
211 if (GP
.world
.hasForeAt(x
, y
) || GP
.world
.hasBackAt(x
, y
)) {
212 if (GameItems
.isWater(GP
.world
.getForeMap(x
, y
))) {
213 shaper
.setColor(Color
.BLUE
);
214 } else if (GameItems
.isLava(GP
.world
.getForeMap(x
, y
))) {
215 shaper
.setColor(Color
.RED
);
217 if (GP
.world
.hasForeAt(x
, y
)) {
218 shaper
.setColor(Color
.BLACK
);
220 shaper
.setColor(Color
.DARK_GRAY
);
223 shaper
.rect(x
, y
- 128, 1, 1);
227 shaper
.setColor(Color
.OLIVE
);
228 shaper
.rect(GP
.player
.getMapX(), GP
.player
.getUpperMapY() - 128, 1, 2);
233 if (GameScreen
.SHOW_DEBUG
) {
235 drawString("FPS: " + GameScreen
.FPS
, 0, 0);
236 drawString("X: " + GP
.player
.getMapX(), 0, 10);
237 drawString("Y: " + GP
.player
.getUpperMapY() / 16, 0, 20);
238 drawString("CurX: " + GP
.input
.getCurX(), 0, 30);
239 drawString("CurY: " + GP
.input
.getCurY(), 0, 40);
240 drawString("Mobs: " + GP
.mobs
.size(), 0, 50);
241 drawString("Drops: " + GP
.drops
.size(), 0, 60);
242 drawString("Block: " + GameItems
.getBlockKey(GP
.world
.getForeMap(GP
.input
.getCurX(), GP
.input
.getCurY())), 0, 70);
243 drawString("Hand: " + GameItems
.getItemKey(GP
.player
.inventory
[GP
.player
.slot
]), 0, 80);
244 drawString("Game mode: " + GP
.player
.gameMode
, 0, 90);