Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions ECS/include/components/graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,8 @@ namespace comp {
this->setOutlineColor(outline);
}

void setOffset(Vector2i offset) { this->offset = offset; }

void draw(float pos_x, float pos_y, window const &window) {
if (this->visible) {
this->setPosition({pos_x + offset.x, pos_y + offset.y});
Expand Down
2 changes: 0 additions & 2 deletions client/include/args.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

#pragma once

#include <cstddef>
#include <string>


struct args {
std::string addr;
int port;
Expand Down
15 changes: 10 additions & 5 deletions client/include/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ struct Vec {
float x, y;
};

struct FinalText {};

struct Score {};

struct player_count {
size_t count;
};
Expand All @@ -34,10 +38,10 @@ enum PlayerAction {

struct KeyBindings {
std::array<sf::Keyboard::Key, ACTION_COUNT> keys = {
sf::Keyboard::Key::W, // MOVE_UP
sf::Keyboard::Key::S, // MOVE_DOWN
sf::Keyboard::Key::A, // MOVE_LEFT
sf::Keyboard::Key::D, // MOVE_RIGHT
sf::Keyboard::Key::Up, // MOVE_UP
sf::Keyboard::Key::Down, // MOVE_DOWN
sf::Keyboard::Key::Left, // MOVE_LEFT
sf::Keyboard::Key::Right, // MOVE_RIGHT
sf::Keyboard::Key::Space // SHOOT
};
};
Expand Down Expand Up @@ -93,7 +97,8 @@ enum Type {
KILL_ENTITY,
SCORE_UPDATE,
START,
STOP,
WIN,
LOSE,
NOT_AUTH,
LOGON,
LOGOFF,
Expand Down
18 changes: 16 additions & 2 deletions client/src/create_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,7 @@ namespace comp {

void end_setup_entity(registry &reg, window &MainWindow) {
entity texttemp(reg.spawn_entity());
entity FinaltextE(reg.spawn_entity());
entity play_button_entity(reg.spawn_entity());
RectButton play_button = createDefaultButton("assets/arial_bold.ttf", "X", {90.f, 90.f}, {60.f, 30.f});

Expand All @@ -575,11 +576,24 @@ namespace comp {
position(.0f, .0f),
MainWindow,
text(
std::string("The final score is 0 !"),
std::string("The final score is 0"),
_assets.safe_get<sf::Font>("assets/arial_bold.ttf"),
48,
{1920 / 2 - 200, 1080 / 2 - 24}
{int(MainWindow->getSize().x) / 2 - 250, int(MainWindow->getSize().y) / 2 - 24}
),
Score(),
enabled());

reg.add_components(FinaltextE,
position(.0f, .0f),
MainWindow,
text(
std::string("The bad guys won..."),
_assets.safe_get<sf::Font>("assets/arial_bold.ttf"),
48,
{int(MainWindow->getSize().x) / 2 - 250, int(MainWindow->getSize().y) / 2 - 200}
),
FinalText(),
enabled());

reg.add_components(play_button_entity,
Expand Down
16 changes: 13 additions & 3 deletions client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,18 @@ namespace comp {
size_t nb = 0;
std::memcpy(&nb, msg.second.data(), sizeof(size_t));
auto &texts = reg.get_components<text>();
auto &Finals = reg.get_components<FinalText>();
auto &Scores = reg.get_components<Score>();

for (auto &&[text] : zipper(texts))
for (auto &&[text, _] : zipper(texts, Scores))
text.setString("The final score is " + std::to_string(nb) + " !");
for (auto &&[text, _] : zipper(texts, Finals)) {
if (nb >= 500) {
text.setString("You have defeated the enemies !");
text.setOffset({SIZEX / 2 - 350, SIZEY / 2 - 200});
} else
text.setString("The bad guys won...");
}
state = END;
}

Expand Down Expand Up @@ -154,7 +163,7 @@ namespace comp {
} else if (msg.first.type == LOBBYCOUNT) {
if (state == WAITING)
update_waiting(regs[state], msg);
} else if (msg.first.type == STOP) {
} else if (msg.first.type == WIN || msg.first.type == LOSE) {
set_end(regs[END], msg, state);
} else
std::cerr << "Unknow code received : " << msg.first.type << std::endl;
Expand Down Expand Up @@ -404,6 +413,8 @@ namespace comp {
reg.register_component<pressed>();
reg.register_component<settingsButton>();
reg.register_component<player_count>();
reg.register_component<FinalText>();
reg.register_component<Score>();

reg.safe_add_system<position, sprite, window, enabled>(sprite_system);
reg.safe_add_system<position, button, window, enabled>(button_system);
Expand All @@ -423,7 +434,6 @@ namespace comp {
reg.safe_add_system<position, text, window, enabled>(text_system);

connect_setup_entity(reg, MainWindow);
// waiting_setup_entity(reg, MainWindow, nb_player);
}

void main(args ClientArgs) {
Expand Down
3 changes: 2 additions & 1 deletion server/include/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ enum Type {
KILL_ENTITY,
SCORE_UPDATE,
START,
STOP,
WIN,
LOSE,
NOT_AUTH,
LOGON,
LOGOFF,
Expand Down
28 changes: 20 additions & 8 deletions server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,20 +76,23 @@ bool check_alive(registry &game) {

void end_game(State &state, server &server, size_t &score) {
state = LOBBY;
server.sendAll(0, BASIC, STOP, sizeof(size_t), &score);
if (score >= 500)
server.sendAll(0, BASIC, WIN, sizeof(size_t), &score);
else
server.sendAll(0, BASIC, LOSE, sizeof(size_t), &score);
server.clear();
server.allowNewClient(true);
}

void main_loop(std::array<registry, 3> &regs, server &server, size_t nb_player) {
void main_loop(std::array<registry, 3> &regs, server &server, size_t nb_player, int ArgsScore) {
State state = LOBBY;
input cin;
clients clients;
sendFct fct([&server](auto &&...args) {
server.pushMessage(args...);
});
size_t spawn_clock = 0;
size_t score = 0;
size_t score = ArgsScore;

auto lastTick = std::chrono::steady_clock::now();
const auto tickDuration = std::chrono::duration<double>(1.0 / TPS);
Expand All @@ -103,7 +106,7 @@ void main_loop(std::array<registry, 3> &regs, server &server, size_t nb_player)
if (elapsed >= tickDuration) {
lastTick = now;
if (state == GAME) {
if (check_alive(regs[state])) {
if (check_alive(regs[state]) or score >= 500) {
end_game(state, server, score);
return;
}
Expand Down Expand Up @@ -133,7 +136,7 @@ void setup_main_game(registry &mainGame) {
mainGame.safe_add_system<comp::position, comp::velocity, comp::enabled>(position_system);
}

void handle_args(int argc, char **argv, size_t &port, int &player_need) {
void handle_args(int argc, char **argv, size_t &port, int &player_need, int &score) {
argsParser parser(argc, argv);

if (parser.hasArg("--nb-player")) {
Expand All @@ -146,26 +149,35 @@ void handle_args(int argc, char **argv, size_t &port, int &player_need) {
if (temp.has_value())
port = temp.value();
}
if (parser.hasArg("--score")) {
auto temp = parser.getArgInt("--score");
if (temp.has_value())
score = temp.value();
}
}

int main(int argc, char **argv) {
std::array<registry, 3> regs;
registry mainGame;
int player_need = 4;
size_t port = 8080;
int score = 0;

handle_args(argc, argv, port, player_need);
handle_args(argc, argv, port, player_need, score);
server server(port);
if (!server.running)
return(84);
std::cout << "Server open on port : " << port << std::endl;
std::cout << "Need " << player_need << " players to start a game" << std::endl;
std::cout << "Start at " << score << " score" << std::endl;

setup_main_game(mainGame);
std::array<registry, 3> regs;


while(true) {
registry actual_game(mainGame);
regs[GAME] = actual_game;
main_loop(regs, server, player_need);
main_loop(regs, server, player_need, score);
}
return 0;
}