#include "trophy.h"
#include <SceneTree.hpp>
using namespace godot;
void Trophy::_register_methods() {
register_method("_area_entered", &Trophy::_area_entered);
register_method("_ready", &Trophy::_ready);
}
void Trophy::_init() {
start = 0;
}
// Constructor for Trophy Object
Trophy::Trophy() {
}
// Destructor for Trophy Object
Trophy::~Trophy() {
}
// Intializer for the Trophy class
void Trophy::_ready()
{
audioNode = get_node("VictorySoundPlayer");
areaNode = get_node("Area");
area = godot::Object::cast_to<Area>(areaNode);
audioPlayer = godot::Object::cast_to<AudioStreamPlayer>(audioNode);
// Connects area object with body_entered signal
area->connect("body_entered", this, "_area_entered");
}
// Function that triggers once the Trophy's area is entered
void Trophy::_area_entered() {
Godot::print("CollisionDetected");
// Plays the audio only on the first collision
if (start == 1)
{
if (audioPlayer)
{
audioPlayer->play();
}
// Change the scene to the game over scene
get_tree()->change_scene("res://main_scenes/GameoverScreen.tscn");
}
start += 1;
}
void GameManager::_host_start() {
// Creates and sets the server
network = NetworkedMultiplayerENet::_new();
network->create_server(port, max_players);
get_tree()->set_network_peer(network);
// Connects network signals to defined functions
network->connect("peer_connected", this, "_player_connected");
network->connect("peer_disconnected", this, "_player_disconnected");
// Stores clients name with network ID in the dictionary
local_player_id = get_tree()->get_network_unique_id();
player_names[0] = current_player_name;
//TEST Print statement
Godot::print("Server started!");
}
void GameManager::_player_connected(int player_id) {
Godot::print("Player connected!");
}
void GameManager::_player_disconnected(int player_id) {
Godot::print("Player disconnected!");
Node* player2 = get_tree()->get_root()->get_node("Player2");
if (player2)
{
get_tree()->get_root()->remove_child(player2);
}
else
{
Godot::print("Player2 node is null in game manager");
}
}
void GameManager::_send_player_info(int client_id, String client_name) {
player_names[1] = client_name;
rpc("_update_player_names", player_names[0], player_names[1]);
rpc("_update_waiting_room");
}
void GameManager::_client_start() {
// Creates and sets the client
network = NetworkedMultiplayerENet::_new();
network->create_client("127.0.0.1", port);
get_tree()->set_network_peer(network);
player_ids[1] = get_tree()->get_network_unique_id();
// Connects network signals to defined functions
get_tree()->connect("connection_failed", this, "_connected_fail");
get_tree()->connect("server_disconnected", this, "_server_disconnected");
get_tree()->connect("connected_to_server", this, "_connected_ok");
//TEST Print statement
Godot::print("...Attempting to join server...");
}