Refactor collision handling.
This commit is contained in:
parent
a1545b9c30
commit
32f9a72c77
|
@ -23,6 +23,7 @@ public class EnemyOne extends Enemy {
|
|||
this.setShootSpeed(2);
|
||||
this.setShootDelay(42);
|
||||
this.setShootSpawn(-17, 32);
|
||||
this.setHealthPoints(5);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -1,33 +1,76 @@
|
|||
package de.teamteamteam.spacescooter.entity;
|
||||
|
||||
import java.awt.Rectangle;
|
||||
import java.util.LinkedList;
|
||||
|
||||
import de.teamteamteam.spacescooter.screen.Screen;
|
||||
|
||||
public abstract class LivingEntity extends Entity {
|
||||
public abstract class LivingEntity extends Entity implements Collidable {
|
||||
|
||||
public LivingEntity(int x, int y) {
|
||||
super(x, y);
|
||||
}
|
||||
|
||||
private int collisionDamage;
|
||||
|
||||
public Rectangle getCollisionBox() {
|
||||
return new Rectangle(this.getX(), this.getY(), this.getWidth(),
|
||||
this.getHeight());
|
||||
}
|
||||
|
||||
public void update() {
|
||||
if(!(this instanceof ShootingEntity)) return; //Only check collisions for ShootingEntity.
|
||||
LinkedList<Entity> entities = Screen.currentScreen.getEntities();
|
||||
for (Entity e : entities) {
|
||||
if (e.equals(this)) //Do not collide with myself!
|
||||
continue;
|
||||
if(!(e instanceof Collidable)) //Do not collide with non-collidable!
|
||||
continue;
|
||||
Collidable ce = (Collidable) e;
|
||||
if (ce.getCollisionBox().intersects(this.getCollisionBox())) {
|
||||
this.collideWith(ce);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle collisions based on what we collide with.
|
||||
*/
|
||||
public void collideWith(Collidable entity) {
|
||||
|
||||
}
|
||||
|
||||
public void setCollisionDamage(int d) {
|
||||
this.collisionDamage = d;
|
||||
}
|
||||
|
||||
public int getCollisionDamage() {
|
||||
return this.collisionDamage;
|
||||
}
|
||||
|
||||
private int healthPoints;
|
||||
private int shieldPoints;
|
||||
|
||||
|
||||
public boolean isAlive() {
|
||||
return healthPoints > 0;
|
||||
}
|
||||
|
||||
|
||||
public void takeDamage(int damage) {
|
||||
//TODO: shield and health logic
|
||||
if(this instanceof Shot) {
|
||||
System.out.println("Shot took damage: " + damage + "left: "+this.getHealthPoints()+" (" + this + ")");
|
||||
}
|
||||
// TODO: shield and health logic
|
||||
this.healthPoints -= damage;
|
||||
if(this.isAlive() == false) {
|
||||
if (this.isAlive() == false) {
|
||||
System.out.println(this + " ist gestorben. RIP");
|
||||
Screen.currentScreen.removeEntity(this);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void setHealthPoints(int hp) {
|
||||
this.healthPoints = hp;
|
||||
}
|
||||
|
||||
|
||||
public int getHealthPoints() {
|
||||
return this.healthPoints;
|
||||
}
|
||||
|
@ -35,7 +78,7 @@ public abstract class LivingEntity extends Entity {
|
|||
public void setShieldPoints(int sp) {
|
||||
this.shieldPoints = sp;
|
||||
}
|
||||
|
||||
|
||||
public int getShieldPoints() {
|
||||
return this.shieldPoints;
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@ public class Player extends ShootingEntity {
|
|||
this.setShootSpawn(50, 16);
|
||||
this.setShootDirection(Shot.RIGHT);
|
||||
this.setShootSpeed(2);
|
||||
this.setHealthPoints(100);
|
||||
}
|
||||
|
||||
public void update() {
|
||||
|
|
|
@ -2,7 +2,7 @@ package de.teamteamteam.spacescooter.entity;
|
|||
|
||||
import de.teamteamteam.spacescooter.screen.Screen;
|
||||
|
||||
public abstract class ShootingEntity extends CollidableEntity {
|
||||
public abstract class ShootingEntity extends LivingEntity {
|
||||
|
||||
private int shootDelay;
|
||||
private int currentShootDelay;
|
||||
|
|
|
@ -2,12 +2,12 @@ package de.teamteamteam.spacescooter.entity;
|
|||
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
public abstract class Shot extends CollidableEntity {
|
||||
public abstract class Shot extends LivingEntity {
|
||||
|
||||
public static final int RIGHT = 1;
|
||||
public static final int LEFT= -1;
|
||||
|
||||
protected int damageValue;
|
||||
private int damageValue;
|
||||
protected int collisionCount;
|
||||
|
||||
private int speed;
|
||||
|
@ -25,17 +25,14 @@ public abstract class Shot extends CollidableEntity {
|
|||
this.setPosition(this.x - this.getImage().getWidth() / 2, this.y - this.getImage().getHeight() / 2);
|
||||
}
|
||||
|
||||
public void collideWith(Collidable entity) {
|
||||
this.collisionCount--;
|
||||
if(this.collisionCount == 0) {
|
||||
//TODO: scoot is over
|
||||
}
|
||||
}
|
||||
|
||||
public int getDamageValue() {
|
||||
return this.damageValue;
|
||||
}
|
||||
|
||||
public void setDamageValue(int dmg) {
|
||||
this.damageValue = dmg;
|
||||
}
|
||||
|
||||
public void update() {
|
||||
this.x += this.direction * this.speed;
|
||||
}
|
||||
|
|
|
@ -22,6 +22,7 @@ public class SingleShot extends Shot {
|
|||
public SingleShot(int x, int y, int shootDirection, int shootSpeed) {
|
||||
super(x, y, shootDirection, shootSpeed);
|
||||
this.setImage(img);
|
||||
this.setDamageValue(5);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue