Add EventManager and event handling system; implement event triggering and popup display
This commit is contained in:
22
Scripts/Core/EventDefinition.cs
Normal file
22
Scripts/Core/EventDefinition.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot.Collections;
|
||||
using ParasiticGod.Scripts.Core.Effects;
|
||||
|
||||
namespace ParasiticGod.Scripts.Core;
|
||||
|
||||
public class EventDefinition
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string Title { get; set; }
|
||||
public string Description { get; set; }
|
||||
public int MeanTimeToHappen { get; set; }
|
||||
public EventTriggerDto Trigger { get; set; }
|
||||
public List<EventOptionDefinition> Options { get; set; } = [];
|
||||
}
|
||||
|
||||
public class EventOptionDefinition
|
||||
{
|
||||
public string Text { get; set; }
|
||||
public string Tooltip { get; set; }
|
||||
public Array<Effect> Effects { get; set; }
|
||||
}
|
1
Scripts/Core/EventDefinition.cs.uid
Normal file
1
Scripts/Core/EventDefinition.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ilyr01u70ciw
|
46
Scripts/Core/EventDto.cs
Normal file
46
Scripts/Core/EventDto.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using System.Collections.Generic;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace ParasiticGod.Scripts.Core;
|
||||
|
||||
public class EventTriggerDto
|
||||
{
|
||||
[JsonProperty("minFollowers")]
|
||||
public long MinFollowers { get; set; } = 0;
|
||||
|
||||
[JsonProperty("maxCorruption")]
|
||||
public double MaxCorruption { get; set; } = 100;
|
||||
}
|
||||
|
||||
public class EventOptionDto
|
||||
{
|
||||
[JsonProperty("text")]
|
||||
public string Text { get; set; }
|
||||
|
||||
[JsonProperty("tooltip")]
|
||||
public string Tooltip { get; set; }
|
||||
|
||||
[JsonProperty("effects")]
|
||||
public List<EffectDto> Effects { get; set; }
|
||||
}
|
||||
|
||||
public class EventDto
|
||||
{
|
||||
[JsonProperty("id")]
|
||||
public string Id { get; set; }
|
||||
|
||||
[JsonProperty("title")]
|
||||
public string Title { get; set; }
|
||||
|
||||
[JsonProperty("description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
[JsonProperty("meanTimeToHappen")]
|
||||
public int MeanTimeToHappen { get; set; } = 100; // in game days
|
||||
|
||||
[JsonProperty("trigger")]
|
||||
public EventTriggerDto Trigger { get; set; }
|
||||
|
||||
[JsonProperty("options")]
|
||||
public List<EventOptionDto> Options { get; set; }
|
||||
}
|
1
Scripts/Core/EventDto.cs.uid
Normal file
1
Scripts/Core/EventDto.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://nwwqn028soa5
|
71
Scripts/Core/EventLoader.cs
Normal file
71
Scripts/Core/EventLoader.cs
Normal file
@@ -0,0 +1,71 @@
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace ParasiticGod.Scripts.Core;
|
||||
|
||||
public static class EventLoader
|
||||
{
|
||||
public static List<EventDefinition> LoadAllEvents()
|
||||
{
|
||||
var loadedEvents = new Dictionary<string, EventDefinition>();
|
||||
|
||||
LoadEventsFromPath("res://Mods/Events", loadedEvents);
|
||||
LoadEventsFromPath("user://Mods/Events", loadedEvents);
|
||||
|
||||
GD.Print($"Finished loading. Total unique events: {loadedEvents.Count}");
|
||||
return new List<EventDefinition>(loadedEvents.Values);
|
||||
}
|
||||
|
||||
private static void LoadEventsFromPath(string path, Dictionary<string, EventDefinition> events)
|
||||
{
|
||||
if (!DirAccess.DirExistsAbsolute(path)) return;
|
||||
|
||||
using var dir = DirAccess.Open(path);
|
||||
dir.ListDirBegin();
|
||||
var fileName = dir.GetNext();
|
||||
while (!string.IsNullOrEmpty(fileName))
|
||||
{
|
||||
if (!dir.CurrentIsDir() && fileName.EndsWith(".json"))
|
||||
{
|
||||
var filePath = path.PathJoin(fileName);
|
||||
var ev = LoadEventFromFile(filePath);
|
||||
if (ev != null)
|
||||
{
|
||||
events[ev.Id] = ev; // Add or overwrite
|
||||
}
|
||||
}
|
||||
fileName = dir.GetNext();
|
||||
}
|
||||
}
|
||||
|
||||
private static EventDefinition LoadEventFromFile(string filePath)
|
||||
{
|
||||
var fileContent = FileAccess.GetFileAsString(filePath);
|
||||
if (string.IsNullOrEmpty(fileContent)) return null;
|
||||
|
||||
var dto = JsonConvert.DeserializeObject<EventDto>(fileContent);
|
||||
if (dto == null) return null;
|
||||
|
||||
var eventDef = new EventDefinition
|
||||
{
|
||||
Id = dto.Id,
|
||||
Title = dto.Title,
|
||||
Description = dto.Description,
|
||||
MeanTimeToHappen = dto.MeanTimeToHappen,
|
||||
Trigger = dto.Trigger
|
||||
};
|
||||
|
||||
foreach (var optionDto in dto.Options)
|
||||
{
|
||||
var optionDef = new EventOptionDefinition
|
||||
{
|
||||
Text = optionDto.Text,
|
||||
Tooltip = optionDto.Tooltip,
|
||||
Effects = MiracleLoader.ConvertEffectDtos(optionDto.Effects)
|
||||
};
|
||||
eventDef.Options.Add(optionDef);
|
||||
}
|
||||
return eventDef;
|
||||
}
|
||||
}
|
1
Scripts/Core/EventLoader.cs.uid
Normal file
1
Scripts/Core/EventLoader.cs.uid
Normal file
@@ -0,0 +1 @@
|
||||
uid://ceimix3ejnadj
|
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Godot;
|
||||
using Godot.Collections;
|
||||
using Newtonsoft.Json;
|
||||
@@ -86,7 +87,17 @@ public static class MiracleLoader
|
||||
Effects = []
|
||||
};
|
||||
|
||||
foreach (var effectDto in miracleDto.Effects)
|
||||
var effects = ConvertEffectDtos(miracleDto.Effects);
|
||||
miracleDef.Effects = effects;
|
||||
return miracleDef;
|
||||
}
|
||||
|
||||
public static Array<Effect> ConvertEffectDtos(List<EffectDto> dtos)
|
||||
{
|
||||
var effects = new Array<Effect>();
|
||||
if (dtos == null) return effects;
|
||||
|
||||
foreach (var effectDto in dtos)
|
||||
{
|
||||
if (EffectRegistry.TryGetValue(effectDto.Type, out var effectType))
|
||||
{
|
||||
@@ -118,11 +129,9 @@ public static class MiracleLoader
|
||||
unlockMiracleEffect.MiraclesToUnlock = new Array<string>(effectDto.MiraclesToUnlock);
|
||||
break;
|
||||
}
|
||||
|
||||
miracleDef.Effects.Add(effectInstance);
|
||||
effects.Add(effectInstance);
|
||||
}
|
||||
}
|
||||
|
||||
return miracleDef;
|
||||
return effects;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user