diff --git a/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java b/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java index 11359e5..6c1c246 100644 --- a/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java @@ -5,8 +5,10 @@ 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; + } } diff --git a/src/de/teamteamteam/spacescooter/entity/Enemy.java b/src/de/teamteamteam/spacescooter/entity/Enemy.java index 7782d1b..6c888f6 100644 --- a/src/de/teamteamteam/spacescooter/entity/Enemy.java +++ b/src/de/teamteamteam/spacescooter/entity/Enemy.java @@ -20,14 +20,6 @@ public abstract class Enemy extends ShootingEntity { if(willShoot == true) 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(); - } - } } diff --git a/src/de/teamteamteam/spacescooter/entity/EnemyOne.java b/src/de/teamteamteam/spacescooter/entity/EnemyOne.java index ccf246e..a29b52c 100644 --- a/src/de/teamteamteam/spacescooter/entity/EnemyOne.java +++ b/src/de/teamteamteam/spacescooter/entity/EnemyOne.java @@ -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 diff --git a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java index 090081e..cf17400 100644 --- a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java @@ -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; + } + } diff --git a/src/de/teamteamteam/spacescooter/entity/Player.java b/src/de/teamteamteam/spacescooter/entity/Player.java index 99f8f8a..15ed84f 100644 --- a/src/de/teamteamteam/spacescooter/entity/Player.java +++ b/src/de/teamteamteam/spacescooter/entity/Player.java @@ -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!"); - } - - } - } diff --git a/src/de/teamteamteam/spacescooter/entity/ShootingEntity.java b/src/de/teamteamteam/spacescooter/entity/ShootingEntity.java index b1b6bac..7066f6f 100644 --- a/src/de/teamteamteam/spacescooter/entity/ShootingEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/ShootingEntity.java @@ -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; diff --git a/src/de/teamteamteam/spacescooter/screen/GameScreen.java b/src/de/teamteamteam/spacescooter/screen/GameScreen.java index 31a5b18..3d9ada4 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameScreen.java @@ -26,7 +26,7 @@ public class GameScreen extends Screen { @Override protected void paint(Graphics g) { - LinkedList list = new LinkedList(this.entities); + LinkedList list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().paint(g); @@ -35,7 +35,7 @@ public class GameScreen extends Screen { @Override protected void update() { - LinkedList list = new LinkedList(this.entities); + LinkedList list = this.getEntities(); Iterator i = list.iterator(); while (i.hasNext()) { i.next().update(); diff --git a/src/de/teamteamteam/spacescooter/screen/Screen.java b/src/de/teamteamteam/spacescooter/screen/Screen.java index fe9d10e..8862b90 100644 --- a/src/de/teamteamteam/spacescooter/screen/Screen.java +++ b/src/de/teamteamteam/spacescooter/screen/Screen.java @@ -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 getEntities() { - return this.entities; + return new LinkedList(this.entities); } protected abstract void paint(Graphics g);