Compare commits

..

2 Commits

Author SHA1 Message Date
37d7c962b8 Fix sprites 2025-10-31 12:37:37 +01:00
b2f888b55f Extract sprite into class 2025-10-31 12:27:31 +01:00
25 changed files with 297 additions and 626 deletions

View File

@ -6,9 +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/GameRenderer.o \ build/src/GameRenderer.o
build/src/effects/Particle.o \
build/src/effects/GoldGainedHint.o
MAKEFLAGS += -j $(shell nproc) MAKEFLAGS += -j $(shell nproc)
@ -23,8 +21,8 @@ else
LINKER_FLAGS += -fsanitize=address,undefined -g -Og -rdynamic LINKER_FLAGS += -fsanitize=address,undefined -g -Og -rdynamic
endif endif
CXX_FLAGS += $(shell pkg-config sdl2 SDL2_image SDL2_ttf --cflags) CXX_FLAGS += $(shell pkg-config sdl2 SDL2_image --cflags)
LINKER_FLAGS += $(shell pkg-config sdl2 SDL2_image SDL2_ttf --libs) LINKER_FLAGS += $(shell pkg-config sdl2 SDL2_image --libs)
build/%.o: %.cpp $(wildcard *.hpp) build/%.o: %.cpp $(wildcard *.hpp)
@mkdir -p `dirname $@` @mkdir -p `dirname $@`

Binary file not shown.

Binary file not shown.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 334 B

View File

@ -1,13 +1,20 @@
#include <cmath> #include <cmath>
#include <numbers> #include <numbers>
#include <memory>
#include "Arrow.hpp" #include "Arrow.hpp"
void Arrow::draw(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y) const Arrow::Arrow(GameRenderer *renderer, const double x, const double y, const double angle)
: renderer(renderer), x(x), y(y), angle(angle)
{
sprite = renderer->load_sprite("./assets/arrow.png", 22, 8);
}
void Arrow::draw(const double player_x, const double player_y) const
{ {
renderer->draw_sprite_rotated( renderer->draw_sprite_rotated(
sprite, *sprite,
(int)(x - sprite.width / 2 + renderer->screen_width / 2 - player_x), (int)(x - sprite->width / 2 + renderer->screen_width / 2 - player_x),
(int)(y - sprite.height / 2 + renderer->screen_height / 2 - player_y), (int)(y - sprite->height / 2 + renderer->screen_height / 2 - player_y),
angle * 180 / std::numbers::pi angle * 180 / std::numbers::pi
); );
} }

View File

@ -1,25 +1,28 @@
#ifndef ARROW_HPP #ifndef ARROW_HPP
#define ARROW_HPP #define ARROW_HPP
#include <memory>
#include "GameRenderer.hpp" #include "GameRenderer.hpp"
#include "Sprite.hpp" #include "Sprite.hpp"
class Arrow class Arrow
{ {
public: private:
GameRenderer *renderer;
std::unique_ptr<Sprite> sprite;
double x; double x;
double y; double y;
double angle; double angle;
static constexpr int speed = 5; static constexpr int speed = 5;
Arrow(double x, double y, double angle) : x(x), y(y), angle(angle) {} public:
Arrow(GameRenderer *renderer, double x, double y, double angle);
void draw(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y) const; void draw(double offset_x, double offset_y) const;
void update(); void update();
bool operator==(const Arrow &other) const = default;
}; };
#endif #endif

View File

@ -1,28 +0,0 @@
#ifndef COLOR_HPP
#define COLOR_HPP
#include <SDL2/SDL_pixels.h>
typedef SDL_Color Color;
inline Color rgb(uint8_t red, uint8_t green, uint8_t blue)
{
SDL_Color color;
color.r = red;
color.g = green;
color.b = blue;
color.a = 255;
return color;
}
inline Color rgba(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha)
{
SDL_Color color;
color.r = red;
color.g = green;
color.b = blue;
color.a = alpha;
return color;
}
#endif

View File

