/// <summary>
/// Spawns another prototype to be shot instead of itself.
/// </summary>
-[RegisterComponent, NetworkedComponent, ComponentReference(typeof(AmmoComponent))]
-public sealed class CartridgeAmmoComponent : AmmoComponent
+[RegisterComponent, NetworkedComponent, ComponentReference(typeof(AmmoComponent)), AutoGenerateComponentState]
+public sealed partial class CartridgeAmmoComponent : AmmoComponent
{
[ViewVariables(VVAccess.ReadWrite), DataField("proto", required: true, customTypeSerializer: typeof(PrototypeIdSerializer<EntityPrototype>))]
public string Prototype = default!;
[ViewVariables(VVAccess.ReadWrite), DataField("spent")]
+ [AutoNetworkedField]
public bool Spent = false;
/// <summary>
namespace Content.Shared.Weapons.Ranged.Components;
-[RegisterComponent, NetworkedComponent]
-public sealed class BallisticAmmoProviderComponent : Component
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class BallisticAmmoProviderComponent : Component
{
[ViewVariables(VVAccess.ReadWrite), DataField("soundRack")]
public SoundSpecifier? SoundRack = new SoundPathSpecifier("/Audio/Weapons/Guns/Cock/smg_cock.ogg");
public int Capacity = 30;
[ViewVariables(VVAccess.ReadWrite), DataField("unspawnedCount")]
+ [AutoNetworkedField]
public int UnspawnedCount;
[ViewVariables(VVAccess.ReadWrite), DataField("whitelist")]
// TODO: Make this use stacks when the typeserializer is done.
[DataField("entities")]
+ [AutoNetworkedField(true)]
public List<EntityUid> Entities = new();
/// <summary>
/// Is the gun ready to shoot; if AutoCycle is true then this will always stay true and not need to be manually done.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("cycled")]
+ [AutoNetworkedField]
public bool Cycled = true;
/// <summary>
-using Robust.Shared.Prototypes;
+using Robust.Shared.GameStates;
+using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
/// Simply provides a certain capacity of entities that cannot be reloaded through normal means and have
/// no special behavior like cycling, magazine
/// </summary>
-[RegisterComponent]
-public sealed class BasicEntityAmmoProviderComponent : AmmoProviderComponent
+[RegisterComponent, AutoGenerateComponentState]
+public sealed partial class BasicEntityAmmoProviderComponent : AmmoProviderComponent
{
[ViewVariables(VVAccess.ReadWrite)]
[DataField("proto", required: true, customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("capacity")]
+ [AutoNetworkedField]
public int? Capacity = null;
/// <summary>
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("count")]
+ [AutoNetworkedField]
public int? Count = null;
}
-
-[Serializable, NetSerializable]
-public sealed class BasicEntityAmmoProviderComponentState : ComponentState
-{
- public int? Capacity;
- public int? Count;
-
- public BasicEntityAmmoProviderComponentState(int? capacity, int? count)
- {
- Capacity = capacity;
- Count = count;
- }
-}
/// <summary>
/// Plays a sound when its non-hard fixture collides with a player.
/// </summary>
-[RegisterComponent, NetworkedComponent]
-public sealed class FlyBySoundComponent : Component
+[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
+public sealed partial class FlyBySoundComponent : Component
{
/// <summary>
/// Probability that the sound plays
public float Prob = 0.10f;
[ViewVariables(VVAccess.ReadWrite), DataField("sound")]
+ [AutoNetworkedField]
public SoundSpecifier Sound = new SoundCollectionSpecifier("BulletMiss")
{
Params = AudioParams.Default,
};
- [DataField("range")] public float Range = 1.5f;
+ [DataField("range")]
+ [AutoNetworkedField]
+ public float Range = 1.5f;
}
namespace Content.Shared.Weapons.Ranged.Components;
[RegisterComponent, NetworkedComponent, Virtual]
-public class GunComponent : Component
+[AutoGenerateComponentState]
+public partial class GunComponent : Component
{
#region Sound
/// What the current spread is for shooting. This gets changed every time the gun fires.
/// </summary>
[DataField("currentAngle")]
+ [AutoNetworkedField]
public Angle CurrentAngle;
/// <summary>
/// The maximum angle allowed for <see cref="CurrentAngle"/>
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("maxAngle")]
+ [AutoNetworkedField]
public Angle MaxAngle = Angle.FromDegrees(2);
/// <summary>
/// The minimum angle allowed for <see cref="CurrentAngle"/>
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("minAngle")]
+ [AutoNetworkedField]
public Angle MinAngle = Angle.FromDegrees(1);
#endregion
/// Used for tracking semi-auto / burst
/// </summary>
[ViewVariables]
+ [AutoNetworkedField]
public int ShotCounter = 0;
/// <summary>
/// How many times it shoots per second.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("fireRate")]
+ [AutoNetworkedField]
public float FireRate = 8f;
/// <summary>
/// Can be set multiple times in a single tick due to guns firing faster than a single tick time.
/// </summary>
[DataField("nextFire", customTypeSerializer:typeof(TimeOffsetSerializer))]
+ [AutoNetworkedField]
public TimeSpan NextFire = TimeSpan.Zero;
/// <summary>
/// What firemodes can be selected.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("availableModes")]
+ [AutoNetworkedField]
public SelectiveFire AvailableModes = SelectiveFire.SemiAuto;
/// <summary>
/// What firemode is currently selected.
/// </summary>
[ViewVariables(VVAccess.ReadWrite), DataField("selectedMode")]
+ [AutoNetworkedField]
public SelectiveFire SelectedMode = SelectiveFire.SemiAuto;
[DataField("selectModeAction")]
public override void Initialize()
{
base.Initialize();
- SubscribeLocalEvent<FlyBySoundComponent, ComponentGetState>(OnGetState);
- SubscribeLocalEvent<FlyBySoundComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<FlyBySoundComponent, ComponentStartup>(OnStartup);
SubscribeLocalEvent<FlyBySoundComponent, ComponentShutdown>(OnShutdown);
}
_fixtures.DestroyFixture(uid, FlyByFixture, body: body);
}
-
- private void OnHandleState(EntityUid uid, FlyBySoundComponent component, ref ComponentHandleState args)
- {
- if (args.Current is not FlyBySoundComponentState state) return;
-
- component.Sound = state.Sound;
- component.Range = state.Range;
- }
-
- private void OnGetState(EntityUid uid, FlyBySoundComponent component, ref ComponentGetState args)
- {
- args.State = new FlyBySoundComponentState()
- {
- Sound = component.Sound,
- Range = component.Range,
- };
- }
-
- [Serializable, NetSerializable]
- private sealed class FlyBySoundComponentState : ComponentState
- {
- public SoundSpecifier Sound = default!;
- public float Range;
- }
}
SubscribeLocalEvent<BallisticAmmoProviderComponent, MapInitEvent>(OnBallisticMapInit);
SubscribeLocalEvent<BallisticAmmoProviderComponent, TakeAmmoEvent>(OnBallisticTakeAmmo);
SubscribeLocalEvent<BallisticAmmoProviderComponent, GetAmmoCountEvent>(OnBallisticAmmoCount);
- SubscribeLocalEvent<BallisticAmmoProviderComponent, ComponentGetState>(OnBallisticGetState);
- SubscribeLocalEvent<BallisticAmmoProviderComponent, ComponentHandleState>(OnBallisticHandleState);
SubscribeLocalEvent<BallisticAmmoProviderComponent, ExaminedEvent>(OnBallisticExamine);
SubscribeLocalEvent<BallisticAmmoProviderComponent, GetVerbsEvent<Verb>>(OnBallisticVerb);
protected abstract void Cycle(BallisticAmmoProviderComponent component, MapCoordinates coordinates);
- private void OnBallisticGetState(EntityUid uid, BallisticAmmoProviderComponent component, ref ComponentGetState args)
- {
- args.State = new BallisticAmmoProviderComponentState()
- {
- UnspawnedCount = component.UnspawnedCount,
- Entities = component.Entities,
- Cycled = component.Cycled,
- };
- }
-
- private void OnBallisticHandleState(EntityUid uid, BallisticAmmoProviderComponent component, ref ComponentHandleState args)
- {
- if (args.Current is not BallisticAmmoProviderComponentState state)
- return;
-
- component.Cycled = state.Cycled;
- component.UnspawnedCount = state.UnspawnedCount;
-
- component.Entities.Clear();
-
- foreach (var ent in state.Entities)
- {
- component.Entities.Add(ent);
- }
- }
-
private void OnBallisticInit(EntityUid uid, BallisticAmmoProviderComponent component, ComponentInit args)
{
component.Container = Containers.EnsureContainer<Container>(uid, "ballistic-ammo");
Appearance.SetData(uid, AmmoVisuals.AmmoCount, GetBallisticShots(component), appearance);
Appearance.SetData(uid, AmmoVisuals.AmmoMax, component.Capacity, appearance);
}
-
- [Serializable, NetSerializable]
- private sealed class BallisticAmmoProviderComponentState : ComponentState
- {
- public int UnspawnedCount;
- public List<EntityUid> Entities = default!;
- public bool Cycled;
- }
}
SubscribeLocalEvent<BasicEntityAmmoProviderComponent, ComponentInit>(OnBasicEntityInit);
SubscribeLocalEvent<BasicEntityAmmoProviderComponent, TakeAmmoEvent>(OnBasicEntityTakeAmmo);
SubscribeLocalEvent<BasicEntityAmmoProviderComponent, GetAmmoCountEvent>(OnBasicEntityAmmoCount);
-
- SubscribeLocalEvent<BasicEntityAmmoProviderComponent, ComponentGetState>(OnBasicEntityGetState);
- SubscribeLocalEvent<BasicEntityAmmoProviderComponent, ComponentHandleState>(OnBasicEntityHandleState);
- }
-
- private void OnBasicEntityGetState(EntityUid uid, BasicEntityAmmoProviderComponent component, ref ComponentGetState args)
- {
- args.State = new BasicEntityAmmoProviderComponentState(component.Capacity, component.Count);
- }
-
- private void OnBasicEntityHandleState(EntityUid uid, BasicEntityAmmoProviderComponent component, ref ComponentHandleState args)
- {
- if (args.Current is BasicEntityAmmoProviderComponentState state)
- {
- component.Capacity = state.Capacity;
- component.Count = state.Count;
- }
}
private void OnBasicEntityInit(EntityUid uid, BasicEntityAmmoProviderComponent component, ComponentInit args)
public abstract partial class SharedGunSystem
{
+ // needed for server system
protected virtual void InitializeCartridge()
{
- SubscribeLocalEvent<CartridgeAmmoComponent, ComponentGetState>(OnCartridgeGetState);
- SubscribeLocalEvent<CartridgeAmmoComponent, ComponentHandleState>(OnCartridgeHandleState);
- }
-
- private void OnCartridgeHandleState(EntityUid uid, CartridgeAmmoComponent component, ref ComponentHandleState args)
- {
- if (args.Current is not CartridgeAmmoComponentState state)
- return;
-
- component.Spent = state.Spent;
- }
-
- private void OnCartridgeGetState(EntityUid uid, CartridgeAmmoComponent component, ref ComponentGetState args)
- {
- args.State = new CartridgeAmmoComponentState()
- {
- Spent = component.Spent,
- };
- }
-
- [Serializable, NetSerializable]
- private sealed class CartridgeAmmoComponentState : ComponentState
- {
- public bool Spent;
}
}
{
Sawmill = Logger.GetSawmill("gun");
Sawmill.Level = LogLevel.Info;
- SubscribeLocalEvent<GunComponent, ComponentGetState>(OnGetState);
SubscribeAllEvent<RequestShootEvent>(OnShootRequest);
SubscribeAllEvent<RequestStopShootEvent>(OnStopShootRequest);
- SubscribeLocalEvent<GunComponent, ComponentHandleState>(OnHandleState);
SubscribeLocalEvent<GunComponent, MeleeAttackAttemptEvent>(OnGunMeleeAttempt);
// Ammo providers
StopShooting(ev.Gun, gun);
}
- private void OnGetState(EntityUid uid, GunComponent component, ref ComponentGetState args)
- {
- args.State = new GunComponentState
- {
- FireRate = component.FireRate,
- CurrentAngle = component.CurrentAngle,
- MinAngle = component.MinAngle,
- MaxAngle = component.MaxAngle,
- NextFire = component.NextFire,
- ShotCounter = component.ShotCounter,
- SelectiveFire = component.SelectedMode,
- AvailableSelectiveFire = component.AvailableModes,
- };
- }
-
- private void OnHandleState(EntityUid uid, GunComponent component, ref ComponentHandleState args)
- {
- if (args.Current is not GunComponentState state)
- return;
-
- Sawmill.Debug($"Handle state: setting shot count from {component.ShotCounter} to {state.ShotCounter}");
- component.FireRate = state.FireRate;
- component.CurrentAngle = state.CurrentAngle;
- component.MinAngle = state.MinAngle;
- component.MaxAngle = state.MaxAngle;
- component.NextFire = state.NextFire;
- component.ShotCounter = state.ShotCounter;
- component.SelectedMode = state.SelectiveFire;
- component.AvailableModes = state.AvailableSelectiveFire;
- }
-
public bool CanShoot(GunComponent component)
{
if (component.NextFire > Timing.CurTime)
}
protected abstract void CreateEffect(EntityUid uid, MuzzleFlashEvent message, EntityUid? user = null);
- [Serializable, NetSerializable]
- protected sealed class GunComponentState : ComponentState
- {
- public Angle CurrentAngle;
- public Angle MinAngle;
- public Angle MaxAngle;
- public TimeSpan NextFire;
- public float FireRate;
- public int ShotCounter;
- public SelectiveFire SelectiveFire;
- public SelectiveFire AvailableSelectiveFire;
- }
-
/// <summary>
/// Used for animated effects on the client.
/// </summary>