Logo

Glusoft

Bullets patterns

I will use a helper function to add one bullet in the vertex array.

void addBullet(sf::VertexArray* v, Bullet *bul, size_t index) {

	// Get the quad contained in the vertex array
	sf::Vertex* quad = &(*v)[index*4];

	sf::Sprite *spr = bul->getSprite();

	// Set the position of the sprite
	quad[0].position = sf::Vector2f(spr->getPosition().x, spr->getPosition().y);
	quad[1].position = sf::Vector2f(spr->getPosition().x + spr->getTextureRect().width, 
	spr->getPosition().y);
	quad[2].position = sf::Vector2f(spr->getPosition().x + spr->getTextureRect().width, 
	spr->getPosition().y + spr->getTextureRect().height);
	quad[3].position = sf::Vector2f(spr->getPosition().x, spr->getPosition().y + 
	spr->getTextureRect().height);

	// Set the texture of the sprite
	quad[0].texCoords = sf::Vector2f(spr->getTextureRect().left, spr->getTextureRect().top);
	quad[1].texCoords = sf::Vector2f(spr->getTextureRect().left + spr->getTextureRect().width, 
	spr->getTextureRect().top);
	quad[2].texCoords = sf::Vector2f(spr->getTextureRect().left + spr->getTextureRect().width, 
	spr->getTextureRect().top + spr->getTextureRect().height);
	quad[3].texCoords = sf::Vector2f(spr->getTextureRect().left, spr->getTextureRect().top + 
	spr->getTextureRect().height);
}

Red bullet pattern

First we do a spiral pattern of medium red bullets.

// Add new red bullet
if (counterTime > bulletTime && counterWave < 1) {
	if (numBullet == maxBullets) {
		numBullet = 0;
		counterWave++;
	}

	if (bullets[numBullet] != NULL) {
		delete(bullets[numBullet]);
		bullets[numBullet] = NULL;
	}

	float x = r*cos((size_t) (numBullet%180) / Pi);
	float y = r*sin((size_t) (numBullet%180) / Pi);
	Bullet *bul = new Bullet(&tex, sf::Vector2f(x*0.01, y*0.01), sf::Vector2f(winW/2 - 8, winH/2 - 8), 
	sf::IntRect(0, 64, 16, 16));
	bullets[numBullet] = bul;
	addBullet(&vertices, bul, numBullet);
	numBullet++;
	counterTime = counterTime - bulletTime;
}

Each time counterTime ms has passed the code will create a red bullet on a circle of radius r. To find the coordinate of the point we use the polar coordinate:
formula

Blue bullet pattern

This time we will use one of the big bullet:

// Add new blue bullet
if (counterTime2 > bulletTime2 && counterWave2 < 3) {
	for (size_t i = 0; i < 45; i++) {
		float y = r*cos((size_t) i / (Pi/2));
		float x = r*sin((size_t) i / (Pi/2));
		Bullet *bul = new Bullet(&tex, sf::Vector2f(x*0.01, y*0.01), 
		sf::Vector2f(winW / 2 - 32, winH / 2 - 32), sf::IntRect(128, 0, 64, 64));
				
		if (numBullet2 == maxBullets) {
			numBullet2 = 0;
			counterWave2++;
		}

		if (bullets[maxBullets + numBullet2] != NULL) {
			delete(bullets[maxBullets + numBullet2]);
			bullets[maxBullets + numBullet2] = NULL;
		}

		bullets[maxBullets + numBullet2] = bul;
		addBullet(&vertices, bul, maxBullets + numBullet2);
		numBullet2++;
		counterTime2 = counterTime2 - bulletTime2;
	}
}

Each counterTime2 ms we create 45 bullets around a circle of radius r.

Purple bullet pattern

Time to get crazy for this last pattern ;)

if (counterTime3 > bulletTime3 && counterWave >= 1) {
	for (size_t i = 0; i < 10; i++) {
		float y = r*cos((size_t)rand()%180 / (Pi));
		float x = r*sin((size_t)rand()%180 / (Pi));
				
		Bullet *bul = new Bullet(&tex, sf::Vector2f(x*0.1, y*0.1), 
		sf::Vector2f(winW / 2 - 4, winH / 2 - 4), sf::IntRect(0, 80, 8, 8));
				
		if (numBullet3 == 14*maxBullets) {
			numBullet3 = 0;
			counterWave3++;
		}

		if (bullets[2 * maxBullets + numBullet3] != NULL) {
			delete(bullets[2 * maxBullets + numBullet3]);
			bullets[2 * maxBullets + numBullet3] = NULL;
		}
				
		bullets[2*maxBullets + numBullet3] = bul;
		addBullet(&vertices, bul, 2*maxBullets + numBullet3);
		numBullet3++;
		counterTime3 = counterTime3 - bulletTime3;
	}
}

Cleaning the mess (Optional)

for (size_t i = 0; i < totalBullets; i++) {
	if (bullets[i] != NULL) {
		if (bullets[i]->getSprite()->getPosition().x > winW 
|| bullets[i]->getSprite()->getPosition().y > winH 
|| (bullets[i]->getSprite()->getPosition().y + bullets[i]->getSprite()->getTextureRect().height) < 0 
|| (bullets[i]->getSprite()->getPosition().x + bullets[i]->getSprite()->getTextureRect().width) < 0) {
			delete(bullets[i]);
			bullets[i] = NULL;
		}
	}
}

If the bullet is outside the screen we can delete it, in this example the bullet will be deleted when the array is full and the vertex array is not updated.
I have added this part to not forget that SFML draws everything even if it's not on the screen.

<-- Event loop --- Render everything -->

Dark theme