Skip to content

Commit

Permalink
updated readme and added some basic level obstacles for layer & colli…
Browse files Browse the repository at this point in the history
…sion testing
  • Loading branch information
vorlac committed Sep 29, 2023
1 parent 11c632a commit e6613d2
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 40 deletions.
47 changes: 34 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -12,25 +26,32 @@ The overall node hierarchy:
<div class="highlight highlight-html"> <pre>
<a href="./src/main.hpp" title="title">Main</a>
├── <a href="./src/ui/main_dialog.hpp" title="title">MainDialog</a>
│ ├── <a href="./project/assets/scenes/ui/dialogs/main_dialog.tscn" title="title">RichTextLabel (In-Game Console)</a>
│ └── <a href="./project/assets/scenes/ui/dialogs/main_dialog.tscn" title="title">MainSubViewport (Primary Game Viewport)</a>
│ └── <a href="./project/assets/scenes/ui/dialogs/main_dialog.tscn" title="title">Canvas Layer</a>
├── <a href="./src/core/level.hpp" title="title">Level</a>
│ ├── <a href="./project/scenes/ui/main_dialog.tscn" title="title">RichTextLabel (In-Game Console)</a>
│ └── <a href="./project/scenes/ui/main_dialog.tscn" title="title">MainSubViewport (Primary Game Viewport)</a>
│ └── <a href="./project/scenes/ui/main_dialog.tscn" title="title">Canvas Layer</a>
├── <a href="./src/nodes/level.hpp" title="title">Level</a>
│ ├── <a href="./src/nodes/character.cpp" title="title">Character</a>
│ │ ├── <a href="./src/nodes/camera.cpp" title="title">Camera</a>
│ │ ├── <a href="./src/nodes/character.cpp" title="title">Sprite2D</a>
│ │ └── <a href="./src/nodes/character.cpp" title="title">CollisionShape2D</a>
│ ├── <a href="./src/core/projectile_spawner.hpp" title="title">Projectile Spawner</a>
│ │ └── <a href="./src/nodes/projectile.cpp" title="title">Projectile</a>
│ └── <a href="./src/core/level.hpp" title="title">Sprite2D (Background)</a>
└── <a href="./src/core/main.hpp" title="title">Main Canvas Layer (reference to Canvas Layer in MainDialog)</a>
│ └── <a href="./src/nodes/projectile_spawner.hpp" title="title">Projectile Spawner</a>
│ └── <a href="./src/nodes/projectile.cpp" title="title">Projectile</a>
└── <a href="./src/ui/main_dialog.hpp" title="title">Main Canvas Layer (reference to Canvas Layer in MainDialog)</a>
</p>
<a href="./src/singletons/console.hpp" title="title">Console (Singleton)</a>
└── <a href="./project/assets/scenes/ui/dialogs/main_dialog.tscn" title="title">RichTextLabel (Reference from MainDialog)</a>
└── <a href="./project/scenes/ui/main_dialog.tscn" title="title">RichTextLabel (Reference from MainDialog)</a>
</pre> </div>

# 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
Expand Down
18 changes: 18 additions & 0 deletions project/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions project/scenes/characters/player.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
68 changes: 62 additions & 6 deletions project/scenes/levels/level1.tscn
Original file line number Diff line number Diff line change
@@ -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")
12 changes: 6 additions & 6 deletions project/scenes/ui/main_dialog.tscn
Original file line number Diff line number Diff line change
@@ -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"]

Expand Down Expand Up @@ -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"]
Expand Down
17 changes: 7 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,21 @@ namespace rl
Main::Main()
{
scene::node::set_unique_name(this, "Main");
}

void Main::_ready()
{
resource::preload::scene<Level> level{ path::scene::Level1 };
resource::preload::scene<MainDialog> dialog{ path::ui::MainDialog };

m_active_level = level.instantiate();
runtime_assert(m_active_level != nullptr);

resource::preload::scene<MainDialog> 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<godot::CanvasLayer>(
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);

Expand All @@ -41,6 +33,11 @@ namespace rl
}
}

void Main::_ready()
{
this->apply_default_settings();
}

void Main::apply_default_settings()
{
engine::set_fps(60);
Expand Down
1 change: 0 additions & 1 deletion src/nodes/level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
5 changes: 3 additions & 2 deletions src/nodes/level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ namespace rl::inline node

private:
std::atomic<bool> m_active{ false };
resource::preload::scene<Character> player_scene{ path::scene::Player };
godot::Node* m_background{ nullptr };
ProjectileSpawner* m_projectile_spawner{ memnew(rl::ProjectileSpawner) };
Character* m_player{ nullptr };
resource::preload::scene<Character> player_scene{ path::scene::Player };
Character* m_player{ player_scene.instantiate() };
};
}
2 changes: 0 additions & 2 deletions src/ui/main_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "util/assert.hpp"
#include "util/conversions.hpp"

#include <fmt/format.h>

#include <godot_cpp/classes/canvas_layer.hpp>
#include <godot_cpp/classes/control.hpp>

Expand Down
28 changes: 28 additions & 0 deletions src/util/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" };
Expand All @@ -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
{
Expand Down

0 comments on commit e6613d2

Please sign in to comment.