G2M_CreateUnitHandler.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using ETModel;
  3. using Google.Protobuf;
  4. namespace ETHotfix
  5. {
  6. [MessageHandler(AppType.Map)]
  7. public class G2M_CreateUnitHandler : AMRpcHandler<G2M_CreateUnit, M2G_CreateUnit>
  8. {
  9. protected override async void Run(Session session, G2M_CreateUnit message, Action<M2G_CreateUnit> reply)
  10. {
  11. M2G_CreateUnit response = new M2G_CreateUnit();
  12. try
  13. {
  14. Unit unit = ComponentFactory.Create<Unit>();
  15. await unit.AddComponent<MailBoxComponent>().AddLocation();
  16. unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
  17. Game.Scene.GetComponent<UnitComponent>().Add(unit);
  18. response.UnitId = unit.Id;
  19. response.Count = Game.Scene.GetComponent<UnitComponent>().Count;
  20. reply(response);
  21. if (response.Count == 2)
  22. {
  23. Actor_CreateUnits actorCreateUnits = new Actor_CreateUnits();
  24. Unit[] units = Game.Scene.GetComponent<UnitComponent>().GetAll();
  25. foreach (Unit u in units)
  26. {
  27. actorCreateUnits.Units.Add(new UnitInfo() {UnitId = u.Id, X = (int)(u.Position.X * 1000), Z = (int)(u.Position.Z * 1000) });
  28. }
  29. MessageHelper.Broadcast(actorCreateUnits);
  30. }
  31. }
  32. catch (Exception e)
  33. {
  34. ReplyError(response, e, reply);
  35. }
  36. }
  37. }
  38. }