2021-11-22 22:00:45 +01:00
|
|
|
/*
|
|
|
|
Copyright (c) 2019-2021 Péter Magyar
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in all
|
|
|
|
copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
#include "terra_surface_2d.h"
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
int TerraSurface2D::get_id() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _id;
|
|
|
|
}
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::set_id(const int value) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_id = value;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
int TerraSurface2D::get_mesher_index() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _mesher_index;
|
|
|
|
}
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::set_mesher_index(const int value) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_mesher_index = value;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
bool TerraSurface2D::get_transparent() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _transparent;
|
|
|
|
}
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::set_transparent(const bool transparent) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_transparent = transparent;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
bool TerraSurface2D::get_liquid() const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _liquid;
|
|
|
|
}
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::set_liquid(const bool value) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_liquid = value;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
Rect2 TerraSurface2D::get_rect(const TerraSurfaceSides2D side) const {
|
2021-11-22 22:00:45 +01:00
|
|
|
return _rects[side];
|
|
|
|
}
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::set_rect(const TerraSurfaceSides2D side, const Rect2 &rect) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_rects[side] = rect;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
Ref<TerramanLibrary2D> TerraSurface2D::get_library() const {
|
|
|
|
return Ref<TerramanLibrary2D>(_library);
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::set_library(Ref<TerramanLibrary2D> library) {
|
2021-11-22 22:00:45 +01:00
|
|
|
_library = (*library);
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
_FORCE_INLINE_ Vector2 TerraSurface2D::transform_uv(const TerraSurfaceSides2D p_side, const Vector2 &p_uv) const {
|
2021-11-22 22:00:45 +01:00
|
|
|
Vector2 uv = p_uv;
|
|
|
|
|
|
|
|
Rect2 r = _rects[p_side];
|
|
|
|
|
|
|
|
uv.x *= r.size.x;
|
|
|
|
uv.y *= r.size.y;
|
|
|
|
uv.x += r.position.x;
|
|
|
|
uv.y += r.position.y;
|
|
|
|
|
|
|
|
return uv;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
_FORCE_INLINE_ Vector2 TerraSurface2D::transform_uv_scaled(const TerraSurfaceSides2D p_side, const Vector2 &p_uv, const int p_current_x, const int p_current_y, const int p_max) const {
|
2021-11-22 22:00:45 +01:00
|
|
|
Vector2 uv = p_uv;
|
|
|
|
|
|
|
|
Rect2 r = _rects[p_side];
|
|
|
|
|
|
|
|
float sizex = r.size.x / static_cast<float>(p_max);
|
|
|
|
float sizey = r.size.x / static_cast<float>(p_max);
|
|
|
|
|
|
|
|
uv.x *= sizex;
|
|
|
|
uv.y *= sizey;
|
|
|
|
|
|
|
|
uv.x += r.position.x + sizex * p_current_x;
|
|
|
|
uv.y += r.position.y + sizey * p_current_y;
|
|
|
|
|
|
|
|
return uv;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::refresh_rects() {
|
2021-11-22 22:00:45 +01:00
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
TerraSurface2D::TerraSurface2D() {
|
2021-11-22 22:00:45 +01:00
|
|
|
_id = 0;
|
|
|
|
_mesher_index = 0;
|
|
|
|
_transparent = false;
|
|
|
|
_liquid = false;
|
|
|
|
_library = NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
TerraSurface2D::~TerraSurface2D() {
|
2021-11-22 22:00:45 +01:00
|
|
|
_library = NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
void TerraSurface2D::_bind_methods() {
|
|
|
|
ClassDB::bind_method(D_METHOD("get_id"), &TerraSurface2D::get_id);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_id", "value"), &TerraSurface2D::set_id);
|
2021-11-22 22:00:45 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "id"), "set_id", "get_id");
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_mesher_index"), &TerraSurface2D::get_mesher_index);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_mesher_index", "value"), &TerraSurface2D::set_mesher_index);
|
2021-11-22 22:00:45 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "mesher_index"), "set_mesher_index", "get_mesher_index");
|
|
|
|
|
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::STRING, "voxel_name"), "set_name", "get_name");
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_transparent"), &TerraSurface2D::get_transparent);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_transparent", "transparent"), &TerraSurface2D::set_transparent);
|
2021-11-22 22:00:45 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "transparent"), "set_transparent", "get_transparent");
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_liquid"), &TerraSurface2D::get_liquid);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_liquid", "transparent"), &TerraSurface2D::set_liquid);
|
2021-11-22 22:00:45 +01:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "liquid"), "set_liquid", "get_liquid");
|
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("get_rect", "side"), &TerraSurface2D::get_rect);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_rect", "side", "rect"), &TerraSurface2D::set_rect);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("transform_uv", "side", "uv"), &TerraSurface2D::transform_uv);
|
|
|
|
ClassDB::bind_method(D_METHOD("transform_uv_scaled", "side", "uv", "p_current_x", "p_current_y", "max"), &TerraSurface2D::transform_uv_scaled);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
2021-11-22 23:51:16 +01:00
|
|
|
ClassDB::bind_method(D_METHOD("refresh_rects"), &TerraSurface2D::refresh_rects);
|
2021-11-22 22:00:45 +01:00
|
|
|
|
|
|
|
BIND_ENUM_CONSTANT(TERRA_SIDE_TOP);
|
|
|
|
BIND_ENUM_CONSTANT(TERRA_SIDE_BOTTOM);
|
|
|
|
BIND_ENUM_CONSTANT(TERRA_SIDE_SIDE);
|
|
|
|
|
|
|
|
BIND_CONSTANT(TERRA_SIDES_COUNT);
|
|
|
|
}
|