DEADSOFTWARE

Update .travis.yml
[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.GL20;
5 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
6 import ru.deadsoftware.cavedroid.CaveGame;
7 import ru.deadsoftware.cavedroid.GameScreen;
8 import ru.deadsoftware.cavedroid.game.mobs.Mob;
9 import ru.deadsoftware.cavedroid.game.objects.Drop;
10 import ru.deadsoftware.cavedroid.misc.Assets;
11 import ru.deadsoftware.cavedroid.misc.Renderer;
13 import static ru.deadsoftware.cavedroid.GameScreen.GP;
15 public class GameRenderer extends Renderer {
17 GameRenderer(float width, float height) {
18 super(width, height);
19 Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
20 }
22 private float drawX(int x) {
23 return x * 16 - getCamX();
24 }
26 private float drawY(int y) {
27 return y * 16 - getCamY();
28 }
30 private void drawWreck(int bl) {
31 if (GP.blockDmg > 0) {
32 spriter.draw(Assets.wreck[
33 10 * GP.blockDmg /
34 GameItems.getBlock(bl).getHp()],
35 GP.curX * 16 - getCamX(),
36 GP.curY * 16 - getCamY());
37 }
38 }
40 private void drawBlock(int x, int y, boolean drawBG) {
41 if (drawBG) {
42 if ((GP.world.getForeMap(x, y) == 0 || GameItems.getBlock(GP.world.getForeMap(x, y)).isTransparent())
43 && GP.world.getBackMap(x, y) > 0) {
44 spriter.draw(
45 Assets.blockTex[GameItems.getBlock(GP.world.getBackMap(x, y)).getTex()],
46 drawX(x), drawY(y));
47 if (GP.world.getForeMap(x, y) == 0 && x == GP.curX && y == GP.curY)
48 drawWreck(GP.world.getBackMap(GP.curX, GP.curY));
49 }
50 }
51 if (GP.world.getForeMap(x, y) > 0 && GameItems.getBlock(GP.world.getForeMap(x, y)).isBackground() == drawBG) {
52 spriter.draw(
53 Assets.blockTex[GameItems.getBlock(GP.world.getForeMap(x, y)).getTex()],
54 drawX(x), drawY(y));
55 if (x == GP.curX && y == GP.curY)
56 drawWreck(GP.world.getForeMap(GP.curX, GP.curY));
57 }
58 }
60 private void drawWorld(boolean bg) {
61 int minX = (int) (getCamX() / 16) - 1;
62 int minY = (int) (getCamY() / 16) - 1;
63 int maxX = (int) ((getCamX() + getWidth()) / 16) + 1;
64 int maxY = (int) ((getCamY() + getHeight()) / 16) + 1;
65 if (minY < 0) minY = 0;
66 if (maxY > GP.world.getHeight()) maxY = GP.world.getHeight();
67 for (int y = minY; y < maxY; y++) {
68 for (int x = minX; x < maxX; x++) {
69 drawBlock(x, y, bg);
70 }
71 }
72 if (bg) {
73 spriter.end();
74 Gdx.gl.glEnable(GL20.GL_BLEND);
75 Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
76 shaper.begin(ShapeRenderer.ShapeType.Filled);
77 shaper.setColor(0f, 0f, 0f, .5f);
78 for (int y = minY; y < maxY; y++) {
79 for (int x = minX; x < maxX; x++) {
80 if ((GP.world.getForeMap(x, y) == 0 || GameItems.getBlock(GP.world.getForeMap(x, y)).isTransparent())
81 && GP.world.getBackMap(x, y) > 0) shaper.rect(drawX(x), drawY(y), 16, 16);
82 }
83 }
84 shaper.end();
85 Gdx.gl.glDisable(GL20.GL_BLEND);
86 spriter.begin();
87 }
88 }
90 private void drawMob(Mob mob) {
91 float mobDrawX = mob.pos.x - getCamX();
92 float mobDrawY = mob.pos.y - getCamY();
94 if (mobDrawX + mob.getWidth() - GP.world.getWidthPx() >= 0 && mobDrawX - GP.world.getWidthPx() <= getWidth())
95 mob.draw(spriter, mobDrawX - GP.world.getWidthPx(), mobDrawY);
97 if (mobDrawX + mob.getWidth() >= 0 && mobDrawX <= getWidth())
98 mob.draw(spriter, mobDrawX, mobDrawY);
100 if (mobDrawX + mob.getWidth() + GP.world.getWidthPx() >= 0 && mobDrawX + GP.world.getWidthPx() <= getWidth())
101 mob.draw(spriter, mobDrawX + GP.world.getWidthPx(), mobDrawY);
104 private void drawDrop(Drop drop) {
105 switch (GameItems.getItem(drop.getId()).getType()) {
106 case 0:
107 Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition(
108 drop.pos.x - getCamX() - GP.world.getWidthPx(),
109 drop.pos.y - getCamY());
110 Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter);
111 Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].setPosition(
112 drop.pos.x - getCamX(),
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() + GP.world.getWidthPx(),
117 drop.pos.y - getCamY());
118 Assets.blockTex[GameItems.getItem(drop.getId()).getTex()].draw(spriter);
122 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
123 private void drawCreative() {
124 float x = getWidth() / 2 - (float) Assets.creativeInv.getRegionWidth() / 2;
125 float y = getHeight() / 2 - (float) Assets.creativeInv.getRegionHeight() / 2;
126 spriter.draw(Assets.creativeInv, x, y);
127 spriter.draw(Assets.creativeScr, x + 156,
128 y + 18 + (GP.creativeScroll * (72f / GP.maxCreativeScroll)));
129 for (int i = GP.creativeScroll * 8; i < GP.creativeScroll * 8 + 40; i++) {
130 if (i > 0 && i < GameItems.getItemsSize())
131 switch (GameItems.getItem(i).getType()) {
132 case 0:
133 spriter.draw(Assets.blockTex[GameItems.getItem(i).getTex()],
134 x + 8 + ((i - GP.creativeScroll * 8) % 8) * 18,
135 y + 18 + ((i - GP.creativeScroll * 8) / 8) * 18);
136 break;
137 case 1:
138 case 2:
139 spriter.draw(Assets.itemTex[GameItems.getItem(i).getTex()],
140 x + 8 + ((i - GP.creativeScroll * 8) % 8) * 18,
141 y + 18 + ((i - GP.creativeScroll * 8) / 8) * 18);
142 break;
145 for (int i = 0; i < 9; i++) {
146 if (GP.player.inv[i] > 0)
147 switch (GameItems.getItem(GP.player.inv[i]).getType()) {
148 case 0:
149 spriter.draw(Assets.blockTex[GameItems.getItem(GP.player.inv[i]).getTex()],
150 x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24);
151 break;
152 case 1:
153 case 2:
154 spriter.draw(Assets.itemTex[GameItems.getItem(GP.player.inv[i]).getTex()],
155 x + 8 + i * 18, y + Assets.creativeInv.getRegionHeight() - 24);
156 break;
161 private void drawGUI() {
162 if (GP.world.getForeMap(GP.curX, GP.curY) > 0 ||
163 GP.world.getBackMap(GP.curX, GP.curY) > 0 ||
164 GP.ctrlMode == 1 ||
165 !CaveGame.TOUCH)
166 spriter.draw(Assets.guiCur,
167 GP.curX * 16 - getCamX(),
168 GP.curY * 16 - getCamY());
169 spriter.draw(Assets.invBar, getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2, 0);
170 for (int i = 0; i < 9; i++) {
171 if (GP.player.inv[i] > 0) {
172 switch (GameItems.getItem(GP.player.inv[i]).getType()) {
173 case 0:
174 spriter.draw(Assets.blockTex[GameItems.getItem(GP.player.inv[i]).getTex()],
175 getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20,
176 3);
177 break;
178 case 1:
179 case 2:
180 spriter.draw(Assets.itemTex[GameItems.getItem(GP.player.inv[i]).getTex()],
181 getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 + 3 + i * 20,
182 3);
183 break;
187 spriter.draw(Assets.invBarCur,
188 getWidth() / 2 - (float) Assets.invBar.getRegionWidth() / 2 - 1 + 20 * GP.player.invSlot,
189 -1);
192 private void drawTouchGui() {
193 spriter.draw(Assets.touchArrows[0], 26, getHeight() - 52);
194 spriter.draw(Assets.touchArrows[1], 0, getHeight() - 26);
195 spriter.draw(Assets.touchArrows[2], 26, getHeight() - 26);
196 spriter.draw(Assets.touchArrows[3], 52, getHeight() - 26);
197 spriter.draw(Assets.touchLMB, getWidth() - 52, getHeight() - 26);
198 spriter.draw(Assets.touchRMB, getWidth() - 26, getHeight() - 26);
199 spriter.draw(Assets.touchMode, 78, getHeight() - 26);
200 if (GP.ctrlMode == 1) {
201 Assets.shade.setPosition(83, getHeight() - 21);
202 Assets.shade.draw(spriter);
206 private void drawGamePlay() {
207 drawWorld(true);
208 GP.player.draw(spriter, GP.player.pos.x - getCamX() - 2, GP.player.pos.y - getCamY());
209 for (Mob mob : GP.mobs) drawMob(mob);
210 for (Drop drop : GP.drops) drawDrop(drop);
211 drawWorld(false);
212 drawGUI();
215 @Override
216 public void render() {
217 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
219 spriter.begin();
220 switch (CaveGame.STATE) {
221 case GAME_PLAY:
222 drawGamePlay();
223 break;
224 case GAME_CREATIVE_INV:
225 drawGamePlay();
226 drawCreative();
227 break;
230 if (CaveGame.TOUCH) drawTouchGui();
232 if (GameScreen.SHOW_DEBUG) {
233 drawString("FPS: " + GameScreen.FPS, 0, 0);
234 drawString("X: " + (int) (GP.player.pos.x / 16), 0, 10);
235 drawString("Y: " + (int) (GP.player.pos.y / 16), 0, 20);
236 drawString("CurX: " + GP.curX, 0, 30);
237 drawString("CurY: " + GP.curY, 0, 40);
238 drawString("Mobs: " + GP.mobs.size(), 0, 50);
239 drawString("Drops: " + GP.drops.size(), 0, 60);
240 drawString("Block: " + GameItems.getBlockKey(GP.world.getForeMap(GP.curX, GP.curY)), 0, 70);
241 drawString("Game mode: " + GP.player.gameMode, 0, 80);
243 spriter.end();