I’m having a ton of issues with performance and it seems to be a CPU issue. I think I might be trying to do far too much with my game.
Its a top down RTS style game and you have 20 units. The problem is that I’m addicted to loops and arraylists. I dont know any other way to do some of the things I need to do. I am using active rendering and FSEM. My FPS is supposed to be 60. I’m going to post most of the relevant code, but it is quite long so don’t feel obligated to sift through my horrible…horrible code.
Be warned, its ugly and probably bad programming. All of this gets done 60 times a second. I’m sure that there is a much better way to do this so it doesn’t need to be done every 60 seconds. But the screen needs to move in response to the mouse going to the edge of the screen so things need to be updated constantly. Also, I have never made a habit out of commenting so seriously, DO NOT FEEL OBLIGATED TO READ MY CODE, but any help would be appreciated. Thanks to anyone who will hold my hand through this =)
private void gameRender(Graphics dbg)
{
counter++;
if(counter == 61)counter = 0;
if(dbImage == null){
dbImage = createImage(pWidth, pHeight);
if(dbImage == null){
System.out.println("dbImage is null!");
return;
}else
dbg = dbImage.getGraphics();
}
g2 = (Graphics2D)dbg;
dbg.setColor(Color.black);
dbg.fillRect(0, 0, pWidth, pHeight);
//draw game elements
//draw main screen
screen = new Rectangle2D.Double(-x, -y, 1024, 588);
for(int i = 0; i < 12; i++){
for(int p = 0; p < 7; p++){
int tile;
int xMod;
int yMod;
int x1Mod;
int y1Mod;
xMod = 0;
yMod = 0;
x1Mod = x%100;
y1Mod = y%100;
if(-x > 99) xMod = -(x/100);
if(-y > 99) yMod = -(y/100);
tile = mL.getTile(xMod + i, yMod + p);
if(tile == 48) g2.drawImage(grass, (i*100)+x1Mod, (p*100)+y1Mod, null);
if(tile == 49) g2.drawImage(tree, (i*100)+x1Mod, (p*100)+y1Mod, null);
}
}
for(int i = 0; i < 20; i++){
if(counter == 60)combat(i);
if(p1Units.get(i).getBox().intersects(screen)){
if(playerNum == 2){
g2.setColor(Color.red);
g2.draw(new Ellipse2D.Double(p1Units.get(i).getBox().getX()+x+2.5, p1Units.get(i).getBox().getY()+45+y, p1Units.get(i).getBox().getWidth(),p1Units.get(i).getBox().getWidth()));
}
for(int p = 0; p < 5; p++){
if(i == selectedUnit[p]){
g2.setColor(Color.white);
g2.draw(new Ellipse2D.Double(p1Units.get(i).getBox().getX()+x+2.5, p1Units.get(i).getBox().getY()+45+y, p1Units.get(i).getBox().getWidth(),p1Units.get(i).getBox().getWidth()));
}
}
if(p1Units.get(i).getType() == "tank"){
g2.drawImage(tank.get(p1Units.get(i).getFacing()-1), (int)p1Units.get(i).getX() + x, (int)p1Units.get(i).getY() + y, null);
}
if(p1Units.get(i).getType() == "artillery"){
g2.drawImage(artillery.get(p1Units.get(i).getFacing()-1), (int)p1Units.get(i).getX() + x, (int)p1Units.get(i).getY() + y, null);
}
}
if(p2Units.get(i).getBox().intersects(screen)){
if(playerNum == 1){
g2.setColor(Color.red);
g2.draw(new Ellipse2D.Double(p2Units.get(i).getBox().getX()+x+2.5, p2Units.get(i).getBox().getY()+45+y, p2Units.get(i).getBox().getWidth(),p2Units.get(i).getBox().getWidth()));
}
g2.drawImage(tank.get(p2Units.get(i).getFacing()), (int)p2Units.get(i).getX() + x, (int)p2Units.get(i).getY() + y, null);
//if(playerNum == 1){
//g2.fill(new Rectangle2D.Double(p2Units.get(i).getX()+x+40, p2Units.get(i).getY()+y+100, 100, 10));
//g2.setColor(Color.green);
// g2.fill(new Rectangle2D.Double(p2Units.get(i).getX()+x+40, p2Units.get(i).getY()+y+100, p2Units.get(i).getHealth(), 10));
// }
}
if(p1Units.get(i).getType() == "artillery" && p1Units.get(i).isFiring()){
Point2D point = explosionHandler.drawExplosion(i, counter);
if(point.getX() != 0 && point.getY() != 0){
g2.drawImage(explosion, (int)point.getX()-55+x, (int)point.getY()-48+y, null);
}
}
}
//draw border
g2.setColor(Color.white);
g2.drawImage(hud, 0, 588, null);
g2.drawImage(minimap, 849, 593, null);
for(int i = 0; i < 20; i++){
g2.setColor(Color.blue);
g2.fill(new Rectangle2D.Double((p1Units.get(i).getX()/22.86) + 849, (p1Units.get(i).getY()/22.86)+593, 4.37, 4.37));
g2.setColor(Color.red);
g2.fill(new Rectangle2D.Double((p2Units.get(i).getX()/22.86) + 849, (p2Units.get(i).getY()/22.86)+593, 4.37, 4.37));
}
if(selectedUnit[0] != 21){
if(p1Units.get(selectedUnit[0]).getType() == "artillery"){
Unit u = p1Units.get(selectedUnit[0]);
if(u.getStance() == "mobile") g2.drawImage(mobile, 775, 598, null);
else g2.drawImage(fire, 775, 598, null);
}
}
g2.setColor(Color.yellow);
g2.draw(new Rectangle2D.Double((849+(-x/22.86)), (593 + (-y/22.86)), 44.79, 25.94));
//g2.fill(new Rectangle2D.Double(0, 568, 1024, 200));
}
private void combat(int index){
int i2 = p1Units.get(index).getTheTarget();
if(p1Units.get(index).isFiring()){
if(i2 != 21){
if(!p2Units.get(i2).isDead()){
if(!p1Units.get(index).isInRange(p2Units.get(p1Units.get(index).getTheTarget()).getX(), p2Units.get(p1Units.get(index).getTheTarget()).getY())){
p1Units.get(index).setIsFiring(false);
p1Units.get(index).setTarget((int)p2Units.get(i2).getX(), (int)p2Units.get(i2).getY());
p1Units.get(index).setIsMoving(true);
}
p2Units.get(i2).attacked(p1Units.get(index).getExplosionAttack(), p1Units.get(index).getBulletAttack());
}else{
p1Units.get(index).setTarget(21);
p1Units.get(index).setIsFiring(false);
}
}
}
for(int i = 0; i < 20; i++){
if(p1Units.get(index).isInRange(p2Units.get(i).getX(), p2Units.get(i).getY())){
p1Units.get(index).setIsFiring(true);
p1Units.get(index).setTarget(i);
}
}
if(i2 != 21){
if(p2Units.get(i2).isDead()){
p1Units.get(index).setTarget(21);
p1Units.get(index).setIsFiring(false);
}
}
}