public void EquipWeapon(Item weapon)
 {
     //unequip weapon if weapon is equipped
     if (equippedWeapon != null)
     {
         //remove weapon stats from play
         characterStats.RemoveStatBonus(this.weapon.GetStats());
         Destroy(equippedWeapon);
         equippedWeapon = null;
         //set appropriate animation
         animator.SetBool(weapon.animationKey, false);
     }
     //creates a new instance of a weapon and spawn it in the players hand
     else
     {
         //load game object from resources and attach to hand
         equippedWeapon = (GameObject)Instantiate(Resources.Load <GameObject>("Weapons/ScriptedWeapons/" + weapon.name), playerHand.transform.position, Quaternion.Euler(new Vector3(0, 0, 0)));
         equippedWeapon.transform.SetParent(playerHand.transform);
         //rotate and position weapon into correct position
         equippedWeapon.transform.localRotation = Quaternion.Euler(new Vector3(weapon.rotation_x, weapon.rotation_y, weapon.rotation_z));
         equippedWeapon.transform.localPosition = new Vector3(weapon.position_x, weapon.position_y, weapon.position_z);
         //set member variable to keep track of current weapon
         this.weapon = equippedWeapon.GetComponent <IWeapon>();
         //add weapon stats to player
         this.weapon.SetStats(weapon.stats);
         characterStats.AddStatBonus(weapon.stats);
         //set appropriate weapon animation
         animator.SetBool(weapon.animationKey, true);
     }
 }
    public void EquipWeapon(Item itemToEquip)
    {
        if (EquippedWeapon != null)
        {
            characterStats.RemoveStatBonus(EquippedWeapon.GetComponent <IWeapon>().Stats);
            Destroy(playerHand.transform.GetChild(0).gameObject);
        }
        EquippedWeapon = (GameObject)Instantiate(Resources.Load <GameObject>("weapons/" + itemToEquip.ObjectSlug),
                                                 playerHand.transform.position, playerHand.transform.rotation);

        equippedWeapon       = EquippedWeapon.GetComponent <IWeapon>();
        equippedWeapon.Stats = itemToEquip.Stats;

        EquippedWeapon.transform.SetParent(playerHand.transform);
        characterStats.AddStatBonus(itemToEquip.Stats);
        Debug.Log(equippedWeapon.Stats[0].GetCalculateStatValue());
    }
Example #3
0
    //public method for equipping weapons. Can be copied and editted easily to equip other things such as armour
    //passes in an item called itemToEquip
    public void EquipWeapon(Item itemToEquip)
    {
        //check if there is an item already equipped
        if (EquippedWeapon != null)
        {
            //removes any stats from previously assigned weapons
            //passes in the list of stats to remove using the IWeapon interface
            characterStats.RemoveStatBonus(EquippedWeapon.GetComponent <IWeapon>().stats);
            //destroy the item in the players hand
            Destroy(playerHand.transform.GetChild(0).gameObject);
        }

        //equip the weapon
        //sets the equipped weapon as a game object instantiated from the
        //resources folder (where weapon prefabs were saved)
        //Loads the weapon from the weapon folder using the name found in itemToEquip
        //instantiates it in the players hand and gets the position from the transform of the player hand object
        EquippedWeapon = (GameObject)Instantiate(Resources.Load <GameObject>("Weapons/" + itemToEquip.objectslug),
                                                 playerHand.transform.position + pos, playerHand.transform.rotation);

        weaponEquipped = EquippedWeapon.GetComponent <IWeapon>();

        //checks to see if the weapon is a projectile weapon
        //future versions of this will use item type
        if (EquippedWeapon.GetComponent <IProjectileWeapon>() != null)
        {
            //tells the equipped weapon that it has a projectile spawn
            EquippedWeapon.GetComponent <IProjectileWeapon> ().ProjectileSpawn = spawnProjectile;
        }

        //copies the stats list to the equipped weapon
        //this is so the stats can be removed again when the weapon is unequipped
        weaponEquipped.stats = itemToEquip.stats;
        //sets the transform of the sword as a child of the playerHand
        EquippedWeapon.transform.SetParent(playerHand.transform);
        //passes the stats of the itemToEquip to the characterStats to Add the stat bonus
        characterStats.AddStatBonus(itemToEquip.stats);

        //to check that the weapon is equipped in the console log
        Debug.Log(weaponEquipped.stats[0].GetCalculatedStatValue());
    }