Exemplo n.º 1
0
        private static AIShooter SetupAIShooter(GameObject outObject)
        {
            AIShooter  shooter = outObject.GetComponent <AIShooter>();
            tk2dSprite sprite  = outObject.GetComponent <tk2dSprite>();

            string m_CachedShooter = JsonUtility.ToJson(shooter);

            // AIShooter has some stuff setup prior to game object getting disabled. This ensures old stuff gets properly nuked.
            GunInventory m_inventory = ReflectionHelpers.ReflectGetField <GunInventory>(typeof(AIShooter), "m_inventory", shooter);
            List <PlayerHandController> m_attachedHands = ReflectionHelpers.ReflectGetField <List <PlayerHandController> >(typeof(AIShooter), "m_attachedHands", shooter);

            // Removes old Smiley/Shades gun sprite renderers from main renderer
            foreach (var gun in m_inventory.AllGuns)
            {
                sprite.DetachRenderer(gun.GetSprite());
                UnityEngine.Object.Destroy(gun.gameObject);
            }

            // just to make sure
            if (m_inventory.CurrentGun)
            {
                sprite.DetachRenderer(m_inventory.CurrentGun.GetSprite());
                UnityEngine.Object.Destroy(m_inventory.CurrentGun.gameObject);
            }

            // Gets rid of old hands
            // AIShooter had also attached the hand's renderers to the gun's sprite but since the previous gun was nuked it doesn't matter.
            foreach (PlayerHandController hand in m_attachedHands)
            {
                UnityEngine.Object.Destroy(hand.gameObject);
            }

            UnityEngine.Object.Destroy(shooter.gunAttachPoint.gameObject);
            UnityEngine.Object.Destroy(shooter);

            // Now that old AIShooter is gone we can add a new one.
            // This time host object is already inactive so don't have to worry about some unneeded trash hanging around. :P
            shooter = outObject.AddComponent <AIShooter>();
            // Restore some fields the previous AIShooter had.
            JsonUtility.FromJsonOverwrite(m_CachedShooter, shooter);

            // Setup new GunAttachPoint to avoid using any remnent of old AIShooter.
            shooter.gunAttachPoint = new GameObject("GunAttachPoint")
            {
                layer = 0
            }.transform;
            // position doesn't need to saved as its manually set to fit each west bro later
            shooter.gunAttachPoint.SetParent(outObject.transform);

            return(shooter);
        }