Gun.cs
using Sandbox;
using Sandbox.Diagnostics;
public sealed class Gun : Component
{
[Property]
public GameObject BulletPrefab;
[Property]
public float FireDelay { get; set; } = 0.2f;
TimeSince bulletFired;
protected override void OnEnabled()
{
base.OnEnabled();
bulletFired = 0;
Assert.IsValid( BulletPrefab );
}
protected override void OnUpdate()
{
if ( bulletFired > FireDelay && Input.Down( "Attack1" ) )
{
Shoot();
}
}
void Shoot()
{
bulletFired = 0;
foreach ( var player in Scene.Components.GetAll<PlayerController>( FindMode.EnabledInSelfAndDescendants ) )
{
if ( !player.GetComponent<Health>().IsAlive ) continue;
var spawnPoint = player.GetComponentInChildren<ModelRenderer>().GetAttachmentObject( "hold_R" );
var cam = player.GetComponentInChildren<CameraComponent>();
var bullet = BulletPrefab.Clone( spawnPoint.WorldTransform, null, false );
bullet.WorldRotation = cam.WorldRotation;
bullet.Tags.Add( "player" );
var bulletInfo = bullet.GetComponent<Bullet>( true );
bulletInfo.Owner = player.GameObject;
bulletInfo.Weapon = GameObject;
bullet.Enabled = true;
}
}
}