alai/src/CameraLimit.cpp

50 lines
1.2 KiB
C++
Raw Normal View History

2022-04-20 11:48:11 -04:00
#include "CameraLimit.h"
2022-08-26 19:27:57 -04:00
#include <Camera2D.hpp>
2022-04-20 11:48:11 -04:00
#include <SceneTree.hpp>
#include <TileMap.hpp>
#include <Viewport.hpp>
2022-08-26 19:27:57 -04:00
void alai::CameraLimit::_register_methods()
2022-04-20 11:48:11 -04:00
{
godot::register_method("_ready", &CameraLimit::_ready);
2022-04-20 11:48:11 -04:00
}
2022-08-26 19:27:57 -04:00
alai::CameraLimit::CameraLimit()
2022-04-20 11:48:11 -04:00
{
}
2022-08-26 19:27:57 -04:00
alai::CameraLimit::~CameraLimit()
2022-04-20 11:48:11 -04:00
{
}
2022-08-26 19:27:57 -04:00
void alai::CameraLimit::_init()
2022-04-20 11:48:11 -04:00
{
}
2022-08-26 19:27:57 -04:00
void alai::CameraLimit::_ready()
2022-04-20 11:48:11 -04:00
{
auto node = find_node("Middleground");
2022-08-26 19:27:57 -04:00
auto middle_ground = cast_to<godot::TileMap>(node);
2022-04-20 11:48:11 -04:00
if (middle_ground != NULL)
{
auto used_rect = middle_ground->get_used_rect();
2022-08-26 19:27:57 -04:00
auto bounds = godot::Vector2(used_rect.position.x + used_rect.size.x, used_rect.position.y + used_rect.size.y);
2022-04-20 11:48:11 -04:00
node = get_tree()->get_root()->find_node("Camera2D", true, false);
2022-08-26 19:27:57 -04:00
auto camera = cast_to<godot::Camera2D>(node);
2022-04-20 11:48:11 -04:00
if (camera != NULL)
{
camera->set_limit(2, bounds.x * middle_ground->get_cell_size().x);
camera->set_limit(3, bounds.y * middle_ground->get_cell_size().y);
}
else
{
ERR_PRINT("Could not find Camera2D node!");
}
}
else
{
ERR_PRINT("Could not find Middleground node!");
}
}