I am creating a 2d game and want to add a feature so that the “cell” kinda floats around in the area from where it spawned. I am trying to get the original location from where it was spawned, but it keeps changing. I have nothing mentioning it or setting it other than when I set it’s spawn in the constructor. Does anyone see why it is changing?
Cell Class:
public class Cell extends Entity{
private Random random;
private CellType cellType;
private Point spawn;
private int angle;
private float xVelocity, yVelocity;
private float maxVelocity = .2f;
public Cell(Point location) {
random = new Random();
cellType = MasterGame.cellTypes.get(random.nextInt(MasterGame.cellTypes.size()));
width = MasterGame.cellSizes.get(cellType);
height = width;
this.spawn = location;
super.location = location;
}
int ticks = 0;
public void tick() {
if(ticks == 15) {
angle = random.nextInt(360);
xVelocity = (float) (maxVelocity * Math.cos(angle));
yVelocity = (float) (maxVelocity * Math.sin(angle));
ticks = 0;
}
if(ticks % 3 == 0){
System.out.println(spawn);
location.x += xVelocity;
location.y += yVelocity;
}
ticks++;
}
public void render(Graphics g) {
g.drawImage(Assets.cell, location.x, location.y, width, height, null);
}
}
And here is the Entity Class that Cell extends
public abstract class Entity {
protected int width, height;
protected Point location;
protected CellType cellType;
abstract void tick();
abstract void render(Graphics g);
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public Point getLocation() {
return location;
}
public CellType getCellType() {
return cellType;
}
}