UnitFactory.cs 1.4 KB

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