world_generator/main/planet.cpp

236 lines
7.4 KiB
C++
Raw Normal View History

/*
Copyright (c) 2019-2020 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.
*/
2019-09-24 02:42:42 +02:00
#include "planet.h"
2019-09-22 22:12:20 +02:00
int Planet::get_current_seed() {
return _current_seed;
2019-10-18 09:00:01 +02:00
}
void Planet::set_current_seed(int value) {
_current_seed = value;
2019-10-18 09:00:01 +02:00
}
2019-11-01 15:42:03 +01:00
Vector2 Planet::get_level_range() {
return _level_range;
}
void Planet::set_level_range(Vector2 value) {
_level_range = value;
}
#ifdef VOXELMAN_PRESENT
Ref<EnvironmentData> Planet::get_environment() {
return _environment;
}
void Planet::set_environment(Ref<EnvironmentData> value) {
_environment = value;
}
#endif
Ref<PlanetData> Planet::get_data() {
return _data;
}
void Planet::set_data(Ref<PlanetData> value) {
_data = value;
setup();
}
Ref<Biome> Planet::get_biome(const int index) const {
ERR_FAIL_INDEX_V(index, _biomes.size(), Ref<Biome>());
return _biomes.get(index);
}
void Planet::set_biome(const int index, const Ref<Biome> biome) {
ERR_FAIL_INDEX(index, _biomes.size());
_biomes.set(index, biome);
}
void Planet::add_biome(const Ref<Biome> biome) {
_biomes.push_back(biome);
}
void Planet::remove_biome(const int index) {
ERR_FAIL_INDEX(index, _biomes.size());
_biomes.remove(index);
}
int Planet::get_biome_count() const {
return _biomes.size();
}
//// Dungeons ////
Ref<Dungeon> Planet::get_dungeon(const int index) const {
ERR_FAIL_INDEX_V(index, _dungeons.size(), Ref<Dungeon>());
return _dungeons.get(index);
}
void Planet::set_dungeon(const int index, const Ref<Dungeon> dungeon) {
ERR_FAIL_INDEX(index, _dungeons.size());
_dungeons.set(index, dungeon);
}
void Planet::add_dungeon(const Ref<Dungeon> dungeon) {
_dungeons.push_back(dungeon);
}
void Planet::remove_dungeon(const int index) {
ERR_FAIL_INDEX(index, _dungeons.size());
_dungeons.remove(index);
}
int Planet::get_dungeon_count() const {
return _dungeons.size();
}
2019-10-18 09:00:01 +02:00
void Planet::setup() {
if (!_data.is_valid())
return;
2019-10-18 09:00:01 +02:00
if (has_method("_setup")) {
call("_setup");
}
}
#ifdef VOXELMAN_PRESENT
void Planet::setup_library(Ref<VoxelmanLibrary> library) {
if (!_data.is_valid())
return;
if (has_method("_setup_library")) {
call("_setup_library", library);
}
}
void Planet::_setup_library(Ref<VoxelmanLibrary> library) {
for (int i = 0; i < _data->get_voxel_surface_count(); ++i) {
Ref<VoxelSurface> s = _data->get_voxel_surface(i);
if (s.is_valid()) {
library->add_voxel_surface(s);
}
}
}
void Planet::generate_chunk(Ref<VoxelChunk> chunk, bool spawn_mobs) {
ERR_FAIL_COND(!chunk.is_valid());
2019-11-06 03:38:02 +01:00
2019-09-24 02:42:42 +02:00
if (has_method("_generate_chunk")) {
call("_generate_chunk", chunk, spawn_mobs);
2019-09-24 02:42:42 +02:00
}
2019-09-22 22:12:20 +02:00
}
#else
void Planet::setup_library(Ref<Resource> library) {
if (!_data.is_valid())
return;
if (has_method("_setup_library")) {
call("_setup_library", library);
}
}
void Planet::generate_chunk(Ref<Resource> chunk, bool spawn_mobs) {
ERR_FAIL_COND(!chunk.is_valid());
if (has_method("_generate_chunk")) {
call("_generate_chunk", chunk, spawn_mobs);
}
}
#endif
2019-11-06 03:38:02 +01:00
2019-10-14 21:50:35 +02:00
Ref<Image> Planet::generate_map() {
ERR_FAIL_COND_V(!has_method("_generate_map"), Ref<Image>());
return call("_generate_map");
}
2019-09-24 02:42:42 +02:00
Planet::Planet() {
_current_seed = 0;
2019-09-22 22:12:20 +02:00
}
2019-09-24 02:42:42 +02:00
Planet::~Planet() {
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
_environment.unref();
2020-01-09 04:28:08 +01:00
#endif
_data.unref();
_biomes.clear();
_dungeons.clear();
2019-09-22 22:12:20 +02:00
}
2019-09-24 02:42:42 +02:00
void Planet::_bind_methods() {
2019-10-18 09:00:01 +02:00
BIND_VMETHOD(MethodInfo("_setup"));
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
BIND_VMETHOD(MethodInfo("_setup_library", PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "VoxelmanLibrary")));
2019-11-06 03:38:02 +01:00
BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "VoxelChunk"), PropertyInfo(Variant::BOOL, "spawn_mobs")));
2020-01-09 04:28:08 +01:00
#else
BIND_VMETHOD(MethodInfo("_setup_library", PropertyInfo(Variant::OBJECT, "library", PROPERTY_HINT_RESOURCE_TYPE, "Resource")));
BIND_VMETHOD(MethodInfo("_generate_chunk", PropertyInfo(Variant::OBJECT, "chunk", PROPERTY_HINT_RESOURCE_TYPE, "Node"), PropertyInfo(Variant::BOOL, "spawn_mobs")));
2020-01-09 04:28:08 +01:00
#endif
2019-09-24 02:42:42 +02:00
2019-10-18 09:00:01 +02:00
ClassDB::bind_method(D_METHOD("setup"), &Planet::setup);
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
ClassDB::bind_method(D_METHOD("generate_chunk", "chunk"), &Planet::generate_chunk);
ClassDB::bind_method(D_METHOD("_setup_library", "library"), &Planet::_setup_library);
2020-01-09 04:28:08 +01:00
#else
ClassDB::bind_method(D_METHOD("generate_chunk", "chunk"), &Planet::generate_chunk);
2020-01-09 04:28:08 +01:00
#endif
2019-10-18 09:00:01 +02:00
ClassDB::bind_method(D_METHOD("setup_library", "library"), &Planet::setup_library);
2020-01-09 04:28:08 +01:00
ClassDB::bind_method(D_METHOD("get_current_seed"), &Planet::get_current_seed);
ClassDB::bind_method(D_METHOD("set_current_seed", "value"), &Planet::set_current_seed);
ADD_PROPERTY(PropertyInfo(Variant::INT, "current_seed"), "set_current_seed", "get_current_seed");
2019-11-01 15:42:03 +01:00
ClassDB::bind_method(D_METHOD("get_level_range"), &Planet::get_level_range);
ClassDB::bind_method(D_METHOD("set_level_range", "value"), &Planet::set_level_range);
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "level_range"), "set_level_range", "get_level_range");
2020-01-09 04:28:08 +01:00
#ifdef VOXELMAN_PRESENT
ClassDB::bind_method(D_METHOD("get_environment"), &Planet::get_environment);
ClassDB::bind_method(D_METHOD("set_environment", "value"), &Planet::set_environment);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "environment", PROPERTY_HINT_RESOURCE_TYPE, "EnvironmentData"), "set_environment", "get_environment");
2020-01-09 04:28:08 +01:00
#endif
ClassDB::bind_method(D_METHOD("get_data"), &Planet::get_data);
ClassDB::bind_method(D_METHOD("set_data", "value"), &Planet::set_data);
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "data", PROPERTY_HINT_RESOURCE_TYPE, "PlanetData"), "set_data", "get_data");
//biomes
ClassDB::bind_method(D_METHOD("get_biome", "index"), &Planet::get_biome);
ClassDB::bind_method(D_METHOD("set_biome", "index", "data"), &Planet::set_biome);
ClassDB::bind_method(D_METHOD("add_biome", "biome"), &Planet::add_biome);
ClassDB::bind_method(D_METHOD("remove_biome", "index"), &Planet::remove_biome);
ClassDB::bind_method(D_METHOD("get_biome_count"), &Planet::get_biome_count);
2019-10-14 21:50:35 +02:00
//Dungeons
ClassDB::bind_method(D_METHOD("get_dungeon", "index"), &Planet::get_dungeon);
ClassDB::bind_method(D_METHOD("set_dungeon", "index", "data"), &Planet::set_dungeon);
ClassDB::bind_method(D_METHOD("add_dungeon", "dungeon"), &Planet::add_dungeon);
ClassDB::bind_method(D_METHOD("remove_dungeon", "index"), &Planet::remove_dungeon);
ClassDB::bind_method(D_METHOD("get_dungeon_count"), &Planet::get_dungeon_count);
2019-10-14 21:50:35 +02:00
BIND_VMETHOD(MethodInfo(PropertyInfo(Variant::OBJECT, "image", PROPERTY_HINT_RESOURCE_TYPE, "Image"), "_generate_map"));
ClassDB::bind_method(D_METHOD("generate_map"), &Planet::generate_map);
2019-09-22 22:12:20 +02:00
}