PlayerRenderer.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package pathgame.rendering;
  2. import me.hammerle.snuviengine.api.Renderer;
  3. import me.hammerle.snuviengine.api.Texture;
  4. import pathgame.gameplay.Player;
  5. import pathgame.tilemap.TileMap;
  6. import pathgame.tilemap.Tiles;
  7. /**
  8. * A container for holding everything about the renderer for the player
  9. *
  10. * @author julia
  11. */
  12. public class PlayerRenderer
  13. {
  14. private static final Texture CHARACTER = new Texture("resources/character.png");
  15. /**
  16. * Recalculates the rendering positions and settings of the menu-elements on
  17. * the screen every rendertick based on the gamelogic in the gametick
  18. *
  19. * @param map the current map
  20. * @param mapR the map renderer
  21. * @param r the renderer
  22. * @param p the current player
  23. * @param lag the current lag
  24. * @param offX the current x-offset of the map
  25. * @param offY the current y-offset of the map
  26. */
  27. public void renderTick(TileMap map, TileMapRenderer mapR, Renderer r, Player p, float lag, float offX, float offY)
  28. {
  29. float playerSize = mapR.getScale() * TileRenderer.TILE_SIZE;
  30. r.setMixColorEnabled(false);
  31. r.setColorEnabled(false);
  32. r.setTextureEnabled(true);
  33. r.translateTo(0.0f, 0.0f);
  34. r.updateMatrix();
  35. float yIndex;
  36. float baseX = (p.getLastX() + (p.getX() - p.getLastX()) * lag);
  37. float ix = baseX * mapR.getScale() * TileRenderer.TILE_SIZE + offX;
  38. float baseY = (p.getLastY() + (p.getY() - p.getLastY()) * lag);
  39. float iy = baseY * mapR.getScale() * TileRenderer.TILE_SIZE + offY;
  40. baseX = baseX - (int) baseX;
  41. baseY = baseY - (int) baseY;
  42. int tIndex = 0;
  43. float xTexOff = 0;
  44. float yTexOff = 0;
  45. CHARACTER.bind();
  46. if(p.getVelX() > 0)
  47. {
  48. //go right
  49. yIndex = 2;
  50. tIndex = checkForAnimationIndex(baseX);
  51. }
  52. else if(p.getVelX() < 0)
  53. {
  54. //go left
  55. yIndex = 1;
  56. tIndex = checkForAnimationIndex(baseX);
  57. }
  58. else if(p.getVelY() > 0)
  59. {
  60. //go down
  61. yIndex = 0;
  62. tIndex = checkForAnimationIndex(baseY);
  63. }
  64. else if(p.getVelY() < 0)
  65. {
  66. //go up
  67. yIndex = 3;
  68. tIndex = checkForAnimationIndex(baseY);
  69. }
  70. else
  71. {
  72. //stand still
  73. yIndex = 0;
  74. }
  75. if(p.isSailing())
  76. {
  77. yTexOff = 0.5f;
  78. }
  79. else if(p.getCurrTile() == Tiles.SHALLOW_WATER)
  80. {
  81. xTexOff = 0.5f;
  82. }
  83. float viewScale = r.getViewScale();
  84. ix = (int) (ix * viewScale) / viewScale;
  85. iy = (int) (iy * viewScale) / viewScale;
  86. r.getTextureRenderer().drawRectangle(ix, iy, ix + playerSize, iy + playerSize,
  87. tIndex * 0.125f + xTexOff, yIndex * 0.125f + yTexOff,
  88. (tIndex + 1) * 0.125f + xTexOff, yIndex * 0.125f + 0.125f + yTexOff);
  89. }
  90. private int checkForAnimationIndex(float base)
  91. {
  92. return ((int) (base * 4 * 2)) % 4;
  93. }
  94. }