ProjectDDD/Packages/SLUnity/UnitSytem/Unit.cs

89 lines
2.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
namespace Superlazy
{
public interface IUnit
{
SLEntity Entity { get; }
SLEntity Message(string message, SLEntity context);
}
public class Unit : IUnit
{
public SLEntity Entity { get; }
public IZone Zone { get; }
public SLEntity Player => Zone.Player;
internal List<SLEntity> startComponents;
internal SortedList<int, List<SLEntity>> components;
internal Dictionary<string, SortedList<int, List<SLEntity>>> componentMessages;
public Unit(SLEntity entity, IZone zone)
{
Entity = entity;
Zone = zone;
}
public void AddComponent<T>() where T : UnitComponent
{
AddComponent(typeof(T).Name, typeof(T).Name, SLEntity.Empty);
}
public void AddComponent<T>(string key, SLEntity context) where T : UnitComponent
{
AddComponent(key, typeof(T).Name, context);
}
public void AddComponent(string component)
{
AddComponent(component, component, SLEntity.Empty);
}
public void AddComponent(string key, string component, SLEntity context)
{
UnitHandler.AddComponent(this, key, component, context);
}
public void RemoveComponent<T>() where T : UnitComponent
{
RemoveComponent(typeof(T).Name);
}
public void RemoveComponent(string component)
{
if (Entity["Components"][component] == false) return;
Entity["Components"][component]["End"] = true;
}
public SLEntity Message(string message, SLEntity context)
{
if (context)
{
context = context.Override();
}
else
{
context = SLEntity.Empty; // TODO: 전달할때 빈거 전달한게 메모리가 아깝다면 최적화가 필요
}
context["Message"] = message;
UnitHandler.Message(message, this, context);
return context;
}
public SLEntity DispatchMessage(string message, SLEntity context, SLEntity component)
{
if (context)
{
context = context.Override();
}
else
{
context = SLEntity.Empty; // TODO: 전달할때 빈거 전달한게 메모리가 아깝다면 최적화가 필요
}
context["Message"] = message;
return UnitHandler.DispatchMessage(message, component["Component"], this, component, context);
}
}
}