Bracket System

This commit is contained in:
h z
2024-09-26 10:29:27 +01:00
parent a9afe94ebc
commit 67dd8ac8dc
25 changed files with 266 additions and 92 deletions

View File

@@ -7,7 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="GodotSharp" Version="4.3.0-beta.3" />
<PackageReference Include="GodotSharp" Version="4.4.0-dev.2" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,11 @@
using Nocturnis.DataStructures.States;
namespace Nocturnis.Controls;
public interface IStateControl
{
string this[string path] { get; set; }
void Load();
void Save();
public void AddRule(string path, StateTransferRule rule);
}

View File

@@ -0,0 +1,35 @@
namespace Nocturnis.DataStructures;
public class DoubleKeyDictionary<TKey, TValue> : Dictionary<TKey, Dictionary<TKey, TValue>>
{
public DoubleKeyDictionary(TValue defaultValue)
{
DefaultValue = defaultValue;
}
private TValue DefaultValue { get; set; }
public TValue this[TKey pk1, TKey pk2]
{
get
{
try
{
return this[pk1][pk2];
}
catch (KeyNotFoundException)
{
return DefaultValue;
}
}
set
{
if(!ContainsKey(pk1))
this[pk1] = new Dictionary<TKey, TValue>();
this[pk1][pk2] = value;
}
}
public bool ContainsKey(TKey pk1, TKey pk2) => ContainsKey(pk1) && this[pk1].ContainsKey(pk2);
}

View File

@@ -0,0 +1,3 @@
namespace Nocturnis.DataStructures.States;
public delegate bool StateTransferRule (string path, string fromState, string toState);

View File

@@ -37,7 +37,7 @@ public class EnigmosControl
EngineUp = true;
}
public double Energy { get; set; } = 0d;
public double Energy { get; set; } = 1d;
public bool EngineUp { get; private set; }
public double IdlePower => CreatureControl.Instance.CurrentCharacter.MotherBoard.IdlePower + VoidPower;
public double VoidPower { get; set; } = 0;

View File

@@ -0,0 +1,76 @@
using System.Text.Json;
using Nocturnis.Controls;
using Nocturnis.DataStructures.States;
using FileAccess = Godot.FileAccess;
namespace Nocturnis.GlobalManagement.Controls;
public sealed class StateControl
{
public const string NullState = "NullState";
private HashSet<StateTransferRule> Rules { get; set; }
public Dictionary<string, string> States { get; set; }
private StateControl()
{
States = new Dictionary<string, string>();
Rules = new HashSet<StateTransferRule>();
}
private static StateControl _instance = null;
public static StateControl Instance => _instance ??= new StateControl();
public void Load()
{
if (!FileAccess.FileExists("States.json"))
return;
using FileAccess file = FileAccess.Open("States.json", FileAccess.ModeFlags.Read);
string jsonData = file.GetAsText();
States = JsonSerializer.Deserialize<Dictionary<string, string>>(jsonData);
}
public void Save()
{
string jsonData = JsonSerializer.Serialize(States);
using FileAccess file = FileAccess.Open("States.json", FileAccess.ModeFlags.Write);
file.StoreString(jsonData);
}
public bool Transfer(string path, string state)
{
foreach (var rule in Rules)
if (rule(path, States[path], state))
{
States[path] = state;
return true;
}
return false;
}
public string this[string path]
{
get
{
try
{
return States[path];
}
catch (KeyNotFoundException)
{
return NullState;
}
}
set
{
if(!States.ContainsKey(path))
States[path] = NullState;
Transfer(path, value);
}
}
public void AddRule(StateTransferRule rule)
{
Rules.Add(rule);
}
}

View File

