From 97d08097fe62f5bf0f099d7fb94055c16ca3be7b Mon Sep 17 00:00:00 2001 From: Tornado Tech <54727692+Tornado-Technology@users.noreply.github.com> Date: Sun, 15 Dec 2024 02:21:40 +1000 Subject: [PATCH] Fix again --- .../_CP14/Fishing/CP14FishingProcessSystem.cs | 167 +++++++----------- .../Components/CP14FishingRodComponent.cs | 2 +- .../_CP14/Fishing/Core/Behaviors/Behavior.cs | 2 +- Content.Shared/_CP14/Fishing/Core/Fish.cs | 24 +-- .../Systems/CP14SharedFishingProcessSystem.cs | 105 +++++++++++ 5 files changed, 177 insertions(+), 123 deletions(-) diff --git a/Content.Server/_CP14/Fishing/CP14FishingProcessSystem.cs b/Content.Server/_CP14/Fishing/CP14FishingProcessSystem.cs index 6bf896c379..88f7d9e2d3 100644 --- a/Content.Server/_CP14/Fishing/CP14FishingProcessSystem.cs +++ b/Content.Server/_CP14/Fishing/CP14FishingProcessSystem.cs @@ -16,17 +16,75 @@ public sealed class CP14FishingProcessSystem : CP14SharedFishingProcessSystem [Dependency] private readonly ThrowingSystem _throwing = default!; [Dependency] private readonly CP14FishingPoolSystem _pool = default!; + /* + private readonly TimeSpan _dirtyDelay = TimeSpan.FromTicks(10000000000000); + private TimeSpan _dirtyDelayTime; + */ + public override void Update(float frameTime) { - base.Update(frameTime); + // DON'T CALL BASE METHOD!!! var query = EntityQueryEnumerator(); while (query.MoveNext(out var entityUid, out var processComponent)) { - Update((entityUid, processComponent), frameTime); + Update((entityUid, processComponent), frameTime * 2); } } + public override void FishPreUpdate(Entity process, Fish fish, float frameTime) + { + base.FishPreUpdate(process, fish, frameTime); + + fish.UpdateSpeed(_random, _timing); + Dirty(process); + } + + public override void UpdateDirty(Entity process) + { + base.UpdateDirty(process); + + /* + if (_timing.CurTime < _dirtyDelayTime) + return; + + _dirtyDelayTime = _timing.CurTime + _dirtyDelay; + Dirty(process); + */ + } + + public override void Finish(Entity process, bool success) + { + base.Finish(process, success); + + if (success) + { + Reward(process); + } + + // 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 override void Reward(Entity process) + { + base.Reward(process); + + var pool = GetPool(process); + var rod = GetRod(process); + + var coordinates = Transform(pool).Coordinates; + var targetCoordinates = Transform(process.Comp.User!.Value).Coordinates; + + var loot = Spawn(process.Comp.LootProtoId, coordinates); + + _throwing.TryThrow(loot, targetCoordinates, rod.Comp.ThrowPower); + } + public Entity Start( Entity fishingRod, Entity fishingPool, @@ -42,7 +100,7 @@ public sealed class CP14FishingProcessSystem : CP14SharedFishingProcessSystem process.Comp.User = user; process.Comp.Player = new Player(fishingRod.Comp.Size); - process.Comp.Fish = new Fish(new MixedBehavior(), _random, _timing); + process.Comp.Fish = new Fish(new MixedBehavior(), _timing.CurTime + TimeSpan.FromSeconds(0.5f)); process.Comp.LootProtoId = loot; process.Comp.StyleSheet = style; @@ -53,112 +111,11 @@ public sealed class CP14FishingProcessSystem : CP14SharedFishingProcessSystem 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 entityUid = SpawnAttachedTo(null, Transform(parent).Coordinates); var ensureComponent = AddComp(entityUid); return (entityUid, ensureComponent); } - - private void UpdateReeling(Entity process, - Entity fishingRod, - float frameTime) - { - if (process.Comp.Player is not { } player) - return; - - if (fishingRod.Comp.Reeling) - { - player.Velocity += fishingRod.Comp.Speed * frameTime; - return; - } - - player.Velocity -= fishingRod.Comp.Gravity * frameTime; - } - - private void UpdateVelocity(Entity process, - Entity fishingRod) - { - if (process.Comp.Player is not { } player) - return; - - player.Velocity *= fishingRod.Comp.Drag; - player.Velocity = Math.Clamp(player.Velocity, - fishingRod.Comp.MinVelocity, - fishingRod.Comp.MaxVelocity); - } - - private void UpdatePosition(Entity process, - float frameTime) - { - if (process.Comp.Player is not { } player) - return; - - player.Position += process.Comp.Player.Velocity * frameTime; - - var halfSize = process.Comp.Player.HalfSize; - process.Comp.Player.Position = Math.Clamp(process.Comp.Player.Position, halfSize, 1 - halfSize); - } - - private void Update(Entity process, float frameTime) - { - if (process.Comp.Player is not { } player || process.Comp.Fish is not { } fish) - return; - - var fishingRod = GetRod(process); - - UpdateReeling(process, fishingRod, frameTime); - UpdateVelocity(process, fishingRod); - UpdatePosition(process, frameTime); - - fish.Update(frameTime); - - var collides = Collide(fish, 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) - { - Reward(process); - } - - // Raising events before deletion - var ev = new CP14FishingFinishedEvent(success); - RaiseLocalEvent(process, ref ev, true); - - // Either way, we need to delete the process - Stop(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; - - var loot = Spawn(process.Comp.LootProtoId, coordinates); - - _throwing.TryThrow(loot, targetCoordinates, rod.Comp.ThrowPower); - } } diff --git a/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs b/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs index 13022a1cde..f4c26ccf55 100644 --- a/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs +++ b/Content.Shared/_CP14/Fishing/Components/CP14FishingRodComponent.cs @@ -17,7 +17,7 @@ public sealed partial class CP14FishingRodComponent : Component public bool Reeling; [DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] - public float Speed = 0.05f; + public float Speed = 0.1f; [DataField, ViewVariables(VVAccess.ReadWrite), AutoNetworkedField] public float Gravity = 0.075f; diff --git a/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs b/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs index aff58cd3aa..349f976b65 100644 --- a/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs +++ b/Content.Shared/_CP14/Fishing/Core/Behaviors/Behavior.cs @@ -9,7 +9,7 @@ namespace Content.Shared._CP14.Fishing.Core.Behaviors; public abstract partial class Behavior { [DataField, ViewVariables(VVAccess.ReadWrite)] - public float Speed { get; set; } = 0.05f; + public float Speed { get; set; } = 0.25f; [DataField, ViewVariables(VVAccess.ReadWrite)] public float Difficulty { get; set; } = 2f; diff --git a/Content.Shared/_CP14/Fishing/Core/Fish.cs b/Content.Shared/_CP14/Fishing/Core/Fish.cs index ed3aa74d36..48e1c9379b 100644 --- a/Content.Shared/_CP14/Fishing/Core/Fish.cs +++ b/Content.Shared/_CP14/Fishing/Core/Fish.cs @@ -17,31 +17,20 @@ public sealed class Fish [ViewVariables(VVAccess.ReadWrite)] private readonly Behavior _behavior; - [NonSerialized] - private readonly IRobustRandom _random; - - [NonSerialized] - private readonly IGameTiming _timing; - [ViewVariables(VVAccess.ReadWrite)] private float _speed; [ViewVariables(VVAccess.ReadWrite)] private TimeSpan _updateSpeedTime; - public Fish(Behavior behavior, IRobustRandom random, IGameTiming timing) + public Fish(Behavior behavior, TimeSpan updateSpeedTime) { _behavior = behavior; - _random = random; - _timing = timing; + _updateSpeedTime = updateSpeedTime; } public void Update(float frameTime) { - // Update speed - if (_timing.CurTime > _updateSpeedTime) - UpdateSpeed(); - // Update position Position += _speed * frameTime; @@ -49,9 +38,12 @@ public sealed class Fish Position = Math.Clamp(Position, MinPosition, MaxPosition); } - private void UpdateSpeed() + public void UpdateSpeed(IRobustRandom random, IGameTiming timing) { - _speed = _behavior.CalculateSpeed(_random); - _updateSpeedTime = _timing.CurTime + TimeSpan.FromSeconds(_random.NextFloat(1f / _behavior.Difficulty, 1f - 1f / _behavior.Difficulty)); + if (_updateSpeedTime > timing.CurTime) + return; + + _speed = _behavior.CalculateSpeed(random); + _updateSpeedTime = timing.CurTime + TimeSpan.FromSeconds(random.NextFloat(1.5f - 1f / _behavior.Difficulty, 2.5f - 1f / _behavior.Difficulty)); } } diff --git a/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs index 4750b36473..00f2819694 100644 --- a/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs +++ b/Content.Shared/_CP14/Fishing/Systems/CP14SharedFishingProcessSystem.cs @@ -1,5 +1,6 @@ using System.Diagnostics.CodeAnalysis; using Content.Shared._CP14.Fishing.Components; +using Content.Shared._CP14.Fishing.Core; using Robust.Shared.Prototypes; namespace Content.Shared._CP14.Fishing.Systems; @@ -19,6 +20,110 @@ public abstract partial class CP14SharedFishingProcessSystem : EntitySystem FishingPool = GetEntityQuery(); } + public override void Update(float frameTime) + { + base.Update(frameTime); + + var query = EntityQueryEnumerator(); + while (query.MoveNext(out var entityUid, out var processComponent)) + { + Update((entityUid, processComponent), frameTime); + } + } + + private void UpdateReeling(Entity process, + Entity fishingRod, + float frameTime) + { + if (process.Comp.Player is not { } player) + return; + + if (fishingRod.Comp.Reeling) + { + player.Velocity += fishingRod.Comp.Speed * frameTime; + return; + } + + player.Velocity -= fishingRod.Comp.Gravity * frameTime; + } + + private void UpdateVelocity(Entity process, + Entity fishingRod) + { + if (process.Comp.Player is not { } player) + return; + + player.Velocity *= fishingRod.Comp.Drag; + player.Velocity = Math.Clamp(player.Velocity, + fishingRod.Comp.MinVelocity, + fishingRod.Comp.MaxVelocity); + } + + private void UpdatePosition(Entity process, + float frameTime) + { + if (process.Comp.Player is not { } player) + return; + + player.Position += process.Comp.Player.Velocity * frameTime; + + var halfSize = process.Comp.Player.HalfSize; + process.Comp.Player.Position = Math.Clamp(process.Comp.Player.Position, halfSize, 1 - halfSize); + } + + public void Update(Entity process, float frameTime) + { + if (process.Comp.Player is not { } player) + return; + + var fishingRod = GetRod(process); + + UpdateReeling(process, fishingRod, frameTime); + UpdateVelocity(process, fishingRod); + UpdatePosition(process, frameTime); + + if (process.Comp.Fish is { } fish) + { + FishPreUpdate(process, fish, frameTime); + + fish.Update(frameTime); + var collides = Collide(fish, player); + + var progressAdditive = collides ? 0.1f : -0.2f; + process.Comp.Progress = Math.Clamp(process.Comp.Progress + progressAdditive * frameTime, 0, 1); + } + + UpdateDirty(process); + + if (process.Comp.Progress is 1 or 0) + Finish(process, process.Comp.Progress is 1); + } + + public virtual void FishPreUpdate(Entity process, Fish fish, float frameTime) + { + } + + public virtual void UpdateDirty(Entity process) + { + } + + public virtual void Finish(Entity process, bool success) + { + } + + public void Stop(Entity process) + { + var rod = GetRod(process); + rod.Comp.Process = null; + Dirty(rod); + + Del(process); + } + + public virtual void Reward(Entity process) + { + } + public bool TryGetByUser(EntityUid userEntityUid, [NotNullWhen(true)] out Entity? process) { process = null;