Esempio n. 1
0
    public void UpdateUnit(UnitDefinition unit)
    {
        bool needsRegen = false;

        if ((MatPairStruct)this.unit.race != unit.race)
        {
            race       = CreatureRaws.Instance[unit.race.mat_type];
            caste      = race.caste[unit.race.mat_index];
            needsRegen = true;
        }
        if (oldWounds != null || unit.wounds != null)
        {
            if (oldWounds == null && unit.wounds != null)
            {
                needsRegen = true;
            }
            else if (oldWounds != null && unit.wounds == null)
            {
                needsRegen = true;
            }
            else if (oldWounds.Count != unit.wounds.Count)
            {
                needsRegen = true;
            }
            else
            {
                for (int i = 0; i < unit.wounds.Count; i++)
                {
                    if (oldWounds[i].severed_part != unit.wounds[i].severed_part)
                    {
                        needsRegen = true;
                        break;
                    }
                    if (oldWounds[i].parts.Count != unit.wounds[i].parts.Count)
                    {
                        needsRegen = true;
                        break;
                    }
                }
            }
        }
        oldWounds = unit.wounds;
        this.unit = unit;
        if (needsRegen || oldChibiSize != GameSettings.Instance.units.chibiness || oldScaleUnits != GameSettings.Instance.units.scaleUnits)
        {
            oldScaleUnits = GameSettings.Instance.units.scaleUnits;
            oldChibiSize  = GameSettings.Instance.units.chibiness;
            MakeBody();
        }

        if (((UnitFlags1)unit.flags1 & UnitFlags1.on_ground) == UnitFlags1.on_ground)
        {
            if (!onGround)
            {
                rootPart.transform.localRotation = Quaternion.Euler(90, 0, 0);
                rootPart.transform.localPosition = new Vector3(0, bounds.max.z, 0);
                onGround = true;
            }
        }
        else
        {
            if (onGround)
            {
                rootPart.transform.localRotation = Quaternion.identity;
                rootPart.transform.localPosition = new Vector3(0, -bounds.min.y, 0);
                onGround = false;
            }
        }
        if (unit.facing != null && GameMap.DFtoUnityDirection(unit.facing).sqrMagnitude > 0 && unit.rider_id < 0)
        {
            transform.rotation = Quaternion.LookRotation(GameMap.DFtoUnityDirection(unit.facing));
        }
        else if (unit.rider_id >= 0)
        {
            transform.rotation = Quaternion.identity;
        }

        if (InventoryChanged(unit.inventory))
        {
            foreach (var part in spawnedParts)
            {
                part.Value.inventory.Clear();
            }
            //Here we add pants first before shirts because otherwise the layering looks bad.
            foreach (var item in unit.inventory)
            {
                if (!ClothingTexture.GetTexture(item.item.type).isDress)
                {
                    AddInventoryItem(item);
                }
            }
            foreach (var item in unit.inventory)
            {
                if (ClothingTexture.GetTexture(item.item.type).isDress)
                {
                    AddInventoryItem(item);
                }
            }
            foreach (var part in spawnedParts)
            {
                part.Value.UpdateItems(unit);
            }
        }
    }
Esempio n. 2
0
 internal void ApplyEquipment(List <BodyPart.Equip> inventory, RemoteFortressReader.UnitDefinition unit)
 {
     //first of all disable any layers that are not used anymore, but not need to remove them entirely.
     for (int i = inventory.Count; i < instantiatedClothingLayers.Count; i++)
     {
         instantiatedClothingLayers[i].gameObject.SetActive(false);
     }
     for (int i = 0; i < inventory.Count; i++)
     {
         var item = inventory[i];
         //If it's not a worn clothing item, hide the respective layers.
         if (item.item.mode == RemoteFortressReader.InventoryMode.Worn)
         {
             if (instantiatedClothingLayers.Count > i && instantiatedClothingLayers[i] != null)
             {
                 instantiatedClothingLayers[i].gameObject.SetActive(false);
             }
         }
         if (specialEquipment.ContainsKey(item.item.item.type))
         {
             var model = specialEquipment[item.item.item.type];
             model.gameObject.SetActive(true);
             model.UpdateMaterial(item.item.item, unit);
         }
         else if (item.item.mode == RemoteFortressReader.InventoryMode.Worn)
         {
             var clothingTexture = ClothingTexture.GetTexture(item.item.item.type);
             if (clothingTexture == null || clothingTexture.baseMat == null)
             {
                 Debug.Log("Could not load texture for " + ItemRaws.Instance[item.item.item.type].id);
                 continue;
             }
             GenericClothingLayer clothing;
             if (clothingTexture.isDress)
             {
                 clothing = dressClothing;
             }
             else
             {
                 clothing = pantClothing;
             }
             if (clothing == null)
             {
                 continue;
             }
             if (i >= instantiatedClothingLayers.Count)
             {
                 instantiatedClothingLayers.AddRange(Enumerable.Repeat <ItemModel>(null, i - instantiatedClothingLayers.Count + 1));
             }
             if (instantiatedClothingLayers[i] == null)
             {
                 GameObject newLayer = new GameObject();
                 newLayer.transform.parent        = transform;
                 newLayer.transform.localPosition = clothing.pos;
                 newLayer.transform.localRotation = clothing.rotation;
                 newLayer.transform.localScale    = clothing.scale;
                 var mf = newLayer.AddComponent <MeshFilter>();
                 mf.sharedMesh = clothing.mesh;
                 var mr  = newLayer.AddComponent <MeshRenderer>();
                 var mat = clothingTexture.GetMaterial(i);
                 mr.sharedMaterial = mat;
                 var im = newLayer.AddComponent <ItemModel>();
                 instantiatedClothingLayers[i] = im;
             }
             if (instantiatedClothingLayers[i] == null)
             {
                 continue;
             }
             instantiatedClothingLayers[i].gameObject.SetActive(true);
             instantiatedClothingLayers[i].UpdateMaterial(item.item.item, unit);
             if (Application.isEditor)
             {
                 instantiatedClothingLayers[i].name = item.itemDef.id;
             }
         }
     }
 }