@@ -0,0 +1,70 @@
using System.Text.Json;
using Nocturnis.GlobalManagement.Providers;
using Nocturnis.Hermeteus.BracketSystem;
namespace Nocturnis.GlobalManagement.Controls;
public class TopicControl
{
private static TopicControl _instance;
public static TopicControl Instance => _instance ??= new TopicControl();
public HashSet<IBracketTopic> ActiveTopics { get; set; }
public Dictionary<string, IBracketTopic> Topics { get; set; }
private TopicControl()
{
ActiveTopics = new HashSet<IBracketTopic>();
Topics = new Dictionary<string, IBracketTopic>();
}
public void Load(string topicName)
{
string jsonContent = GlobalProvider.FileAccessProvider.Read($"res://Resources/Topics/{topicName}.json");
TopicData topicData = JsonSerializer.Deserialize<TopicData>(jsonContent);
IBracketTopic topic = GlobalProvider.ConstructorProvider.NewBracketTopic();
topic.Topic = topicData.topic;
topic.Reusable = topicData.reusable;
topic.Finished = false;
topic.LineMap = new Dictionary<string, IBracketLine>();
foreach (var lineData in topicData.lines)
{
IBracketLine line = GlobalProvider.ConstructorProvider.NewBracketLine();
line.Conditions = lineData.conditions;
line.Line = lineData.line;
line.Successors = new HashSet<IBracketLine>();
topic.LineMap[lineData.line] = line;
line.BraLines = lineData.braLines;
line.KetLines = lineData.ketLines;
line.ArrowMarkers = lineData.arrows;
line.CleanPrevious = lineData.cleanPrevious;
line.Topic = topicData.topic;
}
foreach (LineData lineData in topicData.lines)
{
IBracketLine line = topic.LineMap[lineData.line];
foreach (string successorId in lineData.successors)
line.Successors.Add(topic.LineMap[successorId]);
}
topic.Root = topic.LineMap[topicData.lines[0].line];
topic.CurrentLine = topic.Root;
Topics.Add(topic.Topic, topic);
}
private class TopicData
{
public bool reusable { get; set; }
public string topic { get; set; }
public LineData[] lines { get; set; }
}
private class LineData
{
public bool cleanPrevious { get; set; }
public string line { get; set; }
public string[] conditions { get; set; }
public string[] successors { get; set; }
public string[] braLines { get; set; }
public string[] ketLines { get; set; }
public string[] arrows { get; set; }
}
}

View File

@@ -1,5 +1,4 @@
using Godot;
using Nocturnis.DataStructures.DataTypes;
using Nocturnis.Enigmos.Modules;
using Nocturnis.Inventories.Items;
@@ -15,6 +14,10 @@ public static class GlobalProvider
public static IDataTypeProvider DataTypeProvider { get; set; }
public static IProcessProvider ProcessProvider { get; set; }
public static ITextureProvider TextureProvider { get; set; }
public static IConstructorProvider ConstructorProvider { get; set; }
public static IFileAccessProvider FileAccessProvider { get; set; }
public static class ModulePreviewMapper<TModule>
where TModule : IBaseModule

View File

@@ -0,0 +1,9 @@
using Nocturnis.Hermeteus.BracketSystem;
namespace Nocturnis.GlobalManagement.Providers;
public interface IConstructorProvider
{
IBracketTopic NewBracketTopic();
IBracketLine NewBracketLine();
}

View File

@@ -0,0 +1,10 @@
using FileAccess = Godot.FileAccess;
namespace Nocturnis.GlobalManagement.Providers;
public interface IFileAccessProvider
{
public string Read(string path);
public void Write(string path, string content);
public string ResRead(string path);
}

View File

@@ -0,0 +1,15 @@
namespace Nocturnis.Hermeteus.BracketSystem;
public interface IBracketLine
{
string Topic { get; set; }
string Line { get; set; }
bool CleanPrevious { get; set; }
string[] BraLines { get; set; }
string[] KetLines { get; set; }
string[] ArrowMarkers { get; set; }
string[] Conditions { get; set; }
HashSet<IBracketLine> Successors { get; set; }
bool IsReady { get; }
}

View File

@@ -0,0 +1,13 @@
namespace Nocturnis.Hermeteus.BracketSystem;
public interface IBracketTopic
{
bool Reusable { get; set; }
string Topic { get; set; }
bool Finished { get; set; }
IBracketLine Root { get; set; }
IBracketLine CurrentLine { get; set; }
void Activate();
void Finish();
Dictionary<string, IBracketLine> LineMap { get; set; }
}

View File

@@ -1,4 +1,4 @@
namespace Nocturnis.Hermeteus;
namespace Nocturnis.Hermeteus.BracketSystem;
public interface ICondition
{

View File

@@ -1,13 +1,13 @@
using Godot;
using Nocturnis.UIElements.Layers;
namespace Nocturnis.Hermeteus;
namespace Nocturnis.Hermeteus.BracketSystem;
public interface IInstructionAgent : INodeInterface
{
void Load(string content, Action callBack);
void Clean(Action callBack);
void LoadArrows(Vector2[] ends);
void LoadArrows(string[] ends);
void CleanArrows();
void Init(IInstructionLayer insLayer);
}
}

