LevelRenderer.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package pathgame.rendering;
  2. import me.hammerle.snuviengine.api.Renderer;
  3. import pathgame.gameplay.Camera;
  4. import pathgame.gameplay.Gamestate;
  5. import pathgame.gameplay.Gamestates;
  6. import pathgame.gameplay.Level;
  7. import pathgame.gameplay.Player;
  8. import pathgame.tilemap.TileMap;
  9. public class LevelRenderer
  10. {
  11. private final PlayerRenderer playerRenderer = new PlayerRenderer();
  12. private final TileMapRenderer mapRenderer = new TileMapRenderer();
  13. private final HUDRenderer hudRenderer = new HUDRenderer();
  14. private final Camera cam = new Camera();
  15. private final ScoreMenuRenderer scoreRenderer = new ScoreMenuRenderer();
  16. public void tick(Level level, Gamestate gamestate)
  17. {
  18. cam.tick(level, gamestate);
  19. mapRenderer.tick();
  20. if(level.getPlayer().isMoving())
  21. {
  22. cam.reset();
  23. }
  24. }
  25. public void renderTick(Renderer r, float lag, Level level, Gamestate gamestate)
  26. {
  27. TileMap map = level.getMap();
  28. Player player = level.getPlayer();
  29. float zoomRestrictionX = r.getViewWidth() / (map.getWidth() * TileRenderer.TILE_SIZE);
  30. float zoomRestrictionY = (r.getViewHeight() - HUDRenderer.OFFSET_Y) / (map.getHeight() * TileRenderer.TILE_SIZE);
  31. cam.limitScale(Math.max(zoomRestrictionX, zoomRestrictionY));
  32. float interScale = cam.getInterpolatedScale(lag);
  33. mapRenderer.setScale(interScale);
  34. float offX = getMapOffsetX(map, player, r, lag, interScale);
  35. float offY = getMapOffsetY(map, player, r, lag, interScale) + HUDRenderer.OFFSET_Y;
  36. mapRenderer.renderTick(map, r, false, offX, offY);
  37. if(gamestate.is(Gamestates.GAMEPLAY))
  38. {
  39. playerRenderer.renderTick(map, mapRenderer, r, player, lag, offX, offY);
  40. if(!level.getShowScoreMenu())
  41. {
  42. hudRenderer.renderTick(r, player, lag);
  43. }
  44. else if(level.getShowScoreMenu())
  45. {
  46. scoreRenderer.renderTick(r, lag, level);
  47. }
  48. }
  49. }
  50. private float getMapOffsetX(TileMap map, Player player, Renderer r, float lag, float interScale)
  51. {
  52. float ix = (player.getLastX() + (player.getX() - player.getLastX()) * lag) * interScale * TileRenderer.TILE_SIZE;
  53. float offX = (-ix + r.getViewWidth() * 0.5f) - TileRenderer.TILE_SIZE * 0.5f * interScale;
  54. float minOffX = -mapRenderer.getWidth(map) + r.getViewWidth();
  55. if(offX < minOffX)
  56. {
  57. return cam.getCamOffsetX(minOffX, minOffX, lag, interScale);
  58. }
  59. else if(offX > 0.0f)
  60. {
  61. return cam.getCamOffsetX(0.0f, minOffX, lag, interScale);
  62. }
  63. return cam.getCamOffsetX(offX, minOffX, lag, interScale);
  64. }
  65. private float getMapOffsetY(TileMap map, Player player, Renderer r, float lag, float interScale)
  66. {
  67. float viewHeight = r.getViewHeight() - HUDRenderer.OFFSET_Y;
  68. float iy = (player.getLastY() + (player.getY() - player.getLastY()) * lag) * interScale * TileRenderer.TILE_SIZE;
  69. float offY = (-iy + viewHeight * 0.5f) - TileRenderer.TILE_SIZE * 0.5f * interScale;
  70. float minOffY = -mapRenderer.getHeight(map) + viewHeight;
  71. if(offY < minOffY)
  72. {
  73. return cam.getCamOffsetY(minOffY, minOffY, lag, interScale);
  74. }
  75. else if(offY > 0.0f)
  76. {
  77. return cam.getCamOffsetY(0.0f, minOffY, lag, interScale);
  78. }
  79. return cam.getCamOffsetY(offY, minOffY, lag, interScale);
  80. }
  81. }