Esempio n. 1
0
    private void OnValidate()
    {
        Vector3 ChassisOffset = Vector3.zero;

        quantities = new Dictionary <VehiclePart_Config, int>();
        if (designPrefab != null)
        {
            designName    = designPrefab.name;
            requiredParts = new List <VehiclePart_Assignment>();

            foreach (VehiclePart _PART in designPrefab.GetComponentsInChildren <VehiclePart>())
            {
                if (_PART.partConfig.partType == Vehicle_PartType.CHASSIS)
                {
                    ChassisOffset = _PART.transform.localPosition;
                    break;
                }
            }

            foreach (VehiclePart _PART in designPrefab.GetComponentsInChildren <VehiclePart>())
            {
                VehiclePart_Config     _CONFIG   = _PART.partConfig;
                Vector3                storedPos = (_CONFIG.partType == Vehicle_PartType.CHASSIS) ? _PART.transform.localPosition : _PART.transform.localPosition - ChassisOffset;
                VehiclePart_Assignment _temp     = new VehiclePart_Assignment(
                    _PART.name,
                    _CONFIG, storedPos,
                    _PART.transform.localRotation);

                requiredParts.Add(_temp);
                if (quantities.ContainsKey(_CONFIG))
                {
                    quantities[_CONFIG]++;
                }
                else
                {
                    quantities.Add(_CONFIG, 1);
                }
            }
        }
    }
    public bool AttachPart(VehiclePart_Config _part, GameObject _obj)
    {
        VehiclePart_Assignment _ASSIGNMENT = null;

        //Debug.Log("Trying to attach " + _part);
        foreach (VehiclePart_Assignment _PART_NEEDED in partsNeeded)
        {
            if (_PART_NEEDED.partConfig == _part)
            {
                _ASSIGNMENT = _PART_NEEDED;
                break;
            }
        }

        if (_ASSIGNMENT != null)
        {
            Transform _T = _obj.transform;
            _T.SetParent(transform);
            _T.localPosition = _ASSIGNMENT.position;
            _T.localRotation = _ASSIGNMENT.rotation;
            partsNeeded.Remove(_ASSIGNMENT);
            vehicleIsComplete = (partsNeeded.Count == 0);
            if (!partsFitted.ContainsKey(_ASSIGNMENT.partConfig))
            {
                partsFitted[_ASSIGNMENT.partConfig] = 1;
            }
            else
            {
                partsFitted[_ASSIGNMENT.partConfig]++;
            }
            return(true);
        }
        else
        {
            return(false);
        }
    }