Erste Kollisionsbehandlung.

This commit is contained in:
Jan Philipp Timme 2014-10-28 15:51:19 +01:00
parent 27f1f486d5
commit c772ec9b8f
6 changed files with 46 additions and 14 deletions

View File

@ -1,5 +1,8 @@
package de.teamteamteam.spacescooter.entity;
import java.awt.Rectangle;
public interface Collidable {
public Rectangle getCollisionBox();
public void collideWith(Collidable entity);
}

View File

@ -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<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);
}
}
}
}

View File

@ -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);
}
}

View File

@ -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!");
}
}

View File

@ -17,6 +17,7 @@ public abstract class ShootingEntity extends LivingEntity {
}
public void update() {
super.update();
if(this.currentShootDelay > 0) this.currentShootDelay--;
}

View File

@ -28,6 +28,10 @@ public abstract class Screen {
this.entities.remove(e);
}
public LinkedList<Entity> getEntities() {
return this.entities;
}
protected abstract void paint(Graphics g);
protected abstract void update();