Particle.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package eu.tankernn.gameEngine.particles;
  2. import org.lwjgl.util.vector.Vector2f;
  3. import org.lwjgl.util.vector.Vector3f;
  4. import eu.tankernn.gameEngine.entities.Camera;
  5. import eu.tankernn.gameEngine.entities.Positionable;
  6. import eu.tankernn.gameEngine.renderEngine.DisplayManager;
  7. import eu.tankernn.gameEngine.settings.Physics;
  8. public class Particle implements Positionable {
  9. private Vector3f position;
  10. private Vector3f velocity;
  11. private float gravityEffect;
  12. private float lifeLength;
  13. private float rotation;
  14. private float scale;
  15. private ParticleTexture texture;
  16. private Vector2f texOffset1 = new Vector2f();
  17. private Vector2f texOffset2 = new Vector2f();
  18. private float blend;
  19. private float elapsedTime = 0;
  20. private float distance;
  21. private Vector3f change = new Vector3f();
  22. public Particle(ParticleTexture texture, Vector3f position, Vector3f velocity, float gravityEffect, float lifeLength, float rotation, float scale) {
  23. this.texture = texture;
  24. this.position = position;
  25. this.velocity = velocity;
  26. this.gravityEffect = gravityEffect;
  27. this.lifeLength = lifeLength;
  28. this.rotation = rotation;
  29. this.scale = scale;
  30. ParticleMaster.addParticle(this);
  31. }
  32. public ParticleTexture getTexture() {
  33. return texture;
  34. }
  35. public Vector3f getPosition() {
  36. return position;
  37. }
  38. public float getRotation() {
  39. return rotation;
  40. }
  41. public float getScale() {
  42. return scale;
  43. }
  44. public Vector2f getTexOffset1() {
  45. return texOffset1;
  46. }
  47. public Vector2f getTexOffset2() {
  48. return texOffset2;
  49. }
  50. public float getBlend() {
  51. return blend;
  52. }
  53. public float getDistance() {
  54. return distance;
  55. }
  56. protected boolean update(Camera camera) {
  57. velocity.y += Physics.GRAVITY * gravityEffect * DisplayManager.getFrameTimeSeconds();
  58. change.set(velocity);
  59. change.scale(DisplayManager.getFrameTimeSeconds());
  60. Vector3f.add(change, position, position);
  61. distance = Vector3f.sub(camera.getPosition(), position, null).lengthSquared();
  62. updateTextureCoordInfo();
  63. elapsedTime += DisplayManager.getFrameTimeSeconds();
  64. return elapsedTime < lifeLength;
  65. }
  66. private void updateTextureCoordInfo() {
  67. float lifeFactor = elapsedTime / lifeLength;
  68. int stageCount = texture.getNumberOfRows() * texture.getNumberOfRows();
  69. float atlasProgression = lifeFactor * stageCount;
  70. int index1 = (int) Math.floor(atlasProgression);
  71. int index2 = index1 < stageCount - 1 ? index1 + 1: index1;
  72. this.blend = atlasProgression % 1;
  73. setTextureOffset(texOffset1, index1);
  74. setTextureOffset(texOffset2, index2);
  75. }
  76. private void setTextureOffset(Vector2f offset, int index) {
  77. int column = index % texture.getNumberOfRows();
  78. int row = index / texture.getNumberOfRows();
  79. offset.x = (float) column / texture.getNumberOfRows();
  80. offset.y = (float) row / texture.getNumberOfRows();
  81. }
  82. }