Skip to content

Commit

Permalink
checkpointing, wip
Browse files Browse the repository at this point in the history
  • Loading branch information
vorlac committed Oct 5, 2023
1 parent 41913fb commit b98b180
Show file tree
Hide file tree
Showing 19 changed files with 300 additions and 101 deletions.
6 changes: 0 additions & 6 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
"visualizerFile": "${workspaceFolder}/extern/godot-engine/platform/windows/godot.natvis",
"internalConsoleOptions": "openOnSessionStart",
"launchCompleteCommand": "exec-run",
"targetArchitecture": "x64",
"externalConsole": false,
"symbolLoadInfo": {
"loadAll": true,
"exceptionList": "",
Expand Down Expand Up @@ -64,8 +62,6 @@
"visualizerFile": "${workspaceFolder}/extern/godot-engine/platform/windows/godot.natvis",
"internalConsoleOptions": "openOnSessionStart",
"launchCompleteCommand": "exec-run",
"targetArchitecture": "x64",
"externalConsole": false,
"symbolLoadInfo": {
"loadAll": true,
"exceptionList": ""
Expand Down Expand Up @@ -102,7 +98,6 @@
"visualizerFile": "${workspaceFolder}/extern/godot-engine/platform/windows/godot.natvis",
"internalConsoleOptions": "openOnSessionStart",
"console": "internalConsole",
"externalConsole": false,
},
{
"name": "editor (vsdbg)",
Expand All @@ -128,7 +123,6 @@
"visualizerFile": "${workspaceFolder}/extern/godot-engine/platform/windows/godot.natvis",
"internalConsoleOptions": "openOnSessionStart",
"console": "internalConsole",
"externalConsole": false,
}
]
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This repo and/or the [gdextension developer documentation](https://github.com/vo
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
* https://github.com/godotengine/godot-cpp/tree/master/test/src

# Licensing
#### Code
Expand Down
12 changes: 9 additions & 3 deletions src/api/extension_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

#include "main.hpp"
#include "nodes/camera.hpp"
#include "nodes/character.hpp"
#include "nodes/character/character.hpp"
#include "nodes/character/character_controller.hpp"
#include "nodes/character/enemy.hpp"
#include "nodes/character/enemy_controller.hpp"
#include "nodes/character/player.hpp"
#include "nodes/character/player_controller.hpp"
#include "nodes/level.hpp"
#include "nodes/player.hpp"
#include "nodes/player_controller.hpp"
#include "nodes/projectile_spawner.hpp"
#include "singletons/console.hpp"
#include "ui/main_dialog.hpp"
Expand Down Expand Up @@ -42,8 +45,11 @@ namespace godot
ClassDB::register_class<rl::Console<RichTextLabel>>();
ClassDB::register_class<rl::Projectile>();
ClassDB::register_class<rl::ProjectileSpawner>();
ClassDB::register_class<rl::CharacterController>();
ClassDB::register_class<rl::PlayerController>();
ClassDB::register_class<rl::EnemyController>();
ClassDB::register_class<rl::Camera>();
ClassDB::register_class<rl::Enemy>();
ClassDB::register_class<rl::Character>();
ClassDB::register_class<rl::Player>();
ClassDB::register_class<rl::Level>();
Expand Down
54 changes: 30 additions & 24 deletions src/nodes/character.cpp → src/nodes/character/character.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "nodes/character.hpp"
#include "nodes/character/character.hpp"

#include "nodes/camera.hpp"
#include "nodes/player_controller.hpp"
#include "nodes/character/character_controller.hpp"
#include "util/assert.hpp"
#include "util/bind.hpp"
#include "util/concepts.hpp"
#include "util/constants.hpp"
#include "util/engine.hpp"
#include "util/input.hpp"
Expand All @@ -12,6 +13,7 @@

#include <array>
#include <tuple>
#include <type_traits>
#include <vector>

#include <godot_cpp/classes/input.hpp>
Expand All @@ -28,37 +30,49 @@

namespace rl
{
Character::Character()
Character::Character(CharacterController* controller)
: m_character_controller(controller)
{
this->set_motion_mode(MotionMode::MOTION_MODE_FLOATING);
}

void Character::_ready()
{
this->add_child(m_camera);
this->add_child(m_player_controller);
this->add_child(m_character_controller);

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

runtime_assert(m_firing_point != nullptr);
runtime_assert(m_character_controller != nullptr);

if (m_character_controller != nullptr)
{
signal<event::character_move>::connect<CharacterController>(m_character_controller)
<=> slot(this, on_character_movement);

signal<event::player_move>::connect<PlayerController>(m_player_controller)
<=> slot(this, on_player_movement);
signal<event::character_rotate>::connect<CharacterController>(m_character_controller)
<=> slot(this, on_character_rotate);

signal<event::player_rotate>::connect<PlayerController>(m_player_controller)
<=> slot(this, on_player_rotate);
signal<event::character_shoot>::connect<CharacterController>(m_character_controller)
<=> slot(this, on_character_shoot);
}
}

signal<event::player_shoot>::connect<PlayerController>(m_player_controller)
<=> slot(this, on_player_shoot);
void Character::set_controller(CharacterController* controller)
{
m_character_controller = controller;
runtime_assert(m_character_controller != nullptr);
}

PlayerController* Character::get_controller() const
CharacterController* Character::get_controller() const
{
return m_player_controller;
return m_character_controller;
}

[[signal_slot]]
void Character::on_player_movement(godot::Vector2 movement_velocity, double delta_time)
void Character::on_character_movement(godot::Vector2 movement_velocity, double delta_time)
{
double increment = m_movement_friction * delta_time;
godot::Vector2 velocity{ this->get_velocity().lerp(movement_velocity, increment) };
Expand All @@ -69,15 +83,15 @@ namespace rl
}

[[signal_slot]]
void Character::on_player_rotate(double rotation_angle, double delta_time)
void Character::on_character_rotate(double rotation_angle, double delta_time)
{
double smoothed_angle = godot::Math::lerp_angle(this->get_rotation(), rotation_angle,
m_rotation_speed * delta_time);
this->set_rotation(smoothed_angle);
}

[[signal_slot]]
void Character::on_player_shoot()
void Character::on_character_shoot()
{
// TODO: fix this
this->emit_signal(event::spawn_projectile, m_firing_point);
Expand Down Expand Up @@ -121,14 +135,6 @@ namespace rl

void Character::_bind_methods()
{
bind_member_function(Character, on_player_movement);
bind_member_function(Character, on_player_rotate);
bind_member_function(Character, on_player_shoot);

bind_property(Character, movement_speed, double);
bind_property(Character, movement_friction, double);
bind_property(Character, rotation_speed, double);

signal_binding<Character, event::position_changed>::add<godot::Object*, godot::Vector2>();
Character::bind_methods<Character>();
}
}
42 changes: 34 additions & 8 deletions src/nodes/character.hpp → src/nodes/character/character.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
#pragma once

#include "nodes/camera.hpp"
#include "nodes/player_controller.hpp"
#include "util/attributes.hpp"
#include "util/scene.hpp"
#include "util/bind.hpp"
#include "util/concepts.hpp"
#include "util/constants.hpp"

#include <godot_cpp/classes/character_body2d.hpp>
#include <godot_cpp/classes/collision_shape2d.hpp>
#include <godot_cpp/classes/sprite2d.hpp>
#include <godot_cpp/core/object.hpp>
#include <godot_cpp/variant/vector2.hpp>

namespace godot
{
Expand All @@ -16,17 +19,20 @@ namespace godot

namespace rl
{
class CharacterController;

class Character : public godot::CharacterBody2D
{
GDCLASS(Character, godot::CharacterBody2D);

public:
Character();
Character(CharacterController* controller = nullptr);
virtual ~Character() = default;

void _ready() override;

PlayerController* get_controller() const;
CharacterController* get_controller() const;
void set_controller(CharacterController* controller);

protected:
[[property]] double get_movement_speed() const;
Expand All @@ -36,13 +42,33 @@ namespace rl
[[property]] void set_movement_friction(const double move_friction);
[[property]] void set_rotation_speed(const double rotation_speed);

[[signal_slot]] void on_player_shoot();
[[signal_slot]] void on_player_rotate(double rotation_angle, double delta_time);
[[signal_slot]] void on_player_movement(godot::Vector2 movement_velocity, double delta_time);
[[signal_slot]] void on_character_shoot();
[[signal_slot]] void on_character_rotate(double rotation_angle, double delta_time);
[[signal_slot]] void on_character_movement(godot::Vector2 movement_velocity,
double delta_time);

protected:
static void _bind_methods();

template <typename TCharacter>
requires std::derived_from<T, Character>
static void bind_methods()
{
using character_t = typename std::type_identity_t<T>;

bind_member_function(T, on_character_movement);
bind_member_function(T, on_character_rotate);
bind_member_function(T, on_character_shoot);

bind_property(T, movement_speed, double);
bind_property(T, movement_friction, double);
bind_property(T, rotation_speed, double);

signal_binding<decltype(std::declval<character_t>()),
event::position_changed>::add<godot::Object*, godot::Vector2>();
signal_binding<decltype(std::declval<character_t>()), event::spawn_projectile>::add<>();
}

protected:
// Rate of acceleration/deceleration (unit/s/s)
double m_movement_friction{ 5.0 };
Expand All @@ -56,7 +82,7 @@ namespace rl
// the player character camera
Camera* m_camera{ memnew(Camera) };
// handles all input related player controls
PlayerController* m_player_controller{ memnew(PlayerController) };
CharacterController* m_character_controller{ nullptr };
// marker identifying location where to spwwn projectiles
godot::Marker2D* m_firing_point{ nullptr };
};
Expand Down
45 changes: 45 additions & 0 deletions src/nodes/character/character_controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "nodes/character/character_controller.hpp"

#include "util/bind.hpp"
#include "util/constants.hpp"
#include "util/engine.hpp"
#include "util/input.hpp"
#include "util/io.hpp"

#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/node2d.hpp>
#include <godot_cpp/variant/variant.hpp>

namespace rl
{
void CharacterController::_process(double delta_time)
{
if (engine::editor_active())
return;

godot::Input* input_handler{ input::get() };
if (input_handler != nullptr)
{
this->process_movement_input(input_handler, delta_time);
this->process_rotation_input(input_handler, delta_time);
this->process_action_input(input_handler, delta_time);

m_elapsed_time += delta_time;
if (m_elapsed_time > 1.0)
{
m_elapsed_time = 0.0;
this->emit_signal(event::position_changed, this->get_parent(),
this->get_global_position());
}
}
}

void CharacterController::_bind_methods()
{
signal_binding<CharacterController, event::character_move>::add<godot::Vector2, double>();
signal_binding<CharacterController, event::character_rotate>::add<double, double>();
signal_binding<CharacterController, event::character_shoot>::add<godot::Object*>();
signal_binding<CharacterController, event::position_changed>::add<godot::Object*,
godot::Vector2>();
}
}
71 changes: 71 additions & 0 deletions src/nodes/character/character_controller.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <godot_cpp/classes/input.hpp>
#include <godot_cpp/classes/node2d.hpp>

namespace rl
{
class CharacterController : public godot::Node2D
{
GDCLASS(CharacterController, godot::Node2D);

public:
CharacterController() = default;
virtual ~CharacterController() = default;

void _process(double delta_time) override;

virtual void process_action_input(godot::Input* const input, double delta_time)
{
}

virtual void process_movement_input(godot::Input* const input, double delta_time)
{
}

virtual void process_rotation_input(godot::Input* const input, double delta_time)
{
}

protected:
enum class InputMode
{
MouseAndKeyboard,
Controller
};

InputMode get_input_mode(godot::Input* const input);

static void _bind_methods();

// template <class T, class B>
// static void register_virtuals()
//{
// godot::Node2D::register_virtuals<T, B>();
// if constexpr (
// !std::is_same_v<decltype(&B::process_action_input),
// decltype(&T::process_action_input)>)
// {
// BIND_VIRTUAL_METHOD(T, process_action_input);
// }
// if constexpr (!std::is_same_v<decltype(&B::process_movement_input),
// decltype(&T::process_movement_input)>)
// {
// BIND_VIRTUAL_METHOD(T, process_movement_input);
// }
// if constexpr (!std::is_same_v<decltype(&B::process_rotation_input),
// decltype(&T::process_rotation_input)>)
// {
// BIND_VIRTUAL_METHOD(T, process_rotation_input);
// }
// }

protected:
// the active input mode for character controls
InputMode m_input_mode{ InputMode::MouseAndKeyboard };
// target rotation
double m_rotation_angle{ 0.0 };
// elapsed runtime (seconds)
double m_elapsed_time{ 0.0 };
};
}
Loading

0 comments on commit b98b180

Please sign in to comment.