コード例 #1
0
        private void LoadWeaponWithRandAmmo(ThingWithComps gun)
        {
            CompAmmoUser compAmmo = gun.TryGetComp <CompAmmoUser>();

            if (compAmmo == null)
            {
                return;
            }
            if (!compAmmo.UseAmmo)
            {
                compAmmo.ResetAmmoCount();
                return;
            }
            // Determine ammo
            IEnumerable <AmmoDef> availableAmmo = compAmmo.Props.ammoSet.ammoTypes.Where(a => a.ammo.alwaysHaulable).Select(a => a.ammo); //Running out of options. alwaysHaulable does exist in xml.
            AmmoDef ammoToLoad = availableAmmo.RandomElementByWeight(a => a.generateCommonality);

            compAmmo.ResetAmmoCount(ammoToLoad);
        }
コード例 #2
0
        public override void SpawnSetup(Map map, bool respawningAfterLoad)      //Add mannableComp, ticksUntilAutoReload, register to GenClosestAmmo
        {
            base.SpawnSetup(map, respawningAfterLoad);
            Map.GetComponent <TurretTracker>().Register(this);

            dormantComp     = GetComp <CompCanBeDormant>();
            initiatableComp = GetComp <CompInitiatable>();
            powerComp       = GetComp <CompPowerTrader>();
            mannableComp    = GetComp <CompMannable>();

            if (!everSpawned && (!Map.IsPlayerHome || Faction != Faction.OfPlayer))
            {
                compAmmo?.ResetAmmoCount();
                everSpawned = true;
            }

            if (!respawningAfterLoad)
            {
                CELogger.Message($"top is {top?.ToString() ?? "null"}");
                top.SetRotationFromOrientation();
                burstCooldownTicksLeft = def.building.turretInitialCooldownTime.SecondsToTicks();

                //Delay auto-reload for a few seconds after spawn, so player can operate the turret right after placing it, before other colonists start reserving it for reload jobs
                if (mannableComp != null)
                {
                    ticksUntilAutoReload = minTicksBeforeAutoReload;
                }
            }

            // if (CompAmmo == null || CompAmmo.Props == null || CompAmmo.Props.ammoSet == null || CompAmmo.Props.ammoSet.ammoTypes.NullOrEmpty())
            //     return;

            // //"Subscribe" turret to GenClosestAmmo
            // foreach (var ammo in CompAmmo.Props.ammoSet.ammoTypes.Select(x => x.ammo))
            // {
            //     if (!GenClosestAmmo.listeners.ContainsKey(ammo))
            //         GenClosestAmmo.listeners.Add(ammo, new List<Building_TurretGunCE>() { this });
            //     else
            //         GenClosestAmmo.listeners[ammo].Add(this);

            //     if (!GenClosestAmmo.latestAmmoUpdate.ContainsKey(ammo))
            //         GenClosestAmmo.latestAmmoUpdate.Add(ammo, 0);
            // }
        }