From 81f3cb7536c2c482b6bfcf08453cc65e0d552499 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Thu, 30 Oct 2014 22:42:08 +0100 Subject: [PATCH] =?UTF-8?q?Remove-Methode=20f=C3=BCr=20Entity-Klasse=20hin?= =?UTF-8?q?zugef=C3=BCgt.=20Shots=20leben=20nicht=20au=C3=9Ferhalb=20des?= =?UTF-8?q?=20Bildschirms=20weiter.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/de/teamteamteam/spacescooter/entity/Entity.java | 8 ++++++++ src/de/teamteamteam/spacescooter/entity/LivingEntity.java | 2 +- src/de/teamteamteam/spacescooter/entity/Shot.java | 8 ++++++++ src/de/teamteamteam/spacescooter/screen/GameScreen.java | 2 +- 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/de/teamteamteam/spacescooter/entity/Entity.java b/src/de/teamteamteam/spacescooter/entity/Entity.java index b640032..a53848c 100644 --- a/src/de/teamteamteam/spacescooter/entity/Entity.java +++ b/src/de/teamteamteam/spacescooter/entity/Entity.java @@ -4,6 +4,7 @@ import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; +import de.teamteamteam.spacescooter.screen.Screen; import de.teamteamteam.spacescooter.utility.GameConfig; import de.teamteamteam.spacescooter.utility.Loader; @@ -63,4 +64,11 @@ public abstract class Entity implements Updateable, Paintable { g.drawImage(this.img, this.x, this.y, null); } + + /** + * Removes entity from the game + */ + public void remove() { + Screen.currentScreen.removeEntity(this); + } } diff --git a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java index 6df937d..f826fbd 100644 --- a/src/de/teamteamteam/spacescooter/entity/LivingEntity.java +++ b/src/de/teamteamteam/spacescooter/entity/LivingEntity.java @@ -79,7 +79,7 @@ public abstract class LivingEntity extends Entity implements Collidable { this.healthPoints -= damage; if (this.isAlive() == false) { if(GameConfig.DEBUG) System.out.println(this + " ist gestorben. RIP"); - Screen.currentScreen.removeEntity(this); + this.remove(); } } diff --git a/src/de/teamteamteam/spacescooter/entity/Shot.java b/src/de/teamteamteam/spacescooter/entity/Shot.java index f3d8c8c..48c6f19 100644 --- a/src/de/teamteamteam/spacescooter/entity/Shot.java +++ b/src/de/teamteamteam/spacescooter/entity/Shot.java @@ -1,5 +1,7 @@ package de.teamteamteam.spacescooter.entity; +import de.teamteamteam.spacescooter.utility.GameConfig; + public abstract class Shot extends LivingEntity { public static final int RIGHT = 1; @@ -33,6 +35,12 @@ public abstract class Shot extends LivingEntity { public void update() { this.x += this.direction * this.speed; + //remove the shot in case it is out of the game window. + if ((this.x + this.getWidth()) < 0 || this.x > GameConfig.windowWidth + || (this.y + this.getHeight()) < 0 + || this.y > GameConfig.windowHeight) { + this.remove(); + } } } diff --git a/src/de/teamteamteam/spacescooter/screen/GameScreen.java b/src/de/teamteamteam/spacescooter/screen/GameScreen.java index 5479c1a..55fd6a3 100644 --- a/src/de/teamteamteam/spacescooter/screen/GameScreen.java +++ b/src/de/teamteamteam/spacescooter/screen/GameScreen.java @@ -1,7 +1,6 @@ package de.teamteamteam.spacescooter.screen; import java.awt.Graphics2D; - import java.awt.event.KeyEvent; import java.util.Iterator; import java.util.LinkedList; @@ -46,3 +45,4 @@ public class GameScreen extends Screen { } } +