Skip to content

Commit

Permalink
Rohan Space Invaders game init
Browse files Browse the repository at this point in the history
  • Loading branch information
ron-matt163 committed Dec 1, 2024
1 parent cedcf53 commit d2c60ed
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .targetgames
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
hw4_joshua
hw4_rohan
hw4_mitesh
hw4_mitesh
hw5_rohan_space_invaders
32 changes: 19 additions & 13 deletions src/engine/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,10 @@ void Engine::OnDiscover() {
}
}

void Engine::OnLeave() { this->SendInactiveUpdate(); }
void Engine::OnLeave() {
this->SendInactiveUpdate(
GetClientPlayer(this->GetNetworkInfo().id, this->GetNetworkedEntities()));
}

bool Engine::InitP2PHost() {
ZoneScoped;
Expand Down Expand Up @@ -727,28 +730,31 @@ void Engine::CSClientSendUpdate() {
}
}

void Engine::SendInactiveUpdate() {
void Engine::SendInactiveUpdate(Entity *entity) {
ZoneScoped;

try {
Entity *player = GetClientPlayer(this->network_info.id, this->GetEntities());
// Entity *player = GetClientPlayer(this->network_info.id, this->GetEntities());
EntityUpdate entity_update;
std::snprintf(entity_update.name, sizeof(entity_update.name), "%s",
player->GetName().c_str());
entity_update.position = player->GetComponent<Transform>()->GetPosition();
entity->GetName().c_str());
entity_update.position = entity->GetComponent<Transform>()->GetPosition();
entity_update.active = false;

zmq::message_t update;
this->EncodeMessage(entity_update, update);

if (this->network_info.mode == NetworkMode::ClientServer &&
this->network_info.role == NetworkRole::Client) {
this->client_update_socket.send(update, zmq::send_flags::none);
zmq::message_t server_ack;
zmq::recv_result_t res =
this->client_update_socket.recv(server_ack, zmq::recv_flags::none);
std::string server_ack_message(static_cast<const char *>(server_ack.data()),
server_ack.size());
if (this->network_info.mode == NetworkMode::ClientServer) {
if (this->network_info.role == NetworkRole::Client) {
this->client_update_socket.send(update, zmq::send_flags::none);
zmq::message_t server_ack;
zmq::recv_result_t res =
this->client_update_socket.recv(server_ack, zmq::recv_flags::none);
std::string server_ack_message(static_cast<const char *>(server_ack.data()),
server_ack.size());
} else if (this->network_info.role == NetworkRole::Server) {
this->server_broadcast_socket.send(update, zmq::send_flags::none);
}
}

if (this->network_info.mode == NetworkMode::PeerToPeer) {
Expand Down
16 changes: 15 additions & 1 deletion src/engine/Handler.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "Handler.hpp"
#include "Engine.hpp"
#include "Entity.hpp"
#include "EventManager.hpp"
#include "Utils.hpp"
#include <vector>

Handler::Handler(Entity *entity) {
this->entity = entity;
Expand All @@ -23,4 +26,15 @@ void Handler::SetEventCallback(std::function<void(Entity &, Event &)> event_call

void Handler::Update() { this->update_callback(*this->entity); }

void Handler::OnEvent(Event event) { this->event_callback(*this->entity, event); }
void Handler::OnEvent(Event event) {
InputEvent *input_event = std::get_if<InputEvent>(&(event.data));
int player_id = Engine::GetInstance().GetNetworkInfo().id;
std::vector<Entity *> entities = Engine::GetInstance().GetEntities();

if (input_event && this->entity != GetClientPlayer(player_id, entities)) {
// Return early to avoid triggering input events for objects that inputs aren't meant for
return;
}

this->event_callback(*this->entity, event);
}
18 changes: 9 additions & 9 deletions src/engine/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,18 @@ std::vector<Entity *> GetEntitiesByRole(NetworkInfo network_info, std::vector<En
return entities;
}

if (network_info.role == NetworkRole::Server || network_info.role == NetworkRole::Host) {
for (auto *entity : entities) {
if (entity->GetComponent<Network>() != nullptr &&
entity->GetComponent<Network>()->GetOwner() == network_info.role) {
entity_list.push_back(entity);
}
// if (network_info.role == NetworkRole::Server || network_info.role == NetworkRole::Host) {
for (auto *entity : entities) {
if (entity->GetComponent<Network>() != nullptr &&
entity->GetComponent<Network>()->GetOwner() == network_info.role) {
entity_list.push_back(entity);
}
}
// }

if (network_info.role == NetworkRole::Client || network_info.role == NetworkRole::Peer) {
entity_list.push_back(GetClientPlayer(network_info.id, entities));
}
// if (network_info.role == NetworkRole::Client || network_info.role == NetworkRole::Peer) {
// entity_list.push_back(GetClientPlayer(network_info.id, entities));
// }

return entity_list;
}
Expand Down
4 changes: 2 additions & 2 deletions src/engine/includes/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,6 @@ class Engine {
void P2PReceiveBroadcastFromPeerThread(int player_id, std::string player_address);
void P2PReceiveBroadcastFromHostThread();

void SendInactiveUpdate();

void EncodeMessage(const EntityUpdate &entity_update, zmq::message_t &message);
void DecodeMessage(const zmq::message_t &message, EntityUpdate &entity_update);

Expand Down Expand Up @@ -161,6 +159,8 @@ class Engine {
void CSClientSendUpdate();
void P2PBroadcastUpdates(Entity *entity);

void SendInactiveUpdate(Entity *entity);

void OnJoin(std::string player_address);
void OnDiscover();
void OnLeave();
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d2c60ed

Please sign in to comment.