diff --git a/godot/monitor/Monitor.gd b/godot/monitor/Monitor.gd index 5e34c64..99404ac 100644 --- a/godot/monitor/Monitor.gd +++ b/godot/monitor/Monitor.gd @@ -1,10 +1,8 @@ extends Node -signal monitor_loaded() - - export var monitor_enabled: bool = false +export var debug: bool = false export var development_url: String = "http://localhost:4050/api/v1" var url_real: String = "https://alai.cromer.cl/api/v1" export var use_development_url: bool = false @@ -53,7 +51,11 @@ func _ready() -> void: Event.connect("object_created", self, "_object_created") Event.connect("object_updated", self, "_object_updated") Event.connect("object_removed", self, "_object_removed") + Event.connect("game_started", self, "_on_game_started") + Event.connect("player_died", self, "_on_game_over") + Event.connect("player_won", self, "_on_game_won") Event.connect("coin_collected", self, "_on_coin_update") + game_version = get_parent().game_version player["rut"] = "" @@ -108,7 +110,7 @@ func _physics_process(_delta: float) -> void: if monitor_enabled: if has_node("MonitorGUI") and not $MonitorGUI.visible: $MonitorGUI.visible = true - emit_signal("monitor_loaded") + Event.emit_signal("monitor_loaded") if started and not get_tree().paused: var frame = empty_frame.duplicate(true) @@ -130,13 +132,14 @@ func _physics_process(_delta: float) -> void: start_monitor() else: get_tree().paused = false - emit_signal("monitor_loaded") + Event.emit_signal("monitor_loaded") queue_free() func _on_input_validated(validated_player: Dictionary) -> void: $MonitorGUI.queue_free() get_tree().paused = false + Event.emit_signal("game_started") player = validated_player.duplicate(true) game["player"] = player @@ -188,6 +191,7 @@ func remove_object(name: String) -> void: for i in range(0, objects.size()): if objects[i]["name"] == name: objects.remove(i) + return func _on_coin_update(amount: int) -> void: @@ -219,7 +223,16 @@ func send_data() -> void: print("Body B: " + String(body.length())) print("Body MB: " + String(body.length() / pow(2, 20))) - $HTTPRequest.request(url + "/game", headers, false, HTTPClient.METHOD_POST, body) + if not debug: + $HTTPRequest.request(url + "/game", headers, false, HTTPClient.METHOD_POST, body) + else: + var file = File.new() + if file.open("user://game.json", File.WRITE) != 0: + print_debug("Could not open game.json for writing!") + return + + file.store_string(json) + file.close() func compress_payload(payload: String) -> String: @@ -231,3 +244,21 @@ func compress_payload(payload: String) -> String: return new_payload +func _on_game_started() -> void: + print_debug("started game") + if not started: + start_monitor() + + +func _on_game_over() -> void: + if started: + stop_monitor() + game["won"] = false + send_data() + + +func _on_game_won() -> void: + if started: + stop_monitor() + game["won"] = true + send_data() diff --git a/src/Main.cpp b/src/Main.cpp index 4f895bd..661e0e0 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,5 +1,7 @@ #include "Main.h" +#include "Event.h" + #include void alai::Main::_register_methods() @@ -86,24 +88,21 @@ void alai::Main::_ready() void alai::Main::_on_monitor_loaded() { - if (level != nullptr) - { - auto level_node = load_level(); - connect("monitor_loaded", level_node->get_child(0)->find_node("Player", true, false), "_on_monitor_loaded"); - emit_signal("monitor_loaded"); - } + load_level(); + auto event = get_node("/root/Event"); + event->emit_signal("level_loaded"); + get_tree()->set_pause(true); } void alai::Main::load_monitor() { + auto event = get_node("/root/Event"); + event->connect("monitor_loaded", this, "_on_monitor_loaded"); godot::Ref monitor_scene = _resource_loader->load("res://monitor/Monitor.tscn"); add_child(monitor_scene->instance()); - auto monitor = get_node("Monitor"); - monitor->connect("monitor_loaded", this, "_on_monitor_loaded"); - get_tree()->set_pause(true); } -godot::Node *alai::Main::load_level() +void alai::Main::load_level() { if (level != nullptr) { @@ -111,9 +110,7 @@ godot::Node *alai::Main::load_level() auto loaded_level = level->instance(); auto level_node = get_node("Level"); level_node->add_child(loaded_level); - return level_node; } - return nullptr; } void alai::Main::_physics_process(float delta) diff --git a/src/Main.h b/src/Main.h index 9487338..a3cb313 100644 --- a/src/Main.h +++ b/src/Main.h @@ -224,9 +224,8 @@ namespace alai /** * @brief Loads the selected level. * - * @return Node* The level node which we will later add the monitor to. */ - Node *load_level(); + void load_level(); }; } diff --git a/src/player/Player.cpp b/src/player/Player.cpp index 68df74f..fc3336c 100644 --- a/src/player/Player.cpp +++ b/src/player/Player.cpp @@ -19,7 +19,7 @@ void alai::player::Player::_register_methods() godot::register_method("set_velocity", &Player::set_velocity); godot::register_method("get_velocity", &Player::get_velocity); godot::register_method("_on_player_touched", &Player::_on_player_touched); - godot::register_method("_on_monitor_loaded", &Player::_on_monitor_loaded); + godot::register_method("_on_level_loaded", &Player::_on_level_loaded); //godot::register_property>("sprite_frames", &Player::set_sprite_frames, &Player::get_sprite_frames, godot::Ref(), GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, godot::String("SpriteFrames")); godot::register_property("speed", &Player::set_speed, &Player::get_speed, player::speed); godot::register_property("jump_force", &Player::set_jump_force, &Player::get_jump_force, player::jump_force); @@ -56,6 +56,9 @@ void alai::player::Player::_init() void alai::player::Player::_ready() { + auto event = get_node("/root/Event"); + event->connect("level_loaded", this, "_on_level_loaded"); + animated_sprite = get_node("AnimatedSprite"); if (!animated_sprite) { @@ -77,7 +80,7 @@ void alai::player::Player::_ready() } } -void alai::player::Player::_on_monitor_loaded() { +void alai::player::Player::_on_level_loaded() { auto state = get_node("StateMachine")->get_child(0); if (state != nullptr) { diff --git a/src/player/Player.h b/src/player/Player.h index cedcfb5..ee9067d 100644 --- a/src/player/Player.h +++ b/src/player/Player.h @@ -284,7 +284,7 @@ namespace alai * @brief Called when the monitor is loaded to connect the player to it for tracking. * */ - void _on_monitor_loaded(); + void _on_level_loaded(); }; } }