diff --git a/Makefile b/Makefile index 22cb556..eaee22b 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/assets/zombo.png b/assets/zombo.png new file mode 100644 index 0000000..a8ac42a Binary files /dev/null and b/assets/zombo.png differ diff --git a/src/Game.cpp b/src/Game.cpp index 700d146..27d8c3f 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -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); diff --git a/src/Game.hpp b/src/Game.hpp index d4049c4..2a6523f 100644 --- a/src/Game.hpp +++ b/src/Game.hpp @@ -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 arrows; + std::vector zombos; std::jthread update_thread; std::mutex game_mutex; + unsigned int ticks = 0; void update(std::stop_token stop_token); diff --git a/src/Zombo.cpp b/src/Zombo.cpp new file mode 100644 index 0000000..ea71943 --- /dev/null +++ b/src/Zombo.cpp @@ -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) + ); +} diff --git a/src/Zombo.hpp b/src/Zombo.hpp new file mode 100644 index 0000000..009c108 --- /dev/null +++ b/src/Zombo.hpp @@ -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