From c772ec9b8f847a37454dcd81f79d120b632add14 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 28 Oct 2014 15:51:19 +0100 Subject: [PATCH] Erste Kollisionsbehandlung. --- .../spacescooter/entity/Collidable.java | 3 ++ .../spacescooter/entity/CollidableEntity.java | 31 ++++++++++++------- .../spacescooter/entity/Entity.java | 13 ++++++++ .../spacescooter/entity/Player.java | 8 +++-- .../spacescooter/entity/ShootingEntity.java | 1 + .../spacescooter/screen/Screen.java | 4 +++ 6 files changed, 46 insertions(+), 14 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/entity/Collidable.java b/src/de/teamteamteam/spacescooter/entity/Collidable.java index b1f0b9f..a922d81 100644 --- a/src/de/teamteamteam/spacescooter/entity/Collidable.java +++ b/src/de/teamteamteam/spacescooter/entity/Collidable.java @@ -1,5 +1,8 @@ package de.teamteamteam.spacescooter.entity; +import java.awt.Rectangle; + public interface Collidable { + public Rectangle getCollisionBox(); public void collideWith(Collidable entity); } diff --git a/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java b/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java index 7f15103..11359e5 100644 --- a/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/CollidableEntity.java @@ -1,23 +1,30 @@ package de.teamteamteam.spacescooter.entity; +import java.awt.Rectangle; +import java.util.LinkedList; + +import de.teamteamteam.spacescooter.screen.Screen; + public abstract class CollidableEntity extends Entity implements Collidable{ public CollidableEntity(int x, int y) { super(x, y); } - /** - * Dimensions of the Entity - */ - protected int width; - protected int height; - - public int getWidth() { - return this.width; + public Rectangle getCollisionBox() { + return new Rectangle(this.getX(), this.getY(), this.getWidth(), this.getHeight()); } - - public int getHeight() { - return this.width; + + public void update() { + LinkedList 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); + } + } } - + } diff --git a/src/de/teamteamteam/spacescooter/entity/Entity.java b/src/de/teamteamteam/spacescooter/entity/Entity.java index 21cb79a..dfa188f 100644 --- a/src/de/teamteamteam/spacescooter/entity/Entity.java +++ b/src/de/teamteamteam/spacescooter/entity/Entity.java @@ -1,5 +1,6 @@ package de.teamteamteam.spacescooter.entity; +import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferedImage; @@ -29,6 +30,14 @@ public abstract class Entity implements Updateable, Paintable { return this.y; } + public int getWidth() { + return this.img.getWidth(); + } + + public int getHeight() { + return this.img.getHeight(); + } + public void setPosition(int x, int y) { this.x = x; this.y = y; @@ -43,6 +52,10 @@ public abstract class Entity implements Updateable, Paintable { } public void paint(Graphics g) { + //DEBUG ONLY + g.setColor(new Color(255,0,0)); + g.drawRect(this.x, this.y, this.getWidth(), this.getHeight()); + g.drawImage(this.img, this.x, this.y, null); } } diff --git a/src/de/teamteamteam/spacescooter/entity/Player.java b/src/de/teamteamteam/spacescooter/entity/Player.java index 416913c..99f8f8a 100644 --- a/src/de/teamteamteam/spacescooter/entity/Player.java +++ b/src/de/teamteamteam/spacescooter/entity/Player.java @@ -3,7 +3,6 @@ package de.teamteamteam.spacescooter.entity; import java.awt.event.KeyEvent; import java.awt.image.BufferedImage; import java.io.IOException; - import javax.imageio.ImageIO; import de.teamteamteam.spacescooter.control.Keyboard; @@ -52,7 +51,12 @@ public class Player extends ShootingEntity { } public void collideWith(Collidable entity) { - // TODO Auto-generated method stub + 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 5e97f8e..b1b6bac 100644 --- a/src/de/teamteamteam/spacescooter/entity/ShootingEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/ShootingEntity.java @@ -17,6 +17,7 @@ public abstract class ShootingEntity extends LivingEntity { } public void update() { + super.update(); if(this.currentShootDelay > 0) this.currentShootDelay--; } diff --git a/src/de/teamteamteam/spacescooter/screen/Screen.java b/src/de/teamteamteam/spacescooter/screen/Screen.java index d2c828c..fe9d10e 100644 --- a/src/de/teamteamteam/spacescooter/screen/Screen.java +++ b/src/de/teamteamteam/spacescooter/screen/Screen.java @@ -28,6 +28,10 @@ public abstract class Screen { this.entities.remove(e); } + public LinkedList getEntities() { + return this.entities; + } + protected abstract void paint(Graphics g); protected abstract void update();