G2M_CreateUnitHandler.cs 1.5 KB

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