From 2508658a75e0b5b2094f6192c2d0efb08a4b0c25 Mon Sep 17 00:00:00 2001 From: Reimar Date: Wed, 5 Nov 2025 13:59:32 +0100 Subject: [PATCH] Create effect class, fix zombo hitbox --- Makefile | 5 +++-- src/Game.cpp | 23 ++++++++++++----------- src/Game.hpp | 4 ++-- src/effects/Effect.hpp | 19 +++++++++++++++++++ src/effects/GoldGainedHint.cpp | 17 +++++++++++++++++ src/effects/GoldGainedHint.hpp | 27 +++++++++++++++++++++++++++ src/{ => effects}/Particle.cpp | 8 +++++++- src/{ => effects}/Particle.hpp | 17 +++++++---------- 8 files changed, 94 insertions(+), 26 deletions(-) create mode 100644 src/effects/Effect.hpp create mode 100644 src/effects/GoldGainedHint.cpp create mode 100644 src/effects/GoldGainedHint.hpp rename src/{ => effects}/Particle.cpp (79%) rename src/{ => effects}/Particle.hpp (58%) diff --git a/Makefile b/Makefile index 35318b5..04ec4cb 100644 --- a/Makefile +++ b/Makefile @@ -6,8 +6,9 @@ OBJS= \ build/src/Zombo.o \ build/src/Sprite.o \ build/src/Player.o \ - build/src/Particle.o \ - build/src/GameRenderer.o + build/src/GameRenderer.o \ + build/src/effects/Particle.o \ + build/src/effects/GoldGainedHint.o MAKEFLAGS += -j $(shell nproc) diff --git a/src/Game.cpp b/src/Game.cpp index 68a895e..56a45b0 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -5,6 +5,7 @@ #include "Player.hpp" #include "Game.hpp" #include "Color.hpp" +#include "effects/Particle.hpp" using namespace std::literals::chrono_literals; @@ -30,14 +31,14 @@ void Game::update(std::stop_token stop_token) arrow.update(); } - for (Particle &particle : particles) { - particle.update(); - - if (particle.ticks > particle.max_ticks) { - std::erase(particles, particle); - } + for (std::unique_ptr &effect : effects) { + effect->update(); } + std::erase_if(effects, [](const std::unique_ptr &effect) { + return effect->ticks > effect->max_ticks; + }); + // 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) { @@ -60,15 +61,15 @@ void Game::update(std::stop_token stop_token) // 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 + arrow.x > zombo.x - zombo_sprite->width / 2 && arrow.x < zombo.x + zombo_sprite->width / 2 && + arrow.y > zombo.y - zombo_sprite->height / 2 && arrow.y < zombo.y + zombo_sprite->height / 2 ) { zombo.hp--; zombo.x += std::cos(arrow.angle) * 8; zombo.y += std::sin(arrow.angle) * 8; for (int i = 0; i < 5; i++) { - particles.emplace_back(rgb(255, 0, 0), zombo.x, zombo.y, rand() % 6 - 3, -2 - (double)rand() / RAND_MAX * 4); + effects.push_back(std::make_unique(rgb(255, 0, 0), zombo.x, zombo.y, rand() % 6 - 3, -2 - (double)rand() / RAND_MAX * 4)); } std::erase(arrows, arrow); @@ -111,8 +112,8 @@ void Game::draw() arrow.draw(&renderer, *arrow_sprite, player.x, player.y); } - for (const Particle &particle : particles) { - particle.draw(&renderer, player.x, player.y); + for (const std::unique_ptr &effect : effects) { + effect->draw(&renderer, player.x, player.y); } player.draw(); diff --git a/src/Game.hpp b/src/Game.hpp index 5c2c59c..3dc7c3a 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -8,7 +8,7 @@ #include "Player.hpp" #include "Map.hpp" #include "Zombo.hpp" -#include "Particle.hpp" +#include "effects/Effect.hpp" class Game { @@ -18,7 +18,7 @@ private: Map map; std::vector arrows; std::vector zombos; - std::vector particles; + std::vector> effects; std::unique_ptr arrow_sprite, zombo_sprite, gold_sprite; Font *font; diff --git a/src/effects/Effect.hpp b/src/effects/Effect.hpp new file mode 100644 index 0000000..3fe2414 --- /dev/null +++ b/src/effects/Effect.hpp @@ -0,0 +1,19 @@ +#ifndef EFFECT_HPP +#define EFFECT_HPP + +#include "../GameRenderer.hpp" + +class Effect +{ +public: + int ticks = 0; + int max_ticks; + + virtual ~Effect() = default; + + virtual void update() = 0; + + virtual void draw(const GameRenderer *renderer, double player_x, double player_y) const = 0; +}; + +#endif diff --git a/src/effects/GoldGainedHint.cpp b/src/effects/GoldGainedHint.cpp new file mode 100644 index 0000000..61c59db --- /dev/null +++ b/src/effects/GoldGainedHint.cpp @@ -0,0 +1,17 @@ +#include "GoldGainedHint.hpp" + +GoldGainedHint::GoldGainedHint(double x, double y, int amount, const Sprite *gold_sprite, Font *font) + : x(x), y(y), amount(amount), gold_sprite(gold_sprite), font(font) +{ + max_ticks = 40; +} + +void GoldGainedHint::update() +{ + y--; +} + +void GoldGainedHint::draw(const GameRenderer *renderer, double player_x, double player_y) const +{ + +} diff --git a/src/effects/GoldGainedHint.hpp b/src/effects/GoldGainedHint.hpp new file mode 100644 index 0000000..7f6d8bb --- /dev/null +++ b/src/effects/GoldGainedHint.hpp @@ -0,0 +1,27 @@ +#ifndef GOLD_GAINED_HINT_HPP +#define GOLD_GAINED_HINT_HPP + +#include "Effect.hpp" +#include "../Sprite.hpp" +#include "../GameRenderer.hpp" + +class GoldGainedHint : public Effect +{ +private: + double x, y; + int amount; + + const Sprite *gold_sprite; + Font *font; + +public: + GoldGainedHint(double x, double y, int amount, const Sprite *gold_sprite, Font *font); + + void update() override; + + void draw(const GameRenderer *renderer, double player_x, double player_y) const override; + + bool operator==(const GoldGainedHint &other) const = default; +}; + +#endif \ No newline at end of file diff --git a/src/Particle.cpp b/src/effects/Particle.cpp similarity index 79% rename from src/Particle.cpp rename to src/effects/Particle.cpp index 60390b8..8dd6eab 100644 --- a/src/Particle.cpp +++ b/src/effects/Particle.cpp @@ -1,5 +1,11 @@ #include "Particle.hpp" -#include "GameRenderer.hpp" +#include "../GameRenderer.hpp" + +Particle::Particle(Color color, double x, double y, double x_vel, double y_vel) + : color(color), x(x), y(y), x_vel(x_vel), y_vel(y_vel) +{ + max_ticks = 40; +} void Particle::update() { diff --git a/src/Particle.hpp b/src/effects/Particle.hpp similarity index 58% rename from src/Particle.hpp rename to src/effects/Particle.hpp index 55d810d..f047c63 100644 --- a/src/Particle.hpp +++ b/src/effects/Particle.hpp @@ -1,10 +1,11 @@ #ifndef PARTICLE_HPP #define PARTICLE_HPP -#include "Color.hpp" -#include "GameRenderer.hpp" +#include "Effect.hpp" +#include "../Color.hpp" +#include "../GameRenderer.hpp" -class Particle +class Particle : public Effect { private: Color color; @@ -13,15 +14,11 @@ private: static constexpr int size = 3; public: - int ticks = 0; - static constexpr int max_ticks = 40; + Particle(Color color, double x, double y, double x_vel, double y_vel); - Particle(Color color, double x, double y, double x_vel, double y_vel) - : color(color), x(x), y(y), x_vel(x_vel), y_vel(y_vel) {} + void update() override; - void update(); - - void draw(const GameRenderer *renderer, double player_x, double player_y) const; + void draw(const GameRenderer *renderer, double player_x, double player_y) const override; bool operator==(const Particle &other) const; };