Create effect class, fix zombo hitbox
This commit is contained in:
parent
1ac1211b36
commit
2508658a75
5
Makefile
5
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)
|
||||
|
||||
|
||||
23
src/Game.cpp
23
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> &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) {
|
||||
@ -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<Particle>(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> &effect : effects) {
|
||||
effect->draw(&renderer, player.x, player.y);
|
||||
}
|
||||
|
||||
player.draw();
|
||||
|
||||
@ -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<Arrow> arrows;
|
||||
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;
|
||||
Font *font;
|
||||
|
||||
|
||||
19
src/effects/Effect.hpp
Normal file
19
src/effects/Effect.hpp
Normal 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
|
||||
17
src/effects/GoldGainedHint.cpp
Normal file
17
src/effects/GoldGainedHint.cpp
Normal 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
|
||||
{
|
||||
|
||||
}
|
||||
27
src/effects/GoldGainedHint.hpp
Normal file
27
src/effects/GoldGainedHint.hpp
Normal 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
|
||||
@ -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()
|
||||
{
|
||||
@ -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;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user