G2M_CreateUnitHandler.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 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.CreateWithId<Unit>(IdGenerater.GenerateId());
  15. unit.AddComponent<MoveComponent>();
  16. unit.AddComponent<UnitPathComponent>();
  17. unit.Position = new Vector3(-10, 0, -10);
  18. await unit.AddComponent<MailBoxComponent>().AddLocation();
  19. unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
  20. Game.Scene.GetComponent<UnitComponent>().Add(unit);
  21. response.UnitId = unit.Id;
  22. // 广播创建的unit
  23. Actor_CreateUnits createUnits = new Actor_CreateUnits();
  24. Unit[] units = Game.Scene.GetComponent<UnitComponent>().GetAll();
  25. foreach (Unit u in units)
  26. {
  27. UnitInfo unitInfo = new UnitInfo();
  28. unitInfo.X = u.Position.x;
  29. unitInfo.Y = u.Position.y;
  30. unitInfo.Z = u.Position.z;
  31. unitInfo.UnitId = u.Id;
  32. createUnits.Units.Add(unitInfo);
  33. }
  34. MessageHelper.Broadcast(createUnits);
  35. reply(response);
  36. }
  37. catch (Exception e)
  38. {
  39. ReplyError(response, e, reply);
  40. }
  41. }
  42. }
  43. }