DEADSOFTWARE

84cea1be3d19cf995ba6b92d8ee07a11b65663ed
[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.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.game.world.GameWorld;
16 import ru.deadsoftware.cavedroid.misc.ControlMode;
17 import ru.deadsoftware.cavedroid.misc.Renderer;
19 import javax.inject.Inject;
21 import static ru.deadsoftware.cavedroid.misc.Assets.guiMap;
22 import static ru.deadsoftware.cavedroid.misc.Assets.textureRegions;
24 @GameScope
25 public class GameRenderer extends Renderer {
27 private final MainConfig mMainConfig;
28 private final GameInput mGameInput;
29 private final GameWorld mGameWorld;
30 private final MobsController mMobsController;
31 private final DropController mDropController;
33 @Inject
34 GameRenderer(MainConfig mainConfig,
35 GameInput gameInput,
36 GameWorld gameWorld,
37 MobsController mobsController,
38 DropController dropController) {
39 super(mainConfig.getWidth(), mainConfig.getHeight());
41 mMainConfig = mainConfig;
42 mGameInput = gameInput;
43 mGameWorld = gameWorld;
44 mMobsController = mobsController;
45 mDropController = dropController;
47 Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
48 }
50 private float drawX(int x) {
51 return x * 16 - getCamX();
52 }
54 private float drawY(int y) {
55 return y * 16 - getCamY();
56 }
58 private void drawWreck(int bl) {
59 if (mGameInput.getBlockDamage() > 0) {
60 int index = 10 * mGameInput.getBlockDamage() / GameItems.getBlock(bl).getHp();
61 String key = "break_" + index;
62 spriter.draw(textureRegions.get(key), mGameInput.getCurX() * 16 - getCamX(),
63 mGameInput.getCurY() * 16 - getCamY());
64 }
65 }
67 private void drawBlock(int x, int y, boolean drawBG) {
68 if (drawBG) {
69 if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
70 && mGameWorld.hasBackAt(x, y)) {
71 mGameWorld.getBackMapBlock(x, y).draw(spriter, drawX(x), drawY(y));
72 if (!mGameWorld.hasForeAt(x, y) && x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
73 drawWreck(mGameWorld.getBackMap(mGameInput.getCurX(), mGameInput.getCurY()));
74 }
75 }
76 }
77 if (mGameWorld.hasForeAt(x, y) && mGameWorld.getForeMapBlock(x, y).isBackground() == drawBG) {
78 mGameWorld.getForeMapBlock(x, y).draw(spriter, drawX(x), drawY(y));
79 if (x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
80 drawWreck(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY()));
81 }
82 }
83 }
85 private void drawWorld(boolean bg) {
86 int minX = (int) (getCamX() / 16) - 1;
87 int minY = (int) (getCamY() / 16) - 1;
88 int maxX = (int) ((getCamX() + getWidth()) / 16) + 1;
89 int maxY = (int) ((getCamY() + getHeight()) / 16) + 1;
90 if (minY < 0) {
91 minY = 0;
92 }
93 if (maxY > mGameWorld.getHeight()) {
94 maxY = mGameWorld.getHeight();
95 }
96 for (int y = minY; y < maxY; y++) {
97 for (int x = minX; x < maxX; x++) {
98 drawBlock(x, y, bg);
99 }
101 if (bg) {
102 spriter.end();
103 Gdx.gl.glEnable(GL20.GL_BLEND);
104 Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
105 shaper.begin(ShapeRenderer.ShapeType.Filled);
106 shaper.setColor(0f, 0f, 0f, .5f);
107 for (int y = minY; y < maxY; y++) {
108 for (int x = minX; x < maxX; x++) {
109 if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
110 && mGameWorld.hasBackAt(x, y)) {
111 shaper.rect(drawX(x), drawY(y), 16, 16);
115 shaper.end();
116 Gdx.gl.glDisable(GL20.GL_BLEND);
117 spriter.begin();
121 private void drawMob(Mob mob, float delta) {
122 float mobDrawX = mob.getX() - getCamX();
123 float mobDrawY = mob.getY() - getCamY();
125 if (mobDrawX + mob.getWidth() < 0 && mobDrawX + mGameWorld.getWidthPx() > 0) {
126 mobDrawX += mGameWorld.getWidthPx();
127 } else if (mobDrawX > getWidth() && mobDrawX + mob.getWidth() - mGameWorld.getWidthPx() > 0) {
128 mobDrawX -= mGameWorld.getWidthPx();
129 } else if (mobDrawX + mob.getWidth() < 0 && mobDrawX > getWidth()) {
130 return;
133 mob.draw(spriter, mobDrawX, mobDrawY, delta);
136 private void drawDrop(Drop drop) {
139 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
140 private void drawCreative() {
141 TextureRegion creative = textureRegions.get("creative");
142 float x = getWidth() / 2 - (float) creative.getRegionWidth() / 2;
143 float y = getHeight() / 2 - (float) creative.getRegionHeight() / 2;
144 spriter.draw(creative, x, y);
145 spriter.draw(textureRegions.get("handle"), x + 156,
146 y + 18 + (mGameInput.getCreativeScroll() * (72f / GameProc.MAX_CREATIVE_SCROLL)));
147 for (int i = mGameInput.getCreativeScroll() * 8; i < mGameInput.getCreativeScroll() * 8 + 40; i++) {
148 if (i > 0 && i < GameItems.getItemsSize()) {
149 if (GameItems.getItem(i).isBlock()) {
150 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(i)).getTexture(),
151 x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
152 y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
153 } else {
154 spriter.draw(GameItems.getItem(i).getTexture(),
155 x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
156 y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
160 for (int i = 0; i < 9; i++) {
161 if (mMobsController.getPlayer().inventory[i] > 0) {
162 if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
163 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
164 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
165 } else {
166 spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
167 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
174 private void drawGUI() {
175 TextureRegion cursor = textureRegions.get("cursor");
176 TextureRegion hotbar = textureRegions.get("hotbar");
177 TextureRegion hotbarSelector = textureRegions.get("hotbar_selector");
179 if (mGameWorld.hasForeAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
180 mGameWorld.hasBackAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
181 mGameInput.getControlMode() == ControlMode.CURSOR || mMainConfig.isTouch()) {
182 spriter.draw(cursor, mGameInput.getCurX() * 16 - getCamX(), mGameInput.getCurY() * 16 - getCamY());
184 spriter.draw(hotbar, getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, 0);
185 for (int i = 0; i < 9; i++) {
186 if (mMobsController.getPlayer().inventory[i] > 0) {
187 if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
188 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
189 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
190 3);
191 } else {
192 spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
193 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
194 3);
198 spriter.draw(hotbarSelector,
199 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * mMobsController.getPlayer().slot,
200 -1);
203 private void drawTouchGui() {
204 for (int i = 0; i < guiMap.size; i++) {
205 Rectangle touchKey = guiMap.getValueAt(i).getRect();
206 spriter.draw(textureRegions.get(guiMap.getKeyAt(i)),
207 touchKey.x, touchKey.y, touchKey.width, touchKey.height);
209 if (mGameInput.getControlMode() == ControlMode.CURSOR) {
210 spriter.draw(textureRegions.get("shade"), 83, getHeight() - 21);
214 private void drawGamePlay(float delta) {
215 Player player = mMobsController.getPlayer();
217 drawWorld(true);
218 player.draw(spriter, player.getX() - getCamX() - player.getWidth() / 2, player.getY() - getCamY(), delta);
219 mMobsController.forEach( (mob) -> { drawMob(mob, delta); });
220 mDropController.forEach(this::drawDrop);
221 drawWorld(false);
222 drawGUI();
225 private void updateCameraPosition() {
226 Player player = mMobsController.getPlayer();
227 setCamPos(player.getX() + player.getWidth() / 2 - getWidth() / 2,
228 player.getY() + player.getHeight() / 2 - getHeight() / 2);
231 @Override
232 public void render(float delta) {
233 int fps = (int) (1 / delta);
234 updateCameraPosition();
235 mGameInput.moveCursor(this);
237 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
239 spriter.begin();
241 drawGamePlay(delta);
243 switch (mMainConfig.getGameUiWindow()) {
244 case CREATIVE_INVENTORY:
245 drawCreative();
246 break;
247 //TODO draw other ui windows
251 if (mMainConfig.isTouch()) {
252 drawTouchGui();
255 spriter.end();
257 if (mMainConfig.isShowMap()) {
258 //DRAW MAP
259 shaper.begin(ShapeRenderer.ShapeType.Filled);
260 shaper.setColor(Color.LIGHT_GRAY);
261 shaper.rect(0, 0, mGameWorld.getWidth(), 128);
262 for (int y = 128; y < 256; y++) {
263 for (int x = 0; x < getWidth(); x++) {
264 if (mGameWorld.hasForeAt(x, y) || mGameWorld.hasBackAt(x, y)) {
265 if (GameItems.isWater(mGameWorld.getForeMap(x, y))) {
266 shaper.setColor(Color.BLUE);
267 } else if (GameItems.isLava(mGameWorld.getForeMap(x, y))) {
268 shaper.setColor(Color.RED);
269 } else {
270 if (mGameWorld.hasForeAt(x, y)) {
271 shaper.setColor(Color.BLACK);
272 } else {
273 shaper.setColor(Color.DARK_GRAY);
276 shaper.rect(x, y - 128, 1, 1);
280 shaper.setColor(Color.OLIVE);
281 shaper.rect(mMobsController.getPlayer().getMapX(), mMobsController.getPlayer().getUpperMapY() - 128, 1, 2);
282 shaper.end();
283 //=================
286 if (mMainConfig.isShowInfo()) {
287 spriter.begin();
288 Player player = mMobsController.getPlayer();
289 drawString("FPS: " + fps, 0, 0);
290 drawString("X: " + player.getMapX(), 0, 10);
291 drawString("Y: " + player.getUpperMapY(), 0, 20);
292 drawString("CurX: " + mGameInput.getCurX(), 0, 30);
293 drawString("CurY: " + mGameInput.getCurY(), 0, 40);
294 drawString("Velocity: " + player.getVelocity(), 0, 50);
295 drawString("Mobs: " + mMobsController.getSize(), 0, 60);
296 drawString("Drops: " + mDropController.getSize(), 0, 70);
297 drawString("Block: " + GameItems.getBlockKey(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY())), 0, 80);
298 drawString("Hand: " + GameItems.getItemKey(mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot]), 0, 90);
299 drawString("Game mode: " + player.gameMode, 0, 100);
300 spriter.end();