public void ChangeOrAddGun(GunInventory gunInventory, int pickupObjectID) { FieldInfo m_currentGunSet = typeof(GunInventory).GetField("m_currentGun", BindingFlags.Instance | BindingFlags.NonPublic); Gun currentGun = gunInventory.CurrentGun; if (currentGun != null) { currentGun.CeaseAttack(false, null); currentGun.PostProcessProjectile -= PostProcessEnemyProjectile; currentGun.gameObject.SetActive(false); } Gun NextGun = null; // Find gun in inventory. if (gunInventory.AllGuns.Count > 0) { foreach (Gun gun in gunInventory.AllGuns) { if (gun.PickupObjectId == pickupObjectID) { NextGun = gun; break; } } } // If gun isn't in inventory, load new gun from PickupObjectDatabase and add it to inventory. if (!NextGun) { NextGun = Instantiate((PickupObjectDatabase.GetById(pickupObjectID) as Gun)); NextGun.gameObject.SetActive(false); if (NextGun) { gunInventory.AddGunToInventory(NextGun, true); gunInventory.CurrentGun.PostProcessProjectile += PostProcessEnemyProjectile; if (NextGun.IsHeroSword && !NextGun.HeroSwordDoesntBlank) { NextGun.HeroSwordDoesntBlank = true; } return; } else { return; } } // If gun was found from AllGuns list, this code will bet set. (if gun was added, this code is never reached since it's not needed) m_currentGunSet.SetValue(gunInventory, NextGun); gunInventory.CurrentGun.gameObject.SetActive(true); gunInventory.CurrentGun.PostProcessProjectile += PostProcessEnemyProjectile; if (NextGun.IsHeroSword && !NextGun.HeroSwordDoesntBlank) { NextGun.HeroSwordDoesntBlank = true; } return; }
void OnTriggerEnter(Collider col){ if (col.tag.Equals("Player")) { GunInventory inventory = col.GetComponent<GunInventory>(); if( (inventory.haveGuns[gunType])){ Debug.Log("take ammo: can get ammo"); inventory.SendMessage("addAmmo", new Vector2(ammunition, gunType)); Destroy(gameObject); } } }
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); }