From b1057412cbc83a121a68167b88384ec59975dff2 Mon Sep 17 00:00:00 2001 From: Sara Date: Fri, 31 May 2024 16:07:02 +0200 Subject: [PATCH] feat: started defining game concepts --- src/rts_game_mode.cpp | 5 +++ src/rts_game_mode.hpp | 13 +++++++ src/rts_player.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++ src/rts_player.hpp | 41 +++++++++++++++++++-- src/unit.cpp | 8 ++++ src/unit.hpp | 19 ++++++++++ 6 files changed, 167 insertions(+), 4 deletions(-) create mode 100644 src/rts_game_mode.cpp create mode 100644 src/rts_game_mode.hpp create mode 100644 src/rts_player.cpp create mode 100644 src/unit.cpp create mode 100644 src/unit.hpp diff --git a/src/rts_game_mode.cpp b/src/rts_game_mode.cpp new file mode 100644 index 0000000..5a282d8 --- /dev/null +++ b/src/rts_game_mode.cpp @@ -0,0 +1,5 @@ +#include "rts_game_mode.hpp" + +void RTSGameMode::_bind_methods() { +#define CLASSNAME RTSGameMode +} diff --git a/src/rts_game_mode.hpp b/src/rts_game_mode.hpp new file mode 100644 index 0000000..ee2a316 --- /dev/null +++ b/src/rts_game_mode.hpp @@ -0,0 +1,13 @@ +#ifndef RTS_GAME_MODE_HPP +#define RTS_GAME_MODE_HPP + +#include "utils/game_mode.hpp" + +class RTSGameMode : public utils::GameMode { + GDCLASS(RTSGameMode, utils::GameMode); + static void _bind_methods(); +public: +private: +}; + +#endif // !RTS_GAME_MODE_HPP diff --git a/src/rts_player.cpp b/src/rts_player.cpp new file mode 100644 index 0000000..960b13e --- /dev/null +++ b/src/rts_player.cpp @@ -0,0 +1,85 @@ +#include "rts_player.hpp" +#include + +void RTSPlayer::_bind_methods() { +#define CLASSNAME RTSPlayer +} + +void RTSPlayer::_process(double delta_time) { + this->move(this->camera_keys_motion * this->camera_keys_speed * delta_time); + this->rotate(gd::Vector3{0.f, 1.f, 0.f}, camera_keys_rotation * this->camera_keys_rotation_speed * delta_time); + + this->move(this->camera_mouse_motion * this->camera_mouse_speed); + this->camera_mouse_motion = {0.f, 0.f}; + this->rotate(gd::Vector3{0.f, 1.f, 0.f}, camera_mouse_rotation * this->camera_mouse_rotation_speed); + this->camera_mouse_rotation = 0.f; +} + +void RTSPlayer::setup_player_input(utils::PlayerInput *input) { + input->listen_to("lclick", callable_mp(this, &RTSPlayer::on_lclick)); + input->listen_to("rclick", callable_mp(this, &RTSPlayer::on_rclick)); + input->listen_to("mclick", callable_mp(this, &RTSPlayer::on_mclick)); + input->listen_to("direction_right", "direction_left", callable_mp(this, &RTSPlayer::on_direction_horizontal)); + input->listen_to("direction_down", "direction_up", callable_mp(this, &RTSPlayer::on_direction_vertical)); + input->listen_to("rotate_left", "rotate_right", callable_mp(this, &RTSPlayer::on_rotate_horizontal)); + input->listen_to("_mouse_left", "_mouse_right", callable_mp(this, &RTSPlayer::on_mouse_horizontal)); + input->listen_to("_mouse_down", "_mouse_up", callable_mp(this, &RTSPlayer::on_mouse_vertical)); +} + +gd::Node *RTSPlayer::to_node() { + return gd::Object::cast_to(this); +} + +void RTSPlayer::spawn_at_position(gd::Transform3D const &at) { + this->set_global_transform(at); +} + +void RTSPlayer::move(gd::Vector2 motion) { + gd::Vector3 pos = this->get_global_position(); + this->set_global_position(pos + this->get_forward_direction() * motion.y + this->get_left_direction() * motion.x); +} + +void RTSPlayer::on_mouse_horizontal(gd::Ref event, float value) { + if(this->mmb_down) + this->camera_mouse_motion.x = value; + else if(this->rmb_down) + this->camera_mouse_rotation = value; +} + +void RTSPlayer::on_mouse_vertical(gd::Ref event, float value) { + if(this->mmb_down) + this->camera_mouse_motion.y = value; +} + +void RTSPlayer::on_direction_horizontal(gd::Ref event, float value) { + this->camera_keys_motion.x = value; +} + +void RTSPlayer::on_direction_vertical(gd::Ref event, float value) { + this->camera_keys_motion.y = value; +} + +void RTSPlayer::on_rotate_horizontal(gd::Ref event, float value) { + this->camera_keys_rotation = value; +} + +void RTSPlayer::on_rclick(gd::Ref event, float value) { + this->rmb_down = value != 0.f; +} + +void RTSPlayer::on_lclick(gd::Ref event, float value) { +} + +void RTSPlayer::on_mclick(gd::Ref event, float value) { + this->mmb_down = value != 0.f; +} + +gd::Vector3 RTSPlayer::get_forward_direction() const { + gd::Vector3 forward = this->get_global_basis().get_column(2); + return gd::Vector3{forward.x, 0.f, forward.z}.normalized(); +} + +gd::Vector3 RTSPlayer::get_left_direction() const { + gd::Vector3 left = this->get_global_basis().get_column(0); + return gd::Vector3{left.x, 0.f, left.z}.normalized(); +} diff --git a/src/rts_player.hpp b/src/rts_player.hpp index 499028f..08c9d6f 100644 --- a/src/rts_player.hpp +++ b/src/rts_player.hpp @@ -1,19 +1,52 @@ #ifndef RTS_PLAYER_HPP #define RTS_PLAYER_HPP +#include "godot_cpp/classes/input_event.hpp" #include "utils/player.hpp" #include "utils/player_input.hpp" #include -class RTSPlayer : public godot::Node3D, - public godot::IPlayer { +namespace gd = godot; + +class RTSPlayer : public gd::Node3D, + public utils::IPlayer { GDCLASS(RTSPlayer, godot::Node3D); static void _bind_methods(); public: - virtual void setup_player_input(godot::PlayerInput *input) override; + virtual void _process(double delta_time) override; + + virtual void setup_player_input(utils::PlayerInput *input) override; virtual Node *to_node() override; - virtual void spawn_at_position(godot::Transform3D const &at) override; + virtual void spawn_at_position(gd::Transform3D const &at) override; private: + void move(gd::Vector2 motion); + + // input functions + void on_mouse_horizontal(gd::Ref event, float value); + void on_mouse_vertical(gd::Ref event, float value); + void on_direction_horizontal(gd::Ref event, float value); + void on_direction_vertical(gd::Ref event, float value); + void on_rotate_horizontal(gd::Ref event, float value); + void on_lclick(gd::Ref event, float value); + void on_rclick(gd::Ref event, float value); + void on_mclick(gd::Ref event, float value); + + // getters + gd::Vector3 get_forward_direction() const; + gd::Vector3 get_left_direction() const; +private: + bool mmb_down{false}; + bool rmb_down{false}; + gd::Vector2 camera_mouse_motion{0.f, 0.f}; + float camera_mouse_rotation{0.f}; + + gd::Vector2 camera_keys_motion{0.f, 0.f}; + float camera_keys_rotation{0.f}; + + float const camera_keys_speed{10.f}; + float const camera_keys_rotation_speed{1.f}; + float const camera_mouse_speed{0.01f}; + float const camera_mouse_rotation_speed{-0.003f}; }; #endif // !RTS_PLAYER_HPP diff --git a/src/unit.cpp b/src/unit.cpp new file mode 100644 index 0000000..ecffd33 --- /dev/null +++ b/src/unit.cpp @@ -0,0 +1,8 @@ +#include "unit.hpp" + +void Unit::_bind_methods() { +#define CLASSNAME Unit +} + +void Unit::_enter_tree() { +} diff --git a/src/unit.hpp b/src/unit.hpp new file mode 100644 index 0000000..40d505f --- /dev/null +++ b/src/unit.hpp @@ -0,0 +1,19 @@ +#ifndef RTS_UNIT_HPP +#define RTS_UNIT_HPP + +#include +#include + +namespace gd = godot; + + +class Unit : public gd::Node3D { + GDCLASS(Unit, gd::Node3D); + static void _bind_methods(); +public: + virtual void _enter_tree() override; +private: + gd::NavigationAgent3D *agent{nullptr}; +}; + +#endif // !RTS_UNIT_HPP