Avoid creating tons of iterators instead of recycling them.
Use reset() on the ConcurrentIterator before iterating.
This commit is contained in:
parent
ec48c042f6
commit
904e13f4cf
@ -14,6 +14,7 @@ public class EnemyThree extends Enemy{
|
||||
private double newY;
|
||||
private double ySpeed = 0.4;
|
||||
private Random random;
|
||||
private ConcurrentIterator<Entity> entityIterator;
|
||||
|
||||
public EnemyThree(int x, int y) {
|
||||
super(x, y);
|
||||
@ -28,6 +29,7 @@ public class EnemyThree extends Enemy{
|
||||
this.setCollisionDamage(10);
|
||||
this.setPosition(GameConfig.windowWidth, random.nextInt(GameConfig.windowHeight - this.getHeight()));
|
||||
this.newY = this.getY();
|
||||
this.entityIterator = Screen.currentScreen.createEntityIterator();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,9 +50,9 @@ public class EnemyThree extends Enemy{
|
||||
this.remove();
|
||||
new EnemyThree(0, 0);
|
||||
}
|
||||
ConcurrentIterator<Entity> i = Screen.currentScreen.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
Entity entity = i.next();
|
||||
entityIterator.reset();
|
||||
while (entityIterator.hasNext()) {
|
||||
Entity entity = entityIterator.next();
|
||||
if(entity instanceof Player){
|
||||
Player player = (Player) entity;
|
||||
if(this.getY() < player.getY()){
|
||||
|
@ -6,22 +6,25 @@ import de.teamteamteam.spacescooter.entity.LivingEntity;
|
||||
import de.teamteamteam.spacescooter.entity.Player;
|
||||
import de.teamteamteam.spacescooter.screen.Screen;
|
||||
|
||||
public abstract class Items extends LivingEntity{
|
||||
public abstract class Items extends LivingEntity {
|
||||
|
||||
private ConcurrentIterator<Entity> entityIterator;
|
||||
|
||||
public Items(int x, int y) {
|
||||
super(x, y);
|
||||
this.setHealthPoints(1);
|
||||
this.entityIterator = Screen.currentScreen.createEntityIterator();
|
||||
}
|
||||
|
||||
public void update(){
|
||||
this.setPosition(getX()-1, getY());
|
||||
this.transpose(-1, 0);
|
||||
if(this.getX() < 0-getWidth()){
|
||||
this.remove();
|
||||
};
|
||||
if(!this.isAlive()){
|
||||
ConcurrentIterator<Entity> i = Screen.currentScreen.getEntityIterator();
|
||||
while(i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
entityIterator.reset();
|
||||
while(entityIterator.hasNext()) {
|
||||
Entity e = entityIterator.next();
|
||||
if(e instanceof Player){
|
||||
itemCollected((Player) e);
|
||||
}
|
||||
|
@ -13,15 +13,18 @@ public class HealthBar extends Entity {
|
||||
private int width = 100;
|
||||
private int height = 24;
|
||||
|
||||
private ConcurrentIterator<Entity> entityIterator;
|
||||
|
||||
public HealthBar(int x, int y) {
|
||||
super(x, y);
|
||||
this.entityIterator = Screen.currentScreen.createEntityIterator();
|
||||
}
|
||||
|
||||
public void paint(Graphics2D g) {
|
||||
Player player = null;
|
||||
ConcurrentIterator<Entity> entities = Screen.currentScreen.getEntityIterator();
|
||||
while(entities.hasNext()) {
|
||||
Entity e = entities.next();
|
||||
this.entityIterator.reset();
|
||||
while(this.entityIterator.hasNext()) {
|
||||
Entity e = this.entityIterator.next();
|
||||
if(e instanceof Player){
|
||||
player = ((Player) e);
|
||||
}
|
||||
|
@ -7,8 +7,6 @@ import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import de.teamteamteam.spacescooter.control.Keyboard;
|
||||
import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
|
||||
import de.teamteamteam.spacescooter.entity.Entity;
|
||||
import de.teamteamteam.spacescooter.entity.Player;
|
||||
import de.teamteamteam.spacescooter.gui.Button;
|
||||
import de.teamteamteam.spacescooter.utility.GameConfig;
|
||||
@ -42,9 +40,9 @@ public class GameOverScreen extends Screen {
|
||||
@Override
|
||||
protected void paint(Graphics2D g) {
|
||||
g.drawImage(this.img, 0, 0, null);
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().paint(g);
|
||||
this.entityPaintIterator.reset();
|
||||
while (this.entityPaintIterator.hasNext()) {
|
||||
this.entityPaintIterator.next().paint(g);
|
||||
}
|
||||
g.setFont(new Font("Monospace", 0, 100));
|
||||
g.setColor(new Color(75 + colorValue, 175 + colorValue, 175 + colorValue));
|
||||
@ -57,9 +55,9 @@ public class GameOverScreen extends Screen {
|
||||
|
||||
@Override
|
||||
protected void update() {
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().update();
|
||||
this.entityUpdateIterator.reset();
|
||||
while (this.entityUpdateIterator.hasNext()) {
|
||||
this.entityUpdateIterator.next().update();
|
||||
}
|
||||
|
||||
if(this.colorValueIncrease) {
|
||||
|
@ -7,8 +7,6 @@ import java.awt.event.KeyEvent;
|
||||
import java.awt.image.BufferedImage;
|
||||
|
||||
import de.teamteamteam.spacescooter.control.Keyboard;
|
||||
import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
|
||||
import de.teamteamteam.spacescooter.entity.Entity;
|
||||
import de.teamteamteam.spacescooter.entity.Player;
|
||||
import de.teamteamteam.spacescooter.gui.Button;
|
||||
import de.teamteamteam.spacescooter.utility.GameConfig;
|
||||
@ -42,9 +40,9 @@ public class GamePausedScreen extends Screen {
|
||||
@Override
|
||||
protected void paint(Graphics2D g) {
|
||||
g.drawImage(this.img, 0, 0, null);
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().paint(g);
|
||||
this.entityPaintIterator.reset();
|
||||
while (this.entityPaintIterator.hasNext()) {
|
||||
this.entityPaintIterator.next().paint(g);
|
||||
}
|
||||
g.setFont(new Font("Monospace", 0, 100));
|
||||
g.setColor(new Color(75 + colorValue, 175 + colorValue, 175 + colorValue));
|
||||
@ -57,9 +55,9 @@ public class GamePausedScreen extends Screen {
|
||||
|
||||
@Override
|
||||
protected void update() {
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().update();
|
||||
this.entityUpdateIterator.reset();
|
||||
while (this.entityUpdateIterator.hasNext()) {
|
||||
this.entityUpdateIterator.next().update();
|
||||
}
|
||||
|
||||
if(this.colorValueIncrease) {
|
||||
|
@ -7,8 +7,6 @@ import java.util.ArrayList;
|
||||
|
||||
import de.teamteamteam.spacescooter.background.StarBackground;
|
||||
import de.teamteamteam.spacescooter.control.Keyboard;
|
||||
import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
|
||||
import de.teamteamteam.spacescooter.entity.Entity;
|
||||
import de.teamteamteam.spacescooter.entity.Player;
|
||||
import de.teamteamteam.spacescooter.entity.enemy.EnemyFour;
|
||||
import de.teamteamteam.spacescooter.entity.enemy.EnemyThree;
|
||||
@ -44,17 +42,17 @@ public class GameScreen extends Screen {
|
||||
|
||||
@Override
|
||||
protected void paint(Graphics2D g) {
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().paint(g);
|
||||
this.entityPaintIterator.reset();
|
||||
while (this.entityPaintIterator.hasNext()) {
|
||||
this.entityPaintIterator.next().paint(g);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void update() {
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().update();
|
||||
this.entityUpdateIterator.reset();
|
||||
while (this.entityUpdateIterator.hasNext()) {
|
||||
this.entityUpdateIterator.next().update();
|
||||
}
|
||||
// Pass the collision handler a copy of the entity list
|
||||
CollisionHandler.handleCollisions();
|
||||
|
@ -7,8 +7,6 @@ import java.awt.event.KeyEvent;
|
||||
|
||||
import de.teamteamteam.spacescooter.background.StarBackground;
|
||||
import de.teamteamteam.spacescooter.control.Keyboard;
|
||||
import de.teamteamteam.spacescooter.datastructure.ConcurrentIterator;
|
||||
import de.teamteamteam.spacescooter.entity.Entity;
|
||||
import de.teamteamteam.spacescooter.entity.Player;
|
||||
import de.teamteamteam.spacescooter.gui.Button;
|
||||
import de.teamteamteam.spacescooter.utility.GameConfig;
|
||||
@ -41,9 +39,9 @@ public class MainMenuScreen extends Screen {
|
||||
|
||||
@Override
|
||||
public void paint(Graphics2D g) {
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().paint(g);
|
||||
this.entityPaintIterator.reset();
|
||||
while (this.entityPaintIterator.hasNext()) {
|
||||
this.entityPaintIterator.next().paint(g);
|
||||
}
|
||||
g.setFont(new Font("Monospace", 0, 70));
|
||||
g.setColor(new Color(75 + colorValue, 175 + colorValue, 175 + colorValue));
|
||||
@ -59,9 +57,9 @@ public class MainMenuScreen extends Screen {
|
||||
|
||||
@Override
|
||||
public void update() {
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while (i.hasNext()) {
|
||||
i.next().update();
|
||||
this.entityUpdateIterator.reset();
|
||||
while (this.entityUpdateIterator.hasNext()) {
|
||||
this.entityUpdateIterator.next().update();
|
||||
}
|
||||
|
||||
if(colorValueIncrease) {
|
||||
|
@ -35,6 +35,21 @@ public abstract class Screen {
|
||||
*/
|
||||
private ConcurrentLinkedList<Entity> entities;
|
||||
|
||||
/**
|
||||
* Internal entity Iterator instance for updating
|
||||
*/
|
||||
protected ConcurrentIterator<Entity> entityUpdateIterator;
|
||||
|
||||
/**
|
||||
* Internal entity Iterator instance for painting
|
||||
*/
|
||||
protected ConcurrentIterator<Entity> entityPaintIterator;
|
||||
|
||||
/**
|
||||
* Internal entity Iterator instances for collision detection
|
||||
*/
|
||||
private ConcurrentIterator<Entity> collisionIteratorOne;
|
||||
private ConcurrentIterator<Entity> collisionIteratorTwo;
|
||||
|
||||
/**
|
||||
* Initialize parent, overlay and the Entity list
|
||||
@ -43,6 +58,10 @@ public abstract class Screen {
|
||||
this.setOverlay(null);
|
||||
this.parent = parent;
|
||||
this.entities = new ConcurrentLinkedList<Entity>();
|
||||
this.entityUpdateIterator = this.entities.iterator();
|
||||
this.entityPaintIterator = this.entities.iterator();
|
||||
this.collisionIteratorOne = this.entities.iterator();
|
||||
this.collisionIteratorTwo = this.entities.iterator();
|
||||
}
|
||||
|
||||
|
||||
@ -74,7 +93,7 @@ public abstract class Screen {
|
||||
* Get an Iterator over the Entity List.
|
||||
* Use this within update method context!
|
||||
*/
|
||||
public final ConcurrentIterator<Entity> getEntityIterator() {
|
||||
public final ConcurrentIterator<Entity> createEntityIterator() {
|
||||
return this.entities.iterator();
|
||||
}
|
||||
|
||||
@ -125,10 +144,26 @@ public abstract class Screen {
|
||||
private final void cleanup() {
|
||||
if(this.overlay != null) this.overlay.cleanup();
|
||||
//tell all entities to cleanup themselves.
|
||||
ConcurrentIterator<Entity> i = this.getEntityIterator();
|
||||
while(i.hasNext()) {
|
||||
Entity e = i.next();
|
||||
this.entityUpdateIterator.reset();
|
||||
while(this.entityUpdateIterator.hasNext()) {
|
||||
Entity e = this.entityUpdateIterator.next();
|
||||
e.remove();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns second collision iterator.
|
||||
* These are used for collision detection _only_!
|
||||
*/
|
||||
public ConcurrentIterator<Entity> getCollisionIteratorOne() {
|
||||
return this.collisionIteratorOne;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns second collision iterator.
|
||||
* These are used for collision detection _only_!
|
||||
*/
|
||||
public ConcurrentIterator<Entity> getCollisionIteratorTwo() {
|
||||
return this.collisionIteratorTwo;
|
||||
}
|
||||
}
|
||||
|
@ -23,8 +23,9 @@ public class CollisionHandler {
|
||||
* then intelligently check them against each other to reduce the amount of checks.
|
||||
*/
|
||||
public static void handleCollisions() {
|
||||
ConcurrentIterator<Entity> iteratorOne = Screen.currentScreen.getEntityIterator();
|
||||
ConcurrentIterator<Entity> iteratorTwo = Screen.currentScreen.getEntityIterator();
|
||||
ConcurrentIterator<Entity> iteratorOne = Screen.currentScreen.getCollisionIteratorOne();
|
||||
ConcurrentIterator<Entity> iteratorTwo = Screen.currentScreen.getCollisionIteratorTwo();
|
||||
iteratorOne.reset();
|
||||
while(iteratorOne.hasNext()) {
|
||||
Entity entityOne = iteratorOne.next();
|
||||
//Only check Player and Enemy for the active side of a collision.
|
||||
|
Loading…
x
Reference in New Issue
Block a user