G2M_CreateUnitHandler.cs 1.2 KB

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