Browse Source

Projectile improvements

Tankernn 8 years ago
parent
commit
5fd9d3bf0e

+ 10 - 3
src/main/java/eu/tankernn/gameEngine/TankernnGame3D.java

@@ -12,6 +12,7 @@ import eu.tankernn.gameEngine.entities.Camera;
 import eu.tankernn.gameEngine.entities.Entity3D;
 import eu.tankernn.gameEngine.entities.Light;
 import eu.tankernn.gameEngine.entities.Player;
+import eu.tankernn.gameEngine.entities.Projectile;
 import eu.tankernn.gameEngine.environmentMap.EnvironmentMapRenderer;
 import eu.tankernn.gameEngine.loader.textures.Texture;
 import eu.tankernn.gameEngine.particles.ParticleMaster;
@@ -37,7 +38,8 @@ public class TankernnGame3D extends TankernnGame {
 	protected MousePicker picker;
 
 	protected List<Entity3D> entities = new ArrayList<>();
-	protected List<Light> lights= new ArrayList<>();
+	protected List<Projectile> projectiles = new ArrayList<>();
+	protected List<Light> lights = new ArrayList<>();
 	private Light sun;
 	protected TerrainPack terrainPack;
 	protected Player player;
@@ -62,8 +64,13 @@ public class TankernnGame3D extends TankernnGame {
 
 	public void update() {
 		super.update();
-		for (Entity3D e : entities)
-			e.update();
+		entities.forEach(Entity3D::update);
+		entities.removeIf(Entity3D::isDead);
+		
+		projectiles.forEach(Projectile::update);
+		projectiles.removeIf(Projectile::isDead);
+		projectiles.forEach((p) -> p.checkCollision(entities));
+		
 		player.move();
 		picker.update(terrainPack);
 		camera.update();

+ 25 - 20
src/main/java/eu/tankernn/gameEngine/entities/BasicProjectile.java

@@ -1,20 +1,25 @@
-package eu.tankernn.gameEngine.entities;
-
-import org.lwjgl.util.vector.Vector3f;
-
-import eu.tankernn.gameEngine.loader.models.AABB;
-import eu.tankernn.gameEngine.loader.models.TexturedModel;
-import eu.tankernn.gameEngine.particles.ParticleSystem;
-
-public class BasicProjectile extends Projectile {
-
-	public BasicProjectile(TexturedModel model, Vector3f position, Vector3f velocity, ParticleSystem particleSystem) {
-		super(model, position, velocity, new AABB(new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)), particleSystem);
-	}
-
-	@Override
-	public void onCollision(Entity3D entity) {
-		System.out.println("Collision!");
-	}
-
-}
+package eu.tankernn.gameEngine.entities;
+
+import org.lwjgl.util.vector.Vector3f;
+
+import eu.tankernn.gameEngine.loader.models.AABB;
+import eu.tankernn.gameEngine.particles.ParticleSystem;
+
+public class BasicProjectile extends Projectile {
+	
+	private Entity3D creator;
+	
+	public BasicProjectile(Entity3D creator, Vector3f position, Vector3f velocity, float range, ParticleSystem particleSystem) {
+		super(null, position, velocity, range, new AABB(new Vector3f(0, 0, 0), new Vector3f(1, 1, 1)), particleSystem);
+		this.creator = creator;
+	}
+	
+	@Override
+	public void onCollision(Entity3D entity) {
+		if (entity.equals(creator))
+			return;
+		System.out.println("Collision!");
+		this.kill();
+	}
+	
+}

+ 2 - 2
src/main/java/eu/tankernn/gameEngine/entities/Car.java

@@ -26,10 +26,10 @@ public class Car extends Player {
 		float distance = currentSpeed * DisplayManager.getFrameTimeSeconds();
 		float dx = (float) (distance * Math.sin(Math.toRadians(super.getRotation().y)));
 		float dz = (float) (distance * Math.cos(Math.toRadians(super.getRotation().y)));
-		super.increasePosition(dx, 0, dz);
+		super.increasePosition(new Vector3f(dx, 0, dz));
 
 		upwardsSpeed += Physics.GRAVITY * DisplayManager.getFrameTimeSeconds();
-		super.increasePosition(0, upwardsSpeed * DisplayManager.getFrameTimeSeconds(), 0);
+		super.increasePosition(new Vector3f(0, upwardsSpeed * DisplayManager.getFrameTimeSeconds(), 0));
 
 		Terrain currentTerrain = terrainPack.getTerrainByWorldPos(this.getPosition().x, this.getPosition().z);
 

+ 10 - 5
src/main/java/eu/tankernn/gameEngine/entities/Entity3D.java

@@ -9,10 +9,11 @@ import eu.tankernn.gameEngine.util.IPositionable;
 
 public class Entity3D implements IPositionable {
 	private TexturedModel model;
-	private Vector3f position;
+	protected Vector3f position;
 	private Vector3f rotation;
 	private float scale;
 	private AABB boundingBox;
+	protected boolean dead;
 	
 	public Entity3D(TexturedModel model, Vector3f position, Vector3f rotation, float scale, AABB boundingBox) {
 		this.model = model;
@@ -23,10 +24,10 @@ public class Entity3D implements IPositionable {
 		this.boundingBox.updatePosition(position);
 	}
 	
-	public void increasePosition(float dx, float dy, float dz) {
-		this.position.x += dx;
-		this.position.y += dy;
-		this.position.z += dz;
+	public void increasePosition(Vector3f velocity) {
+		this.position.x += velocity.x;
+		this.position.y += velocity.y;
+		this.position.z += velocity.z;
 	}
 	
 	public void increaseRotation(Vector3f deltaRotation) {
@@ -71,4 +72,8 @@ public class Entity3D implements IPositionable {
 		return boundingBox;
 	}
 	
+	public boolean isDead() {
+		return dead;
+	}
+	
 }

+ 7 - 2
src/main/java/eu/tankernn/gameEngine/entities/Player.java

@@ -38,9 +38,8 @@ public class Player extends Entity3D {
 		float distance = currentSpeed * DisplayManager.getFrameTimeSeconds();
 		float dx = (float) (distance * Math.sin(Math.toRadians(super.getRotation().y)));
 		float dz = (float) (distance * Math.cos(Math.toRadians(super.getRotation().y)));
-		super.increasePosition(dx, 0, dz);
 		upwardsSpeed += Physics.GRAVITY * DisplayManager.getFrameTimeSeconds();
-		super.increasePosition(0, upwardsSpeed * DisplayManager.getFrameTimeSeconds(), 0);
+		super.increasePosition(new Vector3f(dx, upwardsSpeed * DisplayManager.getFrameTimeSeconds(), dz));
 		
 		Terrain currentTerrain = terrainPack.getTerrainByWorldPos(this.getPosition().x, this.getPosition().z);
 		
@@ -107,4 +106,10 @@ public class Player extends Entity3D {
 		return terrainPack.getTerrainByWorldPos(this.getPosition().x, this.getPosition().z);
 	}
 	
+	public Vector3f get2dRotation() {
+		float dx = (float) (Math.sin(Math.toRadians(super.getRotation().y)));
+		float dz = (float) (Math.cos(Math.toRadians(super.getRotation().y)));
+		return new Vector3f(dx, 0, dz);
+	}
+	
 }

+ 47 - 26
src/main/java/eu/tankernn/gameEngine/entities/Projectile.java

@@ -1,26 +1,47 @@
-package eu.tankernn.gameEngine.entities;
-
-import org.lwjgl.util.vector.Vector3f;
-
-import eu.tankernn.gameEngine.loader.models.AABB;
-import eu.tankernn.gameEngine.loader.models.TexturedModel;
-import eu.tankernn.gameEngine.particles.ParticleSystem;
-
-public abstract class Projectile extends Entity3D {
-	
-	private Vector3f velocity;
-	private ParticleSystem particleSystem;
-	
-	public Projectile(TexturedModel model, Vector3f position, Vector3f velocity, AABB boundingBox, ParticleSystem particleSystem) {
-		super(model, position, new Vector3f(0, 0, 0), 1, boundingBox);
-		this.particleSystem = particleSystem;
-		this.velocity = velocity;
-	}
-	
-	public void update() {
-		this.increasePosition(velocity.x, velocity.y, velocity.z);
-		particleSystem.setPosition(this.getPosition());
-	}
-	
-	public abstract void onCollision(Entity3D entity);
-}
+package eu.tankernn.gameEngine.entities;
+
+import java.util.List;
+
+import org.lwjgl.util.vector.Vector3f;
+
+import eu.tankernn.gameEngine.loader.models.AABB;
+import eu.tankernn.gameEngine.loader.models.TexturedModel;
+import eu.tankernn.gameEngine.particles.ParticleSystem;
+import eu.tankernn.gameEngine.renderEngine.DisplayManager;
+
+public abstract class Projectile extends Entity3D {
+	
+	private Vector3f velocity;
+	private ParticleSystem particleSystem;
+	private final float range;
+	private final Vector3f startPosition;
+	
+	public Projectile(TexturedModel model, Vector3f position, Vector3f velocity, float range, AABB boundingBox, ParticleSystem particleSystem) {
+		super(model, position, new Vector3f(0, 0, 0), 1, boundingBox);
+		this.particleSystem = particleSystem;
+		this.velocity = velocity;
+		this.range = range;
+		this.startPosition = new Vector3f(position);
+	}
+	
+	public void update() {
+		this.increasePosition((Vector3f) new Vector3f(velocity).scale(DisplayManager.getFrameTimeSeconds()));
+		particleSystem.setPosition(this.getPosition());
+		Vector3f distance = Vector3f.sub(position, startPosition, null);
+		if (distance.length() > range) {
+			kill();
+		}
+		super.update();
+	}
+	
+	public void checkCollision(List<Entity3D> entities) {
+		entities.stream().filter((e) -> !e.equals(Projectile.this)).filter((e) -> AABB.collides(e.getBoundingBox(), Projectile.this.getBoundingBox())).forEach(this::onCollision);
+	}
+
+	protected void kill() {
+		particleSystem.remove();
+		this.dead = true;
+	}
+	
+	public abstract void onCollision(Entity3D entity);
+}

+ 2 - 0
src/main/java/eu/tankernn/gameEngine/particles/ParticleMaster.java

@@ -47,6 +47,8 @@ public class ParticleMaster {
 			if (!entry.getKey().usesAdditiveBlending())
 				DistanceSorter.sort(list, camera);
 		}
+		
+		systems.removeIf(ParticleSystem::isDead);
 	}
 
 	public void renderParticles(Camera camera) {

+ 9 - 0
src/main/java/eu/tankernn/gameEngine/particles/ParticleSystem.java

@@ -12,6 +12,7 @@ public class ParticleSystem {
 	private Vector3f position;
 	
 	private ParticleTexture texture;
+	private boolean dead;
 	
 	public ParticleSystem(ParticleTexture texture, float pps, float speed, float gravityComplient, float lifeLength) {
 		this.texture = texture;
@@ -48,4 +49,12 @@ public class ParticleSystem {
 		velocity.scale(speed);
 		return new Particle(texture, new Vector3f(center), velocity, gravityComplient, lifeLength, 0, 1);
 	}
+
+	public void remove() {
+		this.dead = true;
+	}
+	
+	public boolean isDead() {
+		return dead;
+	}
 }

+ 2 - 0
src/main/java/eu/tankernn/gameEngine/renderEngine/MasterRenderer.java

@@ -133,6 +133,8 @@ public class MasterRenderer {
 	 */
 	public void processEntity(Entity3D entity) {
 		TexturedModel entityModel = entity.getModel();
+		if (entityModel == null)
+			return;
 		List<Entity3D> batch = entities.get(entityModel);
 		if (batch != null) {
 			batch.add(entity);