From 78c711950a0ec4fb434a0da8202199ccfd1aff0d Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Sun, 15 Dec 2024 00:40:43 +1000 Subject: [PATCH] FUUUUCK --- .../_CP14/Fishing/CP14FishingOverlay.cs | 8 +- .../_CP14/Fishing/CP14FishingProcessSystem.cs | 5 + .../Fishing}/CP14FishingProcessSystem.cs | 168 ++++++++---------- .../_CP14/Fishing/CP14FishingRodSystem.cs | 30 +++- .../Components/CP14FishingProcessComponent.cs | 9 +- .../Components/CP14FishingRodComponent.cs | 3 + .../_CP14/Fishing/Core/Behaviors/Behavior.cs | 6 +- .../Fishing/Core/Behaviors/MixedBehavior.cs | 2 + Content.Shared/_CP14/Fishing/Core/Fish.cs | 6 + .../Events/CP14FishingFinishedEvent.cs | 12 ++ .../CP14SharedFishingProcessSystem.Utils.cs | 38 ++++ .../Systems/CP14SharedFishingProcessSystem.cs | 38 ++++ .../Systems/CP14SharedFishingRodSystem.cs | 19 +- .../Prototypes/_CP14/Fishing/lootTables.yml | 2 +- 14 files changed, 230 insertions(+), 116 deletions(-) create mode 100644 Content.Client/_CP14/Fishing/CP14FishingProcessSystem.cs rename {Content.Shared/_CP14/Fishing/Systems => Content.Server/_CP14/Fishing}/CP14FishingProcessSystem.cs (57%) create mode 100644 Content.Shared/_CP14/Fishing/Events/CP14FishingFinishedEvent.cs create mode 100644 Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.Utils.cs create mode 100644 Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs diff --git a/Content.Client/_CP14/Fishing/CP14FishingOverlay.cs b/Content.Client/_CP14/Fishing/CP14FishingOverlay.cs index 54720de5aa..99c939f2ea 100644 --- a/Content.Client/_CP14/Fishing/CP14FishingOverlay.cs +++ b/Content.Client/_CP14/Fishing/CP14FishingOverlay.cs @@ -22,7 +22,7 @@ public sealed class CP14FishingOverlay : Overlay private readonly SpriteSystem _sprite; private readonly TransformSystem _transform; - private readonly CP14FishingProcessSystem _fishingProcess; + private readonly CP14SharedFishingProcessSystem _sharedFishingProcess; private Texture _backgroundTexture = default!; private Texture _handleTopTexture = default!; @@ -48,7 +48,7 @@ public sealed class CP14FishingOverlay : Overlay _sprite = _entity.System(); _transform = _entity.System(); - _fishingProcess = _entity.System(); + _sharedFishingProcess = _entity.System(); } protected override void Draw(in OverlayDrawArgs args) @@ -56,7 +56,7 @@ public sealed class CP14FishingOverlay : Overlay if (_player.LocalEntity is not { } localEntity) return; - if (!_fishingProcess.TryGetByUser(localEntity, out var fishingProcess)) + if (!_sharedFishingProcess.TryGetByUser(localEntity, out var fishingProcess)) return; // Refresh the texture cache, with a new fishing process @@ -100,7 +100,7 @@ public sealed class CP14FishingOverlay : Overlay private void DrawProgress(DrawingHandleWorld handle, Vector2 position, Vector2 size, float progress) { var vectorA = position; - var vectorB = position + size with { Y = size.Y * progress }; + var vectorB = position + new Vector2(size.X, size.Y * progress); var box = new Box2(vectorA, vectorB); handle.DrawRect(box, Color.Aqua); diff --git a/Content.Client/_CP14/Fishing/CP14FishingProcessSystem.cs b/Content.Client/_CP14/Fishing/CP14FishingProcessSystem.cs new file mode 100644 index 0000000000..a8cf2b6943 --- /dev/null +++ b/Content.Client/_CP14/Fishing/CP14FishingProcessSystem.cs @@ -0,0 +1,5 @@ +using Content.Shared._CP14.Fishing.Systems; + +namespace Content.Client._CP14.Fishing; + +public sealed class CP14FishingProcessSystem : CP14SharedFishingProcessSystem; diff --git a/Content.Shared/_CP14/Fishing/Systems/CP14FishingProcessSystem.cs b/Content.Server/_CP14/Fishing/CP14FishingProcessSystem.cs similarity index 57% rename from Content.Shared/_CP14/Fishing/Systems/CP14FishingProcessSystem.cs rename to Content.Server/_CP14/Fishing/CP14FishingProcessSystem.cs index 90e37868b8..1458adfd11 100644 --- a/Content.Shared/_CP14/Fishing/Systems/CP14FishingProcessSystem.cs +++ b/Content.Server/_CP14/Fishing/CP14FishingProcessSystem.cs @@ -1,31 +1,21 @@ -using System.Diagnostics.CodeAnalysis; -using Content.Shared._CP14.Fishing.Components; +using Content.Shared._CP14.Fishing.Components; using Content.Shared._CP14.Fishing.Core; using Content.Shared._CP14.Fishing.Core.Behaviors; -using Content.Shared._CP14.Fishing.Prototypes; -using Robust.Shared.Prototypes; +using Content.Shared._CP14.Fishing.Events; +using Content.Shared._CP14.Fishing.Systems; +using Content.Shared.Throwing; using Robust.Shared.Random; using Robust.Shared.Timing; -namespace Content.Shared._CP14.Fishing.Systems; +namespace Content.Server._CP14.Fishing; -public sealed class CP14FishingProcessSystem : EntitySystem +public sealed class CP14FishingProcessSystem : CP14SharedFishingProcessSystem { [Dependency] private readonly IGameTiming _timing = default!; - [Dependency] private readonly IPrototypeManager _prototype = default!; [Dependency] private readonly IRobustRandom _random = default!; - + [Dependency] private readonly ThrowingSystem _throwing = default!; [Dependency] private readonly CP14FishingPoolSystem _pool = default!; - private EntityQuery _fishingRod; - - public override void Initialize() - { - base.Initialize(); - - _fishingRod = GetEntityQuery(); - } - public override void Update(float frameTime) { base.Update(frameTime); @@ -37,20 +27,47 @@ public sealed class CP14FishingProcessSystem : EntitySystem } } - private void Update(Entity process, float frameTime) + public Entity Start( + Entity fishingRod, + Entity fishingPool, + EntityUid user) { - var fishingRod = GetRod(process); + var process = CreateProcess(fishingRod); + var loot = _pool.GetLootPrototypeId(fishingPool); + var style = GetStyle(fishingRod); - UpdateReeling(process, fishingRod, frameTime); - UpdateVelocity(process, fishingRod); - UpdatePosition(process, frameTime); + // Save entities + process.Comp.FishingRod = fishingRod; + process.Comp.FishingPool = fishingPool; + process.Comp.User = user; - process.Comp.Fish.Update(frameTime); + process.Comp.Player = new Player(fishingRod.Comp.Size); + process.Comp.Fish = new Fish(new MixedBehavior(), _random, _timing); - var collides = Collide(process.Comp.Fish, process.Comp.Player); + process.Comp.LootProtoId = loot; + process.Comp.StyleSheet = style; - var progressAdditive = collides ? 0.05f : -0.1f; - process.Comp.Progress = Math.Clamp(process.Comp.Progress + progressAdditive * frameTime, 0, 1); + Dirty(process); + + Log.Debug($"Started new fishing process at {process}"); + return process; + } + + public void Stop(Entity process) + { + var rod = GetRod(process); + rod.Comp.Process = null; + Dirty(rod); + + Del(process); + } + + public Entity CreateProcess(EntityUid parent) + { + var entityUid = SpawnAttachedTo("MobHuman", Transform(parent).Coordinates); + var ensureComponent = AddComp(entityUid); + + return (entityUid, ensureComponent); } private void UpdateReeling(Entity process, @@ -84,79 +101,52 @@ public sealed class CP14FishingProcessSystem : EntitySystem process.Comp.Player.Position = Math.Clamp(process.Comp.Player.Position, halfSize, 1 - halfSize); } - public bool TryGetByUser(EntityUid userEntityUid, [NotNullWhen(true)] out Entity? process) + private void Update(Entity process, float frameTime) { - process = null; + var fishingRod = GetRod(process); - var query = EntityQueryEnumerator(); - while (query.MoveNext(out var entityUid, out var processComponent)) + UpdateReeling(process, fishingRod, frameTime); + UpdateVelocity(process, fishingRod); + UpdatePosition(process, frameTime); + + process.Comp.Fish.Update(frameTime); + + var collides = Collide(process.Comp.Fish, process.Comp.Player); + + var progressAdditive = collides ? 0.1f : -0.2f; + process.Comp.Progress = Math.Clamp(process.Comp.Progress + progressAdditive * frameTime, 0, 1); + + Dirty(process); + + //if (process.Comp.Progress is 1 or 0) + // Finish(process, process.Comp.Progress is 1); + } + + private void Finish(Entity process, bool success) + { + if (success) { - if (processComponent.User != userEntityUid) - continue; - - process = (entityUid, processComponent); - return true; + Reward(process); } - return false; + // Raising events before deletion + var ev = new CP14FishingFinishedEvent(success); + RaiseLocalEvent(process, ref ev, true); + + // Either way, we need to delete the process + Stop(process); } - public void Finish(Entity process) + private void Reward(Entity process) { + var pool = GetPool(process); + var rod = GetRod(process); - } + var coordinates = Transform(pool).Coordinates; + var targetCoordinates = Transform(process.Comp.User!.Value).Coordinates; - public Entity Start( - Entity fishingRod, - Entity fishingPool, - EntityUid user) - { - var process = CreateProcess(); - var loot = _pool.GetLootPrototypeId(fishingPool); - var style = GetStyle(fishingRod); + var loot = Spawn(process.Comp.LootProtoId, coordinates); - // Save entities - process.Comp.FishingRod = fishingRod; - process.Comp.FishingPool = fishingPool; - process.Comp.User = user; - - process.Comp.Player = new Player(fishingRod.Comp.Size); - process.Comp.Fish = new Fish(new MixedBehavior(), _random, _timing); - - process.Comp.LootProtoId = loot; - process.Comp.StyleSheet = style; - - return process; - } - - private Entity CreateProcess() - { - var entityUid = Spawn(); - var ensureComponent = EnsureComp(entityUid); - - return (entityUid, ensureComponent); - } - - private Entity GetRod(Entity process) - { - var entityUid = process.Comp.FishingRod!.Value; - var component = _fishingRod.GetComponent(process.Comp.FishingRod!.Value); - return (entityUid, component); - } - - private CP14FishingProcessStyleSheetPrototype GetStyle(Entity fishingRod) - { - if (_prototype.TryIndex(fishingRod.Comp.Style, out var style)) - return style; - - Log.Error($"Failed to retrieve fishing rod style, {fishingRod.Comp.Style} not found. Reverting to default style."); - return _prototype.Index(CP14FishingRodComponent.DefaultStyle); - } - - private static bool Collide(Fish fish, Player player) - { - var playerMin = player.Position - player.HalfSize; - var playerMax = player.Position + player.HalfSize; - return fish.Position >= playerMin && fish.Position <= playerMax; + _throwing.TryThrow(loot, targetCoordinates, rod.Comp.ThrowPower); } } diff --git a/Content.Server/_CP14/Fishing/CP14FishingRodSystem.cs b/Content.Server/_CP14/Fishing/CP14FishingRodSystem.cs index d4daa3bac3..3dc01500f7 100644 --- a/Content.Server/_CP14/Fishing/CP14FishingRodSystem.cs +++ b/Content.Server/_CP14/Fishing/CP14FishingRodSystem.cs @@ -1,5 +1,31 @@ -using Content.Shared._CP14.Fishing.Systems; +using Content.Shared._CP14.Fishing.Components; +using Content.Shared._CP14.Fishing.Systems; +using Content.Shared.Interaction; namespace Content.Server._CP14.Fishing; -public sealed class CP14FishingRodSystem : CP14SharedFishingRodSystem; +public sealed class CP14FishingRodSystem : CP14SharedFishingRodSystem +{ + [Dependency] private readonly CP14FishingProcessSystem _fishingProcess = default!; + + public override void Initialize() + { + base.Initialize(); + + SubscribeLocalEvent(OnAfterInteractUsing); + } + + private void OnAfterInteractUsing(Entity entity, ref AfterInteractUsingEvent args) + { + if (args.Handled || !args.CanReach) + return; + + if (!TryComp(args.Used, out var fishingRodComponent)) + return; + + if (fishingRodComponent.Process is not null) + return; + + fishingRodComponent.Process = _fishingProcess.Start((args.Used, fishingRodComponent), entity, args.User); + } +} diff --git a/Content.Shared/_CP14/Fishing/Components/CP14FishingProcessComponent.cs b/Content.Shared/_CP14/Fishing/Components/CP14FishingProcessComponent.cs index 91ec7f22ce..7bc37ba3bd 100644 --- a/Content.Shared/_CP14/Fishing/Components/CP14FishingProcessComponent.cs +++ b/Content.Shared/_CP14/Fishing/Components/CP14FishingProcessComponent.cs @@ -7,7 +7,8 @@ using Robust.Shared.Prototypes; namespace Content.Shared._CP14.Fishing.Components; -[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, Access(typeof(CP14FishingProcessSystem))] +[Access(typeof(CP14SharedFishingProcessSystem))] +[RegisterComponent, NetworkedComponent, AutoGenerateComponentState] public sealed partial class CP14FishingProcessComponent : Component { /** @@ -32,13 +33,13 @@ public sealed partial class CP14FishingProcessComponent : Component */ [ViewVariables, AutoNetworkedField] - public EntityUid? FishingRod; + public EntityUid? FishingRod = null; [ViewVariables, AutoNetworkedField] - public EntityUid? User; + public EntityUid? User = null; [ViewVariables, AutoNetworkedField] - public EntityUid? FishingPool; + public EntityUid? FishingPool = null; /** * Loot diff --git a/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs b/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs index 5c9f61f4ee..13022a1cde 100644 --- a/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs +++ b/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs @@ -37,6 +37,9 @@ public sealed partial class CP14FishingRodComponent : Component [DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] public float Size = 0.25f; + [DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] + public float ThrowPower = 10f; + [DataField, ViewVariables(VVAccess.ReadWrite)] public ProtoId Style = DefaultStyle; } diff --git a/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs b/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs index aaaed81606..aff58cd3aa 100644 --- a/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs +++ b/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs @@ -1,15 +1,17 @@ using JetBrains.Annotations; using Robust.Shared.Random; +using Robust.Shared.Serialization; namespace Content.Shared._CP14.Fishing.Core.Behaviors; [ImplicitDataDefinitionForInheritors, MeansImplicitUse] +[Serializable, NetSerializable] public abstract partial class Behavior { - [DataField] + [DataField, ViewVariables(VVAccess.ReadWrite)] public float Speed { get; set; } = 0.05f; - [DataField] + [DataField, ViewVariables(VVAccess.ReadWrite)] public float Difficulty { get; set; } = 2f; public abstract float CalculateSpeed(IRobustRandom random); diff --git a/Content.Shared/_CP14/Fishing/Core/Behaviors/MixedBehavior.cs b/Content.Shared/_CP14/Fishing/Core/Behaviors/MixedBehavior.cs index c3282cafe8..cb47293cbf 100644 --- a/Content.Shared/_CP14/Fishing/Core/Behaviors/MixedBehavior.cs +++ b/Content.Shared/_CP14/Fishing/Core/Behaviors/MixedBehavior.cs @@ -1,7 +1,9 @@ using Robust.Shared.Random; +using Robust.Shared.Serialization; namespace Content.Shared._CP14.Fishing.Core.Behaviors; +[Serializable, NetSerializable] public sealed partial class MixedBehavior : Behavior { public override float CalculateSpeed(IRobustRandom random) diff --git a/Content.Shared/_CP14/Fishing/Core/Fish.cs b/Content.Shared/_CP14/Fishing/Core/Fish.cs index 3b15030f64..eddccc6dc4 100644 --- a/Content.Shared/_CP14/Fishing/Core/Fish.cs +++ b/Content.Shared/_CP14/Fishing/Core/Fish.cs @@ -11,13 +11,19 @@ public sealed class Fish private const float MaxPosition = 1f; private const float MinPosition = 0f; + [ViewVariables(VVAccess.ReadWrite)] public float Position { get; private set; } + [ViewVariables(VVAccess.ReadWrite)] private readonly Behavior _behavior; + private readonly IRobustRandom _random; private readonly IGameTiming _timing; + [ViewVariables(VVAccess.ReadWrite)] private float _speed; + + [ViewVariables(VVAccess.ReadWrite)] private TimeSpan _updateSpeedTime; public Fish(Behavior behavior, IRobustRandom random, IGameTiming timing) diff --git a/Content.Shared/_CP14/Fishing/Events/CP14FishingFinishedEvent.cs b/Content.Shared/_CP14/Fishing/Events/CP14FishingFinishedEvent.cs new file mode 100644 index 0000000000..065383885d --- /dev/null +++ b/Content.Shared/_CP14/Fishing/Events/CP14FishingFinishedEvent.cs @@ -0,0 +1,12 @@ +namespace Content.Shared._CP14.Fishing.Events; + +[ByRefEvent] +public readonly struct CP14FishingFinishedEvent +{ + public readonly bool Success; + + public CP14FishingFinishedEvent(bool success) + { + Success = success; + } +} diff --git a/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.Utils.cs b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.Utils.cs new file mode 100644 index 0000000000..80f3c78498 --- /dev/null +++ b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.Utils.cs @@ -0,0 +1,38 @@ +using Content.Shared._CP14.Fishing.Components; +using Content.Shared._CP14.Fishing.Core; +using Content.Shared._CP14.Fishing.Prototypes; + +namespace Content.Shared._CP14.Fishing.Systems; + +public abstract partial class CP14SharedFishingProcessSystem +{ + protected Entity GetRod(Entity process) + { + var entityUid = process.Comp.FishingRod!.Value; + var component = FishingRod.GetComponent(entityUid); + return (entityUid, component); + } + + protected Entity GetPool(Entity process) + { + var entityUid = process.Comp.FishingPool!.Value; + var component = FishingPool.GetComponent(entityUid); + return (entityUid, component); + } + + protected CP14FishingProcessStyleSheetPrototype GetStyle(Entity fishingRod) + { + if (_prototype.TryIndex(fishingRod.Comp.Style, out var style)) + return style; + + Log.Error($"Failed to retrieve fishing rod style, {fishingRod.Comp.Style} not found. Reverting to default style."); + return _prototype.Index(CP14FishingRodComponent.DefaultStyle); + } + + protected static bool Collide(Fish fish, Player player) + { + var playerMin = player.Position - player.HalfSize; + var playerMax = player.Position + player.HalfSize; + return fish.Position >= playerMin && fish.Position <= playerMax; + } +} diff --git a/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs new file mode 100644 index 0000000000..4750b36473 --- /dev/null +++ b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs @@ -0,0 +1,38 @@ +using System.Diagnostics.CodeAnalysis; +using Content.Shared._CP14.Fishing.Components; +using Robust.Shared.Prototypes; + +namespace Content.Shared._CP14.Fishing.Systems; + +public abstract partial class CP14SharedFishingProcessSystem : EntitySystem +{ + [Dependency] private readonly IPrototypeManager _prototype = default!; + + protected EntityQuery FishingRod; + protected EntityQuery FishingPool; + + public override void Initialize() + { + base.Initialize(); + + FishingRod = GetEntityQuery(); + FishingPool = GetEntityQuery(); + } + + public bool TryGetByUser(EntityUid userEntityUid, [NotNullWhen(true)] out Entity? process) + { + process = null; + + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var entityUid, out var processComponent)) + { + if (processComponent.User != userEntityUid) + continue; + + process = (entityUid, processComponent); + return true; + } + + return false; + } +} diff --git a/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingRodSystem.cs b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingRodSystem.cs index 4d96e62cf2..3d9f429ac7 100644 --- a/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingRodSystem.cs +++ b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingRodSystem.cs @@ -1,21 +1,18 @@ using Content.Shared._CP14.Fishing.Components; +using Content.Shared.Hands; using Content.Shared.Hands.Components; -using Content.Shared.Interaction; using Robust.Shared.Serialization; namespace Content.Shared._CP14.Fishing.Systems; public abstract class CP14SharedFishingRodSystem : EntitySystem { - [Dependency] private readonly CP14FishingProcessSystem _fishingProcess = default!; - public override void Initialize() { base.Initialize(); SubscribeAllEvent(OnReel); - - SubscribeLocalEvent(OnAfterInteractUsing); + SubscribeLocalEvent(OnDeselected); } private void OnReel(RequestFishingRodReelMessage msg, EntitySessionEventArgs args) @@ -35,18 +32,12 @@ public abstract class CP14SharedFishingRodSystem : EntitySystem Dirty(hands.ActiveHandEntity.Value, fishingRodComponent); } - private void OnAfterInteractUsing(Entity entity, ref AfterInteractUsingEvent args) + private void OnDeselected(Entity entity, ref HandDeselectedEvent args) { - if (args.Handled || !args.CanReach) - return; - - if (!TryComp(args.Used, out var fishingRodComponent)) - return; - - fishingRodComponent.Process = _fishingProcess.Start((args.Used, fishingRodComponent), entity, args.User); + entity.Comp.Reeling = false; + Dirty(entity); } - [Serializable, NetSerializable] protected sealed class RequestFishingRodReelMessage : EntityEventArgs { diff --git a/Resources/Prototypes/_CP14/Fishing/lootTables.yml b/Resources/Prototypes/_CP14/Fishing/lootTables.yml index 5cf5f394fa..ba2139a11e 100644 --- a/Resources/Prototypes/_CP14/Fishing/lootTables.yml +++ b/Resources/Prototypes/_CP14/Fishing/lootTables.yml @@ -1,4 +1,4 @@ - type: CP14FishingPoolLootTable id: Default prototypes: - - CP14FloorWater + - CP14BaseLockpick