add camera limit script

This commit is contained in:
Chris Cromer 2022-04-20 11:48:11 -04:00
parent 3e63355826
commit 8edcae5adc
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
6 changed files with 112 additions and 2 deletions

8
godot/CameraLimit.gdns Normal file
View File

@ -0,0 +1,8 @@
[gd_resource type="NativeScript" load_steps=2 format=2]
[ext_resource path="res://gdnative/alai.tres" type="GDNativeLibrary" id=1]
[resource]
resource_name = "CameraLimit"
class_name = "CameraLimit"
library = ExtResource( 1 )

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://camera_fix.gd" type="Script" id=1]
[ext_resource path="res://CameraLimit.gdns" type="Script" id=1]
[ext_resource path="res://characters/player/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://assets/backgrounds/hills.png" type="Texture" id=3]
[ext_resource path="res://levels/Level2.tmx" type="PackedScene" id=4]

View File

@ -1,6 +1,6 @@
[gd_scene load_steps=5 format=2]
[ext_resource path="res://camera_fix.gd" type="Script" id=1]
[ext_resource path="res://CameraLimit.gdns" type="Script" id=1]
[ext_resource path="res://characters/player/Player.tscn" type="PackedScene" id=2]
[ext_resource path="res://levels/Prototype.tmx" type="PackedScene" id=3]
[ext_resource path="res://assets/backgrounds/mountains.png" type="Texture" id=4]

52
src/CameraLimit.cpp Normal file
View File

@ -0,0 +1,52 @@
#include "CameraLimit.h"
#include <SceneTree.hpp>
#include <TileMap.hpp>
#include <Camera2D.hpp>
#include <Viewport.hpp>
#include <Node.hpp>
using namespace godot;
void CameraLimit::_register_methods()
{
register_method("_ready", &CameraLimit::_ready);
}
CameraLimit::CameraLimit()
{
}
CameraLimit::~CameraLimit()
{
}
void CameraLimit::_init()
{
}
void CameraLimit::_ready()
{
auto node = find_node("Middleground");
auto middle_ground = cast_to<TileMap>(node);
if (middle_ground != NULL)
{
auto used_rect = middle_ground->get_used_rect();
auto bounds = Vector2(used_rect.position.x + used_rect.size.x, used_rect.position.y + used_rect.size.y - 1);
node = get_tree()->get_root()->find_node("Camera2D", true, false);
auto camera = cast_to<Camera2D>(node);
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!");
}
}

48
src/CameraLimit.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef ALAI_CAMERA_LIMIT_H
#define ALAI_CAMERA_LIMIT_H
#include <Godot.hpp>
#include <Node2D.hpp>
namespace godot
{
class CameraLimit: public Node2D
{
GODOT_CLASS(CameraLimit, Node2D)
public:
/**
* @brief This method registers classes with Godot.
*
* @details This method registers methods, properties, and signals with the Godot engine.
*/
static void _register_methods();
/**
* @brief Construct a new Camera Limit object.
*
*/
CameraLimit();
/**
* @brief Destroy the Camera Limit object.
*
*/
~CameraLimit();
/**
* @brief Initialize the class from Godot.
*
* @details This method is called just once when the Godot engine connects to the instance of the class.
*/
void _init();
/**
* @brief Code to be run when ready.
*
* @details This method is run when all the children of this node are ready.
*/
void _ready();
};
}
#endif

View File

@ -3,6 +3,7 @@
#include "state_machine/StateMachine.h"
#include "state_machine/State.h"
#include "Main.h"
#include "CameraLimit.h"
#include "player/Player.h"
#include "player/states/PlayerIdle.h"
#include "player/states/PlayerMove.h"
@ -42,6 +43,7 @@ extern "C" void GDN_EXPORT godot_nativescript_init(void *handle)
register_class<StateMachine>();
register_class<State>();
register_class<main::Main>();
register_class<CameraLimit>();
register_class<player::Player>();
register_class<player::PlayerIdle>();
register_class<player::PlayerMove>();