Witam
Ostatnio zacząłem bawić się biblioteką SFML
Napisałem kod z tutoriala przedstawiony poniżej, tylko nie rozumiem w jaki sposób działa opóźnienie, w sensie jak jest obliczany w przypadku zmiennych
shootTimer i enemySpawnTimer
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <vector>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "My program");
window.setFramerateLimit(60);
//SHOOT
sf::CircleShape projectile;
projectile.setFillColor(sf::Color::Red);
projectile.setRadius(7.0);
int shootTimer = 0;
//ENEMY
sf::RectangleShape enemy;
enemy.setFillColor(sf::Color::Blue);
enemy.setSize(sf::Vector2f(50.0f, 50.0f));
//PLAYER
sf::CircleShape player;
player.setFillColor(sf::Color::White);
player.setRadius(50.0f);
player.setPosition(window.getSize().x/2- player.getRadius(), window.getSize().y- player.getRadius()*2 - 10.0f);
sf::Vector2f playerCenter;
std::vector<sf::CircleShape>projectiles;
projectiles.push_back(sf::CircleShape(projectile));
std::vector<sf::RectangleShape> enemies;
enemies.push_back(sf::RectangleShape(enemy));
int enemySpawnTimer = 0;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
//UPDATE PLAYER
playerCenter = sf::Vector2f(player.getPosition().x+player.getRadius()-projectile.getRadius(), player.getPosition().y+player.getRadius());
player.setPosition(sf::Mouse::getPosition(window).x, player.getPosition().y);
//UPDATE PROJECTILES
if(shootTimer < 5)
shootTimer++;
if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && shootTimer >=5)
{
projectile.setPosition(playerCenter);
projectiles.push_back(sf::CircleShape(projectile));
shootTimer = 0;
}
for(size_t i =0; i < projectiles.size(); i++)
{
projectiles[i].move(0.f, -10.0f);
if(projectiles[i].getPosition().y <= 0)
projectiles.erase(projectiles.begin() + i);
}
//UPDATE ENEMIES
if(enemySpawnTimer < 120)
enemySpawnTimer++;
if (enemySpawnTimer >= 120)
{
enemy.setPosition((rand() % int(window.getSize().x-enemy.getSize().x)),0.0f);
enemies.push_back(sf::RectangleShape(enemy));
enemySpawnTimer = 0;
}
for(size_t i =0; i < enemies.size(); i++)
{
enemies[i].move(0.0f, 5.0f);
if(enemies[i].getPosition().y > window.getSize().y)
enemies.erase(enemies.begin()+i);
}
//COLLISION
if(!enemies.empty() && !projectiles.empty())
{
for(size_t i = 0; i < projectiles.size(); i++)
{
for(size_t k = 0; k<enemies.size(); k++)
{
if(projectiles[i].getGlobalBounds().intersects(enemies[k].getGlobalBounds()))
{
projectiles.erase(projectiles.begin() + i);
enemies.erase(enemies.begin() + k);
break;
}
}
}
}
//DRAW
window.clear();
window.draw(player);
for(size_t i =0; i < enemies.size(); i++)
{
window.draw(enemies[i]);
}
for(size_t i =0; i < projectiles.size(); i++)
{
window.draw(projectiles[i]);
}
window.display();
}
return 0;
}
Z góry dziękuję za odpowiedź