ManiaMap.Godot
Procedural generation of metroidvania style maps for Godot .NET.
RoomTemplateResource.cs
1using Godot;
2using MPewsey.Common.Serialization;
3using MPewsey.ManiaMap;
5using System.Threading.Tasks;
6
8{
12 [Tool]
13 [GlobalClass]
14 public partial class RoomTemplateResource : Resource
15 {
16 private bool _editId;
20 [Export] public bool EditId { get => _editId; set => SetValidatedField(ref _editId, value); }
21
25 [Export] public int Id { get; set; } = Rand.GetRandomId();
26
30 [Export] public string TemplateName { get; set; } = "<None>";
31
35 [Export] public string ScenePath { get; set; }
36
40 [Export] public string SceneUidPath { get; set; }
41
45 [ExportGroup("Serialized Text")]
46 [Export(PropertyHint.MultilineText)] public string SerializedText { get; set; }
47
48 private void SetValidatedField<T>(ref T field, T value)
49 {
50 field = value;
51 NotifyPropertyListChanged();
52 }
53
54 public override void _ValidateProperty(Godot.Collections.Dictionary property)
55 {
56 base._ValidateProperty(property);
57 var name = property["name"].AsStringName();
58
59 if (name == PropertyName.ScenePath || name == PropertyName.SceneUidPath || name == PropertyName.SerializedText)
60 {
61 property["usage"] = (int)(property["usage"].As<PropertyUsageFlags>() | PropertyUsageFlags.ReadOnly);
62 }
63 else if (name == PropertyName.Id)
64 {
65 var flag = EditId ? PropertyUsageFlags.None : PropertyUsageFlags.ReadOnly;
66 property["usage"] = (int)(PropertyUsageFlags.Default | flag);
67 }
68 }
69
74 public void Initialize(IRoomNode room)
75 {
76 var node = (Node)room;
77 var scenePath = node.SceneFilePath;
78 var sceneUidPath = ResourceUid.IdToText(ResourceLoader.GetResourceUid(scenePath));
79 var template = room.GetMMRoomTemplate(Id, TemplateName);
80
81 SerializedText = JsonSerialization.GetJsonString(template, new JsonWriterSettings());
82 ScenePath = scenePath;
83 SceneUidPath = sceneUidPath;
84 }
85
90 public RoomTemplate GetMMRoomTemplate()
91 {
92 if (string.IsNullOrWhiteSpace(SerializedText))
93 throw new RoomTemplateNotInitializedException($"Serialized text has not been assigned: {ResourcePath}");
94
95 return JsonSerialization.LoadJsonString<RoomTemplate>(SerializedText);
96 }
97
104 public string GetScenePath()
105 {
106 if (string.IsNullOrWhiteSpace(SceneUidPath) || string.IsNullOrWhiteSpace(ScenePath))
107 throw new RoomTemplateNotInitializedException($"Scene path has not been assigned: {ResourcePath}");
108
109 if (ResourceLoader.Exists(SceneUidPath))
110 return SceneUidPath;
111
112 if (ResourceLoader.Exists(ScenePath))
113 return ScenePath;
114
115 throw new PathDoesNotExistException($"Scene paths do not exist: (SceneUidPath = {SceneUidPath}, ScenePath = {ScenePath})");
116 }
117
121 public PackedScene LoadScene()
122 {
123 return ResourceLoader.Load<PackedScene>(GetScenePath());
124 }
125
129 public Task<PackedScene> LoadSceneAsync(bool useSubThreads = false)
130 {
131 return AsyncResourceLoader.LoadAsync<PackedScene>(GetScenePath(), string.Empty, useSubThreads);
132 }
133 }
134}
Contains methods for loading Resources asynchronously.
Contains the random number generator used for ID and random seed assignments.
Definition: Rand.cs:10
static int GetRandomId()
Returns a random positive integer ID.
Definition: Rand.cs:26
Provides a reference to a room scene and information for the room required by the procedural generato...
string SceneUidPath
The associated scene's uid:// path.
void Initialize(IRoomNode room)
Sets the scene paths and serialized text to the room template based on the specified room.
string GetScenePath()
Returns the controlling associated scene path. The uid:// path is prioritized first,...
string SerializedText
The serialized text for the ManiaMap room template.
string ScenePath
The associated scene's path.
RoomTemplate GetMMRoomTemplate()
Returns the ManiaMap room template used by the procedural generator.
PackedScene LoadScene()
Loads and returns the referenced scene.
bool EditId
If true, the Id property becomes editable in the inspector.
Task< PackedScene > LoadSceneAsync(bool useSubThreads=false)
Asynchronously loads and returns the referenced scene.
int Id
The room template's unique ID.
The interface for a room node.
Definition: IRoomNode.cs:9