change the way level gets loaded and use the event bus

This commit is contained in:
Chris Cromer 2022-09-01 11:30:01 -04:00
parent cb81734279
commit e29492447d
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
5 changed files with 53 additions and 23 deletions

View File

@ -1,10 +1,8 @@
extends Node extends Node
signal monitor_loaded()
export var monitor_enabled: bool = false export var monitor_enabled: bool = false
export var debug: bool = false
export var development_url: String = "http://localhost:4050/api/v1" export var development_url: String = "http://localhost:4050/api/v1"
var url_real: String = "https://alai.cromer.cl/api/v1" var url_real: String = "https://alai.cromer.cl/api/v1"
export var use_development_url: bool = false export var use_development_url: bool = false
@ -53,7 +51,11 @@ func _ready() -> void:
Event.connect("object_created", self, "_object_created") Event.connect("object_created", self, "_object_created")
Event.connect("object_updated", self, "_object_updated") Event.connect("object_updated", self, "_object_updated")
Event.connect("object_removed", self, "_object_removed") 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") Event.connect("coin_collected", self, "_on_coin_update")
game_version = get_parent().game_version game_version = get_parent().game_version
player["rut"] = "" player["rut"] = ""
@ -108,7 +110,7 @@ func _physics_process(_delta: float) -> void:
if monitor_enabled: if monitor_enabled:
if has_node("MonitorGUI") and not $MonitorGUI.visible: if has_node("MonitorGUI") and not $MonitorGUI.visible:
$MonitorGUI.visible = true $MonitorGUI.visible = true
emit_signal("monitor_loaded") Event.emit_signal("monitor_loaded")
if started and not get_tree().paused: if started and not get_tree().paused:
var frame = empty_frame.duplicate(true) var frame = empty_frame.duplicate(true)
@ -130,13 +132,14 @@ func _physics_process(_delta: float) -> void:
start_monitor() start_monitor()
else: else:
get_tree().paused = false get_tree().paused = false
emit_signal("monitor_loaded") Event.emit_signal("monitor_loaded")
queue_free() queue_free()
func _on_input_validated(validated_player: Dictionary) -> void: func _on_input_validated(validated_player: Dictionary) -> void:
$MonitorGUI.queue_free() $MonitorGUI.queue_free()
get_tree().paused = false get_tree().paused = false
Event.emit_signal("game_started")
player = validated_player.duplicate(true) player = validated_player.duplicate(true)
game["player"] = player game["player"] = player
@ -188,6 +191,7 @@ func remove_object(name: String) -> void:
for i in range(0, objects.size()): for i in range(0, objects.size()):
if objects[i]["name"] == name: if objects[i]["name"] == name:
objects.remove(i) objects.remove(i)
return
func _on_coin_update(amount: int) -> void: func _on_coin_update(amount: int) -> void:
@ -219,7 +223,16 @@ func send_data() -> void:
print("Body B: " + String(body.length())) print("Body B: " + String(body.length()))
print("Body MB: " + String(body.length() / pow(2, 20))) print("Body MB: " + String(body.length() / pow(2, 20)))
if not debug:
$HTTPRequest.request(url + "/game", headers, false, HTTPClient.METHOD_POST, body) $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: func compress_payload(payload: String) -> String:
@ -231,3 +244,21 @@ func compress_payload(payload: String) -> String:
return new_payload 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()

View File

@ -1,5 +1,7 @@
#include "Main.h" #include "Main.h"
#include "Event.h"
#include <SceneTree.hpp> #include <SceneTree.hpp>
void alai::Main::_register_methods() void alai::Main::_register_methods()
@ -86,24 +88,21 @@ void alai::Main::_ready()
void alai::Main::_on_monitor_loaded() void alai::Main::_on_monitor_loaded()
{ {
if (level != nullptr) load_level();
{ auto event = get_node<alai::Event>("/root/Event");
auto level_node = load_level(); event->emit_signal("level_loaded");
connect("monitor_loaded", level_node->get_child(0)->find_node("Player", true, false), "_on_monitor_loaded"); get_tree()->set_pause(true);
emit_signal("monitor_loaded");
}
} }
void alai::Main::load_monitor() void alai::Main::load_monitor()
{ {
auto event = get_node<alai::Event>("/root/Event");
event->connect("monitor_loaded", this, "_on_monitor_loaded");
godot::Ref<godot::PackedScene> monitor_scene = _resource_loader->load("res://monitor/Monitor.tscn"); godot::Ref<godot::PackedScene> monitor_scene = _resource_loader->load("res://monitor/Monitor.tscn");
add_child(monitor_scene->instance()); 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) if (level != nullptr)
{ {
@ -111,9 +110,7 @@ godot::Node *alai::Main::load_level()
auto loaded_level = level->instance(); auto loaded_level = level->instance();
auto level_node = get_node("Level"); auto level_node = get_node("Level");
level_node->add_child(loaded_level); level_node->add_child(loaded_level);
return level_node;
} }
return nullptr;
} }
void alai::Main::_physics_process(float delta) void alai::Main::_physics_process(float delta)

View File

@ -224,9 +224,8 @@ namespace alai
/** /**
* @brief Loads the selected level. * @brief Loads the selected level.
* *
* @return Node* The level node which we will later add the monitor to.
*/ */
Node *load_level(); void load_level();
}; };
} }

View File

@ -19,7 +19,7 @@ void alai::player::Player::_register_methods()
godot::register_method("set_velocity", &Player::set_velocity); godot::register_method("set_velocity", &Player::set_velocity);
godot::register_method("get_velocity", &Player::get_velocity); godot::register_method("get_velocity", &Player::get_velocity);
godot::register_method("_on_player_touched", &Player::_on_player_touched); 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<Player, godot::Ref<godot::SpriteFrames>>("sprite_frames", &Player::set_sprite_frames, &Player::get_sprite_frames, godot::Ref<godot::SpriteFrames>(), GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, godot::String("SpriteFrames")); //godot::register_property<Player, godot::Ref<godot::SpriteFrames>>("sprite_frames", &Player::set_sprite_frames, &Player::get_sprite_frames, godot::Ref<godot::SpriteFrames>(), GODOT_METHOD_RPC_MODE_DISABLED, GODOT_PROPERTY_USAGE_DEFAULT, GODOT_PROPERTY_HINT_RESOURCE_TYPE, godot::String("SpriteFrames"));
godot::register_property<Player, float>("speed", &Player::set_speed, &Player::get_speed, player::speed); godot::register_property<Player, float>("speed", &Player::set_speed, &Player::get_speed, player::speed);
godot::register_property<Player, float>("jump_force", &Player::set_jump_force, &Player::get_jump_force, player::jump_force); godot::register_property<Player, float>("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() void alai::player::Player::_ready()
{ {
auto event = get_node<alai::Event>("/root/Event");
event->connect("level_loaded", this, "_on_level_loaded");
animated_sprite = get_node<godot::AnimatedSprite>("AnimatedSprite"); animated_sprite = get_node<godot::AnimatedSprite>("AnimatedSprite");
if (!animated_sprite) 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); auto state = get_node("StateMachine")->get_child(0);
if (state != nullptr) if (state != nullptr)
{ {

View File

@ -284,7 +284,7 @@ namespace alai
* @brief Called when the monitor is loaded to connect the player to it for tracking. * @brief Called when the monitor is loaded to connect the player to it for tracking.
* *
*/ */
void _on_monitor_loaded(); void _on_level_loaded();
}; };
} }
} }