Compare commits
6 Commits
83ed7e5190
...
70bc5cfc04
| Author | SHA1 | Date | |
|---|---|---|---|
| 70bc5cfc04 | |||
| bfa0105ebf | |||
| 2508658a75 | |||
| 1ac1211b36 | |||
| 9b15ebc1a5 | |||
| 7cc2b4c64f |
4
Makefile
4
Makefile
@ -6,7 +6,9 @@ OBJS= \
|
||||
build/src/Zombo.o \
|
||||
build/src/Sprite.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)
|
||||
|
||||
|
||||
BIN
assets/Jersey10-Regular.ttf
Normal file
BIN
assets/Jersey10-Regular.ttf
Normal file
Binary file not shown.
BIN
assets/Jersey20-Regular.ttf
Normal file
BIN
assets/Jersey20-Regular.ttf
Normal file
Binary file not shown.
BIN
assets/gold.png
Normal file
BIN
assets/gold.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 334 B |
@ -1,7 +1,7 @@
|
||||
#ifndef COLOR_HPP
|
||||
#define COLOR_HPP
|
||||
|
||||
#include <cstdint>
|
||||
#include <SDL2/SDL_pixels.h>
|
||||
|
||||
typedef SDL_Color Color;
|
||||
|
||||
|
||||
42
src/Game.cpp
42
src/Game.cpp
@ -5,6 +5,8 @@
|
||||
#include "Player.hpp"
|
||||
#include "Game.hpp"
|
||||
#include "Color.hpp"
|
||||
#include "effects/Particle.hpp"
|
||||
#include "effects/GoldGainedHint.hpp"
|
||||
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
@ -14,7 +16,8 @@ Game::Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&rend
|
||||
zombo_sprite = renderer.load_sprite("./assets/zombo.png", 40, 40);
|
||||
gold_sprite = renderer.load_sprite("./assets/gold.png", 30, 20);
|
||||
|
||||
font = renderer.load_font("./assets/Jersey20-Regular.ttf", 26);
|
||||
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)
|
||||
@ -30,6 +33,14 @@ void Game::update(std::stop_token stop_token)
|
||||
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) {
|
||||
@ -52,15 +63,24 @@ 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++) {
|
||||
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);
|
||||
|
||||
zombo.hp--;
|
||||
if (zombo.hp == 0) {
|
||||
std::erase(zombos, zombo);
|
||||
player.gold += 10;
|
||||
effects.push_back(std::make_unique<GoldGainedHint>(zombo.x, zombo.y, 10, gold_sprite.get(), small_font));
|
||||
|
||||
std::erase(zombos, zombo);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -96,13 +116,17 @@ void Game::draw()
|
||||
arrow.draw(&renderer, *arrow_sprite, player.x, player.y);
|
||||
}
|
||||
|
||||
for (const std::unique_ptr<Effect> &effect : effects) {
|
||||
effect->draw(&renderer, player.x, player.y);
|
||||
}
|
||||
|
||||
player.draw();
|
||||
|
||||
renderer.draw_sprite(*gold_sprite, renderer.screen_width - 50, 10);
|
||||
|
||||
int text_width = renderer.get_text_width(font, std::to_string(player.gold));
|
||||
renderer.draw_text(font, std::to_string(player.gold), renderer.screen_width - gold_sprite->width - text_width - 28, 10, rgb(0, 0, 0));
|
||||
renderer.draw_text(font, std::to_string(player.gold), renderer.screen_width - gold_sprite->width - text_width - 30, 8, rgb(255, 255, 255));
|
||||
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();
|
||||
}
|
||||
@ -159,7 +183,7 @@ void Game::run()
|
||||
|
||||
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1 && player.shooting_cooldown == 0) {
|
||||
arrows.emplace_back(player.x, player.y, player.angle);
|
||||
player.shooting_cooldown = 40;
|
||||
player.shooting_cooldown = 20;
|
||||
}
|
||||
|
||||
int mouse_x, mouse_y;
|
||||
|
||||
@ -8,6 +8,7 @@
|
||||
#include "Player.hpp"
|
||||
#include "Map.hpp"
|
||||
#include "Zombo.hpp"
|
||||
#include "effects/Effect.hpp"
|
||||
|
||||
class Game
|
||||
{
|
||||
@ -17,8 +18,9 @@ private:
|
||||
Map map;
|
||||
std::vector<Arrow> arrows;
|
||||
std::vector<Zombo> zombos;
|
||||
std::vector<std::unique_ptr<Effect>> effects;
|
||||
std::unique_ptr<Sprite> arrow_sprite, zombo_sprite, gold_sprite;
|
||||
Font *font;
|
||||
Font *small_font, *normal_font;
|
||||
|
||||
std::mutex game_mutex;
|
||||
unsigned int ticks = 0;
|
||||
|
||||
@ -89,6 +89,14 @@ void GameRenderer::draw_sprite_rotated(const Sprite &sprite, const int x, const
|
||||
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
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.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);
|
||||
|
||||
@ -40,6 +40,8 @@ public:
|
||||
|
||||
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(Color color) const;
|
||||
|
||||
void flush() const;
|
||||
|
||||
@ -15,6 +15,7 @@ Map::Map(GameRenderer *renderer, int tile_size) :
|
||||
int y = renderer->screen_height / tile_size / 2;
|
||||
for (unsigned int x = 0; x < tiles.size(); x++) {
|
||||
if (rand() % 2 == 0) y += rand() % 3 - 1;
|
||||
y = std::clamp(y, 0, (int)tiles.size() - 1);
|
||||
|
||||
tiles[x][y] = Tile::path;
|
||||
}
|
||||
@ -23,6 +24,7 @@ Map::Map(GameRenderer *renderer, int tile_size) :
|
||||
int x = renderer->screen_width / tile_size / 2;
|
||||
for (unsigned int y = 0; y < tiles[x].size(); y++) {
|
||||
if (rand() % 2 == 0) x += rand() % 3 - 1;
|
||||
x = std::clamp(x, 0, (int)tiles[x].size() - 1);
|
||||
|
||||
tiles[x][y] = Tile::path;
|
||||
}
|
||||
@ -38,6 +40,7 @@ std::vector<Tile> Map::generate_tiles(const std::vector<Tile> &prev_tiles) const
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@ -33,4 +33,9 @@ void Zombo::draw(const GameRenderer *renderer, const Sprite &sprite, double play
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,8 +9,9 @@ class Zombo
|
||||
public:
|
||||
double x;
|
||||
double y;
|
||||
int hp = 3;
|
||||
int hp = max_hp;
|
||||
|
||||
static constexpr int max_hp = 3;
|
||||
static constexpr double speed = 0.5;
|
||||
|
||||
Zombo(const GameRenderer *renderer, const Sprite &sprite, double player_x, double player_y);
|
||||
|
||||
21
src/effects/Effect.hpp
Normal file
21
src/effects/Effect.hpp
Normal file
@ -0,0 +1,21 @@
|
||||
#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
|
||||
32
src/effects/GoldGainedHint.cpp
Normal file
32
src/effects/GoldGainedHint.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
#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)
|
||||
);
|
||||
}
|
||||
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
|
||||
44
src/effects/Particle.cpp
Normal file
44
src/effects/Particle.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#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;
|
||||
}
|
||||
26
src/effects/Particle.hpp
Normal file
26
src/effects/Particle.hpp
Normal file
@ -0,0 +1,26 @@
|
||||
#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
|
||||
Loading…
Reference in New Issue
Block a user