Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnthroSystem ECS Component #11

Merged
merged 4 commits into from
Oct 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 149 additions & 0 deletions Content.Client/AnthroSystem/AnthroSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
using System.Collections.Generic;
using System.Linq;
using Content.Shared.AnthroSystem;
using Content.Shared.CharacterAppearance;
using Content.Shared.CharacterAppearance.Systems;
using Content.Shared.Preferences;
using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Utility;

namespace Content.Client.AnthroSystem
{
public class AnthroEntitySystem : EntitySystem
{
[Dependency] private readonly AnthroMarkingManager _markingManager = default!;
[Dependency] private readonly AnthroSpeciesManager _speciesManager = default!;

public override void Initialize()
{
SubscribeLocalEvent<AnthroComponent, ComponentInit>(OnAnthroSystemInit);
SubscribeLocalEvent<AnthroComponent, SharedHumanoidAppearanceSystem.ChangedHumanoidAppearanceEvent>(UpdateMarkings);
}

static HumanoidVisualLayers[] actualBodyParts =
{
HumanoidVisualLayers.Chest,
HumanoidVisualLayers.Head,
HumanoidVisualLayers.RArm,
HumanoidVisualLayers.LArm,
HumanoidVisualLayers.RHand,
HumanoidVisualLayers.LHand,
HumanoidVisualLayers.RLeg,
HumanoidVisualLayers.LLeg,
HumanoidVisualLayers.RFoot,
HumanoidVisualLayers.LFoot
};
public static string DefaultBase = "human";


private void OnAnthroSystemInit(EntityUid uid, AnthroComponent component, ComponentInit __)
{
foreach (HumanoidVisualLayers layer in actualBodyParts)
{
Logger.DebugS("AnthroSystem", $"Activating marking tracking for {layer}");
component.ActiveMarkings.Add(layer, new List<AnthroMarking>());
}

}

public void ToggleMarkingVisibility(EntityUid uid, SpriteComponent body, HumanoidVisualLayers layer, bool toggle)
{
var owner = EntityManager.GetEntity(uid);
if(!owner.TryGetComponent(out AnthroComponent? anthroSystem)) return;

if (anthroSystem.ActiveMarkings.TryGetValue(layer, out List<AnthroMarking>? layerMarkings))
foreach (AnthroMarking marking in layerMarkings)
body.LayerSetVisible(marking.MarkingId, toggle);
}
// we use HumanoidCharacterProfile because it already exists -
// we just grab the inner Appearance from it since we've tied it in
// severely to that
//
// Probably can untie it, though
//
// TODO: Untie Appearance.Markings, Appearance.SpeciesBase from
// HumanoidCharacterAppearance, inline with the whole SQL model
// thing as well probably
public void UpdateMarkings(EntityUid uid, AnthroComponent anthroSystem, SharedHumanoidAppearanceSystem.ChangedHumanoidAppearanceEvent args)
{
var appearance = args.Appearance;
var owner = EntityManager.GetEntity(uid);
if (!EntityManager.TryGetComponent(uid, out SpriteComponent? sprite)) return;

if (appearance.SpeciesBase != anthroSystem.LastBase
&& _speciesManager.SpeciesHasSprites(appearance.SpeciesBase,
out IReadOnlyCollection<KeyValuePair<HumanoidVisualLayers, SpriteSpecifier?>>? speciesParts))
{
Logger.DebugS("AnthroSystem", "Rerendering body sprites due to species difference.");
anthroSystem.LastBase = appearance.SpeciesBase;
foreach (var (layer, speciesSprite) in speciesParts)
if (speciesSprite is not null) sprite.LayerSetSprite(layer, speciesSprite);
}

Logger.DebugS("AnthroSystem", "Recoloring body now.");
foreach (var part in actualBodyParts)
{
if (!sprite.LayerMapTryGet(part, out int targetLayer))
{
Logger.DebugS("AnthroSystem", $"Could not get layer {part}");
continue;
}

sprite.LayerSetColor(targetLayer, appearance.SkinColor);
}

Logger.DebugS("AnthroSystem", "Rendering markings now.");
Logger.DebugS("AnthroSystem", $"Marking count: {appearance.Markings.Count}");


// Top -> Bottom ordering
foreach (var marking in appearance.Markings.Reverse())
{
if (!_markingManager.IsValidMarking(marking, out AnthroMarkingPrototype? markingPrototype))
{
Logger.DebugS("AnthroSystem", $"Invalid marking {marking.MarkingId}");
continue;
}


if (!sprite.LayerMapTryGet(markingPrototype.BodyPart, out int targetLayer))
{
Logger.DebugS("AnthroSystem", "Could not get the target layer");
continue;
}

Logger.DebugS("AnthroSystem", $"Adding {markingPrototype.Sprites.Count()} markings from {markingPrototype.ID} to layer {targetLayer}");

for (int i = 0; i < markingPrototype.Sprites.Count(); i++)
{
string layerId = markingPrototype.ID + markingPrototype.MarkingPartNames[i];

if (sprite.LayerMapTryGet(layerId, out var existingLayer))
{
Logger.DebugS("AnthroSystem", $"Deduplicating {markingPrototype.MarkingPartNames[i]} now from {existingLayer}");
sprite.RemoveLayer(existingLayer);
sprite.LayerMapRemove(marking.MarkingId);
}
Logger.DebugS("AnthroSystem", $"Adding part {markingPrototype.MarkingPartNames[i]} now to {targetLayer + i + 1}");

int layer = sprite.AddLayer(markingPrototype.Sprites[i], targetLayer + i + 1);
sprite.LayerMapSet(layerId, layer);
sprite.LayerSetColor(layerId, marking.MarkingColors[i]);
}

Logger.DebugS("AnthroSystem", $"Marking added: {markingPrototype.ID}");
// _activeMarkings[markingPrototype.BodyPart].Add(marking);
}
}

/*
public void UpdateMarkings(EntityUid uid, HumanoidCharacterProfile profile)
{
UpdateMarkings(uid, profile.Appearance);
}
*/
}
}
17 changes: 17 additions & 0 deletions Content.Shared/AnthroSystem/AnthroComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;
using Content.Shared.CharacterAppearance;
using Robust.Shared.GameObjects;

namespace Content.Shared.AnthroSystem
{
[RegisterComponent]
public class AnthroComponent : Component
{
public override string Name => "AnthroSystem";

public List<AnthroMarking> CurrentMarkingSet = new();
public List<AnthroMarking> LastMarkingSet = new();
public string LastBase = "human";
public Dictionary<HumanoidVisualLayers, List<AnthroMarking>> ActiveMarkings = new();
}
}
2 changes: 2 additions & 0 deletions Resources/Prototypes/Entities/Mobs/Species/human.yml
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@
types:
Heat : 1 #per second, scales with temperature & other constants
- type: HumanoidAppearance
- type: AnthroSystem # ECS!!!
- type: Body
template: HumanoidTemplate
preset: HumanPreset
Expand Down Expand Up @@ -383,6 +384,7 @@
layer:
- Opaque
- type: HumanoidAppearance
- type: AnthroSystem # ECS!!!
- type: Body
template: HumanoidTemplate
preset: HumanPreset
Expand Down