From a34ca72577dc519b4c869f8d8afad684f75bb0e5 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Mon, 29 Aug 2022 20:17:12 -0400 Subject: [PATCH 1/2] fix sounds when jumping on enemy --- src/player/Player.cpp | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/player/Player.cpp b/src/player/Player.cpp index d60cc1d..bfe14a3 100644 --- a/src/player/Player.cpp +++ b/src/player/Player.cpp @@ -130,8 +130,13 @@ void alai::player::Player::_physics_process(float delta) WARN_PRINT("Enemies not found!"); dup->queue_free(); }*/ - auto jump_sound = get_parent()->get_node("Sounds/Jump"); - jump_sound->play(); + auto jump_sound = get_node("Sounds/Jump"); + if (jump_sound != nullptr) { + jump_sound->play(); + } + else { + WARN_PRINT("Player jump sound not found!"); + } velocity.y = -get_bounce_force(); } else if (collider->is_in_group("enemy") && (collider->is_in_group("rideable") && godot::Vector2::DOWN.dot(collision->get_normal()) > 0)) @@ -168,14 +173,6 @@ void alai::player::Player::_physics_process(float delta) { auto event = get_node("/root/Event"); event->emit_signal("player_died"); - /*if (get_parent()->get_class() == "TileMap") - { - auto error = get_tree()->change_scene("res://Main.tscn"); - if (error != godot::Error::OK) - { - ERR_PRINT(godot::String().num((int) error) + " Could not load scene!"); - } - }*/ } } @@ -273,9 +270,11 @@ godot::Vector2 alai::player::Player::get_velocity() void alai::player::Player::_on_player_touched() { - auto error = get_tree()->change_scene("res://Main.tscn"); + /*auto error = get_tree()->change_scene("res://Main.tscn"); if (error != godot::Error::OK) { ERR_PRINT(godot::String().num((int) error) + " Could not load scene!"); - } + }*/ + auto event = get_node("/root/Event"); + event->emit_signal("player_died"); } -- 2.30.2 From 12df454946f3f017490d0e68e294218461faa352 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Mon, 29 Aug 2022 20:48:29 -0400 Subject: [PATCH 2/2] add some more checks to make sure a node is loaded before using it --- src/player/Player.cpp | 52 ++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/player/Player.cpp b/src/player/Player.cpp index bfe14a3..68df74f 100644 --- a/src/player/Player.cpp +++ b/src/player/Player.cpp @@ -100,8 +100,16 @@ void alai::player::Player::_physics_process(float delta) snap_vector = godot::Vector2::DOWN * 20.0; } + auto is_on_platform = false; auto platform_detector = get_node("PlatformDetector"); - auto is_on_platform = platform_detector->is_colliding(); + if (platform_detector != nullptr) + { + is_on_platform = platform_detector->is_colliding(); + } + else + { + WARN_PRINT("PlatformDetector not found!"); + } velocity = move_and_slide_with_snap(velocity, snap_vector, godot::Vector2::UP, !is_on_platform, 4, 0.9, false); //velocity = move_and_slide(velocity, Vector2::UP, !is_on_platform); @@ -131,10 +139,12 @@ void alai::player::Player::_physics_process(float delta) dup->queue_free(); }*/ auto jump_sound = get_node("Sounds/Jump"); - if (jump_sound != nullptr) { + if (jump_sound != nullptr) + { jump_sound->play(); } - else { + else + { WARN_PRINT("Player jump sound not found!"); } velocity.y = -get_bounce_force(); @@ -151,19 +161,26 @@ void alai::player::Player::_physics_process(float delta) // Clamp the player's position inside the camera's limits auto camera = get_node("Camera2D"); - auto position = get_global_position(); - auto sprite_node = get_node("AnimatedSprite"); - if (sprite_node != nullptr) + if (camera != nullptr) { - position.x = godot::Math::clamp((float) position.x, (float) camera->get_limit(0), (float) camera->get_limit(2) - sprite_node->get_sprite_frames()->get_frame("idle", 0)->get_size().x); - position.y = godot::Math::clamp((float) position.y, (float) camera->get_limit(1), (float) camera->get_limit(3) + sprite_node->get_sprite_frames()->get_frame("idle", 0)->get_size().y); + auto position = get_global_position(); + auto sprite_node = get_node("AnimatedSprite"); + if (sprite_node != nullptr) + { + position.x = godot::Math::clamp((float) position.x, (float) camera->get_limit(0), (float) camera->get_limit(2) - sprite_node->get_sprite_frames()->get_frame("idle", 0)->get_size().x); + position.y = godot::Math::clamp((float) position.y, (float) camera->get_limit(1), (float) camera->get_limit(3) + sprite_node->get_sprite_frames()->get_frame("idle", 0)->get_size().y); + } + else { + WARN_PRINT("Could not clamp player based on sprite frame size!"); + position.x = godot::Math::clamp((float) position.x, (float) camera->get_limit(0), (float) camera->get_limit(2)); + position.y = godot::Math::clamp((float) position.y, (float) camera->get_limit(1), (float) camera->get_limit(3)); + } + set_global_position(position); } - else { - WARN_PRINT("Could not clamp player based on sprite frame size!"); - position.x = godot::Math::clamp((float) position.x, (float) camera->get_limit(0), (float) camera->get_limit(2)); - position.y = godot::Math::clamp((float) position.y, (float) camera->get_limit(1), (float) camera->get_limit(3)); + else + { + WARN_PRINT("Camera node not found!"); } - set_global_position(position); // If the player is off screen reload the scene auto notifier = get_node("Camera2D/VisibilityNotifier2D"); @@ -175,6 +192,10 @@ void alai::player::Player::_physics_process(float delta) event->emit_signal("player_died"); } } + else + { + WARN_PRINT("Visibility notifier not found!"); + } auto state = get_node("StateMachine")->get_child(0); if (state != nullptr) @@ -270,11 +291,6 @@ godot::Vector2 alai::player::Player::get_velocity() void alai::player::Player::_on_player_touched() { - /*auto error = get_tree()->change_scene("res://Main.tscn"); - if (error != godot::Error::OK) - { - ERR_PRINT(godot::String().num((int) error) + " Could not load scene!"); - }*/ auto event = get_node("/root/Event"); event->emit_signal("player_died"); } -- 2.30.2