I've written a code that allows me to talk with an NPC. This works as long as there is only 1 NPC on the map. I'll explain why.
public void getNPCCollision(){
for (int i = players.size() - 1; i >= 0; i--) {
Player player = players.get(i);
for (int j = beings.size() - 1; j >= 0; j--) {
Being being = beings.get(j);
if(player.getBounds().overlaps(being.getBounds())) {
setX(oldX);
velocity.x = 0;
setY(oldY);
velocity.y = 0;
}
else if(player.getBounds().overlaps(being.getTalkingBounds())) {
canTalk = true;
talkAbleBeing = being;
}
else{
talkAbleBeing = null;
canTalk = false;
}
}
}
}
I check every update if my Player walks into an NPC's Talking Rectangle. It goes by every NPC on the map. The thing is If I'm not on NPC 1's rectangle but on NPC 2's rectangle, It'll still set canTalk to False because I'm indeed not in his rectangle.
Is there any way I can fix this issue? Please, any help appreciated.