DragonSystem.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. using Content.Server.Objectives.Components;
  2. using Content.Server.Objectives.Systems;
  3. using Content.Server.Popups;
  4. using Content.Server.Roles;
  5. using Content.Shared.Actions;
  6. using Content.Shared.Dragon;
  7. using Content.Shared.Maps;
  8. using Content.Shared.Mind;
  9. using Content.Shared.Mind.Components;
  10. using Content.Shared.Mobs;
  11. using Content.Shared.Mobs.Systems;
  12. using Content.Shared.Movement.Systems;
  13. using Content.Shared.NPC.Systems;
  14. using Content.Shared.Zombies;
  15. using Robust.Shared.Audio.Systems;
  16. using Robust.Shared.Map;
  17. using Robust.Shared.Map.Components;
  18. namespace Content.Server.Dragon;
  19. public sealed partial class DragonSystem : EntitySystem
  20. {
  21. [Dependency] private readonly CarpRiftsConditionSystem _carpRifts = default!;
  22. [Dependency] private readonly ITileDefinitionManager _tileDef = default!;
  23. [Dependency] private readonly MovementSpeedModifierSystem _movement = default!;
  24. [Dependency] private readonly NpcFactionSystem _faction = default!;
  25. [Dependency] private readonly PopupSystem _popup = default!;
  26. [Dependency] private readonly SharedActionsSystem _actions = default!;
  27. [Dependency] private readonly SharedAudioSystem _audio = default!;
  28. [Dependency] private readonly SharedTransformSystem _transform = default!;
  29. [Dependency] private readonly SharedMapSystem _map = default!;
  30. [Dependency] private readonly MobStateSystem _mobState = default!;
  31. private EntityQuery<CarpRiftsConditionComponent> _objQuery;
  32. /// <summary>
  33. /// Minimum distance between 2 rifts allowed.
  34. /// </summary>
  35. private const int RiftRange = 15;
  36. /// <summary>
  37. /// Radius of tiles
  38. /// </summary>
  39. private const int RiftTileRadius = 2;
  40. private const int RiftsAllowed = 3;
  41. public override void Initialize()
  42. {
  43. base.Initialize();
  44. _objQuery = GetEntityQuery<CarpRiftsConditionComponent>();
  45. SubscribeLocalEvent<DragonComponent, MapInitEvent>(OnInit);
  46. SubscribeLocalEvent<DragonComponent, ComponentShutdown>(OnShutdown);
  47. SubscribeLocalEvent<DragonComponent, DragonSpawnRiftActionEvent>(OnSpawnRift);
  48. SubscribeLocalEvent<DragonComponent, RefreshMovementSpeedModifiersEvent>(OnDragonMove);
  49. SubscribeLocalEvent<DragonComponent, MobStateChangedEvent>(OnMobStateChanged);
  50. SubscribeLocalEvent<DragonComponent, EntityZombifiedEvent>(OnZombified);
  51. }
  52. public override void Update(float frameTime)
  53. {
  54. base.Update(frameTime);
  55. var query = EntityQueryEnumerator<DragonComponent>();
  56. while (query.MoveNext(out var uid, out var comp))
  57. {
  58. if (comp.WeakenedAccumulator > 0f)
  59. {
  60. comp.WeakenedAccumulator -= frameTime;
  61. // No longer weakened.
  62. if (comp.WeakenedAccumulator < 0f)
  63. {
  64. comp.WeakenedAccumulator = 0f;
  65. _movement.RefreshMovementSpeedModifiers(uid);
  66. }
  67. }
  68. // At max rifts
  69. if (comp.Rifts.Count >= RiftsAllowed)
  70. continue;
  71. // If there's an active rift don't accumulate.
  72. if (comp.Rifts.Count > 0)
  73. {
  74. var lastRift = comp.Rifts[^1];
  75. if (TryComp<DragonRiftComponent>(lastRift, out var rift) && rift.State != DragonRiftState.Finished)
  76. {
  77. comp.RiftAccumulator = 0f;
  78. continue;
  79. }
  80. }
  81. if (!_mobState.IsDead(uid))
  82. comp.RiftAccumulator += frameTime;
  83. // Delete it, naughty dragon!
  84. if (comp.RiftAccumulator >= comp.RiftMaxAccumulator)
  85. {
  86. Roar(uid, comp);
  87. QueueDel(uid);
  88. }
  89. }
  90. }
  91. private void OnInit(EntityUid uid, DragonComponent component, MapInitEvent args)
  92. {
  93. Roar(uid, component);
  94. //_actions.AddAction(uid, ref component.SpawnRiftActionEntity, component.SpawnRiftAction);
  95. }
  96. private void OnShutdown(EntityUid uid, DragonComponent component, ComponentShutdown args)
  97. {
  98. DeleteRifts(uid, false, component);
  99. }
  100. private void OnSpawnRift(EntityUid uid, DragonComponent component, DragonSpawnRiftActionEvent args)
  101. {
  102. if (component.Weakened)
  103. {
  104. _popup.PopupEntity(Loc.GetString("carp-rift-weakened"), uid, uid);
  105. return;
  106. }
  107. if (component.Rifts.Count >= RiftsAllowed)
  108. {
  109. _popup.PopupEntity(Loc.GetString("carp-rift-max"), uid, uid);
  110. return;
  111. }
  112. if (component.Rifts.Count > 0 && TryComp<DragonRiftComponent>(component.Rifts[^1], out var rift) && rift.State != DragonRiftState.Finished)
  113. {
  114. _popup.PopupEntity(Loc.GetString("carp-rift-duplicate"), uid, uid);
  115. return;
  116. }
  117. var xform = Transform(uid);
  118. // Have to be on a grid fam
  119. if (!TryComp<MapGridComponent>(xform.GridUid, out var grid))
  120. {
  121. _popup.PopupEntity(Loc.GetString("carp-rift-anchor"), uid, uid);
  122. return;
  123. }
  124. // cant stack rifts near eachother
  125. foreach (var (_, riftXform) in EntityQuery<DragonRiftComponent, TransformComponent>(true))
  126. {
  127. if (_transform.InRange(riftXform.Coordinates, xform.Coordinates, RiftRange))
  128. {
  129. _popup.PopupEntity(Loc.GetString("carp-rift-proximity", ("proximity", RiftRange)), uid, uid);
  130. return;
  131. }
  132. }
  133. // cant put a rift on solars
  134. foreach (var tile in _map.GetTilesIntersecting(xform.GridUid.Value, grid, new Circle(_transform.GetWorldPosition(xform), RiftTileRadius), false))
  135. {
  136. if (!tile.IsSpace(_tileDef))
  137. continue;
  138. _popup.PopupEntity(Loc.GetString("carp-rift-space-proximity", ("proximity", RiftTileRadius)), uid, uid);
  139. return;
  140. }
  141. var carpUid = Spawn(component.RiftPrototype, _transform.GetMapCoordinates(uid, xform: xform));
  142. component.Rifts.Add(carpUid);
  143. Comp<DragonRiftComponent>(carpUid).Dragon = uid;
  144. }
  145. // TODO: just make this a move speed modifier component???
  146. private void OnDragonMove(EntityUid uid, DragonComponent component, RefreshMovementSpeedModifiersEvent args)
  147. {
  148. if (component.Weakened)
  149. {
  150. args.ModifySpeed(0.5f, 0.5f);
  151. }
  152. }
  153. private void OnMobStateChanged(EntityUid uid, DragonComponent component, MobStateChangedEvent args)
  154. {
  155. // Deletes all rifts after dying
  156. if (args.NewMobState != MobState.Dead)
  157. return;
  158. if (component.SoundDeath != null)
  159. _audio.PlayPvs(component.SoundDeath, uid);
  160. // objective is explicitly not reset so that it will show how many you got before dying in round end text
  161. DeleteRifts(uid, false, component);
  162. }
  163. private void OnZombified(Entity<DragonComponent> ent, ref EntityZombifiedEvent args)
  164. {
  165. // prevent carp attacking zombie dragon
  166. _faction.AddFaction(ent.Owner, ent.Comp.Faction);
  167. }
  168. private void Roar(EntityUid uid, DragonComponent comp)
  169. {
  170. if (comp.SoundRoar != null)
  171. _audio.PlayPvs(comp.SoundRoar, uid);
  172. }
  173. /// <summary>
  174. /// Delete all rifts this dragon made.
  175. /// </summary>
  176. /// <param name="uid">Entity id of the dragon</param>
  177. /// <param name="resetRole">If true, the role's rift count will be reset too</param>
  178. /// <param name="comp">The dragon component</param>
  179. public void DeleteRifts(EntityUid uid, bool resetRole, DragonComponent? comp = null)
  180. {
  181. if (!Resolve(uid, ref comp))
  182. return;
  183. foreach (var rift in comp.Rifts)
  184. {
  185. QueueDel(rift);
  186. }
  187. comp.Rifts.Clear();
  188. // stop here if not trying to reset the objective's rift count
  189. if (!resetRole || !TryComp<MindContainerComponent>(uid, out var mindContainer) || !mindContainer.HasMind)
  190. return;
  191. var mind = Comp<MindComponent>(mindContainer.Mind.Value);
  192. foreach (var objId in mind.Objectives)
  193. {
  194. if (_objQuery.TryGetComponent(objId, out var obj))
  195. {
  196. _carpRifts.ResetRifts(objId, obj);
  197. break;
  198. }
  199. }
  200. }
  201. /// <summary>
  202. /// Increment the dragon role's charged rift count.
  203. /// </summary>
  204. public void RiftCharged(EntityUid uid, DragonComponent? comp = null)
  205. {
  206. if (!Resolve(uid, ref comp))
  207. return;
  208. if (!TryComp<MindContainerComponent>(uid, out var mindContainer) || !mindContainer.HasMind)
  209. return;
  210. var mind = Comp<MindComponent>(mindContainer.Mind.Value);
  211. foreach (var objId in mind.Objectives)
  212. {
  213. if (_objQuery.TryGetComponent(objId, out var obj))
  214. {
  215. _carpRifts.RiftCharged(objId, obj);
  216. break;
  217. }
  218. }
  219. }
  220. /// <summary>
  221. /// Do everything that needs to happen when a rift gets destroyed by the crew.
  222. /// </summary>
  223. public void RiftDestroyed(EntityUid uid, DragonComponent? comp = null)
  224. {
  225. if (!Resolve(uid, ref comp))
  226. return;
  227. // do reset the rift count since crew destroyed the rift, not deleted by the dragon dying.
  228. DeleteRifts(uid, true, comp);
  229. // We can't predict the rift being destroyed anyway so no point adding weakened to shared.
  230. comp.WeakenedAccumulator = comp.WeakenedDuration;
  231. _movement.RefreshMovementSpeedModifiers(uid);
  232. _popup.PopupEntity(Loc.GetString("carp-rift-destroyed"), uid, uid);
  233. }
  234. }