Add weapon database and equipment component resources with associated services

This commit is contained in:
2025-10-29 01:26:45 +01:00
parent 87169b17ae
commit 626f81cd85
16 changed files with 245 additions and 11 deletions

View File

@@ -18,10 +18,12 @@ namespace CryptonymThunder.Code.Factories;
public class ComponentFactory
{
private readonly Dictionary<Type, Func<Resource, IComponent>> _factories = new();
private readonly EffectFactory _effectFactory = new();
private readonly EffectFactory _effectFactory;
public ComponentFactory()
public ComponentFactory(EffectFactory effectFactory)
{
_effectFactory = effectFactory;
Register<AttributeComponentResource>(CreateAttributeComponent);
Register<WeaponComponentResource>(CreateWeaponComponent);
Register<ProjectileComponentResource>(CreateProjectileComponent);
@@ -33,6 +35,7 @@ public class ComponentFactory
Register<CharacterStateComponentResource>(_ => new CharacterStateComponent());
Register<InventoryComponentResource>(_ => new InventoryComponent());
Register<PickupComponentResource>(CreatePickupComponent);
Register<EquipmentComponentResource>(_ => new EquipmentComponent());
}
public IComponent Create(Resource resource)

View File

@@ -12,6 +12,7 @@ using GameCore.Events;
using GameCore.Input;
using GameCore.Inventory;
using GameCore.Movement;
using GameCore.Player;
using Godot;
namespace CryptonymThunder.Code.Presenters;
@@ -20,6 +21,7 @@ namespace CryptonymThunder.Code.Presenters;
public partial class GamePresenter : Node
{
[Export] private ArchetypeDatabase ArchetypesDatabase { get; set; }
[Export] private WeaponDatabase WeaponDatabase { get; set; }
[Export] private EntityArchetype PlayerArchetype { get; set; }
[Export] private SimulationConfigResource SimulationConfig { get; set; }
@@ -45,8 +47,12 @@ public partial class GamePresenter : Node
}
_world = new World(_inputService, _worldQuery, simConfig);
var effectFactory = new EffectFactory();
var componentFactory = new ComponentFactory(effectFactory);
var weaponDataService = new GodotWeaponDataService(WeaponDatabase, effectFactory);
_presenterFactory = new PresenterFactory(_world, new ComponentFactory(), _presenterRegistry, this);
_presenterFactory = new PresenterFactory(_world, componentFactory, _presenterRegistry, this);
_world.RegisterSystem(new PlayerInputSystem());
_world.RegisterSystem(new RotationSystem());
@@ -58,6 +64,9 @@ public partial class GamePresenter : Node
_world.RegisterSystem(new PickupSystem());
_world.RegisterSystem(new InventorySystem(_world));
_world.RegisterSystem(new ItemAcquisitionSystem(_world));
_world.RegisterSystem(new WeaponAcquisitionSystem(_world));
_world.RegisterSystem(new WeaponSwapSystem());
_world.RegisterSystem(new EquipmentSystem(_world, weaponDataService));
_world.RegisterSystem(new WeaponSystem());
_world.RegisterSystem(new ProjectileSystem());
@@ -69,6 +78,15 @@ public partial class GamePresenter : Node
_world.Subscribe<EntityDiedEvent>(OnEntityDied);
_world.Subscribe<SpawnEntityEvent>(OnSpawnEntity);
_world.Subscribe<EquipWeaponEvent>(OnWeaponEquipped);
_world.Subscribe<WeaponFiredEvent>(e =>
{
GD.Print($"Weapon fired by Entity ID: {e.Owner.Id}");
});
_world.Subscribe<WeaponFireFailedEvent>(e =>
{
GD.Print($"Weapon fire failed!");
});
RegisterAllSceneEntities();
@@ -76,7 +94,12 @@ public partial class GamePresenter : Node
_presenters.Add(playerData.Entity.Id, playerData.Presenter);
_presenterComponents.Add(playerData.Entity.Id, playerData.Components);
}
private void OnWeaponEquipped(EquipWeaponEvent e)
{
GD.Print($"Weapon equipped: {e.NewWeaponItemId} for Entity ID: {e.Owner.Id}");
}
public override void _Input(InputEvent @event)
{
_inputService?.HandleInputEvent(@event);

View File

@@ -0,0 +1,9 @@
using Godot;
namespace CryptonymThunder.Code.Resources;
[GlobalClass]
public partial class EquipmentComponentResource : Resource
{
[Export] public Godot.Collections.Array<string> StartingWeapons { get; set; } = [];
}

View File

@@ -0,0 +1 @@
uid://d0osn7rv6mqa0

View File

@@ -0,0 +1,10 @@
using Godot;
using Godot.Collections;
namespace CryptonymThunder.Code.Resources;
[GlobalClass]
public partial class WeaponDatabase : Resource
{
[Export] public Dictionary<string, WeaponResource> WeaponData { get; set; } = new();
}

View File

@@ -0,0 +1 @@
uid://n4m7jn4mysrt

View File

@@ -14,6 +14,8 @@ public class GodotInputService : IInputService
public bool IsJumping { get; private set; }
public GameCoreMath MoveDirection { get; private set; }
public GameCoreMath LookDirection { get; private set; }
public bool IsSwapWeaponNext { get; private set; }
public bool IsSwapWeaponPrevious { get; private set; }
public void HandleInputEvent(InputEvent e)
{
@@ -38,10 +40,15 @@ public class GodotInputService : IInputService
IsJumping = Input.IsActionJustPressed("jump");
IsFiring = Input.IsActionPressed("fire");
IsInteracting = Input.IsActionPressed("interact");
IsSwapWeaponNext = Input.IsActionJustPressed("swap_weapon_next");
IsSwapWeaponPrevious = Input.IsActionJustPressed("swap_weapon_previous");
}
public void LateUpdate()
{
LookDirection = GameCoreMath.Zero;
IsSwapWeaponNext = false;
IsSwapWeaponPrevious = false;
}
}

View File

@@ -0,0 +1,55 @@
using System.Linq;
using CryptonymThunder.Code.Factories;
using CryptonymThunder.Code.Resources;
using GameCore.Combat;
using GameCore.Combat.Interfaces;
using Godot;
namespace CryptonymThunder.Code.Services;
public class GodotWeaponDataService : IWeaponDataService
{
private readonly WeaponDatabase _database;
private readonly EffectFactory _effectFactory;
public GodotWeaponDataService(WeaponDatabase database, EffectFactory effectFactory)
{
_database = database;
_effectFactory = effectFactory;
}
public WeaponData GetWeaponData(string weaponId)
{
if (!_database.WeaponData.TryGetValue(weaponId, out var weaponResource))
{
GD.PrintErr($"Weapon ID not found in database: {weaponId}");
return null;
}
var fireCosts = weaponResource.FireCosts
.Select(_effectFactory.Create)
.OfType<ICostEffect>()
.ToList();
var onFireEffects = weaponResource.OnFireEffects
.Select(_effectFactory.Create)
.ToList();
var onHitEffects = weaponResource.OnHitEffects
.Select(_effectFactory.Create)
.ToList();
return new WeaponData(
weaponResource.FireRate,
fireCosts,
onFireEffects,
onHitEffects
);
}
public bool TryGetWeaponData(string weaponId, out WeaponData weaponData)
{
weaponData = GetWeaponData(weaponId);
return weaponData != null;
}
}

View File

@@ -0,0 +1 @@
uid://4jlk57176ic5