I am in the process of making a zty.pe game, and I have defined classes for own ship, enemies (those meant to be shot with words) as well as a projectile which own ship fires. The Enemy-class item has a string assigned when it is created. When the user presses a key on the keyboard Enemy-class will then react if it corresponds to the first character of the assigned string:
if (outputString.length() >= 1 && key == outputString.charAt(0) {
manipulString.deleteCharAt(0);
projectiles.add(new Projectile(location.x, location.y));
score = score + 1;
}
This works, however, it is not good enough, as it should only choose the item which is corresponds and is closest.
I am trying to remedy this by making a new ArrayList out of the ArrayList of enemies, where I only include the items with the first character of the word/belonging string.
boolean corresponding() {
if (outputString.length() >= 1 && key == outputString.charAt(0))
return true;
}
I would then like to sort the items after which items have the closest PVector location
, i.e. sort after
enemies.location.dist(new PVector(width/2, height-40));
which is the distance to the mothership/own ship. I have searched for ways to sort ArrayLists with PVector, however, it seems to not be applicable in this case.
Gif of how it currently works. Additionally, if two characters repeat, it will remove both of them.
Thank you in advance. Please tell me if this is not understandable. Alternatively, I have linked my GitHub repository where fjender.pde
is the Enemy class, skib.pde
is the OwnShip class, projektilet.pde
is the Projectile class and the main sketch is Spiludvikling.pde
.