Hello my game lags on android when about 3 zombies are on the map I know the problem however i have no idea how to fix it
The problem is the way i have done the path finding i am calculating it every frame i am not sure what to do i have tried using a delay but the zombie gitter up and down
here is the code the problem starts at “updateAndDebugPathFinding” // TODO FIX LAG!!!
package com.hawk.defend.entity;
import java.util.ArrayList;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.IntArray;
import com.hawk.defend.coop.packets.PacketUpdateAi;
import com.hawk.defend.entity.bullet.Bullet;
import com.hawk.defend.entity.handler.Entity;
import com.hawk.defend.entity.handler.GameActor;
import com.hawk.defend.entity.path.Pathfinder;
import com.hawk.defend.game.CurrentMap;
import com.hawk.defend.game.MainGame;
import com.hawk.defend.utils.Delay;
import com.hawk.defend.window.GameWindow;
public class Enemie extends GameActor {
public static final int ZOMBIE = 0;
public static boolean DEBUG = false;
private Pathfinder pathfinder;
private Delay clientDelay;
private int typeID;
public Enemie(int entityID, float x, float y, float width, float height) {
super(entityID, x, y, width, height);
pathfinder = new Pathfinder((TiledMapTileLayer) MainGame.getMap().getMap().getLayers().get(CurrentMap.MAINLAYER), (TiledMapTileLayer) MainGame.getMap().getMap().getLayers().get(CurrentMap.OBJECTLAYER));
setClientDelay(new Delay(0.02f) {
@Override
public void actionPerformed() {
if (MainGame.client != null && MainGame.isHost()) {
PacketUpdateAi update = new PacketUpdateAi();
update.entityID = getEntityID();
update.typeID = typeID;
update.x = getX();
update.y = getY();
update.health = getHealth();
MainGame.client.sendTCP(update);
}
}
});
}
public Pathfinder getPathfinder() {
return pathfinder;
}
public void setPathfinder(Pathfinder pathfinder) {
this.pathfinder = pathfinder;
}
public int getTypeID() {
return typeID;
}
public void setTypeID(int typeID) {
this.typeID = typeID;
}
@Override
public void render(float delta) {
super.render(delta);
if (MainGame.isHost()) {
updateAndDebugPathFinding(delta); // TODO FIX LAG!!!!!!!
}
}
@Override
public void update(float delta) {
super.update(delta);
clientDelay.update(delta);
updateBulletDamage(delta);
}
private void updateBulletDamage(float delta){
for(int i = 0;i < MainGame.bullets.getBullets().size();i++){
Bullet b = MainGame.bullets.getBullets().get(i);
if(b.getBoundingRectangle().overlaps(getBoundingRectangle())){
removeHealth(b.getDamage());
MainGame.bullets.removeBullet(b);
i--;
}
}
}
private void updateAndDebugPathFinding(float delta) {
int mapWidth = pathfinder.getMap().getWidth();
int mapHeight = pathfinder.getMap().getHeight();
int cellWidth = (int) pathfinder.getMap().getTileWidth();
int cellHeight = (int) pathfinder.getMap().getTileHeight();
int startX = (int) ((getX() + (getWidth() / 2)) / cellWidth);
int startY = (int) ((getY() + getHeight() / 2) / cellHeight);
Vector2 pos = calculateClosesPlayer(getX(), getY());
int targetX = (int) (pos.x + Entity.SIZE/2) / cellWidth;
int targetY = (int) (pos.y + Entity.SIZE/2) / cellHeight;
if (DEBUG) {
GameWindow.cameraShape.begin(ShapeType.Filled);
GameWindow.cameraShape.setColor(Color.GREEN);
GameWindow.cameraShape.rect(startX * cellWidth, startY * cellHeight, cellWidth, cellHeight);
GameWindow.cameraShape.setColor(Color.RED);
GameWindow.cameraShape.rect(targetX * cellWidth, targetY * cellHeight, cellWidth, cellHeight);
}
if (startX >= 0 && startY >= 0 && startX < mapWidth && startY < mapHeight) {
IntArray path = pathfinder.getAstar().getPath(startX, startY, targetX, targetY);
if (path.size <= 0) {
setVelocity(new Vector2());
}
for (int i = 0, n = path.size; i < n; i += 2) {
followNodes(path.get(i), path.get(i + 1), cellWidth, cellHeight);
int x = path.get(i);
int y = path.get(i + 1);
if (DEBUG) {
GameWindow.cameraShape.setColor(Color.GREEN);
GameWindow.cameraShape.circle(x * cellWidth + cellWidth / 2, y * cellHeight + cellHeight / 2, cellWidth / 4, 30);
}
}
}
GameWindow.cameraShape.end();
}
private Vector2 calculateClosesPlayer(float currentX, float currentY) {
Vector2 posVector = new Vector2();
ArrayList<Player> players = new ArrayList<Player>();
// Gets all players
for (int i = 0; i < MainGame.getPlayerHandler().getEntitys().size; i++) {
Entity e = MainGame.getPlayerHandler().getEntitys().get(i);
if (e instanceof Player) {
Player p = (Player) e;
players.add(p);
}
}
if(players.size() <= 0) return new Vector2(currentX,currentY);
if (players.size() <= 1) {
Player p = players.get(0);
return new Vector2(p.getX(), p.getY());
}
ArrayList<Float> posX = new ArrayList<Float>();
ArrayList<Float> posY = new ArrayList<Float>();
for (int i = 0; i < players.size(); i++) {
Player p = players.get(i);
posX.add(Math.abs(p.getX() - currentX));
posY.add(Math.abs(p.getY() - currentY));
}
float smallestNumber = Float.MAX_VALUE;
int index = 0;
for (int i = 0; i < players.size(); i++) {
if (smallestNumber > posX.get(i)) {
smallestNumber = posX.get(i);
index = i;
}
}
Player returnPlayer = (players.get(index));
posVector = new Vector2(returnPlayer.getX(), returnPlayer.getY());
return posVector;
}
private void followNodes(float x, float y, int cellW, int cellH) {
if ((getX() / cellW) < x && (getX() / cellW) < x + getWidth()) {
getVelocity().x = 100;
} else if ((getX() / cellW) > x && (getX() / cellW) > x - getWidth()) {
getVelocity().x = -100;
} else {
getVelocity().x = 0;
}
if ((getY() / cellH) < y && (getY() / cellH) < y + getHeight()) {
getVelocity().y = 100;
} else if ((getY() / cellH) > y && (getY() / cellH) > y - getHeight()) {
getVelocity().y = -100;
} else {
getVelocity().y = 0;
}
}
public Delay getClientDelay() {
return clientDelay;
}
public void setClientDelay(Delay clientDelay) {
this.clientDelay = clientDelay;
}
}