feat: realtime editing
This commit is contained in:
parent
cd4f619a20
commit
fc6034242e
5 changed files with 146 additions and 12 deletions
|
|
@ -10,12 +10,17 @@ void Terrain::ready() {
|
|||
}
|
||||
|
||||
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_entered(Node *node) {
|
||||
|
|
@ -27,9 +32,21 @@ void Terrain::child_entered(Node *node) {
|
|||
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));
|
||||
}
|
||||
|
||||
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:
|
||||
|
|
@ -41,7 +58,6 @@ void Terrain::_notification(int what) {
|
|||
connect("child_order_changed", callable_mp(this, &self_type::update_modifier_list));
|
||||
}
|
||||
return;
|
||||
return;
|
||||
case NOTIFICATION_READY:
|
||||
ready();
|
||||
return;
|
||||
|
|
@ -67,6 +83,9 @@ void Terrain::construct_chunk_grid() {
|
|||
|
||||
void Terrain::generate_meshes() {
|
||||
for (TerrainChunkMesh *mesh : this->meshes) {
|
||||
if (!mesh->is_inside_tree()) {
|
||||
return;
|
||||
}
|
||||
mesh->update_mesh();
|
||||
}
|
||||
}
|
||||
|
|
@ -74,7 +93,42 @@ void Terrain::generate_meshes() {
|
|||
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();
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue