|
| 1 | +using Robust.Shared.Random; |
| 2 | +using Robust.Server.GameObjects; |
| 3 | +using Robust.Shared.Containers; |
| 4 | +using Content.Shared.SS220.MinorFauna.Actions; |
| 5 | +using Content.Shared.SS220.MinorFauna.Events; |
| 6 | +using Content.Shared.SS220.MinorFauna.Components; |
| 7 | +using Content.Shared.Mobs.Systems; |
| 8 | + |
| 9 | +namespace Content.Server.SS220.MinorFauna; |
| 10 | + |
| 11 | +public abstract class SharedMinorFaunaSystem : EntitySystem |
| 12 | +{ |
| 13 | + [Dependency] private readonly IRobustRandom _random = default!; |
| 14 | + [Dependency] private readonly TransformSystem _transform = default!; |
| 15 | + [Dependency] private readonly MobStateSystem _mobState = default!; |
| 16 | + [Dependency] private readonly SharedContainerSystem _container = default!; |
| 17 | + |
| 18 | + public override void Initialize() |
| 19 | + { |
| 20 | + base.Initialize(); |
| 21 | + SubscribeLocalEvent<CocoonerComponent, AfterEntityCocooningEvent>(OnAfterEntityCocooningEvent); |
| 22 | + } |
| 23 | + |
| 24 | + private void OnAfterEntityCocooningEvent(Entity<CocoonerComponent> entity, ref AfterEntityCocooningEvent args) |
| 25 | + { |
| 26 | + if (args.Cancelled || args.Target is not EntityUid target) |
| 27 | + return; |
| 28 | + |
| 29 | + if (!TryComp<TransformComponent>(target, out var transform) || !_mobState.IsDead(target)) |
| 30 | + return; |
| 31 | + var targetCords = _transform.GetMoverCoordinates(target, transform); |
| 32 | + var cocoonPrototypeID = _random.Pick(entity.Comp.CocoonPrototypes); |
| 33 | + var cocoonUid = Spawn(cocoonPrototypeID, targetCords); |
| 34 | + |
| 35 | + if (!TryComp<EntityCocoonComponent>(cocoonUid, out var cocoon) || |
| 36 | + !_container.TryGetContainer(cocoonUid, cocoon.CocoonContainerId, out var container)) |
| 37 | + { |
| 38 | + Log.Error($"{cocoonUid} doesn't have required components to cocooning target"); |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + _container.Insert(target, container); |
| 43 | + } |
| 44 | + |
| 45 | +} |
0 commit comments