PlatformController.java 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package me.hammerle.supersnuvi.entity.components.ai;
  2. import java.util.ArrayList;
  3. import me.hammerle.snuviengine.api.Shader;
  4. import me.hammerle.snuviengine.api.Texture;
  5. import me.hammerle.supersnuvi.entity.Entity;
  6. import me.hammerle.supersnuvi.gamelogic.Level;
  7. import me.hammerle.supersnuvi.tiles.Tile;
  8. import me.hammerle.supersnuvi.util.Face;
  9. import me.hammerle.supersnuvi.util.Utils;
  10. public class PlatformController extends Controller
  11. {
  12. private final static Texture TEXTURE = new Texture("resources/platform.png");
  13. private static class MoveData
  14. {
  15. private final float x;
  16. private final float y;
  17. private final float speedX;
  18. private final float speedY;
  19. private final int waitTicks;
  20. public MoveData(float x, float y, float speedX, float speedY, int waitTicks)
  21. {
  22. this.x = x;
  23. this.y = y;
  24. this.speedX = Math.abs(speedX);
  25. this.speedY = Math.abs(speedY);
  26. this.waitTicks = waitTicks;
  27. }
  28. }
  29. private final ArrayList<MoveData> dataList = new ArrayList<>();
  30. private int moveIndex = 0;
  31. private int waitTicks = 0;
  32. @Override
  33. public boolean isAnimated()
  34. {
  35. return true;
  36. }
  37. public void addMoveData(float x, float y, float speedX, float speedY, int waitTicks)
  38. {
  39. dataList.add(new MoveData(x, y, speedX, speedY, waitTicks));
  40. }
  41. public void clearMoveData()
  42. {
  43. dataList.clear();
  44. }
  45. private float clamp(float f, float min, float max)
  46. {
  47. if(f < min)
  48. {
  49. return min;
  50. }
  51. else if(f > max)
  52. {
  53. return max;
  54. }
  55. return f;
  56. }
  57. @Override
  58. public void tick(Entity ent, Level level)
  59. {
  60. if(dataList.isEmpty())
  61. {
  62. return;
  63. }
  64. MoveData data = dataList.get(moveIndex);
  65. if(ent.isAt(data.x, data.y))
  66. {
  67. waitTicks++;
  68. if(waitTicks >= data.waitTicks)
  69. {
  70. waitTicks = 0;
  71. moveIndex = (moveIndex + 1) % dataList.size();
  72. }
  73. }
  74. else
  75. {
  76. float motionX = clamp(data.x - ent.getX(), -data.speedX, data.speedX);
  77. float motionY = clamp(data.y - ent.getY(), -data.speedY, data.speedY);
  78. ent.applyOwnForce(motionX, motionY);
  79. }
  80. }
  81. @Override
  82. public void renderTick(Entity ent, float lag)
  83. {
  84. TEXTURE.bind();
  85. float x = Utils.interpolate(ent.getLastX(), ent.getX(), lag);
  86. float y = Utils.interpolate(ent.getLastY(), ent.getY(), lag);
  87. int mid = (int) Math.ceil((ent.getWidth() / Tile.SIZE) - 2);
  88. float endY = y + ent.getHeight();
  89. Shader.getTextureRenderer().drawRectangle(x, y, x + Tile.SIZE, endY, 0.0f, 0.25f, 0.25f, 0.4453125f);
  90. x += Tile.SIZE;
  91. for(int i = 0; i < mid; i++)
  92. {
  93. Shader.getTextureRenderer().drawRectangle(x, y, x + Tile.SIZE, endY, 0.25f, 0.25f, 0.5f, 0.4453125f);
  94. x += Tile.SIZE;
  95. }
  96. Shader.getTextureRenderer().drawRectangle(x, y, x + Tile.SIZE, endY, 0.5f, 0.25f, 0.75f, 0.4453125f);
  97. }
  98. @Override
  99. public void onCollideWithEntity(Entity ent, Entity other, Face face)
  100. {
  101. if(face == Face.UP)
  102. {
  103. other.applyForce(ent.getOwnForceX(), 0.0f);
  104. }
  105. else if(face == Face.LEFT || face == Face.RIGHT)
  106. {
  107. other.applyForce(ent.getOwnForceX() - other.getOwnForceX(), 0.0f);
  108. }
  109. }
  110. }