Refactoring Kollisionserkennung grob fertig.
This commit is contained in:
parent
32f9a72c77
commit
4717280ffc
@ -1,60 +0,0 @@
|
||||
package de.teamteamteam.spacescooter.entity;
|
||||
|
||||
import java.awt.Rectangle;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import de.teamteamteam.spacescooter.screen.Screen;
|
||||
|
||||
public abstract class CollidableEntity extends LivingEntity implements Collidable{
|
||||
|
||||
private int collisionDamage;
|
||||
|
||||
public CollidableEntity(int x, int y) {
|
||||
super(x, y);
|
||||
}
|
||||
|
||||
public Rectangle getCollisionBox() {
|
||||
return new Rectangle(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
||||
}
|
||||
|
||||
public void update() {
|
||||
LinkedList<Entity> entities = Screen.currentScreen.getEntities();
|
||||
for(Entity e : entities) {
|
||||
if(e.equals(this)) continue;
|
||||
if(!(e instanceof Collidable)) continue;
|
||||
CollidableEntity ce = (CollidableEntity) e;
|
||||
if(ce.getCollisionBox().intersects(this.getCollisionBox())) {
|
||||
this.collideWith(ce);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle collisions based on what we collide with.
|
||||
*/
|
||||
public void collideWith(Collidable entity) {
|
||||
if(entity instanceof Shot) {
|
||||
Shot s = (Shot) entity;
|
||||
this.takeDamage(s.getDamageValue());
|
||||
}
|
||||
if(entity instanceof Enemy) {
|
||||
//We take the collision damage from the enemy
|
||||
Enemy enemy = (Enemy) entity;
|
||||
this.takeDamage(enemy.getCollisionDamage());
|
||||
}
|
||||
if(entity instanceof Player) {
|
||||
//We take the collision damage from the enemy
|
||||
Player player = (Player) entity;
|
||||
this.takeDamage(player.getCollisionDamage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setCollisionDamage(int d) {
|
||||
this.collisionDamage = d;
|
||||
}
|
||||
|
||||
public int getCollisionDamage() {
|
||||
return this.collisionDamage;
|
||||
}
|
||||
}
|
||||
@ -37,7 +37,22 @@ public abstract class LivingEntity extends Entity implements Collidable {
|
||||
* Handle collisions based on what we collide with.
|
||||
*/
|
||||
public void collideWith(Collidable entity) {
|
||||
|
||||
//this instanceof ShootingEntity
|
||||
if(entity instanceof Shot) {
|
||||
Shot s = (Shot) entity;
|
||||
this.takeDamage(s.getDamageValue());
|
||||
s.takeDamage(this.getCollisionDamage());
|
||||
}
|
||||
if(entity instanceof Player && (!(this instanceof Player))) {
|
||||
Player player = (Player) entity;
|
||||
player.takeDamage(this.getCollisionDamage());
|
||||
this.takeDamage(player.getCollisionDamage());
|
||||
}
|
||||
if(entity instanceof Enemy && (!(this instanceof Enemy))) {
|
||||
Enemy enemy = (Enemy) entity;
|
||||
enemy.takeDamage(this.getCollisionDamage());
|
||||
this.takeDamage(enemy.getCollisionDamage());
|
||||
}
|
||||
}
|
||||
|
||||
public void setCollisionDamage(int d) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user