Skip to content

Commit

Permalink
collision, enter/exit signals, bind utils improvements, gcc build fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlac committed Sep 29, 2023
1 parent e6613d2 commit ab276bd
Show file tree
Hide file tree
Showing 24 changed files with 177 additions and 89 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ This project is meant to show others working with GDExtension how to:
* use CMake, VCPKG, and submodules to configure the project automatically
* configure VSCode (linux & windows) / Visual Studio 2022 (windows) for
* good code navigation for godot engine sources, godot-cpp sources, and the gdextension project sources.
* good type completion / intellisense configuration
* good autocomplete / intellisense configuration
* preconfigured debugger launch profiles to run the project standalone or by launching the editor (both launch settings debuggable)
* good debugger visualizations for internal godot data structures by leveraging the .natvis file provided with godot-cpp

Expand Down
2 changes: 2 additions & 0 deletions project/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,5 @@ shoot={
[rendering]

textures/lossless_compression/force_png=true
anti_aliasing/quality/msaa_2d=3
anti_aliasing/quality/use_taa=true
2 changes: 1 addition & 1 deletion project/scenes/characters/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

[node name="Player" type="Character"]
scale = Vector2(2, 2)
collision_mask = 127
collision_mask = 12

[node name="PlayerCollisionPoly" type="CollisionPolygon2D" parent="."]
position = Vector2(0.5, -1.5)
Expand Down
9 changes: 5 additions & 4 deletions project/scenes/levels/level1.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ z_index = 10
position = Vector2(400, 400)
scale = Vector2(0.25, 0.25)
collision_layer = 8
collision_mask = 0
collision_mask = 7

[node name="WallCollisionPoly" type="CollisionPolygon2D" parent="Wall"]
polygon = PackedVector2Array(512, 512, -512, 512, -512, -512, 512, -512)
Expand Down Expand Up @@ -55,13 +55,14 @@ texture = ExtResource("4_ix12c")
z_index = 10
position = Vector2(400, -400)
collision_layer = 64
collision_mask = 0
collision_mask = 79
contact_monitor = true
linear_damp = 1.0

[node name="WallCollisionPoly" type="CollisionPolygon2D" parent="PhysicsBox"]
[node name="PhysicsBoxCollisionPoly" type="CollisionPolygon2D" parent="PhysicsBox"]
scale = Vector2(0.25, 0.25)
polygon = PackedVector2Array(512, 512, -512, 512, -512, -512, 512, -512)

[node name="Texture02" type="Sprite2D" parent="PhysicsBox"]
[node name="PhysicsBoxTexture" type="Sprite2D" parent="PhysicsBox"]
scale = Vector2(0.25, 0.25)
texture = ExtResource("5_pmdlb")
21 changes: 13 additions & 8 deletions project/scenes/projectiles/bullet.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,22 @@
[ext_resource type="Texture2D" uid="uid://cbx5pr44nef0m" path="res://assets/art/topdown/tanks_redux/projectiles/bulletdark1_outline.png" id="1_sadn1"]

[sub_resource type="CapsuleShape2D" id="CapsuleShape2D_i8hct"]
radius = 10.0
radius = 9.89949
height = 35.0

[node name="Projectile" type="Projectile"]
scale = Vector2(0.5, 0.5)
collision_layer = 4
collision_mask = 122
input_pickable = false
priority = 1
linear_damp_space_override = 1
linear_damp = 0.25
angular_damp_space_override = 1

[node name="Bullet" type="Sprite2D" parent="."]
scale = Vector2(0.666665, 0.666665)
texture = ExtResource("1_sadn1")

[node name="CollisionShape2D" type="CollisionShape2D" parent="."]
rotation = 0.785398
scale = Vector2(0.75, 0.75)
scale = Vector2(0.5, 0.5)
shape = SubResource("CapsuleShape2D_i8hct")

[node name="Bullet" type="Sprite2D" parent="CollisionShape2D"]
scale = Vector2(1.33333, 1.33333)
texture = ExtResource("1_sadn1")
4 changes: 1 addition & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace rl
{
Main::Main()
{
scene::node::set_unique_name(this, "Main");

resource::preload::scene<Level> level{ path::scene::Level1 };
resource::preload::scene<MainDialog> dialog{ path::ui::MainDialog };

Expand All @@ -22,7 +20,7 @@ namespace rl
if (m_main_dialog != nullptr)
{
m_canvas_layer = gdcast<godot::CanvasLayer>(
m_main_dialog->find_child("MainCanvasLayer", true, false));
m_main_dialog->find_child(name::dialog::canvas_layer, true, false));

runtime_assert(m_canvas_layer != nullptr);
if (m_active_level != nullptr && m_canvas_layer != nullptr)
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <godot_cpp/classes/camera2d.hpp>

namespace rl::inline node
namespace rl
{
Camera::Camera()
{
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/camera.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <godot_cpp/classes/camera2d.hpp>

namespace rl::inline node
namespace rl
{
class Camera : public godot::Camera2D
{
Expand Down
19 changes: 7 additions & 12 deletions src/nodes/character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
#include <godot_cpp/variant/typed_array.hpp>
#include <godot_cpp/variant/vector2.hpp>

namespace rl::inline node
namespace rl
{
Character::Character()
{
this->set_name("Player");
scene::node::set_unique_name(this, name::character::player);
this->set_motion_mode(MotionMode::MOTION_MODE_FLOATING);
}

Expand All @@ -39,23 +39,18 @@ namespace rl::inline node
this->add_child(m_camera);
this->add_child(m_player_controller);

m_firing_point = gdcast<godot::Marker2D>(this->find_child("FiringPoint", true, false));
m_firing_point = gdcast<godot::Marker2D>(
this->find_child(name::character::firing_pt, true, false));
runtime_assert(m_firing_point != nullptr);

signal<event::player_move>::connect<PlayerController>(m_player_controller) <=>
[this]() mutable {
return slot(this, on_player_movement);
}();
slot(this, on_player_movement);

signal<event::player_rotate>::connect<PlayerController>(m_player_controller) <=>
[this]() mutable {
return slot(this, on_player_rotate);
}();
slot(this, on_player_rotate);

signal<event::player_shoot>::connect<PlayerController>(m_player_controller) <=>
[this]() mutable {
return slot(this, on_player_shoot);
}();
slot(this, on_player_shoot);
}

PlayerController* Character::get_controller() const
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/character.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace godot
class Marker2D;
}

namespace rl::inline node
namespace rl
{
class Character : public godot::CharacterBody2D
{
Expand Down
39 changes: 31 additions & 8 deletions src/nodes/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,25 @@
#include "util/input.hpp"
#include "util/io.hpp"

#include <godot_cpp/classes/collision_polygon2d.hpp>
#include <godot_cpp/classes/marker2d.hpp>
#include <godot_cpp/classes/rigid_body2d.hpp>
#include <godot_cpp/variant/callable.hpp>
#include <godot_cpp/variant/vector2.hpp>

namespace rl::inline node
namespace rl
{
Level::Level()
{
// TODO: kill magic string
scene::node::set_unique_name(this, "Level1");
scene::node::set_unique_name(this, name::level::level1);
this->activate(true);
}

void Level::_ready()
{
godot::Node* box{ this->find_child(name::level::physics_box) };
m_physics_box = gdcast<godot::RigidBody2D>(box);

this->add_child(m_player);
this->add_child(m_projectile_spawner);

Expand All @@ -39,7 +43,7 @@ namespace rl::inline node
if (engine::editor_active())
return;

if (this->active() && input::cursor_visible()) [[likely]]
if (this->active() && input::cursor_visible()) [[unlikely]]
input::hide_cursor();
else if (!this->active() && !input::cursor_visible()) [[unlikely]]
input::show_cursor();
Expand Down Expand Up @@ -67,20 +71,37 @@ namespace rl::inline node
}

[[signal_slot]]
void Level::on_character_spawn_projectile(godot::Node* obj)
void Level::on_physics_box_entered(godot::Node* node) const
{
console::get()->print("{} > {}", io::yellow("projectile"), to<std::string>(node->get_name()));
}

[[signal_slot]]
void Level::on_physics_box_exited(godot::Node* node) const
{
console::get()->print("{} < {}", io::red("projectile"), to<std::string>(node->get_name()));
}

[[signal_slot]] void Level::on_character_spawn_projectile(godot::Node* obj)
{
godot::Node2D* node{ gdcast<godot::Node2D>(obj) };
Projectile* projectile{ m_projectile_spawner->spawn_projectile() };
if (projectile != nullptr)
{
projectile->set_position(node->get_global_position());
projectile->set_rotation(node->get_global_rotation() - godot::Math::deg_to_rad(45.0));
projectile->set_rotation(node->get_global_rotation());

godot::Marker2D* firing_pt{ gdcast<godot::Marker2D>(node) };
if (firing_pt != nullptr)
{
projectile->set_velocity(
godot::Vector2(0, -1).rotated(firing_pt->get_global_rotation()));

signal<event::body_entered>::connect<Projectile>(projectile) <=>
slot(this, on_physics_box_entered);

signal<event::body_exited>::connect<Projectile>(projectile) <=>
slot(this, on_physics_box_exited);
}

this->add_child(projectile);
Expand All @@ -92,14 +113,16 @@ namespace rl::inline node
godot::Vector2 location) const
{
runtime_assert(node != nullptr);
auto console{ Console<godot::RichTextLabel>::get() };
console->print("{} ({},{})\n", io::green(to<std::string>(node->get_class()) + " location: "),
auto console{ console::get() };
console->print("{} ({},{})", io::green(to<std::string>(node->get_class()) + " location: "),
io::orange(location.x), io::orange(location.y));
}

void Level::_bind_methods()
{
bind_member_function(Level, on_character_position_changed);
bind_member_function(Level, on_character_spawn_projectile);
bind_member_function(Level, on_physics_box_entered);
bind_member_function(Level, on_physics_box_exited);
}
}
10 changes: 9 additions & 1 deletion src/nodes/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
#include <godot_cpp/classes/node2d.hpp>
#include <godot_cpp/classes/sprite2d.hpp>

namespace rl::inline node
namespace godot
{
class RigidBody2D;
}

namespace rl
{
class Character;

Expand All @@ -34,6 +39,8 @@ namespace rl::inline node
protected:
static void _bind_methods();

[[signal_slot]] void on_physics_box_entered(godot::Node* node) const;
[[signal_slot]] void on_physics_box_exited(godot::Node* node) const;
[[signal_slot]] void on_character_spawn_projectile(godot::Node* obj);
[[signal_slot]] void on_character_position_changed(const godot::Object* const obj,
godot::Vector2 location) const;
Expand All @@ -44,5 +51,6 @@ namespace rl::inline node
ProjectileSpawner* m_projectile_spawner{ memnew(rl::ProjectileSpawner) };
resource::preload::scene<Character> player_scene{ path::scene::Player };
Character* m_player{ player_scene.instantiate() };
godot::RigidBody2D* m_physics_box{ nullptr };
};
}
2 changes: 1 addition & 1 deletion src/nodes/player_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <godot_cpp/classes/node2d.hpp>
#include <godot_cpp/variant/variant.hpp>

namespace rl::inline node
namespace rl
{
void PlayerController::_process(double delta_time)
{
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/player_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/node2d.hpp>

namespace rl::inline node
namespace rl
{
class PlayerController : public godot::Node2D
{
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/projectile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#include <godot_cpp/variant/vector2.hpp>

namespace rl::inline node
namespace rl
{
void Projectile::_ready()
{
Expand Down
22 changes: 21 additions & 1 deletion src/nodes/projectile.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#pragma once

#include "singletons/console.hpp"
#include "util/attributes.hpp"
#include "util/bind.hpp"
#include "util/conversions.hpp"
#include "util/io.hpp"

#include <godot_cpp/classes/area2d.hpp>
#include <godot_cpp/variant/vector2.hpp>

namespace rl::inline node
namespace rl
{
class Projectile : public godot::Area2D
{
Expand All @@ -29,9 +33,25 @@ namespace rl::inline node
[[property]] void set_acceleration(const double acceleration);
[[property]] void set_velocity(const godot::Vector2& velocity);

// [[signal_slot]]
// void on_body_enter(godot::Node* node) const
// {
// console::get()->print("Projectile: {} {} {}", to<std::string>(this->get_name()),
// io::yellow("entering >>"), to<std::string>(node->get_name()));
// }

// [[signal_slot]]
// void on_body_exit(godot::Node* node) const
// {
// console::get()->print("Projectile: {} {} {}", to<std::string>(this->get_name()),
// io::orange("exiting <<"), to<std::string>(node->get_name()));
// }

protected:
static void _bind_methods()
{
// bind_member_function(Projectile, on_body_enter);
// bind_member_function(Projectile, on_body_exit);
}

protected:
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/projectile_spawner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "util/bind.hpp"

namespace rl::inline node
namespace rl
{
[[nodiscard]]
Projectile* ProjectileSpawner::spawn_projectile()
Expand Down
2 changes: 1 addition & 1 deletion src/nodes/projectile_spawner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <godot_cpp/classes/node2d.hpp>
#include <godot_cpp/variant/typed_array.hpp>

namespace rl::inline node
namespace rl
{
using namespace std::chrono_literals;

Expand Down
Loading

0 comments on commit ab276bd

Please sign in to comment.