BITFALL/Assets/Artists/Scripts/Player/Socket/PlayerSocketService.cs

48 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.Linq;
using AYellowpaper.SerializedCollections;
using BITFALL.Cosmetic;
using BITKit;
using BITKit.Entities;
using UnityEngine;
namespace BITFALL.HotFix
{
[CustomType(typeof(PlayerSocketService))]
public class PlayerSocketService : EntityBehavior
{
[Inject(true)] private ICosmeticService _cosmeticService;
[SerializeField] private SerializedDictionary <string, Transform> dictionary=new();
public IDictionary<string, Transform> Dictionary => dictionary;
public override void OnAwake()
{
base.OnAwake();
_cosmeticService.OnCosmeticsChange += dictionary.Clear;
_cosmeticService.OnCosmeticsChanged += OnCosmeticsChanged;
OnCosmeticsChanged();
}
public override void OnDestroyComponent()
{
base.OnDestroyComponent();
_cosmeticService.OnCosmeticsChange -= dictionary.Clear;
_cosmeticService.OnCosmeticsChanged -= OnCosmeticsChanged;
}
private void OnCosmeticsChanged()
{
Dictionary.Clear();
var tags = GetComponentsInChildren<Tag>(true);
foreach (var socketComponent in GetComponentsInChildren<PlayerSocketComponent>(true))
{
var target = tags.FirstOrDefault(x => x.GetTags().Contains(socketComponent.Reference));
if (target is null) continue;
socketComponent.SetParent(target.transform);
dictionary.AddConflictAllowed(socketComponent.Reference, target.transform);
}
}
}
}