7 Replies - 226 Views - Last Post: 06 April 2013 - 02:47 PM Rate Topic: -----

#1 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 06:49 AM

Im trying to make a Turret Defence game.

I wanted to know if there was a better way to have my game objects communicating. Right now my code is really messy and i want to clean it up but i cant think of another way to do this.

When the turret vector is iterated, each turret is given a vector of the enemies to iterate through so it knows when and where to shoot. When the bullet vector is iterated, each bullet is given the vector of enemies to see if it collided with any of them.

Here is what is going on in GamePlay_State::Update(). As you can see there is a lot of for loops. Some of it might not make sense because im in the process of switching over to a wave system but my point is that there are a lot of loops and i want to know if there is a better way of having my game objects communicate.

void GamePlay_State::Update(sf::RenderWindow& window)
{
	//Get mouse position
	sf::Vector2i mouse = sf::Mouse::getPosition(window);

	sf::Event E;
	while(window.pollEvent(E))
	{
		//If the window is closed, switch to exit state to exit
		if( E.type == sf::Event::Closed )
		{
			window.close();
			nextState = GS_EXIT;
		}

		//If player has clicked on a turret in the side menu, isPlacingTurret = true. 
		if(isPlacingTurret)
		{
			//move selected tile over tile mouse is over
			selectedTile.setPosition(mouse.x-(mouse.x%TILE_SIZE)+1, mouse.y-mouse.y%TILE_SIZE+1);

			//if player clicks, place a new turret
			if(E.type == sf::Event::MouseButtonPressed)
			{
				if(E.mouseButton.button == sf::Mouse::Left)
				{
					//Create a new turret and add it to the vector
					Turret* newTurret = new Turret(mouse.x-(mouse.x%TILE_SIZE)+1, mouse.y-mouse.y%TILE_SIZE+1,t_spriteSheet);
					activeTurrets.push_back(newTurret);
					isPlacingTurret = false;
				}
			}
		}
	}

	//If mouse is clicked over a turret in the sidemenu then the player wants to 
	//place that turret somewhere.
	if( mouse.x > 480 && mouse.x < 560   &&   mouse.y > 67  && mouse.y < 128 )
	{
		if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
		{
			//player should now be able to select a place for turret
			isPlacingTurret = true;
		}
	}

	//move each enemy in the wave
	currentWave->Update();

	//Iterate through the turrets
	for( std::vector<Turret*>::const_iterator t = activeTurrets.begin(); t != activeTurrets.end(); ++t)
	{
		//manage each turrets shoot timer
		(**t).Update();

		//see if any enemy is in range
		Bullet* newbullet;
		newbullet = (**t).FireAtEnemy(currentWave->GetEnemyVector(), t_spriteSheet);
		if( newbullet == NULL )
			delete newbullet;
		else
			activeBullets.push_back(newbullet);
	}

	//Iterate bullets
	for( int i = 0; i < activeBullets.size(); i++)
	{
		activeBullets[i]->Update();

		//Delete bullet if it is out of bounds, its range, or if it has collided with an enemy
		if(	activeBullets[i]->OutOfBounds() )
		{
			delete activeBullets[i];
			activeBullets.erase(activeBullets.begin() + i);
			i--;
			break;
		}
                
		//here bullet[i] is given the vector of enemies so it can see if it collides with any of them. If there is collision, enemy takes damage
		//Code is in the process of being fixed so i cant really show you it
	}

	if(activeEnemies[e]->GetIsDead())
	{
		delete activeEnemies[e];
		activeEnemies.erase(activeEnemies.begin() + e);
		e--;
	}

	//Handle the wave
	waveTimer--;
	waveTimerText.setString(intToStr(waveTimer));
	if(waveTimer <= 0)
	{
		currentWaveNumber++;
		//launch new Wave
		currentWave = new Wave(currentWaveNumber, t_spriteSheet);

		//reset timer
		waveTimer = 300;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Relationships between turrets, bullets, and enemies

#2 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 09:54 AM

If you have a maximum or known number of turrets, I'm not sure why you have a vector of turret pointers. It doesn't really make sense.

Anyway, to make your implementation a bit more efficient I'd look int Loci. Essentially your turrets will only iterate through and shoot at enemies that are close enough to the length of a given vector that represents the radius of a circle with its origin at the turret location.
Was This Post Helpful? 0
  • +
  • -

#3 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 12:11 PM

The container has to be dynamic (vector) because the player will add or remove turrets as the game progresses. As for the loci, wont i still have to iterate through every enemy to see if its in the loci? In the current code each turret is passed a vector of enemies to iterate through to see if they are in range before firing at them.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 12:55 PM

View PostNano511, on 06 April 2013 - 04:11 PM, said:

The container has to be dynamic (vector) because the player will add or remove turrets as the game progresses.

One things you will learn as you make increasingly more complicated games is that there is a very fine line between clever programming and nonsense. Why not just have a vector of type 'turret' or a linked list of turret nodes (suggested)? The way you have chosen a vector of pointers to turrets is just extra work under the good for nothing.

View PostNano511, on 06 April 2013 - 04:11 PM, said:

As for the loci, wont i still have to iterate through every enemy to see if its in the loci? In the current code each turret is passed a vector of enemies to iterate through to see if they are in range before firing at them.

No, of course you won't have to iterate through every enemy, you will only be iterating through enemies within a turret's range. It's a bad idea, and frankly lazy programming to iterate though enemies that are out of range.
Was This Post Helpful? 0
  • +
  • -

#5 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 01:52 PM

For whatever i reason i always made vectors of objects vectors of pointers to objects. I guess i just thought it would be more efficient of something. I will change it to just a vector a turrets though.

How will a know if an enemy is within a turret's range unless i iterate through the enemies? Isnt this the only way to do it:
for( each enemy e in enemiesVector )
  if( e is in range ) 
     process e


Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 02:03 PM

Well, you will need to have a collision detection mechanism in place, such that a turret will only fire if it 'collides' with an enemy. Sounds odd but think about it. You only need to iterate through the turrets rather than the enemies.
Was This Post Helpful? 0
  • +
  • -

#7 Nano511  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 145
  • Joined: 07-October 12

Re: Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 02:31 PM

Can you give me an idea(preferably psuedocode) of such a collision detection system? I still can think of a way where i wont have to iterate each enemy.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: -4
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Relationships between turrets, bullets, and enemies

Posted 06 April 2013 - 02:47 PM

I did already when I spoke about loci and radii. Also, think about how base defense games work. Each turret focuses on the closest enemy when they are in range, completely ignoring the rest that follow.

This is how your process should be working, and you won't need to iterate through each enemy. See it now?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1