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
.MainConfig
;
10 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Mob
;
11 import ru
.deadsoftware
.cavedroid
.game
.mobs
.MobsController
;
12 import ru
.deadsoftware
.cavedroid
.game
.mobs
.Player
;
13 import ru
.deadsoftware
.cavedroid
.game
.objects
.Drop
;
14 import ru
.deadsoftware
.cavedroid
.game
.objects
.DropController
;
15 import ru
.deadsoftware
.cavedroid
.misc
.ControlMode
;
16 import ru
.deadsoftware
.cavedroid
.misc
.Renderer
;
18 import javax
.inject
.Inject
;
20 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.guiMap
;
21 import static ru
.deadsoftware
.cavedroid
.misc
.Assets
.textureRegions
;
24 public class GameRenderer
extends Renderer
{
26 private static final String TAG
= "GameRenderer";
28 private final MainConfig mMainConfig
;
29 private final GameInput mGameInput
;
30 private final GameWorld mGameWorld
;
31 private final MobsController mMobsController
;
32 private final DropController mDropController
;
35 GameRenderer(MainConfig mainConfig
,
38 MobsController mobsController
,
39 DropController dropController
) {
40 super(mainConfig
.getWidth(), mainConfig
.getHeight());
42 mMainConfig
= mainConfig
;
43 mGameInput
= gameInput
;
44 mGameWorld
= gameWorld
;
45 mMobsController
= mobsController
;
46 mDropController
= dropController
;
48 Gdx
.gl
.glClearColor(0f, .6f, .6f, 1f);
51 private float drawX(int x
) {
52 return x
* 16 - getCamX();
55 private float drawY(int y
) {
56 return y
* 16 - getCamY();
59 private void drawWreck(int bl
) {
60 if (mGameInput
.getBlockDamage() > 0) {
61 int index
= 10 * mGameInput
.getBlockDamage() / GameItems
.getBlock(bl
).getHp();
62 String key
= "break_" + index
;
63 spriter
.draw(textureRegions
.get(key
), mGameInput
.getCurX() * 16 - getCamX(),
64 mGameInput
.getCurY() * 16 - getCamY());
68 private void drawBlock(int x
, int y
, boolean drawBG
) {
70 if ((!mGameWorld
.hasForeAt(x
, y
) || mGameWorld
.getForeMapBlock(x
, y
).isTransparent())
71 && mGameWorld
.hasBackAt(x
, y
)) {
72 spriter
.draw(mGameWorld
.getBackMapBlock(x
, y
).getTexture(), drawX(x
), drawY(y
));
73 if (!mGameWorld
.hasForeAt(x
, y
) && x
== mGameInput
.getCurX() && y
== mGameInput
.getCurY()) {
74 drawWreck(mGameWorld
.getBackMap(mGameInput
.getCurX(), mGameInput
.getCurY()));
78 if (mGameWorld
.hasForeAt(x
, y
) && mGameWorld
.getForeMapBlock(x
, y
).isBackground() == drawBG
) {
79 spriter
.draw(mGameWorld
.getForeMapBlock(x
, y
).getTexture(), drawX(x
), drawY(y
));
80 if (x
== mGameInput
.getCurX() && y
== mGameInput
.getCurY()) {
81 drawWreck(mGameWorld
.getForeMap(mGameInput
.getCurX(), mGameInput
.getCurY()));
86 private void drawWorld(boolean bg
) {
87 int minX
= (int) (getCamX() / 16) - 1;
88 int minY
= (int) (getCamY() / 16) - 1;
89 int maxX
= (int) ((getCamX() + getWidth()) / 16) + 1;
90 int maxY
= (int) ((getCamY() + getHeight()) / 16) + 1;
94 if (maxY
> mGameWorld
.getHeight()) {
95 maxY
= mGameWorld
.getHeight();
97 for (int y
= minY
; y
< maxY
; y
++) {
98 for (int x
= minX
; x
< maxX
; x
++) {
104 Gdx
.gl
.glEnable(GL20
.GL_BLEND
);
105 Gdx
.gl
.glBlendFunc(GL20
.GL_SRC_ALPHA
, GL20
.GL_ONE_MINUS_SRC_ALPHA
);
106 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
107 shaper
.setColor(0f, 0f, 0f, .5f);
108 for (int y
= minY
; y
< maxY
; y
++) {
109 for (int x
= minX
; x
< maxX
; x
++) {
110 if ((!mGameWorld
.hasForeAt(x
, y
) || mGameWorld
.getForeMapBlock(x
, y
).isTransparent())
111 && mGameWorld
.hasBackAt(x
, y
)) {
112 shaper
.rect(drawX(x
), drawY(y
), 16, 16);
117 Gdx
.gl
.glDisable(GL20
.GL_BLEND
);
122 private void drawMob(Mob mob
) {
123 float mobDrawX
= mob
.getX() - getCamX();
124 float mobDrawY
= mob
.getY() - getCamY();
126 if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
+ mGameWorld
.getWidthPx() > 0) {
127 mobDrawX
+= mGameWorld
.getWidthPx();
128 } else if (mobDrawX
> getWidth() && mobDrawX
+ mob
.getWidth() - mGameWorld
.getWidthPx() > 0) {
129 mobDrawX
-= mGameWorld
.getWidthPx();
130 } else if (mobDrawX
+ mob
.getWidth() < 0 && mobDrawX
> getWidth()) {
134 mob
.draw(spriter
, mobDrawX
, mobDrawY
);
137 private void drawDrop(Drop drop
) {
140 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
141 private void drawCreative() {
142 TextureRegion creative
= textureRegions
.get("creative");
143 float x
= getWidth() / 2 - (float) creative
.getRegionWidth() / 2;
144 float y
= getHeight() / 2 - (float) creative
.getRegionHeight() / 2;
145 spriter
.draw(creative
, x
, y
);
146 spriter
.draw(textureRegions
.get("handle"), x
+ 156,
147 y
+ 18 + (mGameInput
.getCreativeScroll() * (72f / GameProc
.MAX_CREATIVE_SCROLL
)));
148 for (int i
= mGameInput
.getCreativeScroll() * 8; i
< mGameInput
.getCreativeScroll() * 8 + 40; i
++) {
149 if (i
> 0 && i
< GameItems
.getItemsSize()) {
150 if (GameItems
.getItem(i
).isBlock()) {
151 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(i
)).getTexture(),
152 x
+ 8 + ((i
- mGameInput
.getCreativeScroll() * 8) % 8) * 18,
153 y
+ 18 + ((i
- mGameInput
.getCreativeScroll() * 8) / 8) * 18);
155 spriter
.draw(GameItems
.getItem(i
).getTexture(),
156 x
+ 8 + ((i
- mGameInput
.getCreativeScroll() * 8) % 8) * 18,
157 y
+ 18 + ((i
- mGameInput
.getCreativeScroll() * 8) / 8) * 18);
161 for (int i
= 0; i
< 9; i
++) {
162 if (mMobsController
.getPlayer().inventory
[i
] > 0) {
163 if (GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).isBlock()) {
164 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(mMobsController
.getPlayer().inventory
[i
])).getTexture(),
165 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
167 spriter
.draw(GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).getTexture(),
168 x
+ 8 + i
* 18, y
+ creative
.getRegionHeight() - 24);
175 private void drawGUI() {
176 TextureRegion cursor
= textureRegions
.get("cursor");
177 TextureRegion hotbar
= textureRegions
.get("hotbar");
178 TextureRegion hotbarSelector
= textureRegions
.get("hotbar_selector");
180 if (mGameWorld
.hasForeAt(mGameInput
.getCurX(), mGameInput
.getCurY()) ||
181 mGameWorld
.hasBackAt(mGameInput
.getCurX(), mGameInput
.getCurY()) ||
182 mGameInput
.getControlMode() == ControlMode
.CURSOR
|| mMainConfig
.isTouch()) {
183 spriter
.draw(cursor
, mGameInput
.getCurX() * 16 - getCamX(), mGameInput
.getCurY() * 16 - getCamY());
185 spriter
.draw(hotbar
, getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2, 0);
186 for (int i
= 0; i
< 9; i
++) {
187 if (mMobsController
.getPlayer().inventory
[i
] > 0) {
188 if (GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).isBlock()) {
189 spriter
.draw(GameItems
.getBlock(GameItems
.getBlockIdByItemId(mMobsController
.getPlayer().inventory
[i
])).getTexture(),
190 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
193 spriter
.draw(GameItems
.getItem(mMobsController
.getPlayer().inventory
[i
]).getTexture(),
194 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 + 3 + i
* 20,
199 spriter
.draw(hotbarSelector
,
200 getWidth() / 2 - (float) hotbar
.getRegionWidth() / 2 - 1 + 20 * mMobsController
.getPlayer().slot
,
204 private void drawTouchGui() {
205 for (int i
= 0; i
< guiMap
.size
; i
++) {
206 Rectangle touchKey
= guiMap
.getValueAt(i
).getRect();
207 spriter
.draw(textureRegions
.get(guiMap
.getKeyAt(i
)),
208 touchKey
.x
, touchKey
.y
, touchKey
.width
, touchKey
.height
);
210 if (mGameInput
.getControlMode() == ControlMode
.CURSOR
) {
211 spriter
.draw(textureRegions
.get("shade"), 83, getHeight() - 21);
215 private void drawGamePlay() {
216 Player player
= mMobsController
.getPlayer();
219 player
.draw(spriter
, player
.getX() - getCamX() - player
.getWidth() / 2, player
.getY() - getCamY());
220 mMobsController
.forEach(this::drawMob
);
221 mDropController
.forEach(this::drawDrop
);
226 private void updateCameraPosition() {
227 Player player
= mMobsController
.getPlayer();
228 setCamPos(player
.getX() + player
.getWidth() / 2 - getWidth() / 2,
229 player
.getY() + player
.getHeight() / 2 - getHeight() / 2);
233 public void render(float delta
) {
234 int fps
= (int) (1 / delta
);
235 updateCameraPosition();
236 mGameInput
.moveCursor(this);
238 Gdx
.gl
.glClear(GL20
.GL_COLOR_BUFFER_BIT
);
244 switch (mMainConfig
.getGameUiWindow()) {
245 case CREATIVE_INVENTORY
:
248 //TODO draw other ui windows
252 if (mMainConfig
.isTouch()) {
258 if (mMainConfig
.isShowMap()) {
260 shaper
.begin(ShapeRenderer
.ShapeType
.Filled
);
261 shaper
.setColor(Color
.LIGHT_GRAY
);
262 shaper
.rect(0, 0, mGameWorld
.getWidth(), 128);
263 for (int y
= 128; y
< 256; y
++) {
264 for (int x
= 0; x
< getWidth(); x
++) {
265 if (mGameWorld
.hasForeAt(x
, y
) || mGameWorld
.hasBackAt(x
, y
)) {
266 if (GameItems
.isWater(mGameWorld
.getForeMap(x
, y
))) {
267 shaper
.setColor(Color
.BLUE
);
268 } else if (GameItems
.isLava(mGameWorld
.getForeMap(x
, y
))) {
269 shaper
.setColor(Color
.RED
);
271 if (mGameWorld
.hasForeAt(x
, y
)) {
272 shaper
.setColor(Color
.BLACK
);
274 shaper
.setColor(Color
.DARK_GRAY
);
277 shaper
.rect(x
, y
- 128, 1, 1);
281 shaper
.setColor(Color
.OLIVE
);
282 shaper
.rect(mMobsController
.getPlayer().getMapX(), mMobsController
.getPlayer().getUpperMapY() - 128, 1, 2);
287 if (mMainConfig
.isShowInfo()) {
289 drawString("FPS: " + fps
, 0, 0);
290 drawString("X: " + mMobsController
.getPlayer().getMapX(), 0, 10);
291 drawString("Y: " + mMobsController
.getPlayer().getUpperMapY(), 0, 20);
292 drawString("CurX: " + mGameInput
.getCurX(), 0, 30);
293 drawString("CurY: " + mGameInput
.getCurY(), 0, 40);
294 drawString("Mobs: " + mMobsController
.getSize(), 0, 50);
295 drawString("Drops: " + mDropController
.getSize(), 0, 60);
296 drawString("Block: " + GameItems
.getBlockKey(mGameWorld
.getForeMap(mGameInput
.getCurX(), mGameInput
.getCurY())), 0, 70);
297 drawString("Hand: " + GameItems
.getItemKey(mMobsController
.getPlayer().inventory
[mMobsController
.getPlayer().slot
]), 0, 80);
298 drawString("Game mode: " + mMobsController
.getPlayer().gameMode
, 0, 90);