UnitFactory.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using Unity.Mathematics;
  2. namespace ET.Client
  3. {
  4. public static class UnitFactory
  5. {
  6. public static Unit Create(Scene currentScene, UnitInfo unitInfo, bool isActor = false)
  7. {
  8. Log.Debug($"Create unit :{unitInfo.UnitId}@{unitInfo.Type}");
  9. UnitComponent unitComponent = currentScene.GetComponent<UnitComponent>();
  10. Unit unit = unitComponent.AddChildWithId<Unit, int>(unitInfo.UnitId, unitInfo.ConfigId);
  11. unit.IsActor = isActor;
  12. unit.Position = unitInfo.Position;
  13. unit.Forward = unitInfo.Forward;
  14. unitComponent.Add(unit);
  15. NumericComponent numericComponent = unit.AddComponent<NumericComponent>();
  16. foreach (var kv in unitInfo.KV)
  17. {
  18. numericComponent.Set(kv.Key, kv.Value);
  19. }
  20. unit.AddComponent<MoveComponent>();
  21. if (unitInfo.MoveInfo != null)
  22. {
  23. if (unitInfo.MoveInfo.Points.Count > 0)
  24. {
  25. unitInfo.MoveInfo.Points[0] = unit.Position;
  26. unit.MoveToAsync(unitInfo.MoveInfo.Points).Coroutine();
  27. }
  28. }
  29. unit.AddComponent<ObjectWait>();
  30. if (unitInfo.Type == (int)UnitType.Monster)
  31. {
  32. unit.AddComponent<XunLuoPathComponent>();
  33. }
  34. EventSystem.Instance.Publish(unit.DomainScene(), new EventType.AfterUnitCreate() {Unit = unit});
  35. return unit;
  36. }
  37. }
  38. }