authority/modules/terrain/terrain.cpp

175 lines
4.3 KiB
C++

#include "terrain.h"
#include "terrain/terrain_chunk.h"
#include "terrain/terrain_modifier.h"
void Terrain::_bind_methods() {
BIND_PROPERTY(Variant::INT, side_length);
BIND_PROPERTY(Variant::INT, chunk_size);
BIND_PROPERTY(Variant::INT, detail);
}
void Terrain::ready() {
threads.resize_initialized(22);
construct_chunk_grid();
generate_meshes();
}
void Terrain::update_modifier_list() {
bool editor{ Engine::get_singleton()->is_editor_hint() };
this->modifiers.clear();
for (Variant var : get_children()) {
if (TerrainModifier * mod{ cast_to<TerrainModifier>(var) }) {
if (editor && !mod->is_connected(TerrainModifier::sig_changed, callable_mp(this, &self_type::on_terrain_changed))) {
mod->connect(TerrainModifier::sig_changed, callable_mp(this, &self_type::on_terrain_changed));
}
this->modifiers.push_back(mod);
}
}
on_terrain_changed();
}
void Terrain::child_order_changed() {
// TODO: check if the order of _modifiers_ actually changed
if (is_ready()) {
update_modifier_list();
}
}
void Terrain::child_entered(Node *node) {
if (cast_to<TerrainModifier>(node) != nullptr && is_ready()) {
update_modifier_list();
}
}
void Terrain::child_exiting(Node *node) {
if (TerrainModifier * mod{ cast_to<TerrainModifier>(node) }) {
this->modifiers.erase(mod);
if (Engine::get_singleton()->is_editor_hint() && !is_queued_for_deletion()) {
mod->disconnect(TerrainModifier::sig_changed, callable_mp(this, &self_type::on_terrain_changed));
}
if (is_ready()) {
on_terrain_changed();
}
}
}
void Terrain::on_terrain_changed() {
if (is_queued_for_deletion() || !is_inside_tree()) {
return;
}
generate_meshes();
}
void Terrain::_notification(int what) {
switch (what) {
default:
return;
case NOTIFICATION_ENTER_TREE:
if (!is_ready()) {
connect("child_entered_tree", callable_mp(this, &self_type::child_entered));
connect("child_exiting_tree", callable_mp(this, &self_type::child_exiting));
connect("child_order_changed", callable_mp(this, &self_type::child_order_changed));
}
return;
case NOTIFICATION_READY:
ready();
return;
}
}
void Terrain::generate_meshes_thread(void *terrain) {
Terrain *self{ static_cast<Terrain *>(terrain) };
for (;;) {
self->workload_lock.lock();
if (self->workload.is_empty()) {
self->workload_lock.unlock();
return;
}
TerrainChunkMesh *mesh{ self->workload[0] };
self->workload.remove_at(0);
self->workload_lock.unlock();
if (!mesh->is_inside_tree()) {
return;
}
mesh->update_mesh();
}
}
void Terrain::construct_chunk_grid() {
size_t const chunks_per_side{ this->side_length / this->chunk_size };
Vector3 const origin{ (float)this->chunk_size / 2.f, 0.f, (float)this->chunk_size / 2.f };
for (size_t y{ 0 }; y < chunks_per_side; ++y) {
for (size_t x{ 0 }; x < chunks_per_side; ++x) {
TerrainChunkMesh *chunk{ memnew(TerrainChunkMesh) };
chunk->set_size(this->chunk_size);
chunk->set_detail(this->detail);
chunk->set_terrain(this);
chunk->set_position(origin + Vector3{ (float)this->chunk_size * (float)x, 0.f, (float)this->chunk_size * (float)y });
add_child(chunk);
chunk->set_owner(this);
this->meshes.push_back(chunk);
}
}
}
void Terrain::generate_meshes() {
if (!is_inside_tree()) {
return;
}
this->workload.append_array(this->meshes);
for (Thread &thread : this->threads) {
thread.start(&self_type::generate_meshes_thread, this);
}
for (Thread &thread : this->threads) {
if (thread.is_started()) {
thread.wait_to_finish();
}
}
}
float Terrain::height_at(Vector2 world_coordinate) {
float height{ 0 };
for (TerrainModifier *mod : this->modifiers) {
if (!mod->is_inside_tree()) {
return height;
}
height = mod->evaluate_at(world_coordinate, height);
}
return height;
}
void Terrain::set_side_length(size_t length) {
this->side_length = length;
if (is_inside_tree()) {
construct_chunk_grid();
generate_meshes();
}
}
size_t Terrain::get_side_length() const {
return this->side_length;
}
void Terrain::set_chunk_size(size_t size) {
this->chunk_size = size;
if (is_inside_tree()) {
construct_chunk_grid();
generate_meshes();
}
}
size_t Terrain::get_chunk_size() const {
return this->chunk_size;
}
void Terrain::set_detail(size_t detail) {
this->detail = detail;
if (is_inside_tree()) {
construct_chunk_grid();
generate_meshes();
}
}
size_t Terrain::get_detail() const {
return this->detail;
}