diff --git a/modules/generative_grammar/generator.cpp b/modules/generative_grammar/generator.cpp index 8782854d7c..10d527c483 100644 --- a/modules/generative_grammar/generator.cpp +++ b/modules/generative_grammar/generator.cpp @@ -1,11 +1,13 @@ #include "generator.h" #include "core/config/engine.h" #include "core/math/math_funcs.h" +#include "core/math/vector2i.h" #include "core/object/class_db.h" #include "macros.h" void Generator::_bind_methods() { BIND_HPROPERTY(Variant::OBJECT, state, PROPERTY_HINT_RESOURCE_TYPE, "Sentence"); + BIND_HPROPERTY(Variant::DICTIONARY, tiles_dict, PROPERTY_HINT_DICTIONARY_TYPE, vformat("String;PackedScene")); } void Generator::initialise_state() { @@ -42,6 +44,20 @@ void Generator::ready() { this->rule->try_apply(this->state); print_line("STATE:"); print_line(this->state->get_symbols_string()); + for (Vector2i at{ 0, 0 }; at.y < this->state->size.y; ++at.y) { + for (; at.x < this->state->size.x; ++at.x) { + Symbol s{ this->state->get_at(at) }; + if (this->tiles.has(s)) { + if (Node * node{ this->tiles.get(s)->instantiate() }) { + if (Node3D * node3d{ cast_to(node) }) { + node3d->set_position({ (float)at.x, 0, (float)at.y }); + } + this->add_child(node); + } + } + } + at.x = 0; + } } void Generator::_notification(int what) { @@ -63,3 +79,23 @@ void Generator::_notification(int what) { return; } } + +void Generator::set_tiles_dict(Dictionary dict) { + this->tiles.clear(); + for (KeyValue kvp : dict) { + String key{ kvp.key }; + Ref value{ kvp.value }; + if (key.is_empty() || value.is_null()) { + continue; + } + this->tiles.insert(key.get(0), value); + } +} + +Dictionary Generator::get_tiles_dict() const { + Dictionary dict{}; + for (TileMap::KV kvp : this->tiles) { + dict.set(String() + kvp.key, kvp.value); + } + return dict; +} diff --git a/modules/generative_grammar/generator.h b/modules/generative_grammar/generator.h index ad330ceaf7..89aa71c478 100644 --- a/modules/generative_grammar/generator.h +++ b/modules/generative_grammar/generator.h @@ -1,7 +1,9 @@ #pragma once +#include "core/templates/hash_map.h" #include "generative_grammar/grammar.h" #include "scene/3d/node_3d.h" +#include "scene/resources/packed_scene.h" class Generator : public Node3D { GDCLASS(Generator, Node3D); @@ -15,7 +17,12 @@ protected: public: Ref state{}; Rule *rule{ nullptr }; + typedef HashMap> TileMap; + TileMap tiles{}; private: GET_SET_FNS(Ref, state); + GET_SET_REF_FNS(TileMap, tiles); + void set_tiles_dict(Dictionary dict); + Dictionary get_tiles_dict() const; }; diff --git a/project/scenes/levels/dungeon.tscn b/project/scenes/levels/dungeon.tscn index 748db792a4..86264db70e 100644 --- a/project/scenes/levels/dungeon.tscn +++ b/project/scenes/levels/dungeon.tscn @@ -1,5 +1,7 @@ [gd_scene format=3 uid="uid://cak2tf2adjv8j"] +[ext_resource type="PackedScene" uid="uid://bn7d8n5mbabu3" path="res://objects/tiles/path.tscn" id="1_wail4"] + [sub_resource type="Sentence" id="Sentence_bb2w7"] size = Vector2i(20, 20) symbols_string = "uuuuuuuuuuuuuuuuuuuu @@ -23,18 +25,6 @@ uuuuuuuuuuuuuuuuuuuu uuuuuuuuuuuuuuuuuuuu uuuuuuuuuuuuuuuuuuuu" -[sub_resource type="Sentence" id="Sentence_wail4"] -size = Vector2i(3, 1) -symbols_string = "ppp" - -[sub_resource type="Sentence" id="Sentence_srduu"] -size = Vector2i(3, 1) -symbols_string = "pxp" - -[sub_resource type="Sentence" id="Sentence_w0mg0"] -size = Vector2i(3, 1) -symbols_string = "ppp" - [sub_resource type="Sentence" id="Sentence_s5a2w"] size = Vector2i(5, 3) symbols_string = "uuuuu @@ -47,9 +37,25 @@ symbols_string = "uuuuu upxpu *pup*" +[sub_resource type="Sentence" id="Sentence_wyxho"] +size = Vector2i(5, 5) +symbols_string = "uuuuu +uuuuu +*p*p* +uuuuu +uuuuu" + +[sub_resource type="Sentence" id="Sentence_q0ye3"] +size = Vector2i(5, 5) +symbols_string = "uuuuu +upxpu +*pup* +upxpu +uuuuu" + [sub_resource type="Sentence" id="Sentence_7f72c"] size = Vector2i(5, 3) -symbols_string = "*pxp* +symbols_string = "*p*p* uuuuu uuuuu" @@ -59,31 +65,55 @@ symbols_string = "*pup* upxpu uuuuu" +[sub_resource type="Sentence" id="Sentence_wail4"] +size = Vector2i(5, 3) +symbols_string = "uuuuu +up*pu +**u**" + +[sub_resource type="Sentence" id="Sentence_srduu"] +size = Vector2i(5, 3) +symbols_string = "upxpu +upupu +**u**" + [node name="Dungeon" type="Node3D" unique_id=719313039] [node name="Generator" type="Generator" parent="." unique_id=1532743122] state = SubResource("Sentence_bb2w7") +tiles_dict = { +"p": ExtResource("1_wail4"), +"x": ExtResource("1_wail4") +} [node name="RepeatRuleUntilFailure" type="RepeatRuleUntilFailure" parent="Generator" unique_id=407316032] [node name="CompositeRule" type="CompositeRule" parent="Generator/RepeatRuleUntilFailure" unique_id=117678547] random_order = true -[node name="ReplaceRule" type="ReplaceRule" parent="Generator/RepeatRuleUntilFailure/CompositeRule" unique_id=1787373751] -pattern = SubResource("Sentence_wail4") -results_dict = { -SubResource("Sentence_srduu"): 1.0, -SubResource("Sentence_w0mg0"): 2.0 -} - -[node name="ReplaceRule2" type="ReplaceRule" parent="Generator/RepeatRuleUntilFailure/CompositeRule" unique_id=1073931523] +[node name="ZigUp" type="ReplaceRule" parent="Generator/RepeatRuleUntilFailure/CompositeRule" unique_id=1073931523] pattern = SubResource("Sentence_s5a2w") results_dict = { SubResource("Sentence_yc5ro"): 1.0 } -[node name="ReplaceRule3" type="ReplaceRule" parent="Generator/RepeatRuleUntilFailure/CompositeRule" unique_id=1665850421] +[node name="Split" type="ReplaceRule" parent="Generator/RepeatRuleUntilFailure/CompositeRule" unique_id=222066055] +pattern = SubResource("Sentence_wyxho") +results_dict = { +SubResource("Sentence_q0ye3"): 1.0 +} + +[node name="ZagDown" type="ReplaceRule" parent="Generator/RepeatRuleUntilFailure/CompositeRule" unique_id=1665850421] pattern = SubResource("Sentence_7f72c") results_dict = { SubResource("Sentence_pbld3"): 1.0 } + +[node name="ExtendLoop" type="ReplaceRule" parent="Generator/RepeatRuleUntilFailure/CompositeRule" unique_id=1207681043] +pattern = SubResource("Sentence_wail4") +results_dict = { +SubResource("Sentence_srduu"): 1.0 +} + +[node name="Camera3D" type="Camera3D" parent="Generator" unique_id=1193344185] +transform = Transform3D(-0.8008847, 0.5054234, -0.32113996, 0, 0.5362893, 0.84403425, 0.5988186, 0.67597413, -0.42950585, 0, 10.870714, 0)