G2M_CreateUnitHandler.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using PF;
  3. using UnityEngine;
  4. namespace ET
  5. {
  6. [ActorMessageHandler]
  7. public class G2M_CreateUnitHandler : AMActorRpcHandler<Scene, G2M_CreateUnit, M2G_CreateUnit>
  8. {
  9. protected override async ETTask Run(Scene scene, G2M_CreateUnit request, M2G_CreateUnit response, Action reply)
  10. {
  11. Unit unit = EntityFactory.CreateWithId<Unit>(scene, IdGenerater.GenerateId());
  12. unit.AddComponent<MoveComponent>();
  13. unit.AddComponent<UnitPathComponent>();
  14. unit.Position = new Vector3(-10, 0, -10);
  15. unit.AddComponent<MailBoxComponent>();
  16. await unit.AddLocation();
  17. unit.AddComponent<UnitGateComponent, long>(request.GateSessionId);
  18. scene.GetComponent<UnitComponent>().Add(unit);
  19. response.UnitId = unit.Id;
  20. // 广播创建的unit
  21. M2C_CreateUnits createUnits = new M2C_CreateUnits();
  22. Unit[] units = scene.GetComponent<UnitComponent>().GetAll();
  23. foreach (Unit u in units)
  24. {
  25. UnitInfo unitInfo = new UnitInfo();
  26. unitInfo.X = u.Position.x;
  27. unitInfo.Y = u.Position.y;
  28. unitInfo.Z = u.Position.z;
  29. unitInfo.UnitId = u.Id;
  30. createUnits.Units.Add(unitInfo);
  31. }
  32. MessageHelper.Broadcast(unit, createUnits);
  33. reply();
  34. }
  35. }
  36. }