Compare commits
No commits in common. "no-worky" and "master" have entirely different histories.
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
||||
build/
|
||||
a.out
|
||||
|
||||
.vscode/
|
||||
|
||||
1
Makefile
1
Makefile
@ -3,7 +3,6 @@ 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
BIN
assets/zombo.png
Binary file not shown.
|
Before Width: | Height: | Size: 370 B |
@ -1,6 +1,4 @@
|
||||
#include "Arrow.hpp"
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
|
||||
Arrow::Arrow(GameRenderer *renderer, const double x, const double y, const double angle)
|
||||
: renderer(renderer), x(x), y(y), angle(angle)
|
||||
@ -14,7 +12,7 @@ void Arrow::draw(const double player_x, const double player_y) const
|
||||
sprite,
|
||||
(int)(x - sprite.width / 2 + renderer->screen_width / 2 - player_x),
|
||||
(int)(y - sprite.height / 2 + renderer->screen_height / 2 - player_y),
|
||||
angle * 180 / std::numbers::pi
|
||||
angle * 180 / M_PI
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
150
src/Game.cpp
150
src/Game.cpp
@ -1,57 +1,49 @@
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include "GameRenderer.hpp"
|
||||
#include "Player.hpp"
|
||||
#include "Game.hpp"
|
||||
#include <chrono>
|
||||
|
||||
using namespace std::literals::chrono_literals;
|
||||
|
||||
Game::~Game()
|
||||
{
|
||||
for (const Arrow *arrow : arrows) {
|
||||
delete arrow;
|
||||
}
|
||||
}
|
||||
|
||||
void Game::update(std::stop_token stop_token)
|
||||
{
|
||||
while (!stop_token.stop_requested()) {
|
||||
{
|
||||
auto lock = std::lock_guard(game_mutex);
|
||||
game_mutex.lock();
|
||||
|
||||
ticks++;
|
||||
player.update();
|
||||
|
||||
player.update();
|
||||
|
||||
for (auto& arrow : arrows) {
|
||||
arrow.update();
|
||||
}
|
||||
|
||||
for (auto& zombo : zombos) {
|
||||
zombo.update(player.x, player.y);
|
||||
}
|
||||
|
||||
if (ticks % 1000 == 0) {
|
||||
zombos.push_back(Zombo(&renderer, 20.0, 20.0)); // doesn't work :((((
|
||||
}
|
||||
|
||||
renderer.redraw();
|
||||
|
||||
map.check_bounds(player.x, player.y);
|
||||
for (Arrow *arrow : arrows) {
|
||||
arrow->update();
|
||||
}
|
||||
|
||||
renderer.redraw();
|
||||
|
||||
map.check_bounds(player.x, player.y);
|
||||
|
||||
game_mutex.unlock();
|
||||
|
||||
std::this_thread::sleep_for(16666us);
|
||||
}
|
||||
}
|
||||
|
||||
void Game::draw()
|
||||
{
|
||||
std::lock_guard lock(game_mutex);
|
||||
const std::lock_guard lock(game_mutex);
|
||||
|
||||
renderer.clear_screen(0x80, 0x40, 0xFF, 0xFF);
|
||||
|
||||
map.draw(player.x, player.y);
|
||||
|
||||
for (const auto& arrow : arrows) {
|
||||
arrow.draw(player.x, player.y);
|
||||
}
|
||||
for (const auto& zombo : zombos) {
|
||||
zombo.draw(player.x, player.y);
|
||||
for (const Arrow *arrow : arrows) {
|
||||
arrow->draw(player.x, player.y);
|
||||
}
|
||||
|
||||
player.draw();
|
||||
@ -61,66 +53,62 @@ void Game::draw()
|
||||
|
||||
void Game::run()
|
||||
{
|
||||
auto update_thread = std::jthread(std::bind_front(&Game::update, this));
|
||||
update_thread = std::jthread(std::bind_front(&Game::update, this));
|
||||
|
||||
while (true) {
|
||||
SDL_Event e;
|
||||
SDL_WaitEvent(&e);
|
||||
|
||||
{
|
||||
auto lock = std::lock_guard(game_mutex);
|
||||
|
||||
if (e.type == SDL_QUIT) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (e.type == SDL_KEYDOWN) {
|
||||
switch (e.key.keysym.sym) {
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
player.x_vel = player.speed;
|
||||
break;
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
player.x_vel = -player.speed;
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
case SDLK_s:
|
||||
player.y_vel = player.speed;
|
||||
break;
|
||||
case SDLK_UP:
|
||||
case SDLK_w:
|
||||
player.y_vel = -player.speed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == SDL_KEYUP) {
|
||||
switch (e.key.keysym.sym) {
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
player.x_vel = 0;
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
case SDLK_s:
|
||||
case SDLK_UP:
|
||||
case SDLK_w:
|
||||
player.y_vel = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) {
|
||||
arrows.push_back(Arrow(&renderer, player.x, player.y, player.angle));
|
||||
}
|
||||
|
||||
int mouse_x, mouse_y;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2);
|
||||
if (e.type == SDL_QUIT) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (e.type == SDL_KEYDOWN) {
|
||||
switch (e.key.keysym.sym) {
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
player.x_vel = player.speed;
|
||||
break;
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
player.x_vel = -player.speed;
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
case SDLK_s:
|
||||
player.y_vel = player.speed;
|
||||
break;
|
||||
case SDLK_UP:
|
||||
case SDLK_w:
|
||||
player.y_vel = -player.speed;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == SDL_KEYUP) {
|
||||
switch (e.key.keysym.sym) {
|
||||
case SDLK_RIGHT:
|
||||
case SDLK_d:
|
||||
case SDLK_LEFT:
|
||||
case SDLK_a:
|
||||
player.x_vel = 0;
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
case SDLK_s:
|
||||
case SDLK_UP:
|
||||
case SDLK_w:
|
||||
player.y_vel = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) {
|
||||
arrows.push_back(new Arrow(&renderer, player.x, player.y, player.angle));
|
||||
}
|
||||
|
||||
int mouse_x, mouse_y;
|
||||
SDL_GetMouseState(&mouse_x, &mouse_y);
|
||||
player.angle = std::atan2(mouse_y - renderer.screen_height / 2, mouse_x - renderer.screen_width / 2);
|
||||
|
||||
draw();
|
||||
}
|
||||
}
|
||||
|
||||
11
src/Game.hpp
11
src/Game.hpp
@ -1,14 +1,12 @@
|
||||
#ifndef GAME_HPP
|
||||
#define GAME_HPP
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include "Arrow.hpp"
|
||||
#include "GameRenderer.hpp"
|
||||
#include "Player.hpp"
|
||||
#include "Map.hpp"
|
||||
#include "Zombo.hpp"
|
||||
#include <stop_token>
|
||||
|
||||
class Game
|
||||
{
|
||||
@ -16,18 +14,17 @@ private:
|
||||
GameRenderer renderer;
|
||||
Player player;
|
||||
Map map;
|
||||
std::vector<Arrow> arrows;
|
||||
std::vector<Zombo> zombos;
|
||||
std::vector<Arrow*> arrows;
|
||||
|
||||
std::jthread update_thread;
|
||||
std::mutex game_mutex;
|
||||
unsigned int ticks = 0;
|
||||
|
||||
void update(std::stop_token stop_token);
|
||||
|
||||
public:
|
||||
Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
|
||||
|
||||
~Game() = default;
|
||||
~Game();
|
||||
|
||||
void run();
|
||||
|
||||
|
||||
@ -34,4 +34,4 @@ public:
|
||||
void redraw() const;
|
||||
};
|
||||
|
||||
#endif
|
||||
#endif
|
||||
@ -1,7 +1,5 @@
|
||||
#include "GameRenderer.hpp"
|
||||
#include "Player.hpp"
|
||||
#include <cmath>
|
||||
#include <numbers>
|
||||
|
||||
Player::Player(GameRenderer *renderer) : renderer(renderer)
|
||||
{
|
||||
@ -21,7 +19,7 @@ void Player::draw() const
|
||||
bow_sprite,
|
||||
(int)(renderer->screen_width / 2 - bow_sprite.width / 2 + std::cos(angle) * 30),
|
||||
(int)(renderer->screen_height / 2 - bow_sprite.height / 2 + std::sin(angle) * 30),
|
||||
angle * 180 / std::numbers::pi
|
||||
angle * 180 / M_PI
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
#ifndef SPRITE_HPP
|
||||
#define SPRITE_HPP
|
||||
|
||||
#include "Whatever.hpp"
|
||||
#include <SDL2/SDL_render.h>
|
||||
|
||||
typedef struct {
|
||||
|
||||
@ -1,4 +0,0 @@
|
||||
#ifndef WHATEVER_HPP
|
||||
#define WHATEVER_HPP
|
||||
|
||||
#endif
|
||||
@ -1,29 +0,0 @@
|
||||
#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)
|
||||
);
|
||||
}
|
||||
@ -1,26 +0,0 @@
|
||||
#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…
x
Reference in New Issue
Block a user