From 61390e37c0f51517f9e437181ce9caef4e8c9bd2 Mon Sep 17 00:00:00 2001 From: Reimar Date: Tue, 4 Nov 2025 14:49:26 +0100 Subject: [PATCH] Implement killing zombos --- src/Arrow.hpp | 2 ++ src/Game.cpp | 14 ++++++++++++++ src/Zombo.hpp | 6 ++++-- 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Arrow.hpp b/src/Arrow.hpp index 2cd4be7..75a5544 100644 --- a/src/Arrow.hpp +++ b/src/Arrow.hpp @@ -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 diff --git a/src/Game.cpp b/src/Game.cpp index b9716f2..c902367 100644 --- a/src/Game.cpp +++ b/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) { diff --git a/src/Zombo.hpp b/src/Zombo.hpp index e1854f9..f939484 100644 --- a/src/Zombo.hpp +++ b/src/Zombo.hpp @@ -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