G2M_CreateUnitHandler.cs 1.3 KB

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