Files
EE2Clone/Assets/Scripts/Authoring/UnitAuthoring.cs
ldbetteridge 58da5d1d71 Initial import
2026-03-31 15:59:23 +01:00

73 lines
3.0 KiB
C#

using Unity.Entities;
using UnityEngine;
using EE2Clone.Components;
using EE2Clone.Core;
using EE2Clone.Data;
namespace EE2Clone.Authoring
{
public class UnitAuthoring : MonoBehaviour
{
public UnitDataSO UnitData;
public class Baker : Baker<UnitAuthoring>
{
public override void Bake(UnitAuthoring authoring)
{
var data = authoring.UnitData;
if (data == null) return;
var entity = GetEntity(TransformUsageFlags.Dynamic);
AddComponent(entity, new UnitTag());
AddComponent(entity, new Health { Current = data.MaxHealth, Max = data.MaxHealth });
AddComponent(entity, new OwnerPlayer { PlayerId = 0 });
AddComponent(entity, new MovementSpeed { Value = data.MoveSpeed });
AddComponent(entity, new MoveTarget { Position = default, IsActive = false });
AddComponent(entity, new AttackData
{
Damage = data.AttackDamage,
Range = data.AttackRange,
AttackCooldown = data.AttackCooldown,
CooldownRemaining = 0
});
AddComponent(entity, new ArmorData { Value = data.Armor });
AddComponent(entity, new UnitClassComponent { Value = data.UnitClass });
AddComponent(entity, new UnitStateComponent { Value = UnitState.Idle });
AddComponent(entity, new LineOfSight { Range = data.LineOfSightRange });
AddComponent(entity, new EpochLevel { Value = data.RequiredEpoch });
AddComponent(entity, new CombatTarget { Target = Entity.Null });
// Counter bonuses
var counterBuffer = AddBuffer<CounterBonusElement>(entity);
if (data.CounterBonuses != null)
{
foreach (var bonus in data.CounterBonuses)
{
counterBuffer.Add(new CounterBonusElement
{
TargetClass = bonus.TargetClass,
Multiplier = bonus.Multiplier
});
}
}
// Citizen-specific components
if (data.UnitClass == UnitClass.Citizen)
{
AddComponent(entity, new CitizenTag());
AddComponent(entity, new CitizenStateComponent { Value = CitizenState.Idle });
AddComponent(entity, new CarriedResource
{
Type = ResourceType.Food,
Amount = 0,
MaxCarryCapacity = data.MaxCarryCapacity
});
AddComponent(entity, new GatherTarget { Target = Entity.Null });
AddComponent(entity, new BuildTarget { Target = Entity.Null });
}
}
}
}
}