public Weapon() : base() { Type type = GetType(); Info.LibraryName = Utils.GetLibraryName(type); WeaponAttribute weaponAttribute = Utils.GetAttribute <WeaponAttribute>(type); if (weaponAttribute != null) { CarriableInfo.Category = weaponAttribute.Category; Primary.AmmoName = weaponAttribute.PrimaryAmmoName ?? Primary.AmmoName; if (Secondary != null) { Secondary.AmmoName = weaponAttribute.SecondaryAmmoName ?? Secondary.AmmoName; } } Tags.Add(IItem.ITEM_TAG); Init(); CurrentClip = Primary; }
public void Activate() { List <Type> ammoTypes = Utils.GetTypesWithAttribute <Ammo, SpawnableAttribute>(); if (Category != null) { List <Type> filteredTypes = new(); // Just spawn ammo that has spawnable weapons List <Type> wepTypes = Utils.GetTypesWithAttribute <Weapon, SpawnableAttribute>(); foreach (Type ammoType in ammoTypes) { foreach (Type wepType in wepTypes) { WeaponAttribute weaponAttribute = Utils.GetAttribute <WeaponAttribute>(wepType); if (weaponAttribute != null && weaponAttribute.Category == Category && weaponAttribute.PrimaryAmmoName == Utils.GetLibraryName(ammoType)) { if (!filteredTypes.Contains(ammoType)) { filteredTypes.Add(ammoType); } } } } ammoTypes = filteredTypes; } if (ammoTypes.Count <= 0) { return; } Type typeToSpawn = ammoTypes[Utils.RNG.Next(ammoTypes.Count)]; Ammo ent = Utils.GetObjectByType <Ammo>(typeToSpawn); ent.Position = Position; ent.Rotation = Rotation; ent.Spawn(); }
public virtual void Activate() { List <Type> wepTypes = Utils.GetTypesWithAttribute <Weapon, SpawnableAttribute>(); if (Category != null) { List <Type> filteredTypes = new(); foreach (Type type in wepTypes) { WeaponAttribute weaponAttribute = Utils.GetAttribute <WeaponAttribute>(type); if (weaponAttribute != null && weaponAttribute.Category == Category) { filteredTypes.Add(type); } } wepTypes = filteredTypes; } if (wepTypes.Count <= 0) { return; } Type weaponTypeToSpawn = Utils.RNG.FromList(wepTypes); Weapon weapon = Utils.GetObjectByType <Weapon>(weaponTypeToSpawn); if (weapon == null || !weapon.IsValid) { Log.Debug($"Failed to initialize random weapon of type '{weaponTypeToSpawn}': {weapon}"); return; } weapon.Position = Position; weapon.Rotation = Rotation; weapon.Spawn(); string ammoName = weapon.Primary.AmmoName; if (ammoName == null) { return; // If the choosen weapon doesn't use ammo we don't need to spawn any } Type ammoType = Utils.GetTypeByLibraryName <Ammo>(ammoName); if (ammoType == null) { return; // If the choosen weapon doesn't use ammo we don't need to spawn any } if (!ammoType.IsSubclassOf(typeof(Ammo))) { Log.Error($"The defined ammo type {ammoType.Name} for the weapon {weapon.Info.LibraryName} is not a descendant of {typeof(Ammo).Name}."); return; } for (int i = 0; i < AmmoToSpawn; i++) { Ammo ammo = Utils.GetObjectByType <Ammo>(ammoType); ammo.Position = weapon.Position + Vector3.Up * AMMO_DISTANCE_UP; ammo.Spawn(); } }