PlayerRenderer.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package pathgame.rendering;
  2. import me.hammerle.snuviengine.api.Renderer;
  3. import me.hammerle.snuviengine.api.Texture;
  4. import pathgame.gameplay.Facing;
  5. import pathgame.gameplay.Player;
  6. import pathgame.gameplay.PlayerAbilities;
  7. import pathgame.tilemap.TileMap;
  8. import pathgame.tilemap.TileRenderType;
  9. /**
  10. * A container for holding everything about the renderer for the player
  11. *
  12. * @author julia
  13. */
  14. public class PlayerRenderer
  15. {
  16. private static final Texture CHARACTER = new Texture("resources/character.png");
  17. private static final Texture CHARACTER_HIKER = new Texture("resources/hiker.png");
  18. private static final Texture CHARACTER_HUNTER = new Texture("resources/hunter.png");
  19. private static final Texture CHARACTER_SAILOR = new Texture("resources/sailor.png");
  20. private static final Texture CHARACTER_SWIMMER = new Texture("resources/swimmer.png");
  21. /**
  22. * Recalculates the rendering positions and settings of the menu-elements on
  23. * the screen every rendertick based on the gamelogic in the gametick
  24. *
  25. * @param map the current map
  26. * @param mapR the map renderer
  27. * @param r the renderer
  28. * @param p the current player
  29. * @param lag the current lag
  30. * @param offX the current x-offset of the map
  31. * @param offY the current y-offset of the map
  32. */
  33. public void renderTick(TileMap map, TileMapRenderer mapR, Renderer r, Player p, float lag, float offX, float offY)
  34. {
  35. boolean inWater = p.getCurrTile().getRenderType() == TileRenderType.WATER;
  36. float playerSize = mapR.getScale() * TileRenderer.TILE_SIZE;
  37. r.setMixColorEnabled(false);
  38. r.setColorEnabled(false);
  39. r.setTextureEnabled(true);
  40. r.translateTo(0.0f, 0.0f);
  41. r.updateMatrix();
  42. float yIndex;
  43. float baseX = (p.getLastX() + (p.getX() - p.getLastX()) * lag);
  44. float ix = baseX * mapR.getScale() * TileRenderer.TILE_SIZE + offX;
  45. float baseY = (p.getLastY() + (p.getY() - p.getLastY()) * lag);
  46. float iy = baseY * mapR.getScale() * TileRenderer.TILE_SIZE + offY;
  47. baseX = baseX - (int) baseX;
  48. baseY = baseY - (int) baseY;
  49. int tIndex = 0;
  50. float xTexOff = 0;
  51. float yTexOff = 0;
  52. bindTexture(p, inWater);
  53. if(p.getVelX() > 0)
  54. {
  55. //go right
  56. yIndex = 2;
  57. tIndex = checkForAnimationIndex(baseX);
  58. }
  59. else if(p.getVelX() < 0)
  60. {
  61. //go left
  62. yIndex = 1;
  63. tIndex = checkForAnimationIndex(baseX);
  64. }
  65. else if(p.getVelY() > 0)
  66. {
  67. //go down
  68. yIndex = 0;
  69. tIndex = checkForAnimationIndex(baseY);
  70. }
  71. else if(p.getVelY() < 0)
  72. {
  73. //go up
  74. yIndex = 3;
  75. tIndex = checkForAnimationIndex(baseY);
  76. }
  77. else
  78. {
  79. //stand still
  80. yIndex = p.getFacing().getIndex();
  81. // idle animation on water
  82. if(inWater && (p.getTicksLived() % 20) < 10)
  83. {
  84. tIndex = 1;
  85. }
  86. }
  87. if(p.isSailing())
  88. {
  89. yTexOff = 0.5f;
  90. }
  91. else if(inWater)
  92. {
  93. xTexOff = 0.5f;
  94. }
  95. float viewScale = r.getViewScale();
  96. ix = (int) (ix * viewScale) / viewScale;
  97. iy = (int) (iy * viewScale) / viewScale;
  98. r.getTextureRenderer().drawRectangle(ix, iy, ix + playerSize, iy + playerSize,
  99. tIndex * 0.125f + xTexOff, yIndex * 0.125f + yTexOff,
  100. (tIndex + 1) * 0.125f + xTexOff, yIndex * 0.125f + 0.125f + yTexOff);
  101. }
  102. private int checkForAnimationIndex(float base)
  103. {
  104. return ((int) (base * 4 * 2)) % 4;
  105. }
  106. private void bindTexture(Player p, boolean inWater)
  107. {
  108. if(p.isSailing() || inWater || p.getAbilities() == PlayerAbilities.NORMAL)
  109. {
  110. CHARACTER.bind();
  111. }
  112. else if(p.getAbilities() == PlayerAbilities.HIKER)
  113. {
  114. CHARACTER_HIKER.bind();
  115. }
  116. else if(p.getAbilities() == PlayerAbilities.HUNTER)
  117. {
  118. CHARACTER_HUNTER.bind();
  119. }
  120. else if(p.getAbilities() == PlayerAbilities.SAILOR)
  121. {
  122. CHARACTER_SAILOR.bind();
  123. }
  124. else if(p.getAbilities() == PlayerAbilities.SWIMMER)
  125. {
  126. CHARACTER_SWIMMER.bind();
  127. }
  128. else
  129. {
  130. CHARACTER.bind();
  131. }
  132. }
  133. }