public override async ETTask C2G_EnterMapHandler(Session session, C2G_EnterMap request, G2C_EnterMap response, Action reply) { Console.WriteLine("C2G_EnterMapHandler"); Player player = session.GetComponent <SessionPlayerComponent>().Player; var createUnitRequest = new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.InstanceId }; createUnitRequest.UnitId = player.UnitId; // 在map服务器上创建战斗Unit long mapInstanceId = StartConfigComponent.Instance.GetByName("Map1").SceneInstanceId; var createUnit = (M2G_CreateUnit)await ActorMessageSenderComponent.Instance.Call(mapInstanceId, createUnitRequest); player.UnitId = createUnit.UnitId; DBComponent.Instance.Save(player).Coroutine(); response.UnitId = createUnit.UnitId; reply(); }
public virtual async ETTask G2M_CreateUnitHandler(Scene scene, G2M_CreateUnit request, M2G_CreateUnit response, Action reply) { }
public override async ETTask G2M_CreateUnitHandler(Scene scene, G2M_CreateUnit request, M2G_CreateUnit response, Action reply) { var copyMap = Game.Scene.Children.Values.ToList().Find((x) => { if (x is Scene s) { return(s.Name == "CopyMap1"); } return(false); }); if (copyMap == null) { var copyMapConfig = StartConfigComponent.Instance.GetByName("CopyMap1"); copyMap = await SceneFactory.Create(Game.Scene, copyMapConfig.GetComponent <SceneConfig>().Name, SceneType.Map); } Unit unit = null; if (request.UnitId != 0) { unit = await DBComponent.Instance.Query <Unit>(request.UnitId); unit.Domain = copyMap; } else { unit = EntityFactory.CreateWithId <Unit>(copyMap, IdGenerater.GenerateId()); unit.PlayerId = request.PlayerId; unit.Setup(); unit.Save().Coroutine(); } unit.AddComponent <MoveComponent>(); unit.AddComponent <Body2dComponent>().CreateBody(.5f, .5f); unit.AddComponent <MailBoxComponent>(); await unit.AddLocation(); unit.AddComponent <UnitGateComponent, long>(request.GateSessionId); copyMap.GetComponent <UnitComponent>().Add(unit); response.UnitId = unit.Id; // 广播创建的unit var inViewUnitsMsg = new M2C_InViewUnits(); var enterViewMsg = new M2C_OnEnterView(); Unit[] units = copyMap.GetComponent <UnitComponent>().GetAll(); foreach (Unit u in units) { var entityInfo = new EntiyInfo(); entityInfo.BsonBytes = new Google.Protobuf.ByteString(); entityInfo.BsonBytes.bytes = MongoHelper.ToBson(u); entityInfo.Type = EntityDefine.GetTypeId <Unit>(); if (u.Id == unit.Id) { enterViewMsg.EnterEntity = entityInfo; inViewUnitsMsg.SelfUnit = entityInfo.BsonBytes; continue; } inViewUnitsMsg.InViewEntitys.Add(entityInfo); } var monsters = copyMap.GetComponent <MonsterComponent>().GetAll(); foreach (var u in monsters) { var entityInfo = new EntiyInfo(); entityInfo.BsonBytes = new Google.Protobuf.ByteString(); entityInfo.BsonBytes.bytes = MongoHelper.ToBson(u); entityInfo.Type = EntityDefine.GetTypeId <Monster>(); inViewUnitsMsg.InViewEntitys.Add(entityInfo); } MessageHelper.BroadcastToOther(unit, enterViewMsg); MessageHelper.Send(unit, inViewUnitsMsg); reply(); }