kakozuzo/Code/swb_base/WeaponRegistry.cs
2024-10-30 19:01:58 +03:00

56 lines
1.2 KiB
C#

using System.Collections.Generic;
namespace SWB.Base;
/*
* Attach this component somewhere in the root of your scene.
* Register all weapons that you want to use in here
*/
[Group( "SWB" )]
[Title( "Weapon Registry" )]
public class WeaponRegistry : Component
{
[Property] public List<PrefabScene> WeaponPrefabs { get; set; } = new();
public Dictionary<string, GameObject> Weapons { get; set; } = new();
static public WeaponRegistry Instance
{
get
{
return Game.ActiveScene.Components.GetInChildren<WeaponRegistry>();
}
}
protected override void OnAwake()
{
WeaponPrefabs.ForEach( weaponPrefab =>
{
var weaponGO = weaponPrefab.Clone();
weaponGO.SetParent( this.GameObject );
weaponGO.Enabled = false;
var weapon = weaponGO.Components.Get<Weapon>( true );
Weapons.TryAdd( weapon.ClassName, weaponGO );
weaponGO.Name = weapon.ClassName;
} );
}
public GameObject Get( string className )
{
if ( className is null ) return null;
Weapons.TryGetValue( className, out var weaponGO );
return weaponGO;
}
public Weapon GetWeapon( string className )
{
var weaponGO = Get( className );
if ( weaponGO is null ) return null;
return weaponGO.Components.Get<Weapon>( true );
}
}