Refactors throw events, makes cream pies ECS (#4500)

This commit is contained in:
Vera Aguilera Puerto
2021-08-21 09:18:23 +02:00
committed by GitHub
parent 140682f92b
commit ea4ce1c6fc
19 changed files with 287 additions and 260 deletions

View File

@@ -0,0 +1,27 @@
using Content.Shared.Nutrition.EntitySystems;
using Content.Shared.Sound;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Nutrition.Components
{
[Friend(typeof(SharedCreamPieSystem))]
[RegisterComponent]
public class CreamPieComponent : Component
{
public override string Name => "CreamPie";
[ViewVariables]
[DataField("paralyzeTime")]
public float ParalyzeTime { get; } = 1f;
[ViewVariables]
[DataField("sound")]
public SoundSpecifier Sound { get; } = new SoundCollectionSpecifier("desecration");
[ViewVariables]
public bool Splatted { get; set; } = false;
}
}

View File

@@ -0,0 +1,25 @@
using System;
using Content.Shared.Nutrition.EntitySystems;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Content.Shared.Nutrition.Components
{
[Friend(typeof(SharedCreamPieSystem))]
[RegisterComponent]
public class CreamPiedComponent : Component
{
public override string Name => "CreamPied";
[ViewVariables]
public bool CreamPied { get; set; } = false;
}
[Serializable, NetSerializable]
public enum CreamPiedVisuals
{
Creamed,
}
}

View File

@@ -1,17 +0,0 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization;
namespace Content.Shared.Nutrition.Components
{
public class SharedCreamPiedComponent : Component
{
public override string Name => "CreamPied";
}
[Serializable, NetSerializable]
public enum CreamPiedVisuals
{
Creamed,
}
}

View File

@@ -0,0 +1,75 @@
using Content.Shared.Nutrition.Components;
using Content.Shared.Stunnable;
using Content.Shared.Throwing;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Content.Shared.Nutrition.EntitySystems
{
[UsedImplicitly]
public abstract class SharedCreamPieSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CreamPieComponent, ThrowDoHitEvent>(OnCreamPieHit);
SubscribeLocalEvent<CreamPieComponent, LandEvent>(OnCreamPieLand);
SubscribeLocalEvent<CreamPiedComponent, ThrowHitByEvent>(OnCreamPiedHitBy);
}
public void SplatCreamPie(EntityUid uid, CreamPieComponent creamPie)
{
// Already splatted! Do nothing.
if (creamPie.Splatted)
return;
creamPie.Splatted = true;
SplattedCreamPie(uid, creamPie);
EntityManager.QueueDeleteEntity(uid);
}
protected virtual void SplattedCreamPie(EntityUid uid, CreamPieComponent creamPie) {}
public void SetCreamPied(EntityUid uid, CreamPiedComponent creamPied, bool value)
{
if (value == creamPied.CreamPied)
return;
creamPied.CreamPied = value;
if (ComponentManager.TryGetComponent(uid, out SharedAppearanceComponent? appearance))
{
appearance.SetData(CreamPiedVisuals.Creamed, value);
}
}
private void OnCreamPieLand(EntityUid uid, CreamPieComponent component, LandEvent args)
{
SplatCreamPie(uid, component);
}
private void OnCreamPieHit(EntityUid uid, CreamPieComponent component, ThrowDoHitEvent args)
{
SplatCreamPie(uid, component);
}
private void OnCreamPiedHitBy(EntityUid uid, CreamPiedComponent creamPied, ThrowHitByEvent args)
{
if (args.Thrown.Deleted || !args.Thrown.TryGetComponent(out CreamPieComponent? creamPie)) return;
SetCreamPied(uid, creamPied, true);
CreamedEntity(uid, creamPied, args);
if (ComponentManager.TryGetComponent(uid, out SharedStunnableComponent? stun))
{
stun.Paralyze(creamPie.ParalyzeTime);
}
}
protected virtual void CreamedEntity(EntityUid uid, CreamPiedComponent creamPied, ThrowHitByEvent args) {}
}
}

View File

@@ -1,63 +0,0 @@
using System;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
namespace Content.Shared.Throwing
{
[RequiresExplicitImplementation]
public interface IThrowCollide
{
void HitBy(ThrowCollideEventArgs eventArgs) {}
void DoHit(ThrowCollideEventArgs eventArgs) {}
}
public class ThrowCollideEventArgs : EventArgs
{
/// <summary>
/// The entity that threw <see cref="Thrown"/> and hit <see cref="Target"/>.
/// </summary>
public IEntity? User { get; }
/// <summary>
/// The entity thrown by <see cref="User"/> that hit <see cref="Target"/>
/// </summary>
public IEntity Thrown { get; }
/// <summary>
/// The entity hit with <see cref="Thrown"/> by <see cref="User"/>
/// </summary>
public IEntity Target { get; }
public ThrowCollideEventArgs(IEntity? user, IEntity thrown, IEntity target)
{
User = user;
Thrown = thrown;
Target = target;
}
}
public class ThrowCollideEvent : HandledEntityEventArgs
{
/// <summary>
/// The entity that threw <see cref="Thrown"/>.
/// </summary>
public IEntity? User { get; }
/// <summary>
/// The entity thrown by <see cref="User"/> that hit <see cref="Target"/>
/// </summary>
public IEntity Thrown { get; }
/// <summary>
/// The entity hit with <see cref="Thrown"/> by <see cref="User"/>
/// </summary>
public IEntity Target { get; }
public ThrowCollideEvent(IEntity? user, IEntity thrown, IEntity target)
{
User = user;
Thrown = thrown;
Target = target;
}
}
}

View File

@@ -0,0 +1,55 @@
using System;
using JetBrains.Annotations;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
namespace Content.Shared.Throwing
{
/// <summary>
/// Base class for all throw events.
/// </summary>
public abstract class ThrowEvent : HandledEntityEventArgs
{
/// <summary>
/// The entity that threw <see cref="Thrown"/>.
/// </summary>
public IEntity? User { get; }
/// <summary>
/// The entity thrown by <see cref="User"/> that hit <see cref="Target"/>
/// </summary>
public IEntity Thrown { get; }
/// <summary>
/// The entity hit with <see cref="Thrown"/> by <see cref="User"/>
/// </summary>
public IEntity Target { get; }
public ThrowEvent(IEntity? user, IEntity thrown, IEntity target)
{
User = user;
Thrown = thrown;
Target = target;
}
}
/// <summary>
/// Raised directed on the target entity being hit by the thrown entity.
/// </summary>
public class ThrowHitByEvent : ThrowEvent
{
public ThrowHitByEvent(IEntity? user, IEntity thrown, IEntity target) : base(user, thrown, target)
{
}
}
/// <summary>
/// Raised directed on the thrown entity that hits another.
/// </summary>
public class ThrowDoHitEvent : ThrowEvent
{
public ThrowDoHitEvent(IEntity? user, IEntity thrown, IEntity target) : base(user, thrown, target)
{
}
}
}

View File

@@ -14,8 +14,6 @@ namespace Content.Shared.Throwing
/// </summary>
public class ThrownItemSystem : EntitySystem
{
private List<IThrowCollide> _throwCollide = new();
private const string ThrowingFixture = "throw-fixture";
public override void Initialize()
@@ -117,46 +115,13 @@ namespace Content.Shared.Throwing
}
/// <summary>
/// Calls ThrowCollide on all components that implement the IThrowCollide interface
/// on a thrown entity and the target entity it hit.
/// Raises collision events on the thrown and target entities.
/// </summary>
public void ThrowCollideInteraction(IEntity? user, IPhysBody thrown, IPhysBody target)
{
// TODO: Just pass in the bodies directly
var collideMsg = new ThrowCollideEvent(user, thrown.Owner, target.Owner);
RaiseLocalEvent(target.Owner.Uid, collideMsg);
if (collideMsg.Handled)
{
return;
}
var eventArgs = new ThrowCollideEventArgs(user, thrown.Owner, target.Owner);
foreach (var comp in target.Owner.GetAllComponents<IThrowCollide>())
{
_throwCollide.Add(comp);
}
foreach (var collide in _throwCollide)
{
if (target.Owner.Deleted) break;
collide.HitBy(eventArgs);
}
_throwCollide.Clear();
foreach (var comp in thrown.Owner.GetAllComponents<IThrowCollide>())
{
_throwCollide.Add(comp);
}
foreach (var collide in _throwCollide)
{
if (thrown.Owner.Deleted) break;
collide.DoHit(eventArgs);
}
_throwCollide.Clear();
RaiseLocalEvent(target.Owner.Uid, new ThrowHitByEvent(user, thrown.Owner, target.Owner));
RaiseLocalEvent(thrown.Owner.Uid, new ThrowDoHitEvent(user, thrown.Owner, target.Owner));
}
}
}