From 8841bded6f515a80a1b43ba5e009a0f67263050c Mon Sep 17 00:00:00 2001 From: Reimar Date: Wed, 29 Oct 2025 12:18:34 +0100 Subject: [PATCH] Add zombies --- Makefile | 1 + assets/zombo.png | Bin 0 -> 370 bytes src/Game.cpp | 4 ++++ src/Game.hpp | 3 +++ src/Zombo.cpp | 29 +++++++++++++++++++++++++++++ src/Zombo.hpp | 26 ++++++++++++++++++++++++++ 6 files changed, 63 insertions(+) create mode 100644 assets/zombo.png create mode 100644 src/Zombo.cpp create mode 100644 src/Zombo.hpp 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 0000000000000000000000000000000000000000..a8ac42af3d446d479d3f94e136e456314b288b1d GIT binary patch literal 370 zcmeAS@N?(olHy`uVBq!ia0vp^8X(NU1|)m_?Z^dEjKx9jPK-BC>eK@{Ea{HEjtmSN z`?>!lvI6-E$sR$z3=CCj3=9n|3=F@3LJcn%7)lKo7+xhXFj&oCU=S~uvn$XBD8ZKG z?d}4kf#9d}Zjfo51s;*b3=De8Ak0{?)V>TT$X?><>&pI;OP0fmdHMU5ML?l#o-U3d z5r^MKIPx_p@Fcg%SA7w;I}%^?%Lee#0oZb6=m}#w^S;kNjfq=={Ct4lsC1{Z$)_uhGV>*|Bu53l`|6SFz%v2%vu@(Fotjk3p{C$Ya^ z*sbzvdA9faXrte~&ki;Pd*lhRYgmdioBYTZpTFgS#l3rvo`OP@!PC{xWt~$(698En Be(L}L literal 0 HcmV?d00001 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