Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
if (MSVC)
add_compile_options(/W4)
else()
add_compile_options(-Wall -Wextra)
add_compile_options(-Wall -Wextra -Wno-overloaded-virtual)
endif()

find_program(CCACHE_PROGRAM ccache)
Expand Down
6 changes: 6 additions & 0 deletions ECS/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ project(ECS LANGUAGES CXX)

find_package(SFML COMPONENTS Network Graphics Window Audio System CONFIG REQUIRED)

if (MSVC)
add_compile_options(/W4)
else()
add_compile_options(-Wno-overloaded-virtual)
endif()

file(GLOB_RECURSE ECS_SOURCES CONFIGURE_DEPENDS src/*.cpp)

add_library(ECS STATIC ${ECS_SOURCES})
Expand Down
4 changes: 4 additions & 0 deletions client/include/client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ enum Type {
POS,
NEW_PLAYER,
NEW_ENEMY,
NEW_STATIC_ENEMY,
NEW_MISSILE,
NEW_MONSTER_MISSILE,
NEW_STATIC_MONSTER_MISSILE,
KILL_ENTITY,
SCORE_UPDATE,
START,
Expand Down Expand Up @@ -136,8 +138,10 @@ void refresh_bg(registry const &,
namespace comp {
void new_missile(registry &, entity &, Vec &, std::queue<location> &);
void new_enemy_missile(registry &, entity &, Vec &, std::queue<location> &);
void new_static_enemy_missile(registry &, entity &, Vec &, std::queue<location> &);
void new_player(registry &, entity &, const info &, std::queue<location> &);
void new_enemy(registry &, entity &, Vec &, std::queue<location> &);
void new_static_enemy(registry &, entity &, Vec &, std::queue<location> &);
void new_you(registry &, entity &, const info &, std::queue<location> &);
void new_window(registry &, size_t &, window &);
void setup_lobby(registry &, window &);
Expand Down
28 changes: 28 additions & 0 deletions client/src/create_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ namespace comp {
enabled());
}

void new_static_enemy_missile(registry &reg, entity &entity, Vec &pos, std::queue<location> &queue) {
sprite missile(_assets.safe_get<sf::Texture>("assets/r-typesheet31.gif"), {14, 14}, {165, 201});

missile.setScale({2.0f, 2.0f});
reg.add_components(entity,
velocity(-2.0f, .0f),
position(pos.x, pos.y),
missile,
network(queue),
enabled());
}

void new_player(registry &reg, entity &entity, const info &info, std::queue<location> &queue) {
std::string asset("assets/r-typesheetShip" + std::to_string(info.color) + ".gif");
sprite player(_assets.safe_get<sf::Texture>(asset), {21, 24}, {507, 6});
Expand Down Expand Up @@ -138,6 +150,22 @@ namespace comp {
enabled());
}

void new_static_enemy(registry &reg, entity &entity, Vec &pos, std::queue<location> &queue) {
sprite enemy(_assets.safe_get<sf::Texture>("assets/r-typesheet31.gif"), {34, 28}, {0, 20});
enemy.setScale({2.0f, 2.0f});

reg.add_components(entity,
position(pos.x, pos.y),
velocity(0.0f, .0f),
hittable({30.0f, 40.0f}, {4.0f, 4.0f}, {{ hittable_signals::hit,
[](registry &, hittable &, size_t) {
// std::cout << "Collide!!!" << std::endl;
} }}),
enemy,
network(queue),
enabled());
}

void new_you(registry &reg, entity &entity, const info &info, std::queue<location> &queue) {
std::string asset("assets/r-typesheetShip" + std::to_string(info.color) + ".gif");
sprite player_s(_assets.safe_get<sf::Texture>(asset), {21, 24}, {507, 6});
Expand Down
4 changes: 3 additions & 1 deletion client/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ namespace comp {
void get_data(std::array<registry, 5> &regs, client &client, server_map &server_map, size_t &score, State &state) {
std::unordered_map<size_t, std::function<void(registry &, entity &, Vec &, std::queue<location> &)>> fct_map({
{ NEW_ENEMY, new_enemy },
{ NEW_STATIC_ENEMY, new_static_enemy },
{ NEW_MISSILE, new_missile },
{ NEW_MONSTER_MISSILE, new_enemy_missile }
{ NEW_MONSTER_MISSILE, new_enemy_missile },
{ NEW_STATIC_MONSTER_MISSILE, new_static_enemy_missile }
});
std::unordered_map<size_t, std::function<void(registry &, entity &, const info &, std::queue<location> &)>> player_map({
{YOU_ID, new_you},
Expand Down
11 changes: 10 additions & 1 deletion server/include/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ enum Type {
POS,
NEW_PLAYER,
NEW_ENEMY,
NEW_STATIC_ENEMY,
NEW_MISSILE,
NEW_MONSTER_MISSILE,
NEW_STATIC_MONSTER_MISSILE,
KILL_ENTITY,
SCORE_UPDATE,
START,
Expand Down Expand Up @@ -114,6 +116,10 @@ struct monster {
sendFct const &pushToSendQueue;
};

struct basic_enemy {};

struct static_enemy {};

struct killable {
int health;
int lastDamager;
Expand Down Expand Up @@ -217,10 +223,13 @@ void player_shoot_collision_system(registry &r,
void spawn_enemy(registry &, server &, size_t &,
const sendFct &, const Vec &, const Vec &);

void spawn_static_enemy(registry &, server &, size_t &,
const sendFct &, const Vec &, const Vec &);

void add_client(registry &, server &, clients &,
sendFct &, std::string const &, size_t &);

void send_existing_players(registry &, server &, std::string const &, size_t ,
sparse_array<comp::position> &, sparse_array<comp::enabled> &);

std::vector<char> serializeInfo(info const &i);
std::vector<char> serializeInfo(info const &);
55 changes: 53 additions & 2 deletions server/src/create_entities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,31 @@ void enemy_shoot(registry &r, comp::clock &clock, size_t id) {
mstr->pushToSendQueue(entity, SAFE, NEW_MONSTER_MISSILE, sizeof(*missile_pos), &*missile_pos);
}

void static_enemy_shoot(registry &r, comp::clock &clock, size_t id) {
auto &pos(r.get_components<comp::position>()[id]);
auto &mstr(r.get_components<monster>()[id]);
entity entity(r.spawn_entity());

clock.restart();
r.add_components(entity,
comp::velocity(-1.5f, .0f),
comp::position(pos->x - 32, pos->y + 25),
monster_shoot(mstr->pushToSendQueue, id, 30),
update_location(5),
comp::hittable({25.0f, 30.0f}),
comp::enabled());
auto &missile_pos(r.get_components<comp::position>()[entity]);
mstr->pushToSendQueue(entity, SAFE, NEW_STATIC_MONSTER_MISSILE, sizeof(*missile_pos), &*missile_pos);
}

void spawn_enemy(registry &reg, server &server, size_t &spawn_clock,
const sendFct &sendQueue, const Vec &min_pos, const Vec &max_pos) {
int count(0);

for (auto const &enemy: reg.get_components<monster>())
for (auto const &enemy: reg.get_components<basic_enemy>())
count += enemy.has_value();
++spawn_clock;
if (spawn_clock >= TPS * 1 && count < 20) {
if (spawn_clock >= TPS * 1 && count < 10) {
spawn_clock = 0;
entity enemy(reg.spawn_entity());

Expand All @@ -54,6 +71,7 @@ void spawn_enemy(registry &reg, server &server, size_t &spawn_clock,
comp::position(pos.x, pos.y),
comp::velocity(-1.0f, .0f),
monster(sendQueue),
basic_enemy(),
update_location(1),
comp::clock(1.0f, true, {
{ comp::clock_signals::ended, add_id(enemy, enemy_shoot) }
Expand All @@ -65,6 +83,39 @@ void spawn_enemy(registry &reg, server &server, size_t &spawn_clock,
}
}

void spawn_static_enemy(registry &reg, server &server, size_t &spawn_clock,
const sendFct &sendQueue, const Vec &min_pos, const Vec &max_pos) {
int count(0);

for (auto const &enemy: reg.get_components<static_enemy>())
count += enemy.has_value();
++spawn_clock;
if (spawn_clock >= TPS * 1 && count < 5) {
spawn_clock = 0;
entity enemy(reg.spawn_entity());

static std::random_device rd;
static std::mt19937 gen(rd());
std::uniform_real_distribution<float> dist_x(min_pos.x, max_pos.x);
std::uniform_real_distribution<float> dist_y(min_pos.y, max_pos.y);

Vec pos(dist_x(gen), dist_y(gen));

reg.add_components(enemy,
comp::position(pos.x, pos.y),
comp::velocity(.0f, .0f),
monster(sendQueue),
static_enemy(),
comp::clock(2.0f, true, {
{ comp::clock_signals::ended, add_id(enemy, static_enemy_shoot) }
}),
killable(20, -1),
comp::hittable({100.0f, 100.0f}),
comp::enabled());
server.sendAll(enemy, BASIC, NEW_STATIC_ENEMY, sizeof(pos), &pos);
}
}

std::vector<char> serializeInfo(info const &i) {
uint16_t nameSize(i.name.size());
size_t totalSize(sizeof(i.color) + sizeof(i.pos) + sizeof(nameSize) + nameSize);
Expand Down
6 changes: 5 additions & 1 deletion server/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ void main_loop(std::array<registry, 3> &regs, server &server, size_t nb_player,
server.pushMessage(args...);
});
size_t spawn_clock = 0;
size_t static_spawn_clock = 0;
size_t score = ArgsScore;
size_t HeartBeatSig = 0;


auto lastTick = std::chrono::steady_clock::now();
const auto tickDuration = std::chrono::duration<double>(1.0 / TPS);

Expand All @@ -124,6 +124,7 @@ void main_loop(std::array<registry, 3> &regs, server &server, size_t nb_player,
return;
}
spawn_enemy(regs[state], server, spawn_clock, fct, {1300.0f, 80.0f}, {1800.0f, 1000.0F});
spawn_static_enemy(regs[state], server, static_spawn_clock, fct, {1800.0f, 80.0f}, {1801.0f, 1000.0F});
}
regs[state].run_systems();
server.sendMessages();
Expand All @@ -147,6 +148,9 @@ void setup_main_game(registry &mainGame) {
mainGame.safe_add_system<comp::position, comp::velocity, update_location, monster, player, comp::enabled>(enemy_pathfind_system);
mainGame.safe_add_system<player, comp::clock, comp::position, comp::enabled>(shoot_system);
mainGame.safe_add_system<comp::position, comp::velocity, comp::enabled>(position_system);
mainGame.register_component<basic_enemy>();
mainGame.register_component<static_enemy>();

}

void handle_args(int argc, char **argv, size_t &port, int &player_need, int &score) {
Expand Down