DEADSOFTWARE

Mobs controller to kotlin
[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 static final String TAG = "GameRenderer";
37 private final MainConfig mMainConfig;
38 private final GameInput mGameInput;
39 private final GameWorld mGameWorld;
40 private final MobsController mMobsController;
41 private final DropController mDropController;
43 @Inject
44 GameRenderer(MainConfig mainConfig,
45 GameInput gameInput,
46 GameWorld gameWorld,
47 MobsController mobsController,
48 DropController dropController) {
49 super(mainConfig.getWidth(), mainConfig.getHeight());
51 mMainConfig = mainConfig;
52 mGameInput = gameInput;
53 mGameWorld = gameWorld;
54 mMobsController = mobsController;
55 mDropController = dropController;
57 Gdx.gl.glClearColor(0f, .6f, .6f, 1f);
58 }
60 private float drawX(int x) {
61 return x * 16 - getCamX();
62 }
64 private float drawY(int y) {
65 return y * 16 - getCamY();
66 }
68 private void drawWreck(int bl) {
69 if (mGameInput.getBlockDamage() > 0) {
70 int index = 10 * mGameInput.getBlockDamage() / GameItems.getBlock(bl).getHp();
71 String key = "break_" + index;
73 if (index > 10 || index < 0) {
74 return;
75 }
77 spriter.draw(textureRegions.get(key), mGameInput.getCurX() * 16 - getCamX(),
78 mGameInput.getCurY() * 16 - getCamY());
79 }
80 }
82 private void drawBlock(int x, int y, boolean drawBG) {
83 if (drawBG) {
84 if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
85 && mGameWorld.hasBackAt(x, y)) {
86 mGameWorld.getBackMapBlock(x, y).draw(spriter, drawX(x), drawY(y));
87 if (!mGameWorld.hasForeAt(x, y) && x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
88 drawWreck(mGameWorld.getBackMap(mGameInput.getCurX(), mGameInput.getCurY()));
89 }
90 }
91 }
92 if (mGameWorld.hasForeAt(x, y) && mGameWorld.getForeMapBlock(x, y).isBackground() == drawBG) {
93 mGameWorld.getForeMapBlock(x, y).draw(spriter, drawX(x), drawY(y));
94 if (x == mGameInput.getCurX() && y == mGameInput.getCurY()) {
95 drawWreck(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY()));
96 }
97 }
98 }
100 private void drawWorld(boolean bg) {
101 int minX = (int) (getCamX() / 16) - 1;
102 int minY = (int) (getCamY() / 16) - 1;
103 int maxX = (int) ((getCamX() + getWidth()) / 16) + 1;
104 int maxY = (int) ((getCamY() + getHeight()) / 16) + 1;
105 if (minY < 0) {
106 minY = 0;
108 if (maxY > mGameWorld.getHeight()) {
109 maxY = mGameWorld.getHeight();
111 for (int y = minY; y < maxY; y++) {
112 for (int x = minX; x < maxX; x++) {
113 drawBlock(x, y, bg);
116 if (bg) {
117 spriter.end();
118 Gdx.gl.glEnable(GL20.GL_BLEND);
119 Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
120 shaper.begin(ShapeRenderer.ShapeType.Filled);
121 shaper.setColor(0f, 0f, 0f, .5f);
122 for (int y = minY; y < maxY; y++) {
123 for (int x = minX; x < maxX; x++) {
124 if ((!mGameWorld.hasForeAt(x, y) || mGameWorld.getForeMapBlock(x, y).isTransparent())
125 && mGameWorld.hasBackAt(x, y)) {
126 shaper.rect(drawX(x), drawY(y), 16, 16);
130 shaper.end();
131 Gdx.gl.glDisable(GL20.GL_BLEND);
132 spriter.begin();
136 private Rectangle getShiftedRectRespectfulToViewPort(final Rectangle rect, final float shift) {
137 return new Rectangle(rect.x + shift - getCamX(), rect.y - getCamY(), rect.width, rect.height);
140 @CheckForNull
141 private Rectangle getDrawingRectIfInsideViewport(final Rectangle rectangle) {
142 final Rectangle viewportRect = new Rectangle(0, 0, getWidth(), getHeight());
144 final Rectangle notShifted = getShiftedRectRespectfulToViewPort(rectangle, 0);
145 if (Intersector.overlaps(viewportRect, notShifted)) {
146 return notShifted;
149 final Rectangle shiftedLeft = getShiftedRectRespectfulToViewPort(rectangle, -mGameWorld.getWidthPx());
150 if (Intersector.overlaps(viewportRect, shiftedLeft)) {
151 return shiftedLeft;
154 final Rectangle shiftedRight = getShiftedRectRespectfulToViewPort(rectangle, mGameWorld.getWidthPx());
155 if (Intersector.overlaps(viewportRect, shiftedRight)) {
156 return shiftedRight;
159 return null;
162 private void drawMob(Mob mob, float delta) {
163 float mobDrawX = mob.getX() - getCamX();
164 float mobDrawY = mob.getY() - getCamY();
166 if (mobDrawX + mob.getWidth() < 0 && mobDrawX + mGameWorld.getWidthPx() > 0) {
167 mobDrawX += mGameWorld.getWidthPx();
168 } else if (mobDrawX > getWidth() && mobDrawX + mob.getWidth() - mGameWorld.getWidthPx() > 0) {
169 mobDrawX -= mGameWorld.getWidthPx();
170 } else if (mobDrawX + mob.getWidth() < 0 && mobDrawX > getWidth()) {
171 return;
174 mob.draw(spriter, mobDrawX, mobDrawY, delta);
177 private void drawDrop(Drop drop) {
178 if (drop.getId() <= 0) {
179 return;
182 @CheckForNull final Rectangle drawingRect = getDrawingRectIfInsideViewport(drop);
184 if (drawingRect == null) {
185 return;
188 final Item item = GameItems.getItem(drop.getId());
189 @CheckForNull final Block block = GameItems.getBlock(GameItems.getItemKey(drop.getId()));
190 @CheckForNull final Sprite sprite = item.isBlock()
191 ? block.getTexture()
192 : item.getSprite();
194 if (sprite == null) {
195 return;
198 sprite.setPosition(drawingRect.x, drawingRect.y);
199 sprite.setSize(drawingRect.width, drawingRect.height);
200 sprite.draw(spriter);
203 @SuppressWarnings("IntegerDivisionInFloatingPointContext")
204 private void drawCreative() {
205 TextureRegion creative = textureRegions.get("creative");
206 float x = getWidth() / 2 - (float) creative.getRegionWidth() / 2;
207 float y = getHeight() / 2 - (float) creative.getRegionHeight() / 2;
208 spriter.draw(creative, x, y);
209 spriter.draw(textureRegions.get("handle"), x + 156,
210 y + 18 + (mGameInput.getCreativeScroll() * (72f / GameProc.MAX_CREATIVE_SCROLL)));
211 for (int i = mGameInput.getCreativeScroll() * 8; i < mGameInput.getCreativeScroll() * 8 + 40; i++) {
212 if (i > 0 && i < GameItems.getItemsSize()) {
213 if (GameItems.getItem(i).isBlock()) {
214 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(i)).getTexture(),
215 x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
216 y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
217 } else {
218 spriter.draw(GameItems.getItem(i).getTexture(),
219 x + 8 + ((i - mGameInput.getCreativeScroll() * 8) % 8) * 18,
220 y + 18 + ((i - mGameInput.getCreativeScroll() * 8) / 8) * 18);
224 for (int i = 0; i < 9; i++) {
225 if (mMobsController.getPlayer().inventory[i] > 0) {
226 if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
227 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
228 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
229 } else {
230 spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
231 x + 8 + i * 18, y + creative.getRegionHeight() - 24);
238 private void drawHealth(float x, float y) {
239 Player player = mMobsController.getPlayer();
241 if (player.gameMode == 1) {
242 return;
245 TextureRegion wholeHeart = textureRegions.get("heart_whole");
246 TextureRegion halfHeart = textureRegions.get("heart_half");
248 int wholeHearts = player.getHealth() / 2;
250 for (int i = 0; i < wholeHearts; i++) {
251 spriter.draw(wholeHeart, x + i * wholeHeart.getRegionWidth(), y);
254 if (player.getHealth() % 2 == 1) {
255 spriter.draw(halfHeart, x + wholeHearts * wholeHeart.getRegionWidth(), y);
259 private void drawGUI() {
260 TextureRegion cursor = textureRegions.get("cursor");
261 TextureRegion hotbar = textureRegions.get("hotbar");
262 TextureRegion hotbarSelector = textureRegions.get("hotbar_selector");
264 if (mGameWorld.hasForeAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
265 mGameWorld.hasBackAt(mGameInput.getCurX(), mGameInput.getCurY()) ||
266 mGameInput.getControlMode() == ControlMode.CURSOR || mMainConfig.isTouch()) {
267 spriter.draw(cursor, mGameInput.getCurX() * 16 - getCamX(), mGameInput.getCurY() * 16 - getCamY());
270 float hotbarX = getWidth() / 2 - (float) hotbar.getRegionWidth() / 2;
271 spriter.draw(hotbar, hotbarX, 0);
272 drawHealth(hotbarX, hotbar.getRegionHeight());
274 for (int i = 0; i < 9; i++) {
275 if (mMobsController.getPlayer().inventory[i] > 0) {
276 if (GameItems.getItem(mMobsController.getPlayer().inventory[i]).isBlock()) {
277 spriter.draw(GameItems.getBlock(GameItems.getBlockIdByItemId(mMobsController.getPlayer().inventory[i])).getTexture(),
278 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
279 3);
280 } else {
281 spriter.draw(GameItems.getItem(mMobsController.getPlayer().inventory[i]).getTexture(),
282 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 + 3 + i * 20,
283 3);
287 spriter.draw(hotbarSelector,
288 getWidth() / 2 - (float) hotbar.getRegionWidth() / 2 - 1 + 20 * mMobsController.getPlayer().slot,
289 -1);
292 private void drawTouchGui() {
293 for (int i = 0; i < guiMap.size; i++) {
294 Rectangle touchKey = guiMap.getValueAt(i).getRect();
295 spriter.draw(textureRegions.get(guiMap.getKeyAt(i)),
296 touchKey.x, touchKey.y, touchKey.width, touchKey.height);
298 if (mGameInput.getControlMode() == ControlMode.CURSOR) {
299 spriter.draw(textureRegions.get("shade"), 83, getHeight() - 21);
303 private void drawGamePlay(float delta) {
304 Player player = mMobsController.getPlayer();
306 drawWorld(true);
307 player.draw(spriter, player.getX() - getCamX() - player.getWidth() / 2, player.getY() - getCamY(), delta);
308 mMobsController.getMobs().forEach( (mob) -> { drawMob(mob, delta); });
309 mDropController.forEach(this::drawDrop);
310 drawWorld(false);
311 drawGUI();
314 private void updateCameraPosition() {
315 Player player = mMobsController.getPlayer();
316 setCamPos(player.getX() + player.getWidth() / 2 - getWidth() / 2,
317 player.getY() + player.getHeight() / 2 - getHeight() / 2);
320 @Nullable
321 private Color getMinimapColor(int x, int y) {
322 @Nullable Color result = null;
324 final boolean hasForeMap = mGameWorld.hasForeAt(x, y);
325 final boolean hasBackMap = mGameWorld.hasBackAt(x, y);
327 if (hasForeMap) {
328 final Block block = mGameWorld.getForeMapBlock(x, y);
330 if (GameItems.isWater(block)) {
331 result = Color.BLUE;
332 } else if (GameItems.isLava(block)) {
333 result = Color.RED;
334 } else {
335 result = Color.BLACK;
337 } else if (hasBackMap) {
338 result = Color.DARK_GRAY;
341 return result;
344 private void drawMiniMap(float miniMapX, float miniMapY, float size) {
345 shaper.begin(ShapeRenderer.ShapeType.Filled);
347 shaper.setColor(Color.LIGHT_GRAY);
348 shaper.rect(miniMapX, miniMapY, size, size);
350 for (int x = 0; x < size; x++) {
351 for (int y = 0; y < size; y++) {
353 final int worldX = (int) (mMobsController.getPlayer().getMapX() - size / 2 + x);
354 final int worldY = (int) (mMobsController.getPlayer().getUpperMapY() - size / 2 + y);
356 @Nullable
357 final Color color = getMinimapColor(worldX, worldY);
359 if (color != null) {
360 shaper.setColor(color);
361 shaper.rect(miniMapX + x, miniMapY + y, 1, 1);
366 shaper.setColor(Color.OLIVE);
367 shaper.rect(miniMapX + size / 2, miniMapY + size / 2, 1, 2);
368 shaper.end();
371 @Override
372 public void render(float delta) {
373 int fps = (int) (1 / delta);
374 updateCameraPosition();
375 mGameInput.moveCursor(this);
377 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
379 spriter.begin();
381 drawGamePlay(delta);
383 switch (mMainConfig.getGameUiWindow()) {
384 case CREATIVE_INVENTORY:
385 drawCreative();
386 break;
387 //TODO draw other ui windows
391 if (mMainConfig.isTouch()) {
392 drawTouchGui();
395 spriter.end();
397 if (mMainConfig.isShowMap()) {
398 drawMiniMap(getWidth() - 64f - 24f, 24f, 64f);
401 if (mMainConfig.isShowInfo()) {
402 spriter.begin();
403 Player player = mMobsController.getPlayer();
404 drawString("FPS: " + fps, 0, 0);
405 drawString("X: " + player.getMapX(), 0, 10);
406 drawString("Y: " + player.getUpperMapY(), 0, 20);
407 drawString("CurX: " + mGameInput.getCurX(), 0, 30);
408 drawString("CurY: " + mGameInput.getCurY(), 0, 40);
409 drawString("Velocity: " + player.getVelocity(), 0, 50);
410 drawString("Swim: " + player.swim, 0, 60);
411 drawString("Mobs: " + mMobsController.getMobs().size(), 0, 70);
412 drawString("Drops: " + mDropController.getSize(), 0, 80);
413 drawString("Block: " + GameItems.getBlockKey(mGameWorld.getForeMap(mGameInput.getCurX(), mGameInput.getCurY())), 0, 90);
414 drawString("Hand: " + GameItems.getItemKey(mMobsController.getPlayer().inventory[mMobsController.getPlayer().slot]), 0, 100);
415 drawString("Game mode: " + player.gameMode, 0, 110);
416 drawString("Check swim: " + GameItems.isFluid(mGameWorld.getForeMap(player.getMapX(), player.getLowerMapY())), 0, 120);
417 spriter.end();