Space Kudzu (#5472)

This commit is contained in:
Moony
2021-11-28 20:25:36 -06:00
committed by GitHub
parent 30c87ca6b2
commit 9075cf6163
30 changed files with 617 additions and 42 deletions

View File

@@ -0,0 +1,17 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Kudzu;
[RegisterComponent]
public class GrowingKudzuComponent : Component
{
public override string Name => "GrowingKudzu";
[DataField("growthLevel")]
public int GrowthLevel = 1;
[DataField("growthTickSkipChance")]
public float GrowthTickSkipChange = 0.0f;
}

View File

@@ -0,0 +1,56 @@
using Content.Shared.Kudzu;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Random;
namespace Content.Server.Kudzu;
public class GrowingKudzuSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
private float _accumulatedFrameTime = 0.0f;
public override void Initialize()
{
SubscribeLocalEvent<GrowingKudzuComponent, ComponentAdd>(SetupKudzu);
}
private void SetupKudzu(EntityUid uid, GrowingKudzuComponent component, ComponentAdd args)
{
if (!EntityManager.TryGetComponent<AppearanceComponent>(uid, out var appearance))
{
return;
}
appearance.SetData(KudzuVisuals.Variant, _robustRandom.Next(1, 3));
appearance.SetData(KudzuVisuals.GrowthLevel, 1);
}
public override void Update(float frameTime)
{
_accumulatedFrameTime += frameTime;
if (!(_accumulatedFrameTime >= 0.5f))
return;
_accumulatedFrameTime -= 0.5f;
foreach (var (kudzu, appearance) in EntityManager.EntityQuery<GrowingKudzuComponent, AppearanceComponent>())
{
if (kudzu.GrowthLevel >= 3 || !_robustRandom.Prob(kudzu.GrowthTickSkipChange)) continue;
kudzu.GrowthLevel += 1;
if (kudzu.GrowthLevel == 3 &&
EntityManager.TryGetComponent<SpreaderComponent>(kudzu.OwnerUid, out var spreader))
{
// why cache when you can simply cease to be? Also saves a bit of memory/time.
EntityManager.RemoveComponent<GrowingKudzuComponent>(kudzu.OwnerUid);
}
appearance.SetData(KudzuVisuals.GrowthLevel, kudzu.GrowthLevel);
}
}
}

View File

@@ -0,0 +1,32 @@
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Kudzu;
/// <summary>
/// Component for rapidly spreading objects, like Kudzu.
/// ONLY USE THIS FOR ANCHORED OBJECTS. An error will be logged if not anchored/static.
/// Currently does not support growing in space.
/// </summary>
[RegisterComponent, Friend(typeof(SpreaderSystem))]
public class SpreaderComponent : Component
{
public override string Name => "Spreader";
/// <summary>
/// Chance for it to grow on any given tick, after the normal growth rate-limit (if it doesn't grow, SpreaderSystem will pick another one.).
/// </summary>
[DataField("chance", required: true)]
public float Chance;
/// <summary>
/// Prototype spawned on growth success.
/// </summary>
[DataField("growthResult", required: true)]
public string GrowthResult = default!;
[DataField("enabled")]
public bool Enabled = true;
}

View File

@@ -0,0 +1,133 @@
using System.Collections.Generic;
using System.Linq;
using Content.Server.Atmos.Components;
using Content.Server.Atmos.EntitySystems;
using Content.Server.Temperature.Systems;
using Content.Shared.Atmos;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Random;
namespace Content.Server.Kudzu;
// Future work includes making the growths per interval thing not global, but instead per "group"
public class SpreaderSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _robustRandom = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
/// <summary>
/// Maximum number of edges that can grow out every interval.
/// </summary>
private const int GrowthsPerInterval = 1;
private float _accumulatedFrameTime = 0.0f;
private readonly HashSet<EntityUid> _edgeGrowths = new ();
public override void Initialize()
{
SubscribeLocalEvent<SpreaderComponent, ComponentAdd>(SpreaderAddHandler);
SubscribeLocalEvent<AirtightChanged>(e => UpdateNearbySpreaders(e.Airtight.OwnerUid, e.Airtight));
}
private void SpreaderAddHandler(EntityUid uid, SpreaderComponent component, ComponentAdd args)
{
if (component.Enabled)
_edgeGrowths.Add(uid); // ez
}
public void UpdateNearbySpreaders(EntityUid blocker, AirtightComponent comp)
{
if (!EntityManager.TryGetComponent<TransformComponent>(blocker, out var transform))
return; // how did we get here?
if (!_mapManager.TryGetGrid(transform.GridID, out var grid)) return;
for (var i = 0; i < Atmospherics.Directions; i++)
{
var direction = (AtmosDirection) (1 << i);
if (!comp.AirBlockedDirection.IsFlagSet(direction)) continue;
foreach (var ent in grid.GetInDir(transform.Coordinates, direction.ToDirection()))
{
if (EntityManager.TryGetComponent<SpreaderComponent>(ent, out var s) && s.Enabled)
_edgeGrowths.Add(ent);
}
}
}
public override void Update(float frameTime)
{
_accumulatedFrameTime += frameTime;
if (!(_accumulatedFrameTime >= 1.0f))
return;
_accumulatedFrameTime -= 1.0f;
var growthList = _edgeGrowths.ToList();
_robustRandom.Shuffle(growthList);
var successes = 0;
foreach (var entity in growthList)
{
if (!TryGrow(entity)) continue;
successes += 1;
if (successes >= GrowthsPerInterval)
break;
}
}
private bool TryGrow(EntityUid ent, TransformComponent? transform = null, SpreaderComponent? spreader = null)
{
if (!Resolve(ent, ref transform, ref spreader, false))
return false;
if (spreader.Enabled == false) return false;
if (!_mapManager.TryGetGrid(transform.GridID, out var grid)) return false;
var didGrow = false;
for (var i = 0; i < 4; i++)
{
var direction = (DirectionFlag) (1 << i);
var coords = transform.Coordinates.Offset(direction.AsDir().ToVec());
if (grid.GetTileRef(coords).Tile.IsEmpty || _robustRandom.Prob(spreader.Chance)) continue;
var ents = grid.GetLocal(coords);
if (ents.Any(x => IsTileBlockedFrom(x, direction))) continue;
// Ok, spawn a plant
didGrow = true;
EntityManager.SpawnEntity(spreader.GrowthResult, transform.Coordinates.Offset(direction.AsDir().ToVec()));
}
return didGrow;
}
public void EnableSpreader(EntityUid ent, SpreaderComponent? component = null)
{
if (!Resolve(ent, ref component))
return;
component.Enabled = true;
_edgeGrowths.Add(ent);
}
private bool IsTileBlockedFrom(EntityUid ent, DirectionFlag dir)
{
if (EntityManager.TryGetComponent<SpreaderComponent>(ent, out _))
return true;
if (!EntityManager.TryGetComponent<AirtightComponent>(ent, out var airtight))
return false;
var oppositeDir = dir.AsDir().GetOpposite().ToAtmosDirection();
return airtight.AirBlocked && airtight.AirBlockedDirection.IsFlagSet(oppositeDir);
}
}