I’m making a vertical shooter game and I am having trouble with collision detection. Collisions are detected through the Rectangle method “intersect”, but in order to keep track of everything that could collide, I need Arraylists to keep track of all the bullets and enemy ships. The Enemy and Player class both spawn Bullets (which also have there own class) so I would like to have 2 different Arraylists in my GameView class (which controls the games graphics and hopefully collisions when I’m done here).
What would be the most efficient way to allow the Bullets to be added to their respective ArrayLists upon being spawned?
Bullet class:
public class Bullet extends Location{
private Fights bulletOwner;
private int damage;
private int velocity;
public Bullet(Fights owner, int dmg, Rectangle loca)
{
super(loca);
bulletOwner = owner;
damage = dmg;
}
public Fights getOwner(){return bulletOwner;}
public int getDamage(){return damage;}
public int getVelocity(){return velocity;}
}
Location class
import java.awt.Rectangle;
public class Location {
protected Rectangle loc;
public Location (Rectangle location)
{
loc = location;
}
public Rectangle getLocation()
{
return loc;
}
public void updateLocation(Rectangle location)
{
loc = location;
}
}
You can have a GameState class that has the arraylist of locations and pass the GameState instance as an argument to the constructor of the Location derived classes.