Erste Kollisionsbehandlung.
This commit is contained in:
parent
27f1f486d5
commit
c772ec9b8f
|
@ -1,5 +1,8 @@
|
||||||
package de.teamteamteam.spacescooter.entity;
|
package de.teamteamteam.spacescooter.entity;
|
||||||
|
|
||||||
|
import java.awt.Rectangle;
|
||||||
|
|
||||||
public interface Collidable {
|
public interface Collidable {
|
||||||
|
public Rectangle getCollisionBox();
|
||||||
public void collideWith(Collidable entity);
|
public void collideWith(Collidable entity);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,23 +1,30 @@
|
||||||
package de.teamteamteam.spacescooter.entity;
|
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 abstract class CollidableEntity extends Entity implements Collidable{
|
||||||
|
|
||||||
public CollidableEntity(int x, int y) {
|
public CollidableEntity(int x, int y) {
|
||||||
super(x, y);
|
super(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public Rectangle getCollisionBox() {
|
||||||
* Dimensions of the Entity
|
return new Rectangle(this.getX(), this.getY(), this.getWidth(), this.getHeight());
|
||||||
*/
|
|
||||||
protected int width;
|
|
||||||
protected int height;
|
|
||||||
|
|
||||||
public int getWidth() {
|
|
||||||
return this.width;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getHeight() {
|
public void update() {
|
||||||
return this.width;
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package de.teamteamteam.spacescooter.entity;
|
package de.teamteamteam.spacescooter.entity;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
import java.awt.Graphics;
|
import java.awt.Graphics;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
|
|
||||||
|
@ -29,6 +30,14 @@ public abstract class Entity implements Updateable, Paintable {
|
||||||
return this.y;
|
return this.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getWidth() {
|
||||||
|
return this.img.getWidth();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getHeight() {
|
||||||
|
return this.img.getHeight();
|
||||||
|
}
|
||||||
|
|
||||||
public void setPosition(int x, int y) {
|
public void setPosition(int x, int y) {
|
||||||
this.x = x;
|
this.x = x;
|
||||||
this.y = y;
|
this.y = y;
|
||||||
|
@ -43,6 +52,10 @@ public abstract class Entity implements Updateable, Paintable {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void paint(Graphics g) {
|
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);
|
g.drawImage(this.img, this.x, this.y, null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package de.teamteamteam.spacescooter.entity;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.image.BufferedImage;
|
import java.awt.image.BufferedImage;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.imageio.ImageIO;
|
import javax.imageio.ImageIO;
|
||||||
|
|
||||||
import de.teamteamteam.spacescooter.control.Keyboard;
|
import de.teamteamteam.spacescooter.control.Keyboard;
|
||||||
|
@ -52,7 +51,12 @@ public class Player extends ShootingEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void collideWith(Collidable entity) {
|
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!");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ public abstract class ShootingEntity extends LivingEntity {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update() {
|
public void update() {
|
||||||
|
super.update();
|
||||||
if(this.currentShootDelay > 0) this.currentShootDelay--;
|
if(this.currentShootDelay > 0) this.currentShootDelay--;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,10 @@ public abstract class Screen {
|
||||||
this.entities.remove(e);
|
this.entities.remove(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LinkedList<Entity> getEntities() {
|
||||||
|
return this.entities;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract void paint(Graphics g);
|
protected abstract void paint(Graphics g);
|
||||||
protected abstract void update();
|
protected abstract void update();
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue