ecs::systems
Last updated
Last updated
void
movePacketHandle(World & world, network::Message & msg)
void
firstMessageHandle(World & world, network::Message & msg)
void
( & world, & msg)
void
( & world, & msg)
void
( & world, & msg)
void
( & world, & msg)
void
( & world, & msg)
std::function< void( &)>
std::function< void( &)>
std::function< void( &)>
size_t
std::unordered_map< char, std::function< void( &, &msg)> >
static void movePacketHandle(
World & world,
network::Message & msg
)
static void firstMessageHandle(
World & world,
network::Message & msg
)
static void deathMessageHandle(
World & world,
network::Message & msg
)
static void playerHealthHandle(
World & world,
network::Message & msg
)
static void createPlayer(
World & world,
network::ClientMessage & msg
)
Parameters:
world The world in which we want to operate
msg The message of a new player coming on the server
In goal of create a player, first, we need to add every components of the player. Specify to the server that there is a new connection and check if the request has been accepted
static void movePlayer(
World & world,
network::ClientMessage & msg
)
Parameters:
world The world we want to operate
msg The message containing the new position of the player
Updating the player position according to the given msg
static void playerShoot(
World & world,
network::ClientMessage & msg
)
Parameters:
world The world we want to operate
msg Only used to compare ID and select the correct player
Used to make the player shoot from the given message
std::function< void(World &)> animate = [](World &world) {
auto &animateds = world.registry.getComponents<component::Animated>();
using chrono = std::chrono::high_resolution_clock;
using chronoDuration = std::chrono::duration<double, std::milli>;
for (size_t i = 0; i < animateds.size(); ++i) {
auto &anim = animateds[i];
if (anim) {
if (chrono::now().time_since_epoch().count() - anim.value().lastSwitch
> static_cast<size_t>(anim.value().getFrame().delay) * 1000000) {
anim.value().nextFrame();
anim.value().lastSwitch = chrono::now().time_since_epoch().count();
}
}
}
};
std::function< void(World &)> draw;
Used to set the scale, the position and the texture of the entity before display it
std::function< void(World &)> executeOnce = [](World &world) {
static bool executed = false;
if (!executed) {
network::Message msg;
msg.fill(0);
network::Client::connect();
network::Client::getOutgoingMessages().push(msg);
executed = true;
}
};
Login to the server only once
static size_t selfId = 0;
static std::unordered_map< char, std::function< void(World &, network::Message &msg)> > packetTypeFunction = {
{utils::constant::getPacketTypeKey(utils::constant::PacketType::ENTITY_MOVE), movePacketHandle},
{0, firstMessageHandle},
{utils::constant::getPacketTypeKey(utils::constant::PacketType::ENTITY_DEATH), deathMessageHandle},
{utils::constant::getPacketTypeKey(utils::constant::PacketType::HEALTH_UPDATE), playerHealthHandle}};
std::function< void(World &)> HandleIncomingMessages = [](World &world) {
while (!network::Client::getReceivedMessages().empty()) {
network::Message msg = network::Client::getReceivedMessages().pop();
if (packetTypeFunction.find(msg[0]) != packetTypeFunction.end())
packetTypeFunction[msg[0]](world, msg);
}
};
std::function< void(World &)> HandleParallaxBounds = [](World &world) {
auto ¶llaxes = world.registry.getComponents<component::Parallax>();
auto &positions = world.registry.getComponents<component::Position>();
for (size_t i = 0; i < parallaxes.size() && i < positions.size(); i++) {
if (!parallaxes[i] || !positions[i])
continue;
if (positions[i].value().x < parallaxes[i].value().threshold) {
positions[i].value().x += parallaxes[i].value().position;
}
}
};
std::function< void(World &)> handleSFMLEvents;
Used to manage Sfml events Currently able to manage the following actions: Close the window KeyPressed, in this case, we check if the bind is known from sfml: if yes, we had it on world's events' stack, nothing otherwise
std::function< void(World &)> handleSFMLKeys;
Used to manage every action ordered by Sfml input by the user Refer to the Controllable.hpp documentation to learn more about managed input
std::function< void(World &)> healthBar = [](World &world) {
auto const &positions = world.registry.getComponents<component::Position>();
auto &sizes = world.registry.getComponents<component::Size>();
auto const &healths = world.registry.getComponents<component::Health>();
for (size_t i = 0; i < positions.size() && i < sizes.size() && i < healths.size(); i++) {
auto const &pos = positions[i];
auto &size = sizes[i];
auto const &health = healths[i];
if (pos && size && health) {
if (health->health >= 0 && health->health < utils::constant::maxPlayerHealth) {
size.value().width = utils::constant::sizeHealthBar * health->health / 100;
} else if (health->health < 0)
size.value().width = 0;
else
size.value().width = utils::constant::sizeHealthBar;
}
}
};
std::function< void(World &)> menuSelect;
std::function< void(World &)> scoreUpdate = [](World &world) {
auto &texts = world.registry.getComponents<component::Text>();
auto &scores = world.registry.getComponents<component::Score>();
for (size_t i = 0; i < texts.size() || i < scores.size(); i++) {
auto &text = texts[i];
auto &score = scores[i];
if (text && score) {
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
auto time = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count() / 10;
auto elapsed = std::to_string(time);
const std::string string = "Score: ";
text->setContent(0, string);
text->setContent(1, elapsed);
score->setScore(time);
}
}
};
Used to set the score of the player
std::function< void(World &)> SendDirection = [](World &world) {
auto const &controllables = world.registry.getComponents<component::Controllable>();
auto &directions = world.registry.getComponents<component::Direction>();
static auto clock = utils::constant::chrono::now();
if (utils::constant::chronoDuration(utils::constant::chrono::now() - clock).count() > 10) {
clock = utils::constant::chrono::now();
for (size_t i = 0; i < controllables.size() && i < directions.size(); i++) {
if (!controllables[i] || !directions[i])
continue;
if (directions[i].value().hasMoved) {
network::Message msg;
msg.fill(0);
msg[0] = utils::constant::PLAYER_MOVE;
msg[1] = directions[i].value().x;
msg[2] = directions[i].value().y;
network::Client::getOutgoingMessages().push(msg);
directions[i].value().hasMoved = false;
}
}
}
};
Send direction system used to fill the array of messages to the server regarding the direction of the player
std::function< void(World &)> manageClientEvents;
Used to manage every client' events Refer to SFMLEvents.hpp documentation to learn more about managed events
std::function< void(World &)> movement;
Used to apply the values of the velocity component to the position component according to the internal clock of the system
std::function< void(World &)> positionLogger = [](World &world) {
auto const &positions = world.registry.getComponents<component::Position>();
auto const &velocities = world.registry.getComponents<component::Velocity>();
for (size_t i = 0; i < positions.size() && i < velocities.size(); ++i) {
auto const &pos = positions[i];
auto const &vel = velocities[i];
if (pos && vel) {
std ::cerr << i << ": Position = { " << pos.value().x << ", " << pos.value().y << " }, Velocity = { "
<< vel.value().x << ", " << vel.value().y << " } " << std ::endl;
}
};
};
Used to display, as a logger, all the value of the Position & Velocity component
std::function< void(World &)> runMovementAI = [](World &world) {
auto &directions = world.registry.getComponents<component::Direction>();
auto &movementAIs = world.registry.getComponents<component::MovementAI>();
using chrono = std::chrono::high_resolution_clock;
using chronoDuration = std::chrono::duration<double, std::milli>;
for (size_t i = 0; i < directions.size() && i < movementAIs.size(); ++i) {
auto &dir = directions[i];
auto &movAI = movementAIs[i];
if (dir && movAI) {
if ((chrono::now().time_since_epoch().count() - movAI.value().getLastMovement()) / 10000000
> static_cast<size_t>(movAI.value().getDelay())) {
auto &tmpDir = movAI.value().getNextDirection();
dir.value().x = tmpDir.first;
dir.value().y = tmpDir.second;
dir.value().hasMoved = true;
movAI.value().setLastMovement(chrono::now().time_since_epoch().count());
}
}
};
};
Runs the AIs stored in the enities
std::function< void(World &)> deathUpdate;
System used to push death of entity message to the server outgoing queue when is life as gone under 0.
std::function< void(World &)> followEntitySystem = [](World &world) {
auto &positions = world.registry.getComponents<component::Position>();
auto const &follows = world.registry.getComponents<component::FollowEntity>();
for (std::size_t i = 0; i < positions.size() && i < follows.size(); i++) {
auto &pos = positions[i];
auto const &fol = follows[i];
if (pos && fol) {
if (fol.value().entityId < positions.size()) {
if (positions[fol.value().entityId]) {
pos.value().x = positions[fol.value().entityId].value().x - 50;
pos.value().y = positions[fol.value().entityId].value().y + 50;
}
}
}
}
};
static std::map< unsigned int, size_t > clientNumToId = {};
static std::unordered_map< char, std::function< void(World &, network::ClientMessage &msg)> > packetTypeFunction = {
{0, createPlayer}, {utils::constant::PLAYER_MOVE, movePlayer}, {utils::constant::PLAYER_SHOT, playerShoot}};
std::function< void(World &)> playerHealthUpdate = [](World &world) {
auto &healths = world.registry.getComponents<component::Health>();
auto &networkId = world.registry.getComponents<component::NetworkId>();
auto &entity = world.registry.getComponents<component::EntityType>();
for (size_t i = 0; i < healths.size(); ++i) {
auto &health = healths[i];
if (health && health.value().health != health.value().lastHealth) {
if (i < entity.size() && i < networkId.size()) {
auto &type = entity[i];
auto &id = networkId[i];
if (type && id && type.value().type == component::EntityType::Types::Player) {
health.value().lastHealth = health.value().health;
std::array<char, 2> idBin = id.value().serialize();
network::Message msg;
msg[0] = utils::constant::getPacketTypeKey(utils::constant::PacketType::HEALTH_UPDATE);
msg[1] = idBin[0];
msg[2] = idBin[1];
msg[3] = health.value().health;
network::Server::getOutgoingMessages().push(
network::ServerMessage(msg, std::vector<unsigned int>()));
}
}
}
}
};
System used to push death of entity message to the server outgoing queue when is life as gone under 0.
std::function< void(World &)> PositionUpdate;
System used to push updated position of entity to the server outgoing queue.
std::function< void(World &)> projectileCollision;
std::function< void(World &)> runAttackAI;
Runs the AIs stored in the enities
std::function< void(World &)> waves;
Updated on 2022-11-13 at 17:21:37 +0100
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::map< unsigned int, size_t >
std::unordered_map< char, std::function< void(World &, network::ClientMessage &msg)> >
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>
std::function< void(World &)>