Add zombies

This commit is contained in:
Reimar 2025-10-29 12:18:34 +01:00
parent e141be92f0
commit 8841bded6f
6 changed files with 63 additions and 0 deletions

View File

@ -3,6 +3,7 @@ OBJS= \
build/src/Map.o \
build/src/Game.o \
build/src/Arrow.o \
build/src/Zombo.o \
build/src/Player.o \
build/src/GameRenderer.o

BIN
assets/zombo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 370 B

View File

@ -24,6 +24,10 @@ void Game::update(std::stop_token stop_token)
arrow->update();
}
for (Zombo *zombo : zombos) {
zombo->update(player.x, player.y); // doesn't work :((((
}
renderer.redraw();
map.check_bounds(player.x, player.y);

View File

@ -7,6 +7,7 @@
#include "GameRenderer.hpp"
#include "Player.hpp"
#include "Map.hpp"
#include "Zombo.hpp"
class Game
{
@ -15,9 +16,11 @@ private:
Player player;
Map map;
std::vector<Arrow*> arrows;
std::vector<Zombo*> zombos;
std::jthread update_thread;
std::mutex game_mutex;
unsigned int ticks = 0;
void update(std::stop_token stop_token);

29
src/Zombo.cpp Normal file
View File

@ -0,0 +1,29 @@
#include "Zombo.hpp"
Zombo::Zombo(GameRenderer *renderer, const double x, const double y) : renderer(renderer), x(x), y(y)
{
sprite = renderer->load_sprite("./assets/zombo.png", 40, 40);
}
void Zombo::update(const double player_x, const double player_y)
{
double x_vel = 0, y_vel = 0;
if (player_x > x) x_vel = speed;
else if (player_x < x) x_vel = -speed;
if (player_y > y) y_vel = speed;
else if (player_y < y) y_vel = -speed;
x += x_vel;
y += y_vel;
}
void Zombo::draw(const double player_x, const double player_y) const
{
renderer->draw_sprite(
sprite,
(int)(x - sprite.width / 2 + renderer->screen_width / 2 - player_x),
(int)(y - sprite.height / 2 + renderer->screen_height / 2 - player_y)
);
}

26
src/Zombo.hpp Normal file
View File

@ -0,0 +1,26 @@
#ifndef ZOMBO_HPP
#define ZOMBO_HPP
#include "Sprite.hpp"
#include "GameRenderer.hpp"
class Zombo
{
private:
GameRenderer *renderer;
Sprite sprite;
double x;
double y;
static constexpr int speed = 3;
public:
Zombo(GameRenderer *renderer, double x, double y);
void update(double player_x, double player_y);
void draw(double player_x, double player_y) const;
};
#endif