DEADSOFTWARE

Refactor drop and its physics
[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.Sprite;
7 import com.badlogic.gdx.graphics.g2d.TextureRegion;
8 import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
9 import com.badlogic.gdx.math.Intersector;
10 import com.badlogic.gdx.math.Rectangle;
11 import com.badlogic.gdx.math.Vector2;
12 import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
13 import ru.deadsoftware.cavedroid.MainConfig;
14 import ru.deadsoftware.cavedroid.game.mobs.Mob;
15 import ru.deadsoftware.cavedroid.game.mobs.MobsController;
16 import ru.deadsoftware.cavedroid.game.mobs.Player;
17 import ru.deadsoftware.cavedroid.game.objects.Block;
18 import ru.deadsoftware.cavedroid.game.objects.Drop;
19 import ru.deadsoftware.cavedroid.game.objects.DropController;
20 import ru.deadsoftware.cavedroid.game.objects.Item;
21 import ru.deadsoftware.cavedroid.game.world.GameWorld;
22 import ru.deadsoftware.cavedroid.misc.ControlMode;
23 import ru.deadsoftware.cavedroid.misc.Renderer;
24 import ru.deadsoftware.cavedroid.misc.utils.SpriteUtilsKt;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
28 import javax.inject.Inject;
30 import static ru.deadsoftware.cavedroid.misc.Assets.*;
32 @GameScope
33 public class GameRenderer extends Renderer {
35 private final MainConfig mMainConfig;
36 private final GameInput mGameInput;
37 private final GameWorld mGameWorld;
38 private final MobsController mMobsController;
39 private final DropController mDropController;
41 @Inject
42 GameRenderer(MainConfig mainConfig,
43 GameInput gameInput,
44 GameWorld gameWorld,
45 MobsController mobsController,
46 DropController dropController) {
47 super(mainConfig.getWidth(), mainConfig.getHeight());
49 mMainConfig = mainConfig;
50 mGameInput = gameInput;
51 mGameWorld = gameWorld;
52 mMobsController = mobsController;
53 mDropController = dropController;
55 Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
56 }
58 private float drawX(int x) {
59 return x * 16 - getCamX();
60 }
62 private float drawY(int y) {
63 return y * 16 - getCamY();
64 }
66 private void drawWreck(int bl) {
67 if (mGameInput.getBlockDamage() > 0) {
68 int index = 10 * mGameInput.getBlockDamage() / GameItems.getBlock(bl).getHp();
69 String key = "break_" + index;
70 spriter.draw(textureRegions.get(key), mGameInput.getCurX() * 16 - getCamX(),
71 mGameInput.getCurY() * 16 - getCamY());
72 }
73 }
75 private void drawBlock(int x, int y, boolean drawBG) {
76 if (drawBG) {
77 if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
78 && mGameWorld.hasBackAt(x, y)) {
79 mGameWorld.getBackMapBlock(x, y).draw(spriter, drawX(x), drawY(y));
80 if (!mGameWorld.hasForeAt(x, y) && x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
81 drawWreck(mGameWorld.getBackMap(mGameInput.getCurX(), mGameInput.getCurY()));
82 }
83 }
84 }
85 if (mGameWorld.hasForeAt(x, y) && mGameWorld.getForeMapBlock(x, y).isBackground() == drawBG) {
86 mGameWorld.getForeMapBlock(x, y).draw(spriter, drawX(x), drawY(y));
87 if (x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
88 drawWreck(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY()));
89 }
90 }
91 }
93 private void drawWorld(boolean bg) {
94 int minX = (int) (getCamX() / 16) - 1;
95 int minY = (int) (getCamY() / 16) - 1;
96 int maxX = (int) ((getCamX() + getWidth()) / 16) + 1;
97 int maxY = (int) ((getCamY() + getHeight()) / 16) + 1;
98 if (minY < 0) {
99 minY = 0;
101 if (maxY > mGameWorld.getHeight()) {
102 maxY = mGameWorld.getHeight();
104 for (int y = minY; y < maxY; y++) {
105 for (int x = minX; x < maxX; x++) {
106 drawBlock(x, y, bg);
109 if (bg) {
110 spriter.end();
111 Gdx.gl.glEnable(GL20.GL_BLEND);
112 Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
113 shaper.begin(ShapeRenderer.ShapeType.Filled);
114 shaper.setColor(0f, 0f, 0f, .5f);
115 for (int y = minY; y < maxY; y++) {
116 for (int x = minX; x < maxX; x++) {
117 if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
118 && mGameWorld.hasBackAt(x, y)) {
119 shaper.rect(drawX(x), drawY(y), 16, 16);
123 shaper.end();
124 Gdx.gl.glDisable(GL20.GL_BLEND);
125 spriter.begin();
129 private Rectangle getShiftedRectRespectfulToViewPort(final Rectangle rect, final float shift) {
130 return new Rectangle(rect.x + shift - getCamX(), rect.y - getCamY(), rect.width, rect.height);
133 @CheckForNull
134 private Rectangle getDrawingRectIfInsideViewport(final Rectangle rectangle) {
135 final Rectangle viewportRect = new Rectangle(0, 0, getWidth(), getHeight());
137 final Rectangle notShifted = getShiftedRectRespectfulToViewPort(rectangle, 0);
138 if (Intersector.overlaps(viewportRect, notShifted)) {
139 return notShifted;
142 final Rectangle shiftedLeft = getShiftedRectRespectfulToViewPort(rectangle, -mGameWorld.getWidthPx());
143 if (Intersector.overlaps(viewportRect, shiftedLeft)) {
144 return shiftedLeft;
147 final Rectangle shiftedRight = getShiftedRectRespectfulToViewPort(rectangle, mGameWorld.getWidthPx());
148 if (Intersector.overlaps(viewportRect, shiftedRight)) {
149 return shiftedRight;
152 return null;
155 private void drawMob(Mob mob, float delta) {
156 float mobDrawX = mob.getX() - getCamX();
157 float mobDrawY = mob.getY() - getCamY();
159 if (mobDrawX + mob.getWidth() < 0 && mobDrawX + mGameWorld.getWidthPx() > 0) {
160 mobDrawX += mGameWorld.getWidthPx();
161 } else if (mobDrawX > getWidth() && mobDrawX + mob.getWidth() - mGameWorld.getWidthPx() > 0) {
162 mobDrawX -= mGameWorld.getWidthPx();
163 } else if (mobDrawX + mob.getWidth() < 0 && mobDrawX > getWidth()) {
164 return;
167 mob.draw(spriter, mobDrawX, mobDrawY, delta);
170 private void drawDrop(Drop drop) {
171 if (drop.getId() <= 0) {
172 return;
175 @CheckForNull final Rectangle drawingRect = getDrawingRectIfInsideViewport(drop);
177 if (drawingRect == null) {
178 return;
181 final Item item = GameItems.getItem(drop.getId());
182 @CheckForNull final Block block = GameItems.getBlock(GameItems.getItemKey(drop.getId()));
183 @CheckForNull final Sprite sprite = item.isBlock()
184 ? block.getTexture()
185 : item.getSprite();
187 if (sprite == null) {
188 return;
191 sprite.setPosition(drawingRect.x, drawingRect.y);
192 sprite.setSize(drawingRect.width, drawingRect.height);
193 sprite.draw(spriter);
196 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
197 private void drawCreative() {
198 TextureRegion creative = textureRegions.get("creative");
199 float x = getWidth() / 2 - (float) creative.getRegionWidth() / 2;
200 float y = getHeight() / 2 - (float) creative.getRegionHeight() / 2;
201 spriter.draw(creative, x, y);
202 spriter.draw(textureRegions.get("handle"), x + 156,
203 y + 18 + (mGameInput.getCreativeScroll() * (72f / GameProc.MAX_CREATIVE_SCROLL)));
204 for (int i = mGameInput.getCreativeScroll() * 8; i < mGameInput.getCreativeScroll() * 8 + 40; i++) {
205 if (i > 0 && i < GameItems.getItemsSize()) {
206 if (GameItems.getItem(i).isBlock()) {
207 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(i)).getTexture(),
208 x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
209 y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
210 } else {
211 spriter.draw(GameItems.getItem(i).getTexture(),
212 x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
213 y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
217 for (int i = 0; i < 9; i++) {
218 if (mMobsController.getPlayer().inventory[i] > 0) {
219 if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
220 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
221 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
222 } else {
223 spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
224 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
231 private void drawGUI() {
232 TextureRegion cursor = textureRegions.get("cursor");
233 TextureRegion hotbar = textureRegions.get("hotbar");
234 TextureRegion hotbarSelector = textureRegions.get("hotbar_selector");
236 if (mGameWorld.hasForeAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
237 mGameWorld.hasBackAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
238 mGameInput.getControlMode() == ControlMode.CURSOR || mMainConfig.isTouch()) {
239 spriter.draw(cursor, mGameInput.getCurX() * 16 - getCamX(), mGameInput.getCurY() * 16 - getCamY());
241 spriter.draw(hotbar, getWidth() / 2 - (float) hotbar.getRegionWidth() / 2, 0);
242 for (int i = 0; i < 9; i++) {
243 if (mMobsController.getPlayer().inventory[i] > 0) {
244 if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
245 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
246 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
247 3);
248 } else {
249 spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
250 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
251 3);
255 spriter.draw(hotbarSelector,
256 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * mMobsController.getPlayer().slot,
257 -1);
260 private void drawTouchGui() {
261 for (int i = 0; i < guiMap.size; i++) {
262 Rectangle touchKey = guiMap.getValueAt(i).getRect();
263 spriter.draw(textureRegions.get(guiMap.getKeyAt(i)),
264 touchKey.x, touchKey.y, touchKey.width, touchKey.height);
266 if (mGameInput.getControlMode() == ControlMode.CURSOR) {
267 spriter.draw(textureRegions.get("shade"), 83, getHeight() - 21);
271 private void drawGamePlay(float delta) {
272 Player player = mMobsController.getPlayer();
274 drawWorld(true);
275 player.draw(spriter, player.getX() - getCamX() - player.getWidth() / 2, player.getY() - getCamY(), delta);
276 mMobsController.forEach( (mob) -> { drawMob(mob, delta); });
277 mDropController.forEach(this::drawDrop);
278 drawWorld(false);
279 drawGUI();
282 private void updateCameraPosition() {
283 Player player = mMobsController.getPlayer();
284 setCamPos(player.getX() + player.getWidth() / 2 - getWidth() / 2,
285 player.getY() + player.getHeight() / 2 - getHeight() / 2);
288 @Nullable
289 private Color getMinimapColor(int x, int y) {
290 @Nullable Color result = null;
292 final boolean hasForeMap = mGameWorld.hasForeAt(x, y);
293 final boolean hasBackMap = mGameWorld.hasBackAt(x, y);
295 if (hasForeMap) {
296 final Block block = mGameWorld.getForeMapBlock(x, y);
298 if (GameItems.isWater(block)) {
299 result = Color.BLUE;
300 } else if (GameItems.isLava(block)) {
301 result = Color.RED;
302 } else {
303 result = Color.BLACK;
305 } else if (hasBackMap) {
306 result = Color.DARK_GRAY;
309 return result;
312 private void drawMiniMap(float miniMapX, float miniMapY, float size) {
313 shaper.begin(ShapeRenderer.ShapeType.Filled);
315 shaper.setColor(Color.LIGHT_GRAY);
316 shaper.rect(miniMapX, miniMapY, size, size);
318 for (int x = 0; x < size; x++) {
319 for (int y = 0; y < size; y++) {
321 final int worldX = (int) (mMobsController.getPlayer().getMapX() - size / 2 + x);
322 final int worldY = (int) (mMobsController.getPlayer().getUpperMapY() - size / 2 + y);
324 @Nullable
325 final Color color = getMinimapColor(worldX, worldY);
327 if (color != null) {
328 shaper.setColor(color);
329 shaper.rect(miniMapX + x, miniMapY + y, 1, 1);
334 shaper.setColor(Color.OLIVE);
335 shaper.rect(miniMapX + size / 2, miniMapY + size / 2, 1, 2);
336 shaper.end();
339 @Override
340 public void render(float delta) {
341 int fps = (int) (1 / delta);
342 updateCameraPosition();
343 mGameInput.moveCursor(this);
345 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
347 spriter.begin();
349 drawGamePlay(delta);
351 switch (mMainConfig.getGameUiWindow()) {
352 case CREATIVE_INVENTORY:
353 drawCreative();
354 break;
355 //TODO draw other ui windows
359 if (mMainConfig.isTouch()) {
360 drawTouchGui();
363 spriter.end();
365 if (mMainConfig.isShowMap()) {
366 drawMiniMap(getWidth() - 64f - 24f, 24f, 64f);
369 if (mMainConfig.isShowInfo()) {
370 spriter.begin();
371 Player player = mMobsController.getPlayer();
372 drawString("FPS: " + fps, 0, 0);
373 drawString("X: " + player.getMapX(), 0, 10);
374 drawString("Y: " + player.getUpperMapY(), 0, 20);
375 drawString("CurX: " + mGameInput.getCurX(), 0, 30);
376 drawString("CurY: " + mGameInput.getCurY(), 0, 40);
377 drawString("Velocity: " + player.getVelocity(), 0, 50);
378 drawString("Swim: " + player.swim, 0, 60);
379 drawString("Mobs: " + mMobsController.getSize(), 0, 70);
380 drawString("Drops: " + mDropController.getSize(), 0, 80);
381 drawString("Block: " + GameItems.getBlockKey(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY())), 0, 90);
382 drawString("Hand: " + GameItems.getItemKey(mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot]), 0, 100);
383 drawString("Game mode: " + player.gameMode, 0, 110);
384 drawString("Check swim: " + GameItems.isFluid(mGameWorld.getForeMap(player.getMapX(), player.getLowerMapY())), 0, 120);
385 spriter.end();