From e6613d24b488bbee0f4a89c34ca31929bec30972 Mon Sep 17 00:00:00 2001 From: sal Date: Fri, 29 Sep 2023 08:00:21 -0400 Subject: [PATCH] updated readme and added some basic level obstacles for layer & collision testing --- README.md | 47 +++++++++++++----- project/project.godot | 18 +++++++ project/scenes/characters/player.tscn | 1 + project/scenes/levels/level1.tscn | 68 ++++++++++++++++++++++++--- project/scenes/ui/main_dialog.tscn | 12 ++--- src/main.cpp | 17 +++---- src/nodes/level.cpp | 1 - src/nodes/level.hpp | 5 +- src/ui/main_dialog.cpp | 2 - src/util/constants.hpp | 28 +++++++++++ 10 files changed, 159 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 33c2ac3..e26b3b7 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,21 @@ # Godot 4.1 GDExtension C++ Prototype - This project is a rough prototype focusing on understanding how to implement most of the core logic of a game in C++ using GDExtension. Everything is done in the code aside from UI/dialog/menu design, and scene file generation. The current focus is just learning more about how to implement certain features / funtionality in the GDExtension library so there isn't much game logic. Since the documentation is very sparse when it comes to pure native development, a lot of what is implemnted in this side project is mostly focused on providing examples for a variety of misc functionality. Game logic will become a focus when most of the essentials are figured out through misc features/functionality. + This project is a rough prototype focusing on understanding how to implementa majority of a game's core logic/scripting in C++ using GDExtension. Everything is done in the code aside from UI/dialog/menu design, and scene file generation. -***Disclaimer:*** I am very new to godot, I've only been using it for about 6 weeks at the time of writing this. There's a very good chance some of the design/implementation approaches used in this project are completely wrong. I just work through most feature additions until something works and stick with that until I find a better/cleaner way. + The current focus is aimed towards learning more about how certain features and funtionality can be implemented in native code leveraging the [godot-cpp](https://github.com/godotengine/godot-cpp) GDExtension bindings so there isn't much game logic implemented yet. Since the documentation is pretty sparse when it comes to pure native development, most of the functionality implemnted in this project is focused on providing a variety of general examples of misc functionality that others will likely find useful. + + Game logic will become a focus when most of the essentials are figured out and implented throughout the project. + +# Overview +This project is meant to show others working with GDExtension how to: +* structure a project +* implement specific features and functionality entirely in C++ +* incorporate 3rd party libraries into their GDExtension code/library +* 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 + * 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 # High Level Project Structure @@ -12,25 +26,32 @@ The overall node hierarchy:
 Main
 ├── MainDialog
-│   ├── RichTextLabel (In-Game Console)
-│   └── MainSubViewport (Primary Game Viewport)
-│       └── Canvas Layer
-├── Level
+│   ├── RichTextLabel (In-Game Console)
+│   └── MainSubViewport (Primary Game Viewport)
+│       └── Canvas Layer
+├── Level
 │   ├── Character
 │   │   ├── Camera
 │   │   ├── Sprite2D
 │   │   └── CollisionShape2D
-│   ├── Projectile Spawner
-│   │   └── Projectile
-│   └── Sprite2D (Background)
-└── Main Canvas Layer (reference to Canvas Layer in MainDialog)
+│   └── Projectile Spawner
+│       └── Projectile
+└── Main Canvas Layer (reference to Canvas Layer in MainDialog)
 

