Create effect class, fix zombo hitbox

This commit is contained in:
Reimar 2025-11-05 13:59:32 +01:00
parent 1ac1211b36
commit 2508658a75
8 changed files with 94 additions and 26 deletions

View File

@ -6,8 +6,9 @@ OBJS= \
build/src/Zombo.o \ build/src/Zombo.o \
build/src/Sprite.o \ build/src/Sprite.o \
build/src/Player.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) MAKEFLAGS += -j $(shell nproc)

View File

@ -5,6 +5,7 @@
#include "Player.hpp" #include "Player.hpp"
#include "Game.hpp" #include "Game.hpp"
#include "Color.hpp" #include "Color.hpp"
#include "effects/Particle.hpp"
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
@ -30,14 +31,14 @@ void Game::update(std::stop_token stop_token)
arrow.update(); arrow.update();
} }
for (Particle &particle : particles) { for (std::unique_ptr<Effect> &effect : effects) {
particle.update(); effect->update();
if (particle.ticks > particle.max_ticks) {
std::erase(particles, particle);
}
} }
std::erase_if(effects, [](const std::unique_ptr<Effect> &effect) {
return effect->ticks > effect->max_ticks;
});
// Remove arrows that are out of bounds // Remove arrows that are out of bounds
std::erase_if(arrows, [this](const Arrow &arrow) { 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) { 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 // Check collision between zombos and arrows
for (Arrow &arrow : arrows) { for (Arrow &arrow : arrows) {
if ( if (
arrow.x > zombo.x && arrow.x < zombo.x + zombo_sprite->width && arrow.x > zombo.x - zombo_sprite->width / 2 && arrow.x < zombo.x + zombo_sprite->width / 2 &&
arrow.y > zombo.y && arrow.y < zombo.y + zombo_sprite->height arrow.y > zombo.y - zombo_sprite->height / 2 && arrow.y < zombo.y + zombo_sprite->height / 2
) { ) {
zombo.hp--; zombo.hp--;
zombo.x += std::cos(arrow.angle) * 8; zombo.x += std::cos(arrow.angle) * 8;
zombo.y += std::sin(arrow.angle) * 8; zombo.y += std::sin(arrow.angle) * 8;
for (int i = 0; i < 5; i++) { 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<Particle>(rgb(255, 0, 0), zombo.x, zombo.y, rand() % 6 - 3, -2 - (double)rand() / RAND_MAX * 4));
} }
std::erase(arrows, arrow); std::erase(arrows, arrow);
@ -111,8 +112,8 @@ void Game::draw()
arrow.draw(&renderer, *arrow_sprite, player.x, player.y); arrow.draw(&renderer, *arrow_sprite, player.x, player.y);
} }
for (const Particle &particle : particles) { for (const std::unique_ptr<Effect> &effect : effects) {
particle.draw(&renderer, player.x, player.y); effect->draw(&renderer, player.x, player.y);
} }
player.draw(); player.draw();

View File

@ -8,7 +8,7 @@
#include "Player.hpp" #include "Player.hpp"
#include "Map.hpp" #include "Map.hpp"
#include "Zombo.hpp" #include "Zombo.hpp"
#include "Particle.hpp" #include "effects/Effect.hpp"
class Game class Game
{ {
@ -18,7 +18,7 @@ private:
Map map; Map map;
std::vector<Arrow> arrows; std::vector<Arrow> arrows;
std::vector<Zombo> zombos; std::vector<Zombo> zombos;
std::vector<Particle> particles; std::vector<std::unique_ptr<Effect>> effects;
std::unique_ptr<Sprite> arrow_sprite, zombo_sprite, gold_sprite; std::unique_ptr<Sprite> arrow_sprite, zombo_sprite, gold_sprite;
Font *font; Font *font;

19
src/effects/Effect.hpp Normal file
View File

@ -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

View File

@ -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
{
}

View File

@ -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

View File

@ -1,5 +1,11 @@
#include "Particle.hpp" #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() void Particle::update()
{ {

View File

@ -1,10 +1,11 @@
#ifndef PARTICLE_HPP #ifndef PARTICLE_HPP
#define PARTICLE_HPP #define PARTICLE_HPP
#include "Color.hpp" #include "Effect.hpp"
#include "GameRenderer.hpp" #include "../Color.hpp"
#include "../GameRenderer.hpp"
class Particle class Particle : public Effect
{ {
private: private:
Color color; Color color;
@ -13,15 +14,11 @@ private:
static constexpr int size = 3; static constexpr int size = 3;
public: public:
int ticks = 0; Particle(Color color, double x, double y, double x_vel, double y_vel);
static constexpr int max_ticks = 40;
Particle(Color color, double x, double y, double x_vel, double y_vel) void update() override;
: color(color), x(x), y(y), x_vel(x_vel), y_vel(y_vel) {}
void update(); void draw(const GameRenderer *renderer, double player_x, double player_y) const override;
void draw(const GameRenderer *renderer, double player_x, double player_y) const;
bool operator==(const Particle &other) const; bool operator==(const Particle &other) const;
}; };