G2M_CreateUnitHandler.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using ETModel;
  3. using PF;
  4. using UnityEngine;
  5. namespace ETHotfix
  6. {
  7. [MessageHandler(AppType.Map)]
  8. public class G2M_CreateUnitHandler : AMRpcHandler<G2M_CreateUnit, M2G_CreateUnit>
  9. {
  10. protected override void Run(Session session, G2M_CreateUnit message, Action<M2G_CreateUnit> reply)
  11. {
  12. RunAsync(session, message, reply).Coroutine();
  13. }
  14. protected async ETVoid RunAsync(Session session, G2M_CreateUnit message, Action<M2G_CreateUnit> reply)
  15. {
  16. M2G_CreateUnit response = new M2G_CreateUnit();
  17. try
  18. {
  19. Unit unit = ComponentFactory.CreateWithId<Unit>(IdGenerater.GenerateId());
  20. unit.AddComponent<MoveComponent>();
  21. unit.AddComponent<UnitPathComponent>();
  22. unit.Position = new Vector3(-10, 0, -10);
  23. await unit.AddComponent<MailBoxComponent>().AddLocation();
  24. unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
  25. Game.Scene.GetComponent<UnitComponent>().Add(unit);
  26. response.UnitId = unit.Id;
  27. // 广播创建的unit
  28. M2C_CreateUnits createUnits = new M2C_CreateUnits();
  29. Unit[] units = Game.Scene.GetComponent<UnitComponent>().GetAll();
  30. foreach (Unit u in units)
  31. {
  32. UnitInfo unitInfo = new UnitInfo();
  33. unitInfo.X = u.Position.x;
  34. unitInfo.Y = u.Position.y;
  35. unitInfo.Z = u.Position.z;
  36. unitInfo.UnitId = u.Id;
  37. createUnits.Units.Add(unitInfo);
  38. }
  39. MessageHelper.Broadcast(createUnits);
  40. reply(response);
  41. }
  42. catch (Exception e)
  43. {
  44. ReplyError(response, e, reply);
  45. }
  46. }
  47. }
  48. }