Add zombies
This commit is contained in:
parent
e141be92f0
commit
bedac59775
1
Makefile
1
Makefile
@ -3,6 +3,7 @@ OBJS= \
|
|||||||
build/src/Map.o \
|
build/src/Map.o \
|
||||||
build/src/Game.o \
|
build/src/Game.o \
|
||||||
build/src/Arrow.o \
|
build/src/Arrow.o \
|
||||||
|
build/src/Zombo.o \
|
||||||
build/src/Player.o \
|
build/src/Player.o \
|
||||||
build/src/GameRenderer.o
|
build/src/GameRenderer.o
|
||||||
|
|
||||||
|
|||||||
BIN
assets/zombo.png
Normal file
BIN
assets/zombo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 370 B |
10
src/Game.cpp
10
src/Game.cpp
@ -18,12 +18,22 @@ void Game::update(std::stop_token stop_token)
|
|||||||
while (!stop_token.stop_requested()) {
|
while (!stop_token.stop_requested()) {
|
||||||
game_mutex.lock();
|
game_mutex.lock();
|
||||||
|
|
||||||
|
ticks++;
|
||||||
|
|
||||||
player.update();
|
player.update();
|
||||||
|
|
||||||
for (Arrow *arrow : arrows) {
|
for (Arrow *arrow : arrows) {
|
||||||
arrow->update();
|
arrow->update();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (Zombo *zombo : zombos) {
|
||||||
|
zombo->update(player.x, player.y);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ticks % 1000 == 0) {
|
||||||
|
zombos.push_back(new Zombo(&renderer, 20.0, 20.0));
|
||||||
|
}
|
||||||
|
|
||||||
renderer.redraw();
|
renderer.redraw();
|
||||||
|
|
||||||
map.check_bounds(player.x, player.y);
|
map.check_bounds(player.x, player.y);
|
||||||
|
|||||||
@ -7,6 +7,7 @@
|
|||||||
#include "GameRenderer.hpp"
|
#include "GameRenderer.hpp"
|
||||||
#include "Player.hpp"
|
#include "Player.hpp"
|
||||||
#include "Map.hpp"
|
#include "Map.hpp"
|
||||||
|
#include "Zombo.hpp"
|
||||||
|
|
||||||
class Game
|
class Game
|
||||||
{
|
{
|
||||||
@ -15,9 +16,11 @@ private:
|
|||||||
Player player;
|
Player player;
|
||||||
Map map;
|
Map map;
|
||||||
std::vector<Arrow*> arrows;
|
std::vector<Arrow*> arrows;
|
||||||
|
std::vector<Zombo*> zombos;
|
||||||
|
|
||||||
std::jthread update_thread;
|
std::jthread update_thread;
|
||||||
std::mutex game_mutex;
|
std::mutex game_mutex;
|
||||||
|
unsigned int ticks = 0;
|
||||||
|
|
||||||
void update(std::stop_token stop_token);
|
void update(std::stop_token stop_token);
|
||||||
|
|
||||||
|
|||||||
29
src/Zombo.cpp
Normal file
29
src/Zombo.cpp
Normal 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
26
src/Zombo.hpp
Normal 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
|
||||||
Loading…
Reference in New Issue
Block a user