It’s all strange…
public class ArmorConfig : EquipableItem, IModifierProvider
{
[SerializeField] Armor equippedArmor = null;
[SerializeField] Armor equippedArmor2 = null;
[SerializeField]
Modifier[] additiveModifiers;
[SerializeField]
Modifier[] percentageModifiers;
[System.Serializable]
struct Modifier
{
public Stat stat;
public float value;
}
public enum ArmorVariant { Helmet, Necklace, Body, Trousers, Boots, Gloves}
[SerializeField] ArmorVariant armorVarianter;
const string EquipNameHelmet = "Helmet";
const string EquipNameNecklace = "Necklace";
const string EquipNameBody = "Body";
const string EquipNameTrousers = "Trousers";
const string EquipNameBoots = "Boots";//right
const string EquipNameBootsL = "BootsL";//left
const string EquipNameGloves = "Gloves"; //right
const string EquipNameGlovesL = "GlovesL"; //left
Transform oldEquip;
Transform oldEquip2;
private Armor Spawn(Transform slot, string itemName, bool equip2 = false)
{
if (!slot)
{
Debug.Log($"Attemping to spawn slot {itemName}, but no transform is defined");
return null;
}
if (equippedArmor == null)
{
Debug.Log($"Armor {name} is trying to equip an {itemName}, but does not have an equippedArmor assigned.");
return null;
}
if (equip2 && equippedArmor2 == null)
{
Debug.Log($"Armor {name} is trying to equip {itemName}, but has no equippedArmor2 assigned");
return null;
}
DestroyOldArmor(slot, itemName);
var ItemToInstantiate = equip2 ? equippedArmor2 : equippedArmor;
Armor armor = Instantiate(ItemToInstantiate, slot);
armor.name = itemName;
return armor;
}
public Armor Spawn(Transform Helmet, Transform Necklace, Transform Body, Transform Trousers, Transform Boots, Transform Left_Boots, Transform Gloves, Transform Left_Gloves)
{
switch (armorVarianter) {
case ArmorVariant.Helmet:
return Spawn(Helmet, EquipNameHelmet);
case ArmorVariant.Necklace:
return Spawn(Necklace, EquipNameNecklace);
case ArmorVariant.Body:
return Spawn(Body, EquipNameBody);
case ArmorVariant.Trousers:
return Spawn(Trousers, EquipNameTrousers);
case ArmorVariant.Boots:
Spawn(Left_Boots, EquipNameBootsL, true); //spawns left boot
return Spawn(Boots, EquipNameBoots);
case ArmorVariant.Gloves:
Spawn(Left_Gloves, EquipNameGlovesL, true);
return Spawn(Gloves, EquipNameGloves);
}
return null;
}
private Transform FindByName(Transform slot, string nameToFind)
{
foreach (Transform child in slot)
{
if (child.name == nameToFind) return child;
}
return null;
}
private void DestroyOldArmor(Transform slot, string armorName)
{
var oldItem = FindByName(slot, armorName);
if (oldItem)
{
oldItem.name = "DESTROYING";
Destroy(oldItem.gameObject);
}
}
public IEnumerable<float> GetAdditiveModifiers(Stat stat)
{
foreach (var modifier in additiveModifiers)
{
if (modifier.stat == stat)
{
yield return modifier.value;
}
}
}
public IEnumerable<float> GetPercentageModifiers(Stat stat)
{
foreach (var modifier in percentageModifiers)
{
if (modifier.stat == stat)
{
yield return modifier.value;
}
}
}
}
public class Fighter : MonoBehaviour, IAction
{
[SerializeField] float timeBetweenAttacks = 1f;
[SerializeField] Transform rightHandTransform = null;
[SerializeField] Transform leftHandTransform = null;
[SerializeField] WeaponConfig defaultWeapon = null;
[SerializeField] ShieldConfig defaultShield = null;
[SerializeField] float autoAttackRange = 4f;
//armor
[SerializeField] Transform armorTransformHelmet = null;
[SerializeField] Transform armorTransformNecklace = null;
[SerializeField] Transform armorTransformBody = null;
[SerializeField] Transform armorTransformTrousers = null;
[SerializeField] Transform armorTransformBoots_Left = null;
[SerializeField] Transform armorTransformBoots_Right = null;
[SerializeField] Transform armorTransformGloves_Left = null;
[SerializeField] Transform armorTransformGloves_Right = null;
[SerializeField] ArmorConfig defaultarmorHelmet = null;
[SerializeField] ArmorConfig defaultarmorNecklace = null;
[SerializeField] ArmorConfig defaultarmorBody = null;
[SerializeField] ArmorConfig defaultarmorTrousers = null;
[SerializeField] ArmorConfig defaultarmorBoots = null;
[SerializeField] ArmorConfig defaultarmorGloves = null;
ArmorConfig currentArmorConfig;
LazyValue<Armor> currentArmor;
//armor
Health target;
Equipment equipment;
float timeSinceLastAttack = Mathf.Infinity;
WeaponConfig currentWeaponConfig;
LazyValue<Weapon> currentWeapon;
ShieldConfig currentShield;
LazyValue<Shield> currentEquippedShield;
private void Awake()
{
currentArmorConfig = defaultarmorHelmet;
currentArmor = new LazyValue<Armor>(SetupDefaultArmor);
currentWeaponConfig = defaultWeapon;
currentWeapon = new LazyValue<Weapon>(SetupDefaultWeapon);
currentShield = defaultShield;
currentEquippedShield = new LazyValue<Shield>(null);
equipment = GetComponent<Equipment>();
if (equipment)
{
equipment.equipmentUpdated += UpdateWeapon;
equipment.equipmentUpdated += UpdateEquipment;
}
}
//armor
private Armor SetupDefaultArmor()
{
return AttachArmor(defaultarmorHelmet);
}
public void EquipArmor(ArmorConfig armor)
{
currentArmorConfig = armor;
currentArmor.value = AttachArmor(armor);
}
private Armor AttachArmor(ArmorConfig armor)
{
//Debug.Log("AttachArmor");
return armor.Spawn(armorTransformHelmet, armorTransformNecklace, armorTransformBody, armorTransformTrousers, armorTransformBoots_Left, armorTransformBoots_Right, armorTransformGloves_Right, armorTransformGloves_Left);
}
public void UpdateEquipment()
{
var Helmet = equipment.GetItemInSlot(EquipLocation.Helmet) as ArmorConfig;
var Body = equipment.GetItemInSlot(EquipLocation.Body) as ArmorConfig;
var Trousers = equipment.GetItemInSlot(EquipLocation.Trousers) as ArmorConfig;
var Boots = equipment.GetItemInSlot(EquipLocation.Boots) as ArmorConfig;
var Gloves = equipment.GetItemInSlot(EquipLocation.Gloves) as ArmorConfig;
var Necklace = equipment.GetItemInSlot(EquipLocation.Necklace) as ArmorConfig;
if (Helmet == null) EquipArmor(defaultarmorHelmet);
else EquipArmor(Helmet);
if (Body == null) EquipArmor(defaultarmorBody);
else EquipArmor(Body);
if (Trousers == null) EquipArmor(defaultarmorTrousers);
else EquipArmor(Trousers);
if (Boots == null) EquipArmor(defaultarmorBoots);
else EquipArmor(Boots);
if (Gloves == null) EquipArmor(defaultarmorGloves);
else EquipArmor(Gloves);
if (Necklace == null) EquipArmor(defaultarmorNecklace);
else EquipArmor(Necklace);
}
//armor
}