123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- package eu.tankernn.gameEngine.particles;
- import org.lwjgl.util.vector.Vector2f;
- import org.lwjgl.util.vector.Vector3f;
- import eu.tankernn.gameEngine.entities.Camera;
- import eu.tankernn.gameEngine.entities.Positionable;
- import eu.tankernn.gameEngine.renderEngine.DisplayManager;
- import eu.tankernn.gameEngine.settings.Physics;
- public class Particle implements Positionable {
- private Vector3f position;
- private Vector3f velocity;
- private float gravityEffect;
- private float lifeLength;
- private float rotation;
- private float scale;
-
- private ParticleTexture texture;
-
- private Vector2f texOffset1 = new Vector2f();
- private Vector2f texOffset2 = new Vector2f();
- private float blend;
-
- private float elapsedTime = 0;
- private float distance;
-
- private Vector3f change = new Vector3f();
- public Particle(ParticleTexture texture, Vector3f position, Vector3f velocity, float gravityEffect, float lifeLength, float rotation, float scale) {
- this.texture = texture;
- this.position = position;
- this.velocity = velocity;
- this.gravityEffect = gravityEffect;
- this.lifeLength = lifeLength;
- this.rotation = rotation;
- this.scale = scale;
- ParticleMaster.addParticle(this);
- }
- public ParticleTexture getTexture() {
- return texture;
- }
-
- public Vector3f getPosition() {
- return position;
- }
- public float getRotation() {
- return rotation;
- }
- public float getScale() {
- return scale;
- }
-
- public Vector2f getTexOffset1() {
- return texOffset1;
- }
- public Vector2f getTexOffset2() {
- return texOffset2;
- }
- public float getBlend() {
- return blend;
- }
- public float getDistance() {
- return distance;
- }
- protected boolean update(Camera camera) {
- velocity.y += Physics.GRAVITY * gravityEffect * DisplayManager.getFrameTimeSeconds();
- change.set(velocity);
- change.scale(DisplayManager.getFrameTimeSeconds());
- Vector3f.add(change, position, position);
- distance = Vector3f.sub(camera.getPosition(), position, null).lengthSquared();
- updateTextureCoordInfo();
- elapsedTime += DisplayManager.getFrameTimeSeconds();
- return elapsedTime < lifeLength;
- }
-
- private void updateTextureCoordInfo() {
- float lifeFactor = elapsedTime / lifeLength;
- int stageCount = texture.getNumberOfRows() * texture.getNumberOfRows();
- float atlasProgression = lifeFactor * stageCount;
- int index1 = (int) Math.floor(atlasProgression);
- int index2 = index1 < stageCount - 1 ? index1 + 1: index1;
- this.blend = atlasProgression % 1;
- setTextureOffset(texOffset1, index1);
- setTextureOffset(texOffset2, index2);
- }
-
- private void setTextureOffset(Vector2f offset, int index) {
- int column = index % texture.getNumberOfRows();
- int row = index / texture.getNumberOfRows();
- offset.x = (float) column / texture.getNumberOfRows();
- offset.y = (float) row / texture.getNumberOfRows();
- }
- }
|