From 1ac1211b360dbd65734876a36b1d0c0ad268cd17 Mon Sep 17 00:00:00 2001 From: Reimar Date: Wed, 5 Nov 2025 12:42:27 +0100 Subject: [PATCH] Add blood particles --- Makefile | 1 + src/Color.hpp | 2 +- src/Game.cpp | 16 ++++++++++++++++ src/Game.hpp | 2 ++ src/Particle.cpp | 38 ++++++++++++++++++++++++++++++++++++++ src/Particle.hpp | 29 +++++++++++++++++++++++++++++ 6 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/Particle.cpp create mode 100644 src/Particle.hpp diff --git a/Makefile b/Makefile index b5588c7..35318b5 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ OBJS= \ build/src/Zombo.o \ build/src/Sprite.o \ build/src/Player.o \ + build/src/Particle.o \ build/src/GameRenderer.o MAKEFLAGS += -j $(shell nproc) diff --git a/src/Color.hpp b/src/Color.hpp index 0d29e81..c6191be 100644 --- a/src/Color.hpp +++ b/src/Color.hpp @@ -1,7 +1,7 @@ #ifndef COLOR_HPP #define COLOR_HPP -#include +#include typedef SDL_Color Color; diff --git a/src/Game.cpp b/src/Game.cpp index c2f00a6..68a895e 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -30,6 +30,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); + } + } + // 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) { @@ -59,6 +67,10 @@ void Game::update(std::stop_token stop_token) 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); + } + std::erase(arrows, arrow); if (zombo.hp == 0) { @@ -99,6 +111,10 @@ void Game::draw() arrow.draw(&renderer, *arrow_sprite, player.x, player.y); } + for (const Particle &particle : particles) { + particle.draw(&renderer, player.x, player.y); + } + player.draw(); renderer.draw_sprite(*gold_sprite, renderer.screen_width - 50, 10); diff --git a/src/Game.hpp b/src/Game.hpp index db38560..5c2c59c 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -8,6 +8,7 @@ #include "Player.hpp" #include "Map.hpp" #include "Zombo.hpp" +#include "Particle.hpp" class Game { @@ -17,6 +18,7 @@ private: Map map; std::vector arrows; std::vector zombos; + std::vector particles; std::unique_ptr arrow_sprite, zombo_sprite, gold_sprite; Font *font; diff --git a/src/Particle.cpp b/src/Particle.cpp new file mode 100644 index 0000000..60390b8 --- /dev/null +++ b/src/Particle.cpp @@ -0,0 +1,38 @@ +#include "Particle.hpp" +#include "GameRenderer.hpp" + +void Particle::update() +{ + ticks++; + + x += x_vel; + y += y_vel; + + y_vel += 0.2; + + if (x_vel > 0) x_vel -= 0.1; + else if (x_vel < 0) x_vel += 0.1; +} + +void Particle::draw(const GameRenderer *renderer, double player_x, double player_y) const +{ + renderer->draw_rect( + (int)(x + renderer->screen_width / 2 - player_x), + (int)(y + renderer->screen_height / 2 - player_y), + size, size, + color + ); +} + +bool Particle::operator==(const Particle &other) const +{ + return x == other.x + && y == other.y + && x_vel == other.x_vel + && y_vel == other.y_vel + && ticks == other.ticks + && color.r == other.color.r + && color.g == other.color.g + && color.b == other.color.b + && color.a == other.color.a; +} diff --git a/src/Particle.hpp b/src/Particle.hpp new file mode 100644 index 0000000..55d810d --- /dev/null +++ b/src/Particle.hpp @@ -0,0 +1,29 @@ +#ifndef PARTICLE_HPP +#define PARTICLE_HPP + +#include "Color.hpp" +#include "GameRenderer.hpp" + +class Particle +{ +private: + Color color; + double x, y, x_vel, y_vel; + + 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) + : 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; + + bool operator==(const Particle &other) const; +}; + +#endif \ No newline at end of file