using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using BITKit;
using BITKit.Modification;
using UnityEngine;
namespace BITFALL.Guns.Modify
{
[Serializable]
public sealed class GunModifyOptional:ICloneable
{
[SerializeReference,SubclassSelector] private GunModifyElement index;
[SerializeField] private AssetableItem value;
public bool HasValue => value is not null;
public IModifyElement Index => index;
public AssetableItem Value
{
get => value;
set => this.value = value;
}
public object Clone()
{
return new GunModifyOptional
{
index = index,
value = value
};
}
}
///
/// 基本武器改装容器
///
[CustomType(typeof(IModifyManager))]
[Serializable]
public sealed class GunModify : ModifyManager,IProperty,ICloneable
{
[SerializeField] private GunModifyOptional[] elements=Array.Empty();
public override IDictionary Modifies =>
new Dictionary(elements.Select(
x=>new KeyValuePair(x.Index,x.Value)
));
public override void Add(IModifyElement modify, object obj)
{
base.Add(modify, obj);
foreach (var x in elements.Where(x=>x.Index.Equals(modify)))
{
x.Value = obj.As();
}
}
public override void Remove(IModifyElement modify, out object obj)
{
base.Remove(modify, out obj);
foreach (var x in elements.Where(x=>x.Index.Equals(modify)))
{
obj = x.Value;
x.Value = null;
return;
}
throw new InvalidOperationException($"没有找到对应的改装:{modify}");
}
public override string ToString()
{
var stringBuilder = new System.Text.StringBuilder();
foreach (var x in elements)
{
stringBuilder.AppendLine(x.HasValue ? $"{x.Index}:{x.Value.Name}" : $"{x.Index}:empty");
}
return stringBuilder.ToString();
}
public object Clone()
{
var clone = new GunModify();
if (elements is null) return clone;
clone.elements = elements.Select(x => x.Clone().As()).ToArray();
return clone;
}
}
///
/// 抽象武器改装元素
///
public abstract class GunModifyElement : IModifyElement
{
public override bool Equals(object obj)
{
return GetType() == obj?.GetType();
}
public override int GetHashCode() => GetType().GetHashCode();
[SerializeReference,SubclassSelector] private GunModifyElement[] requires;
[SerializeReference,SubclassSelector] private GunModifyElement[] incompatibles;
public virtual IModifyElement[] Require=>requires;
public virtual IModifyElement[] Incompatible => incompatibles;
}
///
/// 枪口,例如消音器,枪口制退器,枪口补偿器
///
[Serializable]
public sealed class Muzzle:GunModifyElement
{
}
///
/// 枪管,例如重型枪管,短枪管和T50前线之类的
///
[Serializable]
public sealed class Barrel:GunModifyElement
{
}
///
/// 弹匣,例如扩容弹匣,快速弹匣,9mm改装弹匣和弹鼓等
///
[Serializable]
public sealed class Magazine:GunModifyElement
{
}
///
/// 瞄具,例如全息瞄准镜,红点瞄准镜,狙击镜和夜视仪等
///
[Serializable]
public sealed class Sights:GunModifyElement
{
}
///
/// 下挂,例如快速握把,垂直握把和榴弹发射器等
///
[Serializable]
public sealed class Underbarrel:GunModifyElement
{
}
///
/// 战术侧挂,例如战术灯,激光瞄准器和红外线瞄准器等
///
[Serializable]
public sealed class Tactical:GunModifyElement
{
}
}