Console (Singleton) -└── RichTextLabel (Reference from MainDialog) +└── RichTextLabel (Reference from MainDialog)
-# Development Setup -See the [wiki](https://github.com/vorlac/godot-roguelite/wiki) for development/tooling setup instructions +# Setup Guide +See the [wiki](https://github.com/vorlac/godot-roguelite/wiki) for a detailed guide that will walk you through the steps required to get this project running and debugging on linux or windows. + +# Contributing +This repo and/or the [gdextension developer documentation](https://github.com/vorlac/gdextension-docs/blob/main/main.md) I recently started working on is open to anyone that wants to contribute. Feel free to submit PRs for any changes that implement new features, fix bugs, or improve upon any existing features, functionality, or design. + +# Additional Resources +The projects below are also worth checking out for some additional GDExtension examples to use as references +* https://github.com/paddy-exe/GDExtensionSummator +* https://github.com/asmaloney/GDExtensionTemplate # Licensing #### Code diff --git a/project/project.godot b/project/project.godot index 82be77d..cd938e1 100644 --- a/project/project.godot +++ b/project/project.godot @@ -140,3 +140,21 @@ shoot={ "events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null) ] } + +[layer_names] + +2d_physics/layer_1="player" +2d_physics/layer_2="npcs" +2d_physics/layer_3="projectiles" +2d_physics/layer_4="walls" +2d_physics/layer_5="damage_zones" +2d_physics/layer_6="death_zones" +2d_physics/layer_7="physics_objects" + +[physics] + +2d/default_gravity_vector=Vector2(0, 0) + +[rendering] + +textures/lossless_compression/force_png=true diff --git a/project/scenes/characters/player.tscn b/project/scenes/characters/player.tscn index dc6a55c..2bcac3d 100644 --- a/project/scenes/characters/player.tscn +++ b/project/scenes/characters/player.tscn @@ -4,6 +4,7 @@ [node name="Player" type="Character"] scale = Vector2(2, 2) +collision_mask = 127 [node name="PlayerCollisionPoly" type="CollisionPolygon2D" parent="."] position = Vector2(0.5, -1.5) diff --git a/project/scenes/levels/level1.tscn b/project/scenes/levels/level1.tscn index 3234694..d60ed5c 100644 --- a/project/scenes/levels/level1.tscn +++ b/project/scenes/levels/level1.tscn @@ -1,11 +1,67 @@ -[gd_scene load_steps=2 format=3 uid="uid://c8tur70b73kih"] +[gd_scene load_steps=6 format=3 uid="uid://c8tur70b73kih"] -[ext_resource type="Texture2D" uid="uid://b76tyx7byavoa" path="res://assets/art/prototype_textures/dark/texture_03.png" id="1_g3701"] +[ext_resource type="Texture2D" uid="uid://dsq2y3cfi6f0r" path="res://assets/art/prototype_textures/light/texture_03.png" id="1_nl15v"] +[ext_resource type="Texture2D" uid="uid://cl68k75es1iy3" path="res://assets/art/prototype_textures/dark/texture_04.png" id="2_6qqh6"] +[ext_resource type="Texture2D" uid="uid://b65xeiq7etdod" path="res://assets/art/prototype_textures/red/texture_02.png" id="3_blm5m"] +[ext_resource type="Texture2D" uid="uid://b4wu73uyjafmf" path="res://assets/art/prototype_textures/green/texture_10.png" id="4_ix12c"] +[ext_resource type="Texture2D" uid="uid://djiso2qx4vtt5" path="res://assets/art/prototype_textures/purple/texture_02.png" id="5_pmdlb"] [node name="Level1" type="Level"] [node name="BackgroundTexture" type="Sprite2D" parent="."] -z_index = -10 -scale = Vector2(3, 3) -texture = ExtResource("1_g3701") -metadata/_edit_lock_ = true +z_index = -100 +scale = Vector2(4, 4) +texture = ExtResource("1_nl15v") + +[node name="Wall" type="StaticBody2D" parent="."] +z_index = 10 +position = Vector2(400, 400) +scale = Vector2(0.25, 0.25) +collision_layer = 8 +collision_mask = 0 + +[node name="WallCollisionPoly" type="CollisionPolygon2D" parent="Wall"] +polygon = PackedVector2Array(512, 512, -512, 512, -512, -512, 512, -512) + +[node name="WallTexture" type="Sprite2D" parent="Wall"] +texture = ExtResource("2_6qqh6") + +[node name="DeathPit" type="StaticBody2D" parent="."] +z_index = -90 +position = Vector2(-400, -400) +scale = Vector2(0.25, 0.25) +collision_layer = 32 +collision_mask = 0 + +[node name="DeathCollisionPoly" type="CollisionPolygon2D" parent="DeathPit"] +polygon = PackedVector2Array(512, 512, -512, 512, -512, -512, 512, -512) + +[node name="DeathPitTexture" type="Sprite2D" parent="DeathPit"] +texture = ExtResource("3_blm5m") + +[node name="DamageZone" type="StaticBody2D" parent="."] +position = Vector2(-400, 400) +scale = Vector2(0.25, 0.25) +collision_layer = 16 +collision_mask = 0 + +[node name="DamageCollisionPoly" type="CollisionPolygon2D" parent="DamageZone"] +polygon = PackedVector2Array(512, 512, -512, 512, -512, -512, 512, -512) + +[node name="DamageTexture" type="Sprite2D" parent="DamageZone"] +texture = ExtResource("4_ix12c") + +[node name="PhysicsBox" type="RigidBody2D" parent="."] +z_index = 10 +position = Vector2(400, -400) +collision_layer = 64 +collision_mask = 0 +contact_monitor = true + +[node name="WallCollisionPoly" 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"] +scale = Vector2(0.25, 0.25) +texture = ExtResource("5_pmdlb") diff --git a/project/scenes/ui/main_dialog.tscn b/project/scenes/ui/main_dialog.tscn index 71dbe24..87d4cf4 100644 --- a/project/scenes/ui/main_dialog.tscn +++ b/project/scenes/ui/main_dialog.tscn @@ -1,10 +1,10 @@ -[gd_scene load_steps=10 format=3] +[gd_scene load_steps=10 format=3 uid="uid://bshwhsqouic3e"] -[ext_resource type="Theme" path="res://assets/themes/ui_theme.tres" id="1_lp5so"] +[ext_resource type="Theme" uid="uid://bjiaijiglw4a3" path="res://assets/themes/ui_theme.tres" id="1_lp5so"] [ext_resource type="Theme" path="res://assets/themes/fira_code_font_theme.tres" id="2_jha0r"] -[ext_resource type="FontFile" path="res://assets/themes/fonts/firacode/firacode_regular.ttf" id="3_rno71"] -[ext_resource type="FontFile" path="res://assets/themes/fonts/firacode/firacode_bold.ttf" id="4_l2181"] -[ext_resource type="FontFile" path="res://assets/themes/fonts/firacode/firacode_retina.ttf" id="5_gla07"] +[ext_resource type="FontFile" uid="uid://gfim3353elck" path="res://assets/themes/fonts/firacode/firacode_regular.ttf" id="3_rno71"] +[ext_resource type="FontFile" uid="uid://c06onx6hjeqes" path="res://assets/themes/fonts/firacode/firacode_bold.ttf" id="4_l2181"] +[ext_resource type="FontFile" uid="uid://b782x7gq8vkya" path="res://assets/themes/fonts/firacode/firacode_retina.ttf" id="5_gla07"] [ext_resource type="StyleBox" path="res://assets/themes/console_text_box.tres" id="5_k0ydj"] [ext_resource type="Theme" path="res://assets/themes/default.tres" id="7_hq11r"] @@ -105,7 +105,7 @@ physics_object_picking = true physics_object_picking_sort = true gui_disable_input = true gui_embed_subwindows = true -size = Vector2i(512, 256) +size = Vector2i(1900, 895) render_target_update_mode = 4 [node name="MainCanvasLayer" type="CanvasLayer" parent="TopVerticalLayout/OuterDialogMargin/VerticalSplit/SubViewportContainer/MainSubViewport"] diff --git a/src/main.cpp b/src/main.cpp index 492783e..1e1fccd 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,29 +10,21 @@ namespace rl Main::Main() { scene::node::set_unique_name(this, "Main"); - } - void Main::_ready() - { resource::preload::scene level{ path::scene::Level1 }; + resource::preload::scene dialog{ path::ui::MainDialog }; + m_active_level = level.instantiate(); runtime_assert(m_active_level != nullptr); - - resource::preload::scene dialog{ path::ui::MainDialog }; m_main_dialog = dialog.instantiate(); runtime_assert(m_main_dialog != nullptr); - this->apply_default_settings(); - - runtime_assert(m_main_dialog != nullptr); if (m_main_dialog != nullptr) { m_canvas_layer = gdcast( m_main_dialog->find_child("MainCanvasLayer", true, false)); runtime_assert(m_canvas_layer != nullptr); - runtime_assert(m_active_level != nullptr); - if (m_active_level != nullptr && m_canvas_layer != nullptr) m_canvas_layer->add_child(m_active_level); @@ -41,6 +33,11 @@ namespace rl } } + void Main::_ready() + { + this->apply_default_settings(); + } + void Main::apply_default_settings() { engine::set_fps(60); diff --git a/src/nodes/level.cpp b/src/nodes/level.cpp index 14d95cc..b92e64d 100644 --- a/src/nodes/level.cpp +++ b/src/nodes/level.cpp @@ -16,7 +16,6 @@ namespace rl::inline node { Level::Level() - : m_player{ player_scene.instantiate() } { // TODO: kill magic string scene::node::set_unique_name(this, "Level1"); diff --git a/src/nodes/level.hpp b/src/nodes/level.hpp index 81ee446..6a7e7c3 100644 --- a/src/nodes/level.hpp +++ b/src/nodes/level.hpp @@ -40,8 +40,9 @@ namespace rl::inline node private: std::atomic m_active{ false }; - resource::preload::scene player_scene{ path::scene::Player }; + godot::Node* m_background{ nullptr }; ProjectileSpawner* m_projectile_spawner{ memnew(rl::ProjectileSpawner) }; - Character* m_player{ nullptr }; + resource::preload::scene player_scene{ path::scene::Player }; + Character* m_player{ player_scene.instantiate() }; }; } diff --git a/src/ui/main_dialog.cpp b/src/ui/main_dialog.cpp index bb2ded1..627ba88 100644 --- a/src/ui/main_dialog.cpp +++ b/src/ui/main_dialog.cpp @@ -4,8 +4,6 @@ #include "util/assert.hpp" #include "util/conversions.hpp" -#include - #include #include diff --git a/src/util/constants.hpp b/src/util/constants.hpp index 2c59cda..2a62c3f 100644 --- a/src/util/constants.hpp +++ b/src/util/constants.hpp @@ -4,6 +4,13 @@ namespace rl::inline constants { + + namespace node::name + { + constexpr inline auto level1{ "Level1" }; + constexpr inline auto level2{ "Level2" }; + } + namespace event { constexpr inline auto position_changed{ "position_changed" }; @@ -15,7 +22,28 @@ namespace rl::inline constants constexpr inline auto player_shoot{ "player_shoot" }; } + enum layer : uint32_t + { + Player = 0x00000001, + NPCs = 0x00000002, + Projectiles = 0x00000004, + Walls = 0x00000008, + DamageZones = 0x00000010, + DeathZones = 0x00000020, + PhysicsObjects = 0x00000040, + Layer08 = 0x00000080, + Layer09 = 0x00000100, + Layer10 = 0x00000200, + Layer11 = 0x00000400, + Layer12 = 0x00000800, + Layer13 = 0x00001000, + Layer14 = 0x00002000, + Layer15 = 0x00004000, + Layer16 = 0x00008000, + }; + namespace path + { namespace scene {