93 lines
3.1 KiB
C#
93 lines
3.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using CryptonymThunder.Code.Resources;
|
|
using GameCore.Attributes;
|
|
using GameCore.Combat;
|
|
using GameCore.ECS.Interfaces;
|
|
using GameCore.Input;
|
|
using GameCore.Inventory;
|
|
using GameCore.Movement;
|
|
using GameCore.Physics;
|
|
using GameCore.Player;
|
|
using Godot;
|
|
|
|
namespace CryptonymThunder.Code.Factories;
|
|
|
|
public class ComponentFactory
|
|
{
|
|
private readonly Dictionary<Type, Func<Resource, IComponent>> _factories = new();
|
|
private readonly EffectFactory _effectFactory = new();
|
|
|
|
public ComponentFactory()
|
|
{
|
|
Register<AttributeComponentResource>(CreateAttributeComponent);
|
|
Register<WeaponComponentResource>(CreateWeaponComponent);
|
|
Register<ProjectileComponentResource>(CreateProjectileComponent);
|
|
Register<PositionComponentResource>(_ => new PositionComponent());
|
|
Register<VelocityComponentResource>(_ => new VelocityComponent());
|
|
Register<InputStateComponentResource>(_ => new InputStateComponent());
|
|
Register<PlayerComponentResource>(_ => new PlayerComponent());
|
|
Register<RotationComponentResource>(_ => new RotationComponent());
|
|
Register<CharacterStateComponentResource>(_ => new CharacterStateComponent());
|
|
Register<InventoryComponentResource>(_ => new InventoryComponent());
|
|
Register<PickupComponentResource>(CreatePickupComponent);
|
|
}
|
|
|
|
public IComponent Create(Resource resource)
|
|
{
|
|
return _factories.TryGetValue(resource.GetType(), out var factory)
|
|
? factory(resource)
|
|
: null;
|
|
}
|
|
|
|
private void Register<TResource>(Func<TResource, IComponent> factory) where TResource : Resource
|
|
{
|
|
_factories[typeof(TResource)] = res => factory((TResource)res);
|
|
}
|
|
|
|
private static AttributeComponent CreateAttributeComponent(AttributeComponentResource resource)
|
|
{
|
|
var component = new AttributeComponent();
|
|
foreach (var (attribute, value) in resource.BaseValues)
|
|
{
|
|
component.SetBaseValue(attribute, value);
|
|
}
|
|
|
|
return component;
|
|
}
|
|
|
|
private WeaponComponent CreateWeaponComponent(WeaponComponentResource resource)
|
|
{
|
|
if (resource.WeaponData == null) return null;
|
|
var weaponData = resource.WeaponData;
|
|
|
|
var onFireEffects = weaponData.OnFireEffects.Select(_effectFactory.Create).ToList();
|
|
var onHitEffects = weaponData.OnHitEffects.Select(_effectFactory.Create).ToList();
|
|
|
|
return new WeaponComponent
|
|
{
|
|
FireRate = resource.WeaponData.FireRate,
|
|
OnFireEffects = onFireEffects,
|
|
OnHitEffects = onHitEffects,
|
|
};
|
|
}
|
|
|
|
private ProjectileComponent CreateProjectileComponent(ProjectileComponentResource resource)
|
|
{
|
|
return new ProjectileComponent
|
|
{
|
|
Lifetime = resource.Lifetime,
|
|
};
|
|
}
|
|
|
|
private PickupComponent CreatePickupComponent(PickupComponentResource resource)
|
|
{
|
|
return new PickupComponent
|
|
{
|
|
ItemId = resource.ItemId,
|
|
Quantity = resource.Quantity,
|
|
IsInstantUse = resource.IsInstantUse
|
|
};
|
|
}
|
|
} |