View File

@@ -1,6 +1,4 @@
using Nocturnis.UIElements;
namespace Nocturnis.Hermeteus.Processors;
namespace Nocturnis.Hermeteus.BracketSystem.Processors;
public interface IBracketProcessor
{
@@ -13,5 +11,5 @@ public interface IBracketProcessor
double BracketEsc { get; set; }
string Status { get; set; }
void Init(IInstructionAgent bra, IInstructionAgent pointer, IInstructionAgent ket);
void SyncLoad(IBracketTalk talk);
void SyncLoad(IBracketLine talk);
}

View File

@@ -1,16 +0,0 @@
namespace Nocturnis.Hermeteus;
public static class EBracketRouter
{
public static IBracketRouter RouteAndSkip(this IBracketRouter router)
{
IBracketRouter pr = router.Route();
while (pr is IBracketTalk { CanSkip: true } prt)
{
prt.Finished = true;
pr = pr.RouteAndSkip();
}
return pr;
}
}

View File

@@ -1,14 +0,0 @@
using Skeleton.DataStructure.Tree;
namespace Nocturnis.Hermeteus;
public interface IBracketChapter : IBracketRouter
{
IBracketStory Story { get; set; }
string State { get; set; }
bool Finished { get; set; }
string Chapter { get; set; }
IBracketTopic CurrentTopic { get; set; }
HashSet<IBracketTopic> Children { get; set; }
HashSet<ICondition> Conditions { get; set; }
}

View File

@@ -1,6 +0,0 @@
namespace Nocturnis.Hermeteus;
public interface IBracketRouter
{
IBracketRouter Route();
}

View File

@@ -1,12 +0,0 @@
using Nocturnis.Hermeteus.Processors;
using Skeleton.DataStructure.Tree;
namespace Nocturnis.Hermeteus;
public interface IBracketStory : IBracketRouter
{
IBracketChapter CurrentChapter { get; set; }
IBracketChapter[] Children { get; set; }
IBracketRouter CurrentRouter { get; set; }
bool Process(IBracketProcessor processor);
}

View File

@@ -1,18 +0,0 @@
using Godot;
namespace Nocturnis.Hermeteus;
public interface IBracketTalk : IBracketRouter
{
IBracketTopic Topic { get; set; }
string TriggerStatus { get; set; }
string EnterStatus { get; set; }
double TriggerEsc { get; set; }
bool CleanPrevious { get; set; }
string[] BraLines { get; set; }
string[] KetLines { get; set; }
HashSet<ICondition> Conditions { get; set; }
Vector2[] ArrowMarkers { get; set; }
bool Finished { get; set; }
bool CanSkip { get; set; }
}

View File

@@ -1,13 +0,0 @@
using Skeleton.DataStructure.Tree;
namespace Nocturnis.Hermeteus;
public interface IBracketTopic : IBracketRouter
{
IBracketChapter Chapter { get; set; }
string Topic { get; set; }
bool Finished { get; set; }
HashSet<IBracketTalk> Children { get; set; }
IBracketTalk CurrentTalk { get; set; }
HashSet<ICondition> Conditions { get; set; }
}

View File

@@ -0,0 +1,8 @@
using Godot;
namespace Nocturnis.Hermeteus.QuerySystem;
public interface IQueryEngine
{
bool Check(string query);
}

View File

@@ -7,4 +7,5 @@ public interface INodeInterface
void RemoveChild(Node node);
void AddChild(Node node, bool forceReadableName = false, Node.InternalMode @internal = (Node.InternalMode)(0));
bool Visible { get; set; }
Node GetNode(NodePath path);
}

View File

@@ -5,7 +5,7 @@ using Nocturnis.UIElements.Layers;
namespace Nocturnis.Scenes;
public interface IRootScene
public interface IRootScene : INodeInterface
{
void ChangeScene(Node scene);
IKeyListener KeyListener { get; set; }

View File

@@ -1,5 +1,6 @@
using Nocturnis.Hermeteus;
using Nocturnis.Hermeteus.Processors;
using Nocturnis.Hermeteus.BracketSystem;
using Nocturnis.Hermeteus.BracketSystem.Processors;
namespace Nocturnis.UIElements.Layers;