public void DropWeapon(int _index, bool swapping)
    {
        Weapon toDrop = weapons[_index];

        currentWeapon.isEquipped = false;

        UniqueWeaponStats statsToDrop = toDrop.uniqueStats;

        toDrop.ResetBaseWeaponStats(statsToDrop.baseStats);
        GameObject droppedWeapon = Instantiate(pickups[_index], transform.position + (transform.forward * 2.5f), Quaternion.identity);

        droppedWeapon.name = droppedWeapon.name.Replace("(Clone)", "");
        droppedWeapon.GetComponent <WeaponPickup>().stats = statsToDrop;
        if (!swapping)
        {
            SelectWeapon(0);
        }

        droppedWeapon.GetComponent <WeaponPickup>().DisableColliderMethod();



        //if (currentWeapon)
        //{
        //    currentWeapon.isEquipped = false;

        //    UniqueWeaponStats statsToDrop = currentWeapon.uniqueStats;
        //    currentWeapon.ResetBaseWeaponStats(statsToDrop.baseStats);
        //    GameObject droppedWeapon = Instantiate(pickups[currentWeaponIndex], transform.position + (transform.forward * 2), Quaternion.identity);
        //    droppedWeapon.name = droppedWeapon.name.Replace("(Clone)", "");
        //    droppedWeapon.GetComponent<WeaponPickup>().stats = statsToDrop;
        //    SelectWeapon(1);
        //}
    }
    Dictionary <string, List <float> > CalculateStats(UniqueWeaponStats _pickupStats, UniqueWeaponStats _currentStats)
    {
        Dictionary <string, List <float> > statsToReturn = new Dictionary <string, List <float> >();

        // Get Array of all variable names in class
        var weaponVariableNames = _pickupStats.GetType()
                                  .GetFields()
                                  .Select(field => field.Name);

        foreach (var stat in weaponVariableNames)
        {
            // skip dictionary variable
            if (stat == "baseStats")
            {
                continue;
            }
            // dictionary Key
            string key = stat;

            key = AddSpacesAndCapitalize(key);
            // weapon stat base value (set in weapon inspector)
            float baseValue = _currentStats.baseStats[stat];

            // FieldInfo reference to variable multipler in pickup object
            var multiplier_pick = _pickupStats.GetType().GetField(stat);
            // multiplied value
            float finalValue_pick = baseValue * (float)multiplier_pick.GetValue(_pickupStats);

            // FieldInfo reference to variable multipler in current object
            var multiplier_curr = _currentStats.GetType().GetField(stat);
            // multiplied value
            float finalValue_curr = baseValue * (float)multiplier_curr.GetValue(_currentStats);

            if (key.Contains("Max") || key.Contains("Mag"))
            {
                finalValue_curr = (float)Math.Round((double)finalValue_curr);
                finalValue_pick = (float)Math.Round((double)finalValue_pick);
            }
            else
            {
                finalValue_curr = (float)Math.Round((double)finalValue_curr, 2);
                finalValue_pick = (float)Math.Round((double)finalValue_pick, 2);
            }

            // Add values to dictionary: Key, current val, pickup val
            statsToReturn[key] = new List <float> {
                finalValue_curr, finalValue_pick
            };
        }
        return(statsToReturn);
    }
    void CompareWeapons()
    {
        RaycastHit hit;

        if (Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward * 10, out hit, inspectWeaponDist, weaponPickup, QueryTriggerInteraction.Collide))
        {
            UniqueWeaponStats pickupStats = hit.transform.GetComponent <WeaponPickup>().stats;
            string            pickupName  = hit.transform.name.Replace("_Pickup", "");
            foreach (var weapon in weapons)
            {
                if (weapon.name == pickupName && weapon.isEquipped)
                {
                    if (Input.GetKeyDown(KeyCode.E))// && hit.transform.GetComponent<WeaponPickup>())
                    {
                        Debug.Log("pickup");
                        SwitchWeapon(hit.transform.GetComponent <WeaponPickup>());
                        UI.weaponStatCompare.IsComparing = false;
                    }

                    if (!UI.weaponStatCompare.IsComparing)
                    {
                        UniqueWeaponStats currentStats;
                        // switch weapons

                        // If the weapon is spawned with unique stats
                        if (weapon.GetComponent <Weapon>().uniqueStats)
                        {
                            // get those stats
                            currentStats = weapon.GetComponent <Weapon>().uniqueStats;
                        }
                        else // create unique stats that mirror base stats
                        {
                            currentStats = ScriptableObject.CreateInstance <UniqueWeaponStats>();
                            currentStats.Init(0);
                        }

                        UI.weaponStatCompare.ShowStatComparison(pickupStats, currentStats);
                        return;
                    }
                }
            }
        }
        else
        {
            UI.weaponStatCompare.IsComparing = false;
        }
    }
Example #4
0
    public virtual void Awake()
    {
        if (spawnWithUniqueStats)
        {
            if (uniqueStats == null)
            {
                UniqueWeaponStats setup = ScriptableObject.CreateInstance <UniqueWeaponStats>();
                setup.Init(statVariation);
                uniqueStats = setup;
                ApplyUniqueWeaponStats(uniqueStats);
            }
        }
        internalCheck    = shootPoint.GetComponent <InsideCollider>();
        baseAccuracy     = accuracy;
        camStartRotation = Camera.main.transform.localRotation;
        currentReserves  = maxReserves;
        currentMag       = magSize;
        startScopeZoom   = Camera.main.fieldOfView;
        Debug.Log(Camera.main.fieldOfView);
        DefaultReload();

        startingAccuracy = accuracy;
    }
