DEADSOFTWARE

Fix codestyle
[cavedroid.git] / core / src / ru / deadsoftware / cavedroid / game / GameRenderer.java
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) {
23 super(width, height);
24 Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
25 }
27 private float drawX(int x) {
28 return x * 16 - getCamX();
29 }
31 private float drawY(int y) {
32 return y * 16 - getCamY();
33 }
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());
41 }
42 }
44 private void drawBlock(int x, int y, boolean drawBG) {
45 if (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()));
51 }
52 }
53 }
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()));
58 }
59 }
60 }
62 private void drawWorld(boolean bg) {
63 int minX = (int) (getCamX() / 16) - 1;
64 int minY = (int) (getCamY() / 16) - 1;
65 int maxX = (int) ((getCamX() + getWidth()) / 16) + 1;
66 int maxY = (int) ((getCamY() + getHeight()) / 16) + 1;
67 if (minY < 0) {
68 minY = 0;
69 }
70 if (maxY > GP.world.getHeight()) {
71 maxY = GP.world.getHeight();
72 }
73 for (int y = minY; y < maxY; y++) {
74 for (int x = minX; x < maxX; x++) {
75 drawBlock(x, y, bg);
76 }
77 }
78 if (bg) {
79 spriter.end();
80 Gdx.gl.glEnable(GL20.GL_BLEND);
81 Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
82 shaper.begin(ShapeRenderer.ShapeType.Filled);
83 shaper.setColor(0f, 0f, 0f, .5f);
84 for (int y = minY; y < maxY; y++) {
85 for (int x = minX; x < maxX; x++) {
86 if ((!GP.world.hasForeAt(x, y) || GP.world.getForeMapBlock(x, y).isTransparent())
87 && GP.world.hasBackAt(x, y)) {
88 shaper.rect(drawX(x), drawY(y), 16, 16);
89 }
90 }
91 }
92 shaper.end();
93 Gdx.gl.glDisable(GL20.GL_BLEND);
94 spriter.begin();
95 }
96 }
98 private void drawMob(Mob mob) {
99 float mobDrawX = mob.getX() - getCamX();
100 float mobDrawY = mob.getY() - getCamY();
102 if (mobDrawX + mob.getWidth() < 0 && mobDrawX + GP.world.getWidthPx() > 0) {
103 mobDrawX += GP.world.getWidthPx();
104 } else if (mobDrawX > getWidth() && mobDrawX + mob.getWidth() - GP.world.getWidthPx() > 0) {
105 mobDrawX -= GP.world.getWidthPx();
106 } else if (mobDrawX + mob.getWidth() < 0 && mobDrawX > getWidth()) {
107 return;
110 mob.draw(spriter, mobDrawX, mobDrawY);
113 private void drawDrop(Drop drop) {
116 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
117 private void drawCreative() {
118 TextureRegion creative = textureRegions.get("creative");
119 float x = getWidth() / 2 - (float) creative.getRegionWidth() / 2;
120 float y = getHeight() / 2 - (float) creative.getRegionHeight() / 2;
121 spriter.draw(creative, x, y);
122 spriter.draw(textureRegions.get("handle"), x + 156,
123 y + 18 + (GP.input.getCreativeScroll() * (72f / GameProc.MAX_CREATIVE_SCROLL)));
124 for (int i = GP.input.getCreativeScroll() * 8; i < GP.input.getCreativeScroll() * 8 + 40; i++) {
125 if (i > 0 && i < GameItems.getItemsSize()) {
126 if (GameItems.getItem(i).isBlock()) {
127 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(i)).getTex(),
128 x + 8 + ((i - GP.input.getCreativeScroll() * 8) % 8) * 18,
129 y + 18 + ((i - GP.input.getCreativeScroll() * 8) / 8) * 18);
130 } else {
131 spriter.draw(GameItems.getItem(i).getTex(),
132 x + 8 + ((i - GP.input.getCreativeScroll() * 8) % 8) * 18,
133 y + 18 + ((i - GP.input.getCreativeScroll() * 8) / 8) * 18);
137 for (int i = 0; i < 9; i++) {
138 if (GP.player.inventory[i] > 0) {
139 if (GameItems.getItem(GP.player.inventory[i]).isBlock()) {
140 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(),
141 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
142 } else {
143 spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(),
144 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
151 private void drawGUI() {
152 TextureRegion cursor = textureRegions.get("cursor");
153 TextureRegion hotbar = textureRegions.get("hotbar");
154 TextureRegion hotbarSelector = textureRegions.get("hotbar_selector");
156 if (GP.world.hasForeAt(GP.input.getCurX(), GP.input.getCurY()) ||
157 GP.world.hasBackAt(GP.input.getCurX(), GP.input.getCurY()) ||
158 GP.controlMode == ControlMode.CURSOR ||
159 !CaveGame.TOUCH) {
160 spriter.draw(cursor,
161 GP.input.getCurX() * 16 - getCamX(),
162 GP.input.getCurY() * 16 - getCamY());
164 spriter.draw(hotbar, getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, 0);
165 for (int i = 0; i < 9; i++) {
166 if (GP.player.inventory[i] > 0) {
167 if (GameItems.getItem(GP.player.inventory[i]).isBlock()) {
168 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(GP.player.inventory[i])).getTex(),
169 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
170 3);
171 } else {
172 spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(),
173 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
174 3);
178 spriter.draw(hotbarSelector,
179 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * GP.player.slot,
180 -1);
183 private void drawTouchGui() {
184 for (int i = 0; i < guiMap.size; i++) {
185 Rectangle touchKey = guiMap.getValueAt(i).getRect();
186 spriter.draw(textureRegions.get(guiMap.getKeyAt(i)),
187 touchKey.x, touchKey.y, touchKey.width, touchKey.height);
189 if (GP.controlMode == ControlMode.CURSOR) {
190 spriter.draw(textureRegions.get("shade"), 83, getHeight() - 21);
194 private void drawGamePlay() {
195 drawWorld(true);
196 GP.player.draw(spriter, GP.player.getX() - getCamX() - 2, GP.player.getY() - getCamY());
197 GP.mobs.forEach(this::drawMob);
198 GP.drops.forEach(this::drawDrop);
199 drawWorld(false);
200 drawGUI();
203 @Override
204 public void render() {
205 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
207 spriter.begin();
208 switch (CaveGame.GAME_STATE) {
209 case PLAY:
210 drawGamePlay();
211 break;
212 case CREATIVE_INV:
213 drawGamePlay();
214 drawCreative();
215 break;
218 if (CaveGame.TOUCH) {
219 drawTouchGui();
222 spriter.end();
224 if (GameScreen.SHOW_MAP) {
225 //DRAW MAP
226 shaper.begin(ShapeRenderer.ShapeType.Filled);
227 shaper.setColor(Color.LIGHT_GRAY);
228 shaper.rect(0, 0, GP.world.getWidth(), 128);
229 for (int y = 128; y < 256; y++) {
230 for (int x = 0; x < getWidth(); x++) {
231 if (GP.world.hasForeAt(x, y) || GP.world.hasBackAt(x, y)) {
232 if (GameItems.isWater(GP.world.getForeMap(x, y))) {
233 shaper.setColor(Color.BLUE);
234 } else if (GameItems.isLava(GP.world.getForeMap(x, y))) {
235 shaper.setColor(Color.RED);
236 } else {
237 if (GP.world.hasForeAt(x, y)) {
238 shaper.setColor(Color.BLACK);
239 } else {
240 shaper.setColor(Color.DARK_GRAY);
243 shaper.rect(x, y - 128, 1, 1);
247 shaper.setColor(Color.OLIVE);
248 shaper.rect(GP.player.getMapX(), GP.player.getUpperMapY() - 128, 1, 2);
249 shaper.end();
250 //=================
253 if (GameScreen.SHOW_DEBUG) {
254 spriter.begin();
255 drawString("FPS: " + GameScreen.FPS, 0, 0);
256 drawString("X: " + GP.player.getMapX(), 0, 10);
257 drawString("Y: " + GP.player.getUpperMapY() / 16, 0, 20);
258 drawString("CurX: " + GP.input.getCurX(), 0, 30);
259 drawString("CurY: " + GP.input.getCurY(), 0, 40);
260 drawString("Mobs: " + GP.mobs.size(), 0, 50);
261 drawString("Drops: " + GP.drops.size(), 0, 60);
262 drawString("Block: " + GameItems.getBlockKey(GP.world.getForeMap(GP.input.getCurX(), GP.input.getCurY())), 0, 70);
263 drawString("Hand: " + GameItems.getItemKey(GP.player.inventory[GP.player.slot]), 0, 80);
264 drawString("Game mode: " + GP.player.gameMode, 0, 90);
265 spriter.end();