DEADSOFTWARE

Add adaptive icon
[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.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) {
24 super(width, height);
25 Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
26 }
28 private float drawX(int x) {
29 return x * 16 - getCamX();
30 }
32 private float drawY(int y) {
33 return y * 16 - getCamY();
34 }
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());
42 }
43 }
45 private void drawBlock(int x, int y, boolean drawBG) {
46 if (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()));
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 }
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++) {
70 drawBlock(x, y, bg);
71 }
72 }
73 if (bg) {
74 spriter.end();
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);
84 }
85 }
86 shaper.end();
87 Gdx.gl.glDisable(GL20.GL_BLEND);
88 spriter.begin();
89 }
90 }
92 private void drawMob(Mob mob) {
93 float mobDrawX = mob.getX() - getCamX();
94 float mobDrawY = mob.getY() - getCamY();
95 mob.draw(spriter, mobDrawX, mobDrawY);
96 }
98 private void drawDrop(Drop drop) {
99 }
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);
115 } else {
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);
126 } else {
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 ||
142 !CaveGame.TOUCH)
143 spriter.draw(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,
152 3);
153 } else {
154 spriter.draw(GameItems.getItem(GP.player.inventory[i]).getTex(),
155 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
156 3);
160 spriter.draw(hotbarSelector,
161 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * GP.player.slot,
162 -1);
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() {
177 drawWorld(true);
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);
181 drawWorld(false);
182 drawGUI();
185 @Override
186 public void render() {
187 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
189 spriter.begin();
190 switch (CaveGame.GAME_STATE) {
191 case PLAY:
192 drawGamePlay();
193 break;
194 case CREATIVE_INV:
195 drawGamePlay();
196 drawCreative();
197 break;
200 if (CaveGame.TOUCH) drawTouchGui();
202 spriter.end();
204 if (GameScreen.SHOW_MAP) {
205 //DRAW 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);
216 } else {
217 if (GP.world.hasForeAt(x, y)) {
218 shaper.setColor(Color.BLACK);
219 } else {
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);
229 shaper.end();
230 //=================
233 if (GameScreen.SHOW_DEBUG) {
234 spriter.begin();
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);
245 spriter.end();