Example #5
0
    public override void Awake()
    {
        isRocket = name.Contains("Rocket") ? true : false;

        base.Awake();
        //SetBeamScale();

        if (isRocket)
        {
            rends = GetComponentsInChildren <Renderer>();
            cols  = GetComponentsInChildren <Collider>();
        }
        else
        {
            rend = GetComponentInChildren <Renderer>();
            col  = GetComponent <Collider>();
        }

        if (stats == null)
        {
            stats = ScriptableObject.CreateInstance <UniqueWeaponStats>();
            stats.Init(statVariation);
        }
    }
    public void ShowStatComparison(UniqueWeaponStats _pickupStats, UniqueWeaponStats _currentStats)
    {
        // enable backdrop
        IsComparing = true;

        // clear dictionary
        textObjects.Clear();
        textObjects.TrimExcess();

        // Create dictionary with: key(variable name) and Value 0: current stat, Value 1: pickup stat
        Dictionary <string, List <float> > weaponStatsCollated = CalculateStats(_pickupStats, _currentStats);

        // create heading text boxes, set color and alignment
        NewText("", Color.black, true);
        NewText("Current", Color.black, false);
        NewText("Pickup", Color.black, false);

        // initialize color variables to set text in foreach
        Color val0, val1;

        // Iterate through dictionary to fill instantiated text objects with correct display text
        foreach (KeyValuePair <string, List <float> > statPair in weaponStatsCollated)
        {
            if (statPair.Value[0] == statPair.Value[1])
            {
                val0 = Color.black;
                val1 = Color.black;
            }
            else if (statPair.Value[0] > statPair.Value[1])
            {
                val0 = Color.green;
                val1 = Color.red;
            }
            else
            {
                val0 = Color.red;
                val1 = Color.green;
            }

            // variable name
            NewText(statPair.Key + ":", Color.black, true);
            // current stats
            NewText(statPair.Value[0].ToString(), val0, false);
            // pickup stats
            NewText(statPair.Value[1].ToString(), val1, false);
        }
        NewText("", Color.blue, false);
        NewText(switchPickupText, Color.blue, false);

        // Set This rect to desired size of whole canvas (scale value)
        rect.sizeDelta = new Vector2(baseCanvas.width, baseCanvas.height) * scale;

        // Set Cell size to 1/totalCells of the rect area
        Vector2 cellSize = new Vector2(rect.sizeDelta.x / layout.constraintCount,
                                       rect.sizeDelta.y / ((textObjects.Count + 1) / layout.constraintCount));

        layout.cellSize = cellSize;

        // reset scale on cells (don't know why these change...)
        for (int i = 0; i < textObjects.Count; i++)
        {
            textObjects[i].GetComponent <RectTransform>().localScale = new Vector3(1, 1, 1);
        }
    }
Example #7
0
    public void ApplyUniqueWeaponStats(UniqueWeaponStats _uniqueStats)
    {
        // Get Array of all variable names in class
        var weaponVariableNames = this.GetType()
                                  .GetFields()
                                  .Select(field => field.Name);

        // Get Array of all stat multipliers in uniqueStats (paramater)
        var uniqueVariableNames = _uniqueStats.GetType()
                                  .GetFields()
                                  .Select(field => field.Name);

        _uniqueStats.baseStats.Clear();
        // Match variav\ble pairs with same name
        foreach (var multi in uniqueVariableNames)
        {
            if (weaponVariableNames.Contains(multi))
            {
                // Get field on weapon(as FieldInfo)
                var statField = this.GetType().GetField(multi);
                // Get matching field on UniqueWeaponStats class
                var statMultiplier = _uniqueStats.GetType().GetField(multi);

                try
                {
                    // Collect weapon's base stats to reapply to it later (if weapon dropped)
                    _uniqueStats.baseStats.Add(multi, Convert.ToSingle(statField.GetValue(this)));
                }
                catch
                {
                    Debug.Log("catch: " + multi);
                    _uniqueStats.baseStats.Add(multi, 0);
                }


                // if statfield is an int - cast it as such when applying value
                if (statField.GetValue(this) is int)
                {
                    int newValue = Mathf.RoundToInt(((int)statField.GetValue(this) * (float)statMultiplier.GetValue(_uniqueStats)));
                    statField.SetValue(this, newValue);
                }
                else if (statField.GetValue(this) is float)// if statfield is an float - cast it as such when applying value
                {
                    float newValue = (float)statField.GetValue(this) * (float)statMultiplier.GetValue(_uniqueStats);
                    newValue = (float)Math.Round((double)newValue, 2);
                    statField.SetValue(this, newValue);
                }
                else
                {
                    Debug.Log("SETTING ENUM");
                    Elements.Element newValue = (Elements.Element)statMultiplier.GetValue(this);
                    statField.SetValue(this, newValue);
                }
            }
            else
            {
                //Debug.Log("UniqueWeaponStats variable: " + multi + ", does not have a counterpart to mutate in weapon script");
            }
        }
        uniqueStats = _uniqueStats;
    }