[Added] The ability to fire bullets to the player
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
|
||||
#include "services/Service.h"
|
||||
#include "entities/Game.h"
|
||||
#include "entities/PBullet.h"
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
class GameService : public Service<GameService>
|
||||
{
|
||||
@@ -52,10 +55,64 @@ public:
|
||||
// return this->game;
|
||||
// }
|
||||
|
||||
void addPlayerBullet(PBullet bullet)
|
||||
{
|
||||
this->playerBullets.push_back(bullet);
|
||||
}
|
||||
|
||||
// std::list<PBullet>& getPlayerBullets() {
|
||||
// return this->playerBullets;
|
||||
// }
|
||||
|
||||
// void removeCollidedBullets()
|
||||
// {
|
||||
// for (auto it = playerBullets.begin(); it != playerBullets.end(); /* no increment here */)
|
||||
// {
|
||||
// if (it->getHasCollided())
|
||||
// {
|
||||
// it = playerBullets.erase(it); // Remove element and get the next valid iterator
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// ++it; // Only increment if no removal happens
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
void bulletLogic()
|
||||
{
|
||||
for (auto it = this->playerBullets.begin(); it != this->playerBullets.end();)
|
||||
{
|
||||
it->logic();
|
||||
if (it->getHasCollided())
|
||||
it = this->playerBullets.erase(it);
|
||||
else
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void drawBullets()
|
||||
{
|
||||
for (auto it = this->playerBullets.begin(); it != this->playerBullets.end();)
|
||||
{
|
||||
it->draw();
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
void resetGame()
|
||||
{
|
||||
paused = false;
|
||||
gameOver = false;
|
||||
// playerBullets.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
bool paused = false;
|
||||
bool gameOver = false;
|
||||
Camera2D camera;
|
||||
|
||||
std::list<PBullet> playerBullets;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -57,10 +57,15 @@ public:
|
||||
for (auto button : watchedMouseButtons)
|
||||
{
|
||||
mouseButtonsPressed[button] = false;
|
||||
mouseButtonsDown[button] = false;
|
||||
if (IsMouseButtonPressed(button))
|
||||
{
|
||||
mouseButtonsPressed[button] = true;
|
||||
}
|
||||
if (IsMouseButtonDown(button))
|
||||
{
|
||||
mouseButtonsDown[button] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,22 +83,29 @@ public:
|
||||
return mouseButtonsPressed[button];
|
||||
}
|
||||
|
||||
bool isMouseButtonDown(MouseButton button) {
|
||||
return mouseButtonsDown[button];
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_set<KeyboardKey> watchedKeys;
|
||||
std::unordered_map<KeyboardKey, bool> keysPressed;
|
||||
std::unordered_map<KeyboardKey, bool> keysDown;
|
||||
std::unordered_set<MouseButton> watchedMouseButtons;
|
||||
std::unordered_map<MouseButton, bool> mouseButtonsPressed;
|
||||
std::unordered_map<MouseButton, bool> mouseButtonsDown;
|
||||
|
||||
void setMouseButtonsToWatch(std::vector<MouseButton> mouseButtonsToWatch)
|
||||
{
|
||||
this->watchedMouseButtons.clear();
|
||||
this->mouseButtonsPressed.clear();
|
||||
this->mouseButtonsDown.clear();
|
||||
|
||||
for (int i = 0; i < mouseButtonsToWatch.size(); i++)
|
||||
{
|
||||
this->watchedMouseButtons.insert(mouseButtonsToWatch[i]);
|
||||
this->mouseButtonsPressed[mouseButtonsToWatch[i]] = false;
|
||||
this->mouseButtonsDown[mouseButtonsToWatch[i]] = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user