So this is not so much a debug problem but more of a “is this the best approach” but since I am new, it goes here.
I am in the process of coding the core of my game and I am using a few LibGDX arrays however the question applies to any arrays since I am mainly concerned with my use of foreach loops.
Currently at the moment I have 2 arrays in the world, 1 to hold enemies and 1 to hold collidable objects, such as asteroids or debris.
The ships in the game, enemy and player have 3 arrays each, one for holding every weapon object they have, one for holding every electronic system (most basic one being a targetting system, others to come) and one for holding every defense system (such as hull repairers, shield upgrades etc etc) and one holding system modifiers, such as damage amplifiers, engine mods, extra fuel bay, ammo bay etc etc.
It is essentially modules that get stored in these and in order to get access to them I am CONSTANTLY needing to use loops such as :
for (Electronic electronic : currentShip.getElectronic()) {
for (Ship ship : GameScreen.ships) {
if (touchPos.x > ship.getBody().getPosition().x
- (ship.getWidth() / GameScreen.WORLD_TO_BOX_WIDTH)
&& touchPos.x < ship.getPosition().x + ship.getWidth()
/ GameScreen.WORLD_TO_BOX_WIDTH
&& touchPos.y > ship.getBody().getPosition().y
- (ship.getHeight() / GameScreen.WORLD_TO_BOX_HEIGHT)
&& touchPos.y < ship.getBody().getPosition().y
+ ship.getHeight()
/ GameScreen.WORLD_TO_BOX_HEIGHT) {
tmpSelect = ship;
if (ships.contains(tmpSelect, false)) {
return true;
}
} else {
if (electronic.selectedTarget != null) {
return false;
}
}
}
}
Now these are literally everywhere in my code and almost any class that handles any sort of module activation/usage has to have access to that array so that the game knows which target you want to shoot, lock, unlock etc etc, so I made it static (i know, bad idea. Some classes have it passed to it in the constructor).
Another example where I am using the same code:
for (Electronic electronic : currentShip.getElectronic()) {
if (currentShip != null) {
if (button == 0)
if (electronic.targetClicked(touchPos, currentShip, ships)) {
electronic.selectedTarget = electronic.getTmpSelect();
} else {
electronic.selectedTarget = null;
uICore.removeRightClickMenu();
}
if (button == 1) {
if (uICore.hasRightClickMenu()) {
uICore.removeRightClickMenu();
}
if(!electronic.inRange(currentShip.getPosition(), tmpVector)){
uICore.rightClickMenu(false, touchPos);
}
if(electronic.targetClicked(touchPos, currentShip, ships) && electronic.lockedTargets.contains(electronic.selectedTarget, false)){
uICore.rightClickMenu(false, touchPosUI);
}
if (electronic.targetClicked(touchPos, currentShip, ships))
uICore.rightClickMenu(true, touchPosUI);
}
}
}
The above code is in my controller based class, which pretty much talks to everything in order to draw the very little UI I have atm.
Is this the best way to access my arrays? I don’t think it is inefficient since none of these loops are in the game loop and being iterated 60 times a second, well one is in the loop but it has a conditional statement before it will execute.
Anyone know any other way or am I ok to use those loops? I so far have zero broken code with using them, now that I am used to it lol.