Add blood particles

This commit is contained in:
Reimar 2025-11-05 12:42:27 +01:00
parent 9b15ebc1a5
commit 1ac1211b36
6 changed files with 87 additions and 1 deletions

View File

@ -6,6 +6,7 @@ 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
MAKEFLAGS += -j $(shell nproc) MAKEFLAGS += -j $(shell nproc)

View File

@ -1,7 +1,7 @@
#ifndef COLOR_HPP #ifndef COLOR_HPP
#define COLOR_HPP #define COLOR_HPP
#include <cstdint> #include <SDL2/SDL_pixels.h>
typedef SDL_Color Color; typedef SDL_Color Color;

View File

@ -30,6 +30,14 @@ void Game::update(std::stop_token stop_token)
arrow.update(); 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 // 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) {
@ -59,6 +67,10 @@ void Game::update(std::stop_token stop_token)
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++) {
particles.emplace_back(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);
if (zombo.hp == 0) { if (zombo.hp == 0) {
@ -99,6 +111,10 @@ 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) {
particle.draw(&renderer, player.x, player.y);
}
player.draw(); player.draw();
renderer.draw_sprite(*gold_sprite, renderer.screen_width - 50, 10); renderer.draw_sprite(*gold_sprite, renderer.screen_width - 50, 10);

View File

@ -8,6 +8,7 @@
#include "Player.hpp" #include "Player.hpp"
#include "Map.hpp" #include "Map.hpp"
#include "Zombo.hpp" #include "Zombo.hpp"
#include "Particle.hpp"
class Game class Game
{ {
@ -17,6 +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::unique_ptr<Sprite> arrow_sprite, zombo_sprite, gold_sprite; std::unique_ptr<Sprite> arrow_sprite, zombo_sprite, gold_sprite;
Font *font; Font *font;

38
src/Particle.cpp Normal file
View File

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

29
src/Particle.hpp Normal file
View File

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