Skip to content

Commit

Permalink
Optimize the entity handler to avoid shifting and resizing the array
Browse files Browse the repository at this point in the history
Also keeps some debug stuff (the entity handler alive/dead entities)
rendered, but I'll keep that for now.
  • Loading branch information
evanw555 committed Sep 3, 2021
1 parent 35b00d8 commit 805733b
Show file tree
Hide file tree
Showing 49 changed files with 100 additions and 14 deletions.
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Asteroids.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added out/production/Asteroids/ADriver.class
Binary file not shown.
Binary file added out/production/Asteroids/Amoeba.class
Binary file not shown.
Binary file added out/production/Asteroids/Asteroid.class
Binary file not shown.
Binary file added out/production/Asteroids/ClusterMissile.class
Binary file not shown.
Binary file added out/production/Asteroids/Comet.class
Binary file not shown.
Binary file added out/production/Asteroids/Controls.class
Binary file not shown.
Binary file added out/production/Asteroids/ControlsComponent.class
Binary file not shown.
Binary file added out/production/Asteroids/Electrode.class
Binary file not shown.
Binary file added out/production/Asteroids/Entity.class
Binary file not shown.
Binary file added out/production/Asteroids/EntityHandler.class
Binary file not shown.
Binary file added out/production/Asteroids/Event.class
Binary file not shown.
Binary file added out/production/Asteroids/EventEntity.class
Binary file not shown.
Binary file added out/production/Asteroids/Game$1.class
Binary file not shown.
Binary file added out/production/Asteroids/Game.class
Binary file not shown.
Binary file added out/production/Asteroids/GameCanvas.class
Binary file not shown.
Binary file added out/production/Asteroids/GameSetup.class
Binary file not shown.
Binary file added out/production/Asteroids/Item.class
Binary file not shown.
Binary file added out/production/Asteroids/ItemEntity.class
Binary file not shown.
Binary file added out/production/Asteroids/KeyHandler.class
Binary file not shown.
Binary file added out/production/Asteroids/LogEntry.class
Binary file not shown.
Binary file added out/production/Asteroids/Missile.class
Binary file not shown.
Binary file added out/production/Asteroids/Player.class
Binary file not shown.
Binary file added out/production/Asteroids/PlayerSettings$1.class
Binary file not shown.
Binary file added out/production/Asteroids/PlayerSettings$2.class
Binary file not shown.
Binary file added out/production/Asteroids/PlayerSettings$3.class
Binary file not shown.
Binary file added out/production/Asteroids/PlayerSettings$4.class
Binary file not shown.
Binary file added out/production/Asteroids/PlayerSettings.class
Binary file not shown.
Binary file added out/production/Asteroids/Refs.class
Binary file not shown.
Binary file added out/production/Asteroids/ScreenSizer$1.class
Binary file not shown.
Binary file added out/production/Asteroids/ScreenSizer$2.class
Binary file not shown.
Binary file added out/production/Asteroids/ScreenSizer$3.class
Binary file not shown.
Binary file added out/production/Asteroids/ScreenSizer.class
Binary file not shown.
Binary file added out/production/Asteroids/Shard.class
Binary file not shown.
Binary file added out/production/Asteroids/ShipDesignCanvas.class
Binary file not shown.
Binary file added out/production/Asteroids/Stalker.class
Binary file not shown.
Binary file added out/production/Asteroids/TextFloat.class
Binary file not shown.
Binary file added out/production/Asteroids/TextLog.class
Binary file not shown.
Binary file added out/production/Asteroids/TimedEntityShell.class
Binary file not shown.
Binary file added out/production/Asteroids/TimedParticle.class
Binary file not shown.
Binary file added out/production/Asteroids/Util.class
Binary file not shown.
Binary file added out/production/Asteroids/Wave.class
Binary file not shown.
75 changes: 62 additions & 13 deletions src/EntityHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class EntityHandler{
private ArrayList<Entity> ents, purgatory;
private ArrayList<Entity> ents;
private Queue<Entity> purgatory;
private ArrayList<TimedEntityShell> timedPurgatory;
private Player[] players;
private int asteroidsDestroyed;
private long framesSurpassed;

public EntityHandler(Player...players){
ents = new ArrayList<Entity>();
purgatory = new ArrayList<Entity>();
purgatory = new LinkedList<>();
timedPurgatory = new ArrayList<TimedEntityShell>();
this.players = players;
this.asteroidsDestroyed = 0;
Expand All @@ -34,30 +37,57 @@ public void update(){
//remove duplicates of players
removePlayerDuplicates();
//transfer each entity from purgatory to ents
for(Entity e : purgatory)
ents.add(e);
purgatory.clear();
allocateEntities();
//update each entity on ents
for(Entity e : ents)
e.update();
int lastAliveIndex = -1;
for(int i = 0; i < ents.size(); i++) {
final Entity e = ents.get(i);
if (!e.isDead()) {
e.update();
lastAliveIndex = i;
}
}
if (lastAliveIndex > 0 && lastAliveIndex + 1 < ents.size() && lastAliveIndex < Math.floor(ents.size() * .8)) {
ents.subList(lastAliveIndex + 1, ents.size()).clear();
}
//check collisions
checkCollisions();
//handle random entity addition if not between waves
if(!Refs.game.isInterwavePhase())
addRandomEntities();
}

private void allocateEntities() {
int entsIndex = 0;
Entity poppedPurgatoryEntity = purgatory.poll();
while (poppedPurgatoryEntity != null) {
if (entsIndex < ents.size()) {
if (ents.get(entsIndex).isDead()) {
ents.set(entsIndex, poppedPurgatoryEntity);
poppedPurgatoryEntity = purgatory.poll();
}
entsIndex++;
} else {
ents.add(poppedPurgatoryEntity);
poppedPurgatoryEntity = purgatory.poll();
}
}
purgatory.clear();
}

public void cleanUp(){
for(int i = 0; i < ents.size(); i++)
if(ents.get(i).isDead()){
ents.remove(i);
i--;
}
// for(int i = 0; i < ents.size(); i++)
// if(ents.get(i).isDead()){
// ents.remove(i);
// i--;
// }
}

public void paint(Graphics g){
for(Entity e : ents)
e.paint(g);
if (!e.isDead()) {
e.paint(g);
}
//game over text
if(!Util.contains(ents, Player.class) && timedPurgatory.isEmpty()){
g.setColor(Color.RED);
Expand All @@ -76,6 +106,25 @@ public void paint(Graphics g){
g.drawString("Finished 11 June 2012",
Refs.canvas.getPlayableWidth()/4, Refs.canvas.getHeight()/2+210);
}

// Paint representation of the entity list
for (int i = 0; i < ents.size(); i++) {
if (ents.get(i).isDead()) {
g.setColor(Color.RED);
} else {
g.setColor(Color.GREEN);
}
final int numPerRow = Refs.canvas.getPlayableWidth() / 8;
final int row = i / numPerRow;
final int col = i % numPerRow;
g.fillRect(col * 8, row * 8, 8, 8);
g.setColor(Color.BLACK);
g.drawRect(col * 8, row * 8, 8, 8);
}

g.setFont(new Font("Arial", Font.PLAIN, 14));
g.setColor(Color.WHITE);
g.drawString(String.format("ents size = %d, purgatory size = %d", ents.size(), purgatory.size()), 100, 40);
}

public void add(Entity e){
Expand Down
1 change: 1 addition & 0 deletions src/Game.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
Expand Down
4 changes: 3 additions & 1 deletion src/TimedParticle.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ public TimedParticle(int type, int x, int y, double vx, double vy, Color color,
public void update(){
x += vx;
y += vy;
duration--;
if (duration > 0) {
duration--;
}
switch(type){
case EMP:
vx *= .99;
Expand Down

0 comments on commit 805733b

Please sign in to comment.