Problem with game (PLEASE HELP!)

Hello, People :)
I'm creating a tower defence game with SFML engine. I have been creating it until i reached a problem which i can't solve for about 2 weeks. I am trying to make that cannons would shoot the enemy which is furtherest from tower but in the tower shooting range AND make the cannons look at the same enemy (rotate cannons to the target). The problem is, that cannons are shooting the enemy which they should shoot, but are rotating to the enemy which is furtherest at the WHOLE MAP (not at the cannon shooting range there cannons are shooting). SO PLEASE HELP ME IF YOU CAN, BECAUSE I CAN'T CONTINUE THIS GAME MAKING WITHOUT SOLVING THIS.

There's some of the code !

engine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
for (unsigned int i = 0; i < Towers.size(); i++) {
	Towers[i].tower_shooting_time+=dt;
		float maxNum=0;
		int   maxindex=-1;
		for (unsigned int e = 0; e < enemy.size(); e++) {
			vector2 collision = Towers[i].position - enemy[e].position;
			float lenght = collision.Length();
			if (lenght < Towers[i].radius + enemy[e].radius) { //Radius = tower shooting range
				if (enemy[e].k_p > maxNum) { //k_p is every single enemies walked road distance
					maxNum=enemy[e].k_p;  
					maxindex=e;		//finds the furtherest enemy at cannons shooting range
					for (unsigned int c = 0; c < Cannons.size(); c++) {
						Cannons[c].direction(enemy[maxindex].pozition - Cannons[c].pozition); //Cannon rotation
					}
				}
			}
		}
        }


engine.h
std::vector<object> Towers, Cannons, enemy;
object is the second class of my game (first is engine class), in object class are the functions to control sprite speed, rotation, direction, scale, center and etc.

int maxindex is the integer of the furtherest enemy. Cannons shoot to the furtherest enemy at their shooting range (radius), but looks at the furtherest enemy of all map. So how should I count maxindex properly, that the cannons would look how they should?

IF YOU NEED MORE CODE JUST ASK AND SAY WHAT YOU NEED I'LL COPY IT AND EXPLAIN, AND PLEASE IF YOU CAN, HELP ME, I WOULD BE VERY THANKFUL.
Last edited on
closed account (N36fSL3A)
Maybe the cannons could have multiple circles. The cannon checks collision for each one that enters the furthermost circle. Pretty straight forward.

EDIT - You'd have problems with 2 enemies in the same circle though.
Last edited on by Fredbill30
EDIT - You'd have problems with 2 enemies in the same circle though.


Solved... No problems with 2 enemies in circle, because k_p shows which enemy is further and shoot him. If not k_p the game would crash.

I solved it by giving every tower unique id and if cannon id matches with towers id, it can be rotated (and it finds which cannons need to be rotated).

Thanks for your help.
Topic archived. No new replies allowed.