1
0

CivResearchSystem.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using Robust.Shared.Map;
  2. using Robust.Shared.Timing;
  3. namespace Content.Shared.Civ14.CivResearch;
  4. public sealed partial class CivResearchSystem : EntitySystem
  5. {
  6. [Dependency] private readonly IGameTiming _timing = default!;
  7. [Dependency] private readonly IMapManager _mapManager = default!;
  8. [Dependency] private readonly ILogManager _logManager = default!;
  9. private ISawmill _sawmill = default!;
  10. /// <summary>
  11. /// Sets up the research system by subscribing to map creation events and initialising the research logger.
  12. /// </summary>
  13. public override void Initialize()
  14. {
  15. base.Initialize();
  16. SubscribeLocalEvent<MapCreatedEvent>(OnMapCreated);
  17. _sawmill = _logManager.GetSawmill("research");
  18. }
  19. /// <summary>
  20. /// Ensures that a CivResearchComponent is attached to the entity representing a newly created map.
  21. /// </summary>
  22. /// <param name="ev">The event containing information about the newly created map.</param>
  23. private void OnMapCreated(MapCreatedEvent ev)
  24. {
  25. var mapUid = _mapManager.GetMapEntityId(ev.MapId);
  26. EnsureComp<CivResearchComponent>(mapUid);
  27. _sawmill.Info("research", $"Ensured ResearchComponent on new map {ev.MapId} (Entity: {mapUid})");
  28. }
  29. /// <summary>
  30. /// Advances research progression on all active maps by incrementing their research level if research is enabled and not yet at the maximum.
  31. /// </summary>
  32. /// <param name="frameTime">Elapsed time since the last update, in seconds.</param>
  33. public override void Update(float frameTime)
  34. {
  35. base.Update(frameTime);
  36. // Iterate through all active maps
  37. foreach (var mapId in _mapManager.GetAllMapIds())
  38. {
  39. // Get the entity UID associated with the map
  40. var mapUid = _mapManager.GetMapEntityId(mapId);
  41. // Try to get the ResearchComponent from the map's entity UID
  42. if (TryComp<CivResearchComponent>(mapUid, out var comp))
  43. {
  44. // Now run your logic
  45. if (!comp.ResearchEnabled)
  46. continue;
  47. // Use frameTime for frame-rate independent accumulation
  48. // comp.ResearchLevel += comp.ResearchSpeed * frameTime * Timing.TickRate; // More robust way
  49. // Or keep the original logic if ResearchSpeed is per-tick
  50. if (comp.ResearchLevel >= comp.MaxResearch)
  51. {
  52. continue;
  53. }
  54. comp.ResearchLevel += comp.ResearchSpeed;
  55. // Mark component dirty if necessary (often handled automatically for networked components)
  56. Dirty(mapUid, comp);
  57. }
  58. }
  59. }
  60. }