Erste Schadensverrechnung bei Kollision.

This commit is contained in:
Jan Philipp Timme 2014-10-28 16:08:50 +01:00
parent c772ec9b8f
commit a1545b9c30
8 changed files with 69 additions and 29 deletions

View File

@ -5,7 +5,9 @@ import java.util.LinkedList;
import de.teamteamteam.spacescooter.screen.Screen;
public abstract class CollidableEntity extends Entity implements Collidable{
public abstract class CollidableEntity extends LivingEntity implements Collidable{
private int collisionDamage;
public CollidableEntity(int x, int y) {
super(x, y);
@ -27,4 +29,32 @@ public abstract class CollidableEntity extends Entity implements Collidable{
}
}
/**
* 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;
}
}

View File

@ -21,13 +21,5 @@ public abstract class Enemy extends ShootingEntity {
this.shoot();
}
public void collideWith(Collidable e) {
if(e instanceof Shot) {
Shot s = (Shot) e;
//TODO: Expand with shield logic and stuff.
this.healthPoints -= s.getDamageValue();
}
}
}

View File

@ -22,7 +22,7 @@ public class EnemyOne extends Enemy {
this.setImage(EnemyOne.img);
this.setShootSpeed(2);
this.setShootDelay(42);
this.setShootSpawn(0, 32);
this.setShootSpawn(-17, 32);
}
@Override

View File

@ -1,15 +1,43 @@
package de.teamteamteam.spacescooter.entity;
public abstract class LivingEntity extends CollidableEntity {
import de.teamteamteam.spacescooter.screen.Screen;
public abstract class LivingEntity extends Entity {
public LivingEntity(int x, int y) {
super(x, y);
}
protected int healthPoints;
protected int shieldPoints;
private int healthPoints;
private int shieldPoints;
public boolean isAlive() {
return healthPoints > 0;
}
public void takeDamage(int damage) {
//TODO: shield and health logic
this.healthPoints -= damage;
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;
}
public void setShieldPoints(int sp) {
this.shieldPoints = sp;
}
public int getShieldPoints() {
return this.shieldPoints;
}
}

View File

@ -25,7 +25,7 @@ public class Player extends ShootingEntity {
super(x, y);
this.setImage(Player.img);
this.setShootDelay(40);
this.setShootSpawn(32, 16);
this.setShootSpawn(50, 16);
this.setShootDirection(Shot.RIGHT);
this.setShootSpeed(2);
}
@ -50,14 +50,4 @@ public class Player extends ShootingEntity {
}
}
public void collideWith(Collidable entity) {
if(entity instanceof Shot) {
System.out.println("Player Kollision mit Schuss!");
}
if(entity instanceof Enemy) {
System.out.println("Player Kollision mit Feind!");
}
}
}

View File

@ -2,7 +2,7 @@ package de.teamteamteam.spacescooter.entity;
import de.teamteamteam.spacescooter.screen.Screen;
public abstract class ShootingEntity extends LivingEntity {
public abstract class ShootingEntity extends CollidableEntity {
private int shootDelay;
private int currentShootDelay;

View File

@ -26,7 +26,7 @@ public class GameScreen extends Screen {
@Override
protected void paint(Graphics g) {
LinkedList<Entity> list = new LinkedList<Entity>(this.entities);
LinkedList<Entity> list = this.getEntities();
Iterator<Entity> i = list.iterator();
while (i.hasNext()) {
i.next().paint(g);
@ -35,7 +35,7 @@ public class GameScreen extends Screen {
@Override
protected void update() {
LinkedList<Entity> list = new LinkedList<Entity>(this.entities);
LinkedList<Entity> list = this.getEntities();
Iterator<Entity> i = list.iterator();
while (i.hasNext()) {
i.next().update();

View File

@ -24,12 +24,12 @@ public abstract class Screen {
this.entities.add(e);
}
protected void removeEntity(Entity e) {
public void removeEntity(Entity e) {
this.entities.remove(e);
}
public LinkedList<Entity> getEntities() {
return this.entities;
return new LinkedList<Entity>(this.entities);
}
protected abstract void paint(Graphics g);