feat: camera zoom

This commit is contained in:
Sara 2024-09-09 20:23:00 +02:00
parent dc25687db2
commit 8fe3bf10a8
4 changed files with 30 additions and 8 deletions

View file

@ -3,6 +3,7 @@
#include "unit.hpp"
#include "utils/godot_macros.hpp"
#include "utils/util_functions.hpp"
#include <godot_cpp/classes/curve.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/physics_direct_space_state3d.hpp>
#include <godot_cpp/classes/physics_direct_space_state3d.hpp>
@ -14,6 +15,7 @@
void RTSPlayer::_bind_methods() {
#define CLASSNAME RTSPlayer
GDPROPERTY_HINTED(ground_marker_scene, gd::Variant::OBJECT, gd::PROPERTY_HINT_RESOURCE_TYPE, "PackedScene");
GDPROPERTY_HINTED(zoom_curve, gd::Variant::OBJECT, gd::PROPERTY_HINT_RESOURCE_TYPE, "Curve");
}
void RTSPlayer::_ready() {
@ -145,7 +147,9 @@ void RTSPlayer::select_unit(Unit *unit) {
void RTSPlayer::set_zoom_level(float level) {
this->current_zoom_level = gd::Math::clamp(level, this->min_zoom_level, this->max_zoom_level);
this->camera->set_position(this->local_zoom_direction * this->current_zoom_level);
float const zoom_factor{(this->current_zoom_level - this->min_zoom_level) / (this->max_zoom_level - this->min_zoom_level)};
gd::Vector3 const outward{gd::Vector3{this->local_zoom_direction.x, 0.f, this->local_zoom_direction.z}.normalized() * this->zoom_curve->sample(zoom_factor)};
this->camera->set_position((this->local_zoom_direction * this->current_zoom_level) + outward);
gd::Vector3 const forward_vector{(this->camera->get_global_position() - this->get_global_position()).normalized()};
gd::Vector3 const left_vector{gd::Vector3{0.f, 1.f, 0.f}.cross(forward_vector)};
this->camera->set_global_basis({left_vector, forward_vector.cross(left_vector), forward_vector});
@ -216,6 +220,14 @@ gd::Ref<gd::PackedScene> RTSPlayer::get_ground_marker_scene() const {
return this->ground_marker_scene;
}
void RTSPlayer::set_zoom_curve(gd::Ref<gd::Curve> curve) {
this->zoom_curve = curve;
}
gd::Ref<gd::Curve> RTSPlayer::get_zoom_curve() const {
return this->zoom_curve;
}
#ifdef DEBUG_ENABLED
void RTSPlayer::DEBUG_enable_debug(gd::Ref<gd::InputEvent>, float value) {
if(value != 0) {

View file

@ -5,10 +5,11 @@
#include "utils/player.hpp"
#include "utils/player_input.hpp"
#include <cstdint>
#include <godot_cpp/classes/camera3d.hpp>
#include <godot_cpp/classes/curve.hpp>
#include <godot_cpp/classes/input_event.hpp>
#include <godot_cpp/classes/node3d.hpp>
#include <godot_cpp/classes/packed_scene.hpp>
#include <godot_cpp/classes/camera3d.hpp>
namespace gd = godot;
@ -58,6 +59,8 @@ private:
gd::Vector3 get_left_direction() const;
void set_ground_marker_scene(gd::Ref<gd::PackedScene> scene);
gd::Ref<gd::PackedScene> get_ground_marker_scene() const;
void set_zoom_curve(gd::Ref<gd::Curve> curve);
gd::Ref<gd::Curve> get_zoom_curve() const;
private:
gd::Callable const on_selected_unit_destroyed{callable_mp(this, &RTSPlayer::clear_selected_unit)};
private:
@ -79,6 +82,7 @@ private:
gd::Vector3 cursor_camera_normal{0.f, 0.f, 0.f};
gd::Camera3D *camera{nullptr};
gd::Ref<gd::PackedScene> ground_marker_scene{nullptr};
gd::Ref<gd::Curve> zoom_curve{};
gd::Vector3 local_zoom_direction{0.f, 0.f, 0.f};
float current_zoom_level{1.f};
float max_zoom_level{1.f};