Compare commits
1 Commits
127b96a92f
...
89cea44086
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89cea44086 |
42
src/Game.cpp
42
src/Game.cpp
@ -1,4 +1,3 @@
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
#include <functional>
|
||||
#include "GameRenderer.hpp"
|
||||
@ -8,32 +7,33 @@
|
||||
|
||||
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);
|
||||
|
||||
ticks++;
|
||||
game_mutex.lock();
|
||||
|
||||
player.update();
|
||||
|
||||
for (auto& arrow : arrows) {
|
||||
arrow.update();
|
||||
for (Arrow *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 :((((
|
||||
for (Zombo *zombo : zombos) {
|
||||
zombo->update(player.x, player.y); // doesn't work :((((
|
||||
}
|
||||
|
||||
renderer.redraw();
|
||||
|
||||
map.check_bounds(player.x, player.y);
|
||||
}
|
||||
|
||||
game_mutex.unlock();
|
||||
|
||||
std::this_thread::sleep_for(16666us);
|
||||
}
|
||||
@ -41,14 +41,14 @@ void Game::update(std::stop_token stop_token)
|
||||
|
||||
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 Arrow *arrow : arrows) {
|
||||
arrow->draw(player.x, player.y);
|
||||
}
|
||||
|
||||
player.draw();
|
||||
@ -58,15 +58,12 @@ 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;
|
||||
}
|
||||
@ -110,13 +107,12 @@ void Game::run()
|
||||
}
|
||||
|
||||
if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == 1) {
|
||||
arrows.push_back(Arrow(&renderer, player.x, player.y, player.angle));
|
||||
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();
|
||||
}
|
||||
|
||||
@ -1,6 +1,7 @@
|
||||
#ifndef GAME_HPP
|
||||
#define GAME_HPP
|
||||
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
#include "Arrow.hpp"
|
||||
@ -15,9 +16,10 @@ private:
|
||||
GameRenderer renderer;
|
||||
Player player;
|
||||
Map map;
|
||||
std::vector<Arrow> arrows;
|
||||
std::vector<Zombo> zombos;
|
||||
std::vector<Arrow*> arrows;
|
||||
std::vector<Zombo*> zombos;
|
||||
|
||||
std::jthread update_thread;
|
||||
std::mutex game_mutex;
|
||||
unsigned int ticks = 0;
|
||||
|
||||
@ -26,6 +28,8 @@ private:
|
||||
public:
|
||||
Game() : renderer("Zombo Shooter", 800, 450), player(&renderer), map(&renderer, 40) {}
|
||||
|
||||
~Game();
|
||||
|
||||
void run();
|
||||
|
||||
void draw();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user