G2M_CreateUnitHandler.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System;
  2. using UnityEngine;
  3. namespace ET
  4. {
  5. [ActorMessageHandler]
  6. public class G2M_CreateUnitHandler : AMActorRpcHandler<Scene, G2M_CreateUnit, M2G_CreateUnit>
  7. {
  8. protected override async ETTask Run(Scene scene, G2M_CreateUnit request, M2G_CreateUnit response, Action reply)
  9. {
  10. UnitComponent unitComponent = scene.GetComponent<UnitComponent>();
  11. Unit unit = unitComponent.AddChildWithId<Unit, int>(IdGenerater.Instance.GenerateId(), 1001);
  12. unit.Type = UnitType.Player;
  13. unit.AddComponent<MoveComponent>();
  14. unit.AddComponent<PathfindingComponent, string>("solo");
  15. unit.Position = new Vector3(-10, 0, -10);
  16. NumericComponent numericComponent = unit.AddComponent<NumericComponent>();
  17. numericComponent.Set(NumericType.Speed, 6f); // 速度是6米每秒
  18. numericComponent.Set(NumericType.AOI, 15000); // 视野15米
  19. unit.AddComponent<MailBoxComponent>();
  20. await unit.AddLocation();
  21. unit.AddComponent<UnitGateComponent, long>(request.GateSessionId);
  22. unitComponent.Add(unit);
  23. // 加入aoi
  24. unit.AddComponent<AOIEntity, int, Vector3>(15, unit.Position);
  25. M2C_CreateUnits m2CCreateUnits = new M2C_CreateUnits();
  26. m2CCreateUnits.Units.Add(UnitHelper.CreateUnitInfo(unit));
  27. MessageHelper.SendToClient(unit, m2CCreateUnits);
  28. response.MyId = unit.Id;
  29. reply();
  30. }
  31. }
  32. }