Skip to content

Weapon

namespace Griff.Modules.FirstPerson;
public sealed class Weapon : Component

A gun held in first person, with no code: Fire shoots where the player looks. It hits the first thing in the way straight away (or launches a Projectile), hurting anything with Health. Several weapons on one player are picked with the number keys (their Slot) or the mouse wheel. Put it on the entity a FirstPersonView looks through.

Ammo comes from the player’s Inventory: a weapon with Ammo “Shells” uses up a shell a shot, a Pickup with Item “Shells” gives more, and a Counter with Item “Shells” shows how many are left. A weapon with NeedsItem can’t be used until it’s picked up, and is taken out as soon as it is.

This is a component: its saved properties are on its component page.

public string? Action { get; set; }

The input action that fires it, e.g. “Fire” (the left mouse button, J, or the gamepad’s right trigger).

public string? Ammo { get; set; }

The Inventory item each shot uses up, e.g. “Bullets”. Empty never runs out.

public bool Automatic { get; set; }

Keep firing while the action is held, like a machine gun. Otherwise it’s one shot a press.

public Color Color { get; set; }

The gun’s color when it has no Image (or a tint over the image).

public float Damage { get; set; }

Hit points each bullet (or pellet) takes away.

public string? EmptySound { get; set; }

A click when it’s out of ammo.

public string? FireImage { get; set; }

The picture shown for a moment as it fires, e.g. with a flash at the muzzle. Empty uses Image.

public float FireRate { get; set; }

Shots a second, at most.

public string? Image { get; set; }

The picture of the weapon in hand, drawn at the bottom of the screen. Without one it’s drawn as a simple gun in Color.

public string? ImpactEffect { get; set; }

A prefab spawned where a bullet hits, e.g. “Puff”: sparks off a wall, a splash on an enemy.

public bool IsCoolingDown { get; }

True while it can’t fire again yet.

public float Kick { get; set; }

How far it jumps back as it fires, as a fraction of its size.

public float Knockback { get; set; }

How hard a hit pushes what it hits, in units per second. 0 doesn’t push.

public Color MuzzleFlash { get; set; }

The color of the flash at the muzzle as it fires. Transparent for none.

public string? Name { get; set; }

The weapon’s name, e.g. “Shotgun”.

public string? NeedsItem { get; set; }

An item the player must have to use it, e.g. “Shotgun”, given by a Pickup. Empty is always ready.

public int Pellets { get; set; }

Bullets in each shot: 1 for a pistol, 7 or so for a shotgun.

public string? Projectile { get; set; }

A prefab to launch instead of hitting straight away, e.g. “Rocket” or “Plasma”. It needs a Body to fly.

public float ProjectileSpeed { get; set; }

How fast a Projectile flies, in world units per second.

public float Range { get; set; }

How far bullets reach, in world units.

public float Shake { get; set; }

Shakes the screen by this much with each shot. 0 for none.

public int Shots { get; set; }

How many shots it has fired. The game counts them.

public float Size { get; set; }

How big it’s held, as a fraction of the screen’s height.

public int Slot { get; set; }

The number key that takes it out, 1 to 9.

public string? Sound { get; set; }

The sound of a shot, e.g. “Sounds/shoot.wav”.

public float Spread { get; set; }

Degrees the bullets scatter across.

public void Fire()

Fires it once, as pressing its Action does, if it’s the weapon in its player’s hands and ready, using up its Ammo, whether or not the player has the input: a note hit on the beat, a card or a trigger fires it.

public event Action<Weapon>? Fired

Raised with each shot fired.

public event Action<Entity>? Hit

Raised when a bullet hits something, with what it hit (a wall’s tilemap, or something with health).