feat: started defining game concepts

This commit is contained in:
Sara 2024-05-31 16:07:02 +02:00
parent 9d687df553
commit b1057412cb
6 changed files with 167 additions and 4 deletions

5
src/rts_game_mode.cpp Normal file
View file

@ -0,0 +1,5 @@
#include "rts_game_mode.hpp"
void RTSGameMode::_bind_methods() {
#define CLASSNAME RTSGameMode
}

13
src/rts_game_mode.hpp Normal file
View file

@ -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

85
src/rts_player.cpp Normal file
View file

@ -0,0 +1,85 @@
#include "rts_player.hpp"
#include <godot_cpp/variant/callable_method_pointer.hpp>
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<Node>(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<gd::InputEvent> 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<gd::InputEvent> event, float value) {
if(this->mmb_down)
this->camera_mouse_motion.y = value;
}
void RTSPlayer::on_direction_horizontal(gd::Ref<gd::InputEvent> event, float value) {
this->camera_keys_motion.x = value;
}
void RTSPlayer::on_direction_vertical(gd::Ref<gd::InputEvent> event, float value) {
this->camera_keys_motion.y = value;
}
void RTSPlayer::on_rotate_horizontal(gd::Ref<gd::InputEvent> event, float value) {
this->camera_keys_rotation = value;
}
void RTSPlayer::on_rclick(gd::Ref<gd::InputEvent> event, float value) {
this->rmb_down = value != 0.f;
}
void RTSPlayer::on_lclick(gd::Ref<gd::InputEvent> event, float value) {
}
void RTSPlayer::on_mclick(gd::Ref<gd::InputEvent> 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();
}

View file

@ -1,19 +1,52 @@
#ifndef RTS_PLAYER_HPP #ifndef RTS_PLAYER_HPP
#define RTS_PLAYER_HPP #define RTS_PLAYER_HPP
#include "godot_cpp/classes/input_event.hpp"
#include "utils/player.hpp" #include "utils/player.hpp"
#include "utils/player_input.hpp" #include "utils/player_input.hpp"
#include <godot_cpp/classes/node3d.hpp> #include <godot_cpp/classes/node3d.hpp>
class RTSPlayer : public godot::Node3D, namespace gd = godot;
public godot::IPlayer {
class RTSPlayer : public gd::Node3D,
public utils::IPlayer {
GDCLASS(RTSPlayer, godot::Node3D); GDCLASS(RTSPlayer, godot::Node3D);
static void _bind_methods(); static void _bind_methods();
public: 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 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: private:
void move(gd::Vector2 motion);
// input functions
void on_mouse_horizontal(gd::Ref<gd::InputEvent> event, float value);
void on_mouse_vertical(gd::Ref<gd::InputEvent> event, float value);
void on_direction_horizontal(gd::Ref<gd::InputEvent> event, float value);
void on_direction_vertical(gd::Ref<gd::InputEvent> event, float value);
void on_rotate_horizontal(gd::Ref<gd::InputEvent> event, float value);
void on_lclick(gd::Ref<gd::InputEvent> event, float value);
void on_rclick(gd::Ref<gd::InputEvent> event, float value);
void on_mclick(gd::Ref<gd::InputEvent> 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 #endif // !RTS_PLAYER_HPP

8
src/unit.cpp Normal file
View file

@ -0,0 +1,8 @@
#include "unit.hpp"
void Unit::_bind_methods() {
#define CLASSNAME Unit
}
void Unit::_enter_tree() {
}

19
src/unit.hpp Normal file
View file

@ -0,0 +1,19 @@
#ifndef RTS_UNIT_HPP
#define RTS_UNIT_HPP
#include <godot_cpp/classes/navigation_agent3d.hpp>
#include <godot_cpp/classes/node3d.hpp>
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