@ -1,25 +1,11 @@
#include <thread> #include <thread>
#include <functional> #include <functional>
#include <string>
#include "GameRenderer.hpp" #include "GameRenderer.hpp"
#include "Player.hpp" #include "Player.hpp"
#include "Game.hpp" #include "Game.hpp"
#include "Color.hpp"
#include "effects/Particle.hpp"
#include "effects/GoldGainedHint.hpp"
using namespace std::literals::chrono_literals; using namespace std::literals::chrono_literals;
Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40)
{
arrow_sprite = renderer.load_sprite("./assets/arrow.png", 22, 8);
zombo_sprite = renderer.load_sprite("./assets/zombo.png", 40, 40);
gold_sprite = renderer.load_sprite("./assets/gold.png", 30, 20);
small_font = renderer.load_font("./assets/Jersey10-Regular.ttf", 20);
normal_font = renderer.load_font("./assets/Jersey20-Regular.ttf", 26);
}
void Game::update(std::stop_token stop_token) void Game::update(std::stop_token stop_token)
{ {
while (!stop_token.stop_requested()) { while (!stop_token.stop_requested()) {
@ -33,61 +19,12 @@ void Game::update(std::stop_token stop_token)
arrow.update(); arrow.update();
} }
for (std::unique_ptr<Effect> &effect : effects) {
effect->update();
}
std::erase_if(effects, [](const std::unique_ptr<Effect> &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) {
return true;
}
if (
arrow.x > (double)map.tiles.size() * map.tile_size + map.tile_offset_x ||
arrow.y > (double)map.tiles[0].size() * map.tile_size + map.tile_offset_y
) {
return true;
}
return false;
});
for (Zombo &zombo : zombos) { for (Zombo &zombo : zombos) {
zombo.update(player.x, player.y); zombo.update(player.x, player.y);
// Check collision between zombos and arrows
for (Arrow &arrow : arrows) {
if (
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++) {
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);
if (zombo.hp == 0) {
player.gold += 10;
effects.push_back(std::make_unique<GoldGainedHint>(zombo.x, zombo.y, 10, gold_sprite.get(), small_font));
std::erase(zombos, zombo);
}
}
}
} }
if (ticks % 100 == 0 && zombos.size() < 20) { if (ticks % 100 == 0 && zombos.size() < 20) {
zombos.emplace_back(&renderer, *zombo_sprite, player.x, player.y); zombos.emplace_back(&renderer, 20.0, 20.0);
} }
renderer.redraw(); renderer.redraw();
@ -104,30 +41,20 @@ void Game::draw()
{ {
const std::lock_guard lock(game_mutex); const std::lock_guard lock(game_mutex);
renderer.clear_screen(rgb(0x80, 0x40, 0xFF)); renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF);
map.draw(player.x, player.y); map.draw(player.x, player.y);
for (const Zombo &zombo : zombos) { for (const Zombo &zombo : zombos) {
zombo.draw(&renderer, *zombo_sprite, player.x, player.y); zombo.draw(player.x, player.y);
} }
for (const Arrow &arrow : arrows) { for (const Arrow &arrow : arrows) {
arrow.draw(&renderer, *arrow_sprite, player.x, player.y); arrow.draw(player.x, player.y);
}
for (const std::unique_ptr<Effect> &effect : effects) {
effect->draw(&renderer, player.x, player.y);
} }
player.draw(); player.draw();
renderer.draw_sprite(*gold_sprite, renderer.screen_width - 50, 10);
int text_width = renderer.get_text_width(normal_font, std::to_string(player.gold));
renderer.draw_text(normal_font, std::to_string(player.gold), renderer.screen_width - gold_sprite->width - text_width - 28, 10, rgb(0, 0, 0));
renderer.draw_text(normal_font, std::to_string(player.gold), renderer.screen_width - gold_sprite->width - text_width - 30, 8, rgb(255, 255, 255));
renderer.flush(); renderer.flush();
} }
@ -181,9 +108,8 @@ void Game::run()
} }
} }
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1 && player.shooting_cooldown == 0) { if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) {
arrows.emplace_back(player.x, player.y, player.angle); arrows.emplace_back(&renderer, player.x, player.y, player.angle);
player.shooting_cooldown = 20;
} }
int mouse_x, mouse_y; int mouse_x, mouse_y;

View File

@ -8,7 +8,6 @@
#include "Player.hpp" #include "Player.hpp"
#include "Map.hpp" #include "Map.hpp"
#include "Zombo.hpp" #include "Zombo.hpp"
#include "effects/Effect.hpp"
class Game class Game
{ {
@ -18,9 +17,6 @@ private:
Map map; Map map;
std::vector<Arrow> arrows; std::vector<Arrow> arrows;
std::vector<Zombo> zombos; std::vector<Zombo> zombos;
std::vector<std::unique_ptr<Effect>> effects;
std::unique_ptr<Sprite> arrow_sprite, zombo_sprite, gold_sprite;
Font *small_font, *normal_font;
std::mutex game_mutex; std::mutex game_mutex;
unsigned int ticks = 0; unsigned int ticks = 0;
@ -28,7 +24,7 @@ private:
void update(std::stop_token stop_token); void update(std::stop_token stop_token);
public: public:
Game(); Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
~Game() = default; ~Game() = default;

View File

@ -1,5 +1,4 @@
#include <SDL2/SDL.h> #include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <memory> #include <memory>
#include <iostream> #include <iostream>
#include "Sprite.hpp" #include "Sprite.hpp"
@ -13,11 +12,6 @@ GameRenderer::GameRenderer(const std::string &title, const int screen_width, con
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (TTF_Init() < 0) {
std::cerr << "Unable to initialize SDL_ttf" << std::endl;
exit(EXIT_FAILURE);
}
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0"); SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
window = SDL_CreateWindow( window = SDL_CreateWindow(
@ -46,26 +40,6 @@ GameRenderer::~GameRenderer()
SDL_DestroyWindow(window); SDL_DestroyWindow(window);
} }
Font *GameRenderer::load_font(const std::string &file, int font_size)
{
return TTF_OpenFont(file.c_str(), font_size);
}
int GameRenderer::get_text_width(Font *font, const std::string &text) const
{
SDL_Surface *surface = TTF_RenderText_Solid(font, text.c_str(), rgb(0, 0, 0));
return surface->w;
}
void GameRenderer::draw_text(Font *font, const std::string &text, int x, int y, Color color) const
{
SDL_Surface *surface = TTF_RenderText_Solid(font, text.c_str(), color);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_Rect textRect = { .x = x, .y = y, .w = surface->w, .h = surface->h };
SDL_RenderCopy(renderer, texture, nullptr, &textRect);
}
std::unique_ptr<Sprite> GameRenderer::load_sprite(const std::string &file, const int width, const int height) const std::unique_ptr<Sprite> GameRenderer::load_sprite(const std::string &file, const int width, const int height) const
{ {
return std::make_unique<Sprite>(renderer, file, width, height); return std::make_unique<Sprite>(renderer, file, width, height);
@ -77,29 +51,15 @@ void GameRenderer::draw_sprite(const Sprite &sprite, const int x, const int y) c
SDL_RenderCopy(renderer, sprite.texture, nullptr, &rect); SDL_RenderCopy(renderer, sprite.texture, nullptr, &rect);
} }
void GameRenderer::draw_sprite_flipped(const Sprite &sprite, const int x, const int y) const
{
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, 0, nullptr, SDL_FLIP_HORIZONTAL);
}
void GameRenderer::draw_sprite_rotated(const Sprite &sprite, const int x, const int y, const double angle) const void GameRenderer::draw_sprite_rotated(const Sprite &sprite, const int x, const int y, const double angle) const
{ {
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height }; const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE); SDL_RenderCopyEx(renderer, sprite.texture, nullptr, &rect, angle, nullptr, SDL_FLIP_NONE);
} }
void GameRenderer::draw_rect(int x, int y, int width, int height, Color color) const void GameRenderer::clear_screen(const uint8_t r, const uint8_t g, const uint8_t b, const uint8_t a) const
{ {
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a); SDL_SetRenderDrawColor(renderer, r, g, b, a);
const SDL_Rect rect = { .x = x, .y = y, .w = width, .h = height };
SDL_RenderFillRect(renderer, &rect);
}
void GameRenderer::clear_screen(Color color) const
{
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.a);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
} }

View File

@ -4,11 +4,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include "Sprite.hpp" #include "Sprite.hpp"
#include "Color.hpp"
typedef TTF_Font Font;
class GameRenderer class GameRenderer
{ {
@ -26,23 +22,13 @@ public:
~GameRenderer(); ~GameRenderer();
[[nodiscard]] Font *load_font(const std::string &file, int font_size); std::unique_ptr<Sprite> load_sprite(const std::string &file, int width, int height) const;
int get_text_width(Font *font, const std::string &text) const;
void draw_text(Font *font, const std::string &text, int x, int y, Color color) const;
[[nodiscard]] std::unique_ptr<Sprite> load_sprite(const std::string &file, int width, int height) const;
void draw_sprite(const Sprite &sprite, int x, int y) const; void draw_sprite(const Sprite &sprite, int x, int y) const;
void draw_sprite_flipped(const Sprite &sprite, int x, int y) const;
void draw_sprite_rotated(const Sprite &sprite, int x, int y, double angle) const; void draw_sprite_rotated(const Sprite &sprite, int x, int y, double angle) const;
void draw_rect(int x, int y, int width, int height, Color color) const; void clear_screen(uint8_t r, uint8_t g, uint8_t b, uint8_t a) const;
void clear_screen(Color color) const;
void flush() const; void flush() const;

View File

@ -15,7 +15,6 @@ Map::Map(GameRenderer *renderer, int tile_size) :
int y = renderer->screen_height / tile_size / 2; int y = renderer->screen_height / tile_size / 2;
for (unsigned int x = 0; x < tiles.size(); x++) { for (unsigned int x = 0; x < tiles.size(); x++) {
if (rand() % 2 == 0) y += rand() % 3 - 1; if (rand() % 2 == 0) y += rand() % 3 - 1;
y = std::clamp(y, 0, (int)tiles.size() - 1);
tiles[x][y] = Tile::path; tiles[x][y] = Tile::path;
} }
@ -24,7 +23,6 @@ Map::Map(GameRenderer *renderer, int tile_size) :
int x = renderer->screen_width / tile_size / 2; int x = renderer->screen_width / tile_size / 2;
for (unsigned int y = 0; y < tiles[x].size(); y++) { for (unsigned int y = 0; y < tiles[x].size(); y++) {
if (rand() % 2 == 0) x += rand() % 3 - 1; if (rand() % 2 == 0) x += rand() % 3 - 1;
x = std::clamp(x, 0, (int)tiles[x].size() - 1);
tiles[x][y] = Tile::path; tiles[x][y] = Tile::path;
} }
@ -40,7 +38,6 @@ std::vector<Tile> Map::generate_tiles(const std::vector<Tile> &prev_tiles) const
} }
if (rand() % 2 == 0) path_idx += rand() % 3 - 1; if (rand() % 2 == 0) path_idx += rand() % 3 - 1;
path_idx = std::clamp(path_idx, 0, (int)prev_tiles.size() - 1);
new_tiles[path_idx] = Tile::path; new_tiles[path_idx] = Tile::path;

View File

@ -11,6 +11,7 @@ class Map
{ {
private: private:
GameRenderer *renderer; GameRenderer *renderer;
std::vector<std::vector<Tile>> tiles;
std::unique_ptr<Sprite> grass_sprite, path_sprite; std::unique_ptr<Sprite> grass_sprite, path_sprite;
@ -21,8 +22,6 @@ public:
double tile_offset_y; double tile_offset_y;
int tile_size; int tile_size;
std::vector<std::vector<Tile>> tiles;
Map(GameRenderer *renderer, int tile_size); Map(GameRenderer *renderer, int tile_size);
~Map() = default; ~Map() = default;

View File

@ -5,8 +5,7 @@
Player::Player(GameRenderer *renderer) : renderer(renderer) Player::Player(GameRenderer *renderer) : renderer(renderer)
{ {
hero_sprite = renderer->load_sprite("./assets/hero_front.png", 40, 40); hero_sprite = renderer->load_sprite("./assets/hero_front.png", 40, 40);
bow_arrow_sprite = renderer->load_sprite("./assets/bow_arrow.png", 22, 32); bow_sprite = renderer->load_sprite("./assets/bow_arrow.png", 22, 32);
bow_sprite = renderer->load_sprite("./assets/bow.png", 22, 32);
} }
void Player::draw() const void Player::draw() const
@ -18,7 +17,7 @@ void Player::draw() const
); );
renderer->draw_sprite_rotated( renderer->draw_sprite_rotated(
shooting_cooldown > 0 ? *bow_sprite : *bow_arrow_sprite, *bow_sprite,
(int)(renderer->screen_width / 2 - bow_sprite->width / 2 + std::cos(angle) * 30), (int)(renderer->screen_width / 2 - bow_sprite->width / 2 + std::cos(angle) * 30),
(int)(renderer->screen_height / 2 - bow_sprite->height / 2 + std::sin(angle) * 30), (int)(renderer->screen_height / 2 - bow_sprite->height / 2 + std::sin(angle) * 30),
angle * 180 / std::numbers::pi angle * 180 / std::numbers::pi
@ -29,6 +28,4 @@ void Player::update()
{ {
x += x_vel; x += x_vel;
y += y_vel; y += y_vel;
if (shooting_cooldown > 0) shooting_cooldown--;
} }

View File

@ -8,7 +8,7 @@ class Player
{ {
private: private:
GameRenderer *renderer; GameRenderer *renderer;
std::unique_ptr<Sprite> hero_sprite, bow_sprite, bow_arrow_sprite; std::unique_ptr<Sprite> hero_sprite, bow_sprite;
public: public:
double x = 0; double x = 0;
@ -19,10 +19,6 @@ public:
double angle = 0.0; double angle = 0.0;
int shooting_cooldown = 0;
int gold = 0;
static constexpr double speed = 1.5; static constexpr double speed = 1.5;
Player(GameRenderer *renderer); Player(GameRenderer *renderer);

View File

@ -1,6 +1,7 @@
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <SDL2/SDL_image.h> #include <SDL2/SDL_image.h>
#include "Sprite.hpp" #include "Sprite.hpp"
#include <iostream>
Sprite::Sprite(SDL_Renderer *renderer, const std::string &file, int width, int height) : width(width), height(height) Sprite::Sprite(SDL_Renderer *renderer, const std::string &file, int width, int height) : width(width), height(height)
{ {

View File

@ -1,19 +1,8 @@
#include "Zombo.hpp" #include "Zombo.hpp"
Zombo::Zombo(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y) Zombo::Zombo(GameRenderer *renderer, const double x, const double y) : renderer(renderer), x(x), y(y)
{ {
if (rand() % 2 == 0) { sprite = renderer->load_sprite("./assets/zombo.png", 40, 40);
x = rand() % renderer->screen_width - renderer->screen_width / 2;
y = renderer->screen_height / 2 + sprite.height;
if (rand() % 2 == 0) y = -y;
} else {
x = renderer->screen_width / 2 + sprite.width;
y = rand() % renderer->screen_height - renderer->screen_height / 2;
if (rand() % 2 == 0) x = -x;
}
x += player_x;
y += player_y;
} }
void Zombo::update(const double player_x, const double player_y) void Zombo::update(const double player_x, const double player_y)
@ -24,18 +13,11 @@ void Zombo::update(const double player_x, const double player_y)
y += std::sin(angle) * speed; y += std::sin(angle) * speed;
} }
void Zombo::draw(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y) const void Zombo::draw(const double player_x, const double player_y) const
{ {
int draw_x = (int)(x - sprite.width / 2 + renderer->screen_width / 2 - player_x); renderer->draw_sprite(
int draw_y = (int)(y - sprite.height / 2 + renderer->screen_height / 2 - player_y); *sprite,
(int)(x - sprite->width / 2.0 + renderer->screen_width / 2.0 - player_x),
if (x < player_x) (int)(y - sprite->height / 2.0 + renderer->screen_height / 2.0 - player_y)
renderer->draw_sprite(sprite, draw_x, draw_y); );
else
renderer->draw_sprite_flipped(sprite, draw_x, draw_y);
if (hp < max_hp) {
renderer->draw_rect(draw_x, draw_y - 10, sprite.width, 5, rgb(0, 0, 0));
renderer->draw_rect(draw_x, draw_y - 10, hp * sprite.width / max_hp, 5, rgb(255, 0, 0));
}
} }

View File

@ -1,26 +1,27 @@
#ifndef ZOMBO_HPP #ifndef ZOMBO_HPP
#define ZOMBO_HPP #define ZOMBO_HPP
#include <memory>
#include "Sprite.hpp" #include "Sprite.hpp"
#include "GameRenderer.hpp" #include "GameRenderer.hpp"
class Zombo class Zombo
{ {
public: private:
GameRenderer *renderer;
std::unique_ptr<Sprite> sprite;
double x; double x;
double y; double y;
int hp = max_hp;
static constexpr int max_hp = 3;
static constexpr double speed = 0.5; static constexpr double speed = 0.5;
Zombo(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y); public:
Zombo(GameRenderer *renderer, double x, double y);
void update(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; void draw(double player_x, double player_y) const;
bool operator==(const Zombo &other) const = default;
}; };
#endif #endif

View File

@ -1,21 +0,0 @@
#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;
bool operator==(const Effect &other) const = default;
};
#endif

View File

@ -1,32 +0,0 @@
#include <format>
#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()
{
ticks++;
y--;
}
void GoldGainedHint::draw(const GameRenderer *renderer, double player_x, double player_y) const
{
std::string text = std::format("+{}", amount);
int draw_x = (int)(x + renderer->screen_width / 2 - player_x);
int draw_y = (int)(y + renderer->screen_height / 2 - player_y);
int draw_width = gold_sprite->width + 5 + renderer->get_text_width(font, text);
renderer->draw_sprite(*gold_sprite, draw_x - draw_width / 2, draw_y);
renderer->draw_text(
font, text,
draw_x - draw_width / 2 + gold_sprite->width + 5,
draw_y - 2,
rgb(255, 255, 255)
);
}

View File

@ -1,27 +0,0 @@
#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,44 +0,0 @@
#include "Particle.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()
{
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;
}

View File

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