Files
crystall-punk-14/Content.Server/_CP14/Farming/CP14FarmingSystem.cs
Ed 8cbd26ae2f Farming WIP (#269)
* setup planting doafter

* spawn plants after use seed

* some work

* move comps to separate folder

* public check daylight

* daylight plant energy regeneration

* some fixes

* bruh, im thinking

* added ingame soil

* added hoe

* attaching plants

* hoe system

* block plant dublicating

* some planting refactor

* shovel as pant + soil remover

* plant growing visualizer

* soil resprite

* split farming system to second partial class

* update throught event refactor

* tring sync update plant with update visuals

* finish visual sync

* plants growing

* more partial splitting, naming work, clean up

* Update FARMINGTEST.yml

* Update FARMINGTEST.yml

* fix typo

* prototype value validating

* Оно бухает воду!

* solution visualizer in soil

* forgot some fix

* part of Tornado review fix

* destroy RemovePlantComponent

* more fixes

* simple harvesting

* refactor plant component values changing

* harvest redo

* gather on destroyByTool

* sickle resprite

* plant fading

* fading per minute!

* wheat sprites

* YML restruct

* fixes

* auto root plants

* add comments

* move sprites

* split structures from object textures

* wheat farming!

* Update CP14FarmingSystem.Interactions.cs

* seedbed (#297)

seedbed :^)

* a

* Update soil.yml

---------

Co-authored-by: Jaraten <116667537+Jaraten@users.noreply.github.com>
2024-07-15 23:58:03 +03:00

115 lines
4.1 KiB
C#

using Content.Server._CP14.Farming.Components;
using Content.Server.Destructible;
using Content.Server.DoAfter;
using Content.Server.Popups;
using Content.Shared._CP14.DayCycle;
using Content.Shared._CP14.Farming;
using Content.Shared.Chemistry.EntitySystems;
using Content.Shared.Damage;
using Content.Shared.Tag;
using Content.Shared.Whitelist;
using Robust.Server.Audio;
using Robust.Server.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Map.Components;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
using Robust.Shared.Timing;
namespace Content.Server._CP14.Farming;
public sealed partial class CP14FarmingSystem : CP14SharedFarmingSystem
{
[Dependency] private readonly DoAfterSystem _doAfter = default!;
[Dependency] private readonly CP14DayCycleSystem _dayCycle = default!;
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly TransformSystem _transform = default!;
[Dependency] private readonly PopupSystem _popup = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedSolutionContainerSystem _solutionContainer = default!;
[Dependency] private readonly EntityWhitelistSystem _whitelist = default!;
[Dependency] private readonly AudioSystem _audio = default!;
[Dependency] private readonly TagSystem _tag = default!;
[Dependency] private readonly DamageableSystem _damageable = default!;
[Dependency] private readonly DestructibleSystem _destructible = default!;
[Dependency] private readonly SharedMapSystem _map = default!;
public override void Initialize()
{
base.Initialize();
InitializeInteractions();
InitializeResources();
SubscribeLocalEvent<CP14PlantComponent, EntityUnpausedEvent>(OnUnpaused);
SubscribeLocalEvent<CP14PlantComponent, MapInitEvent>(OnMapInit);
SubscribeLocalEvent<CP14PlantAutoRootComponent, MapInitEvent>(OnAutoRootMapInit);
}
public override void Update(float frameTime)
{
base.Update(frameTime);
var query = EntityQueryEnumerator<CP14PlantComponent>();
while (query.MoveNext(out var uid, out var plant))
{
if (_timing.CurTime <= plant.NextUpdateTime)
continue;
var newTime = _random.NextFloat(plant.UpdateFrequency);
plant.NextUpdateTime = _timing.CurTime + TimeSpan.FromSeconds(newTime);
plant.Age += TimeSpan.FromSeconds(plant.UpdateFrequency);
var ev = new CP14PlantUpdateEvent((uid, plant));
RaiseLocalEvent(uid, ev);
AffectResource((uid, plant), ev.ResourceDelta);
AffectEnergy((uid, plant), ev.EnergyDelta);
var ev2 = new CP14AfterPlantUpdateEvent((uid, plant));
RaiseLocalEvent(uid, ev2);
Dirty(uid, plant);
}
}
private void OnUnpaused(Entity<CP14PlantComponent> ent, ref EntityUnpausedEvent args)
{
ent.Comp.NextUpdateTime += args.PausedTime;
}
private void OnMapInit(Entity<CP14PlantComponent> plant, ref MapInitEvent args)
{
var newTime = _random.NextFloat(plant.Comp.UpdateFrequency);
plant.Comp.NextUpdateTime = _timing.CurTime + TimeSpan.FromSeconds(newTime);
}
private void OnAutoRootMapInit(Entity<CP14PlantAutoRootComponent> autoRoot, ref MapInitEvent args)
{
var grid = Transform(autoRoot).GridUid;
if (grid == null || !TryComp<MapGridComponent>(grid, out var gridComp))
return;
var targetPos = new EntityCoordinates(grid.Value,Transform(autoRoot).LocalPosition);
var anchored = _map.GetAnchoredEntities(grid.Value, gridComp, targetPos);
foreach (var entt in anchored)
{
if (!TryComp<CP14SoilComponent>(entt, out var soil))
continue;
_transform.SetParent(autoRoot, entt);
soil.PlantUid = autoRoot;
if (TryComp<CP14PlantComponent>(autoRoot, out var plantComp))
{
plantComp.SoilUid = entt;
}
break;
}
}
}