Barrel

The Barrel is the core extension of a weapon that defines how it shoots. It controls spawn points, shooting modes, firing rate, damage, and triggers all visual/audio cues via UnityEvents.

Warning

Barrel is an abstract base class. Do not use it directly — use a subclass like RaycastBarrel or ProjectileBarrel.

Summary

This component:

  • Defines spawn points for bullets or effects
  • Holds its firing stats in config, replaceable at runtime with ApplyConfig
  • Fires grouped UnityEvents on start, shot, and end
  • Loops through multiple spawn points if defined
  • Meant to be extended for specific firing behavior

Fields

FieldTypeDescription
_configBarrelStatsFiring stats: bulletsFired, fireRate, damage (MinMaxCurve).
_spawnPointsList<Transform>Locations used to spawn projectiles or traces.
eventsBarrelEventsThe barrel’s UnityEvents.

Properties

PropertyTypeDescription
configBarrelStatsThe current firing stats. Read-only; use ApplyConfig to change them.
ownerGameObjectWho fires this barrel, reported as the instigator of its damage. The weapon sets it to itself; without one it is the barrel’s own GameObject.
FireRatefloatShots per second.
BulletsFiredintBullets per shot, and the ammo one shot uses.

Unity Events

EventDescription
events.OnStartShootingCalled when shooting begins.
events.OnShotCalled when a shot is fired.
events.OnEndShootingCalled when shooting ends.

Methods

MethodDescription
void ApplyConfig(BarrelConfig config)Copies a BarrelConfig’s stats onto the barrel.
void SetSpawnPoints(IEnumerable<Transform> points)Replaces the spawn points, e.g. for upgrades, and starts again from the first one.
virtual void StartShooting()Triggers events.OnStartShooting. Override for custom behavior.
virtual void StopShooting()Triggers events.OnEndShooting. Override for custom behavior.
virtual void Shoot()Called when it’s time to fire. Advances the spawn point and triggers events.OnShot. Override to implement projectile or ray logic.
void Prepare(Action proceed, Action cancel)Called by the weapon when the trigger is pulled. Runs the barrel modifiers, then calls proceed once all of them proceeded; cancel stops the shooting.
protected Transform GetSpawnPoint(int bullet)The spawn point of one bullet of the current shot.
protected int RollDamage()Rolls the damage of one bullet from config.damage.

Spawn Point Behavior

Each bullet of a shot takes the next spawn point in order, wrapping around when there are more bullets than points, and the next shot starts on the following point. With no spawn points, bullets come from the barrel’s own transform.

BarrelConfig

BarrelConfig is a ScriptableObject (Create > Glowing Rifts > Modular Weapon System > Barrel Config) with the same fields as BarrelStats. Apply one at runtime for power-ups or upgrade levels:

[SerializeField] private Barrel _barrel;
[SerializeField] private BarrelConfig _poweredUp;

public void OnPowerUp() => _barrel.ApplyConfig(_poweredUp);

Barrel Modifiers

Barrel is a Modifiable (from GlowingRifts.ModularHealthSystem). Components deriving from BarrelModifier on the same GameObject hook into it while enabled, and the barrel runs them before every shooting and when the trigger is released. FiringMode and Spool are the built-in ones.

To hold or block shooting, derive from BarrelModifier and implement BeforeShooting:

using System;
using GlowingRifts.ModularWeaponSystem;

public class SafetyCatch : BarrelModifier
{
    public bool safetyOn;

    public override void BeforeShooting(Action proceed, Action cancel)
    {
        if (safetyOn) cancel();
        else proceed();
    }
}

Note

Call proceed when the shot may go ahead (it can be later, like the spool does) or cancel to stop it. With several modifiers, the barrel fires once all of them proceeded. Once one cancels, the rest aren’t asked.

To react to the trigger release, override BeforeStopping. Call stop to let the barrel stop; hold it to keep shooting, like a burst does. The default stops right away.

public override void BeforeStopping(Action stop)
{
    stop();
}

The weapon runs them through barrel.Prepare(proceed, cancel) when the trigger is pulled and barrel.Release(stop) when it is released.

Usage Example

Do not use Barrel directly. Instead:

public class MyBarrel : Barrel
{
    public override void Shoot()
    {
        base.Shoot();
        for (int i = 0; i < BulletsFired; i++)
        {
            Transform origin = GetSpawnPoint(i);
            int damage = RollDamage();
            // Fire from origin here
        }
    }
}

See Also