PlatformController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. private int samePosX = 0;
  33. private int samePosY = 0;
  34. @Override
  35. public boolean isAnimated()
  36. {
  37. return true;
  38. }
  39. public void addMoveData(float x, float y, float speedX, float speedY, int waitTicks)
  40. {
  41. dataList.add(new MoveData(x, y, speedX, speedY, waitTicks));
  42. }
  43. public void clearMoveData()
  44. {
  45. dataList.clear();
  46. }
  47. private float clamp(float f, float min, float max)
  48. {
  49. if(f < min)
  50. {
  51. return min;
  52. }
  53. else if(f > max)
  54. {
  55. return max;
  56. }
  57. return f;
  58. }
  59. @Override
  60. public void tick(Entity ent, Level level)
  61. {
  62. if(dataList.isEmpty())
  63. {
  64. return;
  65. }
  66. MoveData data = dataList.get(moveIndex);
  67. if(ent.isAt(data.x, data.y))
  68. {
  69. waitTicks++;
  70. if(waitTicks >= data.waitTicks)
  71. {
  72. waitTicks = 0;
  73. moveIndex = (moveIndex + 1) % dataList.size();
  74. }
  75. }
  76. else
  77. {
  78. float motionX = clamp(data.x - ent.getX(), -data.speedX, data.speedX);
  79. float motionY = clamp(data.y - ent.getY(), -data.speedY, data.speedY);
  80. ent.applyOwnForce(motionX, motionY);
  81. }
  82. }
  83. @Override
  84. public void renderTick(Entity ent, float lag)
  85. {
  86. TEXTURE.bind();
  87. float x = Utils.interpolate(ent.getLastX(), ent.getX(), lag);
  88. float y = Utils.interpolate(ent.getLastY(), ent.getY(), lag);
  89. int mid = (int) Math.ceil((ent.getWidth() / Tile.SIZE) - 2);
  90. float endY = y + ent.getHeight();
  91. Shader.getTextureRenderer().drawRectangle(x, y, x + Tile.SIZE, endY, 0.0f, 0.25f, 0.25f, 0.4453125f);
  92. x += Tile.SIZE;
  93. for(int i = 0; i < mid; i++)
  94. {
  95. Shader.getTextureRenderer().drawRectangle(x, y, x + Tile.SIZE, endY, 0.25f, 0.25f, 0.5f, 0.4453125f);
  96. x += Tile.SIZE;
  97. }
  98. Shader.getTextureRenderer().drawRectangle(x, y, x + Tile.SIZE, endY, 0.5f, 0.25f, 0.75f, 0.4453125f);
  99. }
  100. @Override
  101. public void onCollideWithEntity(Entity ent, Entity other, Face face)
  102. {
  103. if(face == Face.UP)
  104. {
  105. if(ent.getOwnForceY() < 0)
  106. {
  107. other.applyForce(ent.getOwnForceX(), ent.getOwnForceY() - Entity.GRAVITY * other.getMovement().getGravityFactor());
  108. }
  109. else
  110. {
  111. other.applyForce(ent.getOwnForceX(), ent.getOwnForceY());
  112. }
  113. }
  114. else if(face == Face.LEFT || face == Face.RIGHT)
  115. {
  116. other.applyForce(ent.getOwnForceX(), 0.0f);
  117. }
  118. }
  119. }