Using my universities API, I'm making a basic 2D game where pacman can shoot ghosts
I have a bullet class but at the moment, I only have one instance of the bullet
could anyone help edit this code/give me hints so that whenever I click i add a new bullet to the screen and after a certain amount of time it is deleted
void Bullet::update(int elapsedTime, Input::MouseState* mouseState, Player* pacman)
{
if (mouseState->LeftButton == Input::ButtonState::PRESSED && !shooting)
{
setX(mouseState);
setY(mouseState);
shooting = true;
}
if (shooting)
{
float xDistance = mouseX - position->X;
float yDistance = mouseY - position->Y;
float hypotenuse = sqrt((xDistance * xDistance) + (yDistance * yDistance));
position->X += elapsedTime * ((xDistance) / hypotenuse) / 2;
position->Y += elapsedTime * ((yDistance) / hypotenuse) / 2;
currentTime += (float)elapsedTime / 1000;
if (currentTime >= maxTime)
{
setPosition(pacman->getPosition()->X + pacman->getRect()->Width / 2, pacman->getPosition()->Y + pacman->getRect()->Height / 2);
currentTime = 0;
shooting = false;
}
}
else
{
setPosition(pacman->getPosition()->X + pacman->getRect()->Width / 2, pacman->getPosition()->Y + pacman->getRect()->Height / 2);
}
}
and the bullet is loaded here
Texture2D* bulletTex = new Texture2D();
bulletTex->Load("Textures/bullet.png", false);
Vector2* bulletPos = new Vector2(pacman->getPosition()->X, pacman->getPosition()->Y);
Rect* bulletRect = new Rect(0.0f, 0.0f, 5, 5);
bullet = new Bullet(bulletPos, bulletRect, bulletTex);
Thanks in advance