Implement killing zombos
This commit is contained in:
parent
d2718ca045
commit
61390e37c0
@ -18,6 +18,8 @@ public:
|
||||
void draw(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y) const;
|
||||
|
||||
void update();
|
||||
|
||||
bool operator==(const Arrow &other) const = default;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
14
src/Game.cpp
14
src/Game.cpp
@ -25,6 +25,7 @@ void Game::update(std::stop_token stop_token)
|
||||
arrow.update();
|
||||
}
|
||||
|
||||
// Remove arrows that are out of bounds
|
||||
std::erase_if(arrows, [this](const Arrow &arrow) {
|
||||
if (arrow.x < map.tile_offset_x * map.tile_size || arrow.y < map.tile_offset_x * map.tile_size) {
|
||||
return true;
|
||||
@ -42,6 +43,19 @@ void Game::update(std::stop_token stop_token)
|
||||
|
||||
for (Zombo &zombo : zombos) {
|
||||
zombo.update(player.x, player.y);
|
||||
|
||||
// Check collision between zombos and arrows
|
||||
for (Arrow &arrow : arrows) {
|
||||
if (
|
||||
arrow.x > zombo.x && arrow.x < zombo.x + zombo_sprite->width &&
|
||||
arrow.y > zombo.y && arrow.y < zombo.y + zombo_sprite->height
|
||||
) {
|
||||
std::erase(arrows, arrow);
|
||||
|
||||
zombo.hp--;
|
||||
if (zombo.hp == 0) std::erase(zombos, zombo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (ticks % 100 == 0 && zombos.size() < 20) {
|
||||
|
||||
@ -6,18 +6,20 @@
|
||||
|
||||
class Zombo
|
||||
{
|
||||
private:
|
||||
public:
|
||||
double x;
|
||||
double y;
|
||||
int hp = 3;
|
||||
|
||||
static constexpr double speed = 0.5;
|
||||
|
||||
public:
|
||||
Zombo(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y);
|
||||
|
||||
void update(double player_x, double player_y);
|
||||
|
||||
void draw(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y) const;
|
||||
|
||||
bool operator==(const Zombo &other) const = default;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Loading…
Reference in New Issue
Block a user