#include "server_node.h" #include "ydi_server.h" #include ServerNode *ServerNode::singleton_instance{ nullptr }; String const ServerNode::sig_clue_revealed{ "clue_revealed" }; String const ServerNode::sig_connection_established{ "connection_established" }; String const ServerNode::sig_connection_lost{ "connection_lost" }; void ServerNode::_bind_methods() { ADD_SIGNAL(MethodInfo(sig_clue_revealed, PropertyInfo(Variant::INT, "id"))); ADD_SIGNAL(MethodInfo(sig_connection_established)); ADD_SIGNAL(MethodInfo(sig_connection_lost)); } void ServerNode::enter_tree() { if (singleton_instance) { print_error("Attempt to create duplicate ServerNode, aborting"); abort(); } singleton_instance = this; ydi::server::open(); } void ServerNode::process(double delta) { Vector new_clues{}; if (ydi::server::receive::new_clues(new_clues)) { for (NetworkData::ClueID clue : new_clues) { emit_signal(sig_clue_revealed, clue); } } bool new_is_connected{ ydi::server::has_client() }; if (this->is_connected != new_is_connected) { this->is_connected = new_is_connected; emit_signal(this->is_connected ? sig_connection_established : sig_connection_lost); } } void ServerNode::exit_tree() { ydi::server::close(); if (singleton_instance == this) { singleton_instance = nullptr; } } void ServerNode::_notification(int what) { if (Engine::get_singleton()->is_editor_hint()) { return; } switch (what) { case NOTIFICATION_ENTER_TREE: set_process(true); enter_tree(); return; case NOTIFICATION_PROCESS: process(get_process_delta_time()); return; case NOTIFICATION_EXIT_TREE: exit_tree(); return; default: return; } } ServerNode *ServerNode::get_singleton() { return singleton_instance; }