public static Bullet Fire(this Unit self, UnitOperation message) { var bullet = EntityFactory.Create <Bullet>(self.Domain); bullet.OwnerId = self.Id; bullet.Setup(self.Position); var msg = new M2C_OnEnterView(); msg.EnterEntity = new EntiyInfo(); msg.EnterEntity.Type = EntityDefine.GetTypeId <Bullet>(); msg.EnterEntity.BsonBytes.bytes = MongoHelper.ToBson(bullet); var p = bullet.Position; msg.X = (int)(p.x * 100); msg.Y = (int)(p.y * 100); msg.Z = (int)(p.z * 100); MessageHelper.Broadcast(self.Domain, msg); var targetPoint = new Vector3(); targetPoint.x = message.IntParams[0] / 100f; targetPoint.y = message.IntParams[1] / 100f; targetPoint.z = message.IntParams[2] / 100f; bullet.MoveTo(targetPoint); return(bullet); }
public static void Destroy(this Monster self) { MessageHelper.Broadcast(self.Domain, new M2C_OnLeaveView() { LeaveEntity = self.Id, EntityType = EntityDefine.GetTypeId(self.GetType()) }); }
public static async ETTask <ETModel.Entity> OnEnterView(EntiyInfo entityInfo) { try { Log.Debug($"{EntityDefine.GetType(entityInfo.Type).Name}"); if (entityInfo.Type == EntityDefine.GetTypeId <Unit>()) { var remoteUnit = MongoHelper.FromBson <Unit>(entityInfo.BsonBytes.bytes); foreach (var item in remoteUnit.Components) { Log.Debug($"remoteUnit {item.GetType().Name}"); } //remoteUnit.Domain = ETModel.Game.Scene; //Unit unit = UnitFactory.Create(ETModel.Game.Scene, remoteUnit.Id); var go = UnityEngine.Object.Instantiate(PrefabHelper.GetUnitPrefab("RemoteUnit")); go.transform.position = remoteUnit.Position; GameObject.DontDestroyOnLoad(go); //var unit = EntityFactory.CreateWithId<Unit>(domain, id); ETModel.Game.EventSystem.RegisterSystem(remoteUnit); ETModel.Game.EventSystem.Awake(remoteUnit); remoteUnit.Awake(go); UnitComponent.Instance.Add(remoteUnit); remoteUnit.Position = remoteUnit.Position; //remoteUnit.Dispose(); return(remoteUnit); } if (entityInfo.Type == EntityDefine.GetTypeId <Bullet>()) { var remoteBullet = MongoHelper.FromBson <Bullet>(entityInfo.BsonBytes.bytes); //Log.Debug($"{remoteBullet}"); //var bullet = ETModel.EntityFactory.CreateWithId<Bullet>(ETModel.Game.Scene, remoteBullet.Id); Log.Debug($"{remoteBullet.Components.Count}"); ETModel.Game.EventSystem.RegisterSystem(remoteBullet); ETModel.Game.EventSystem.Awake(remoteBullet); BulletComponent.Instance.Add(remoteBullet); //remoteBullet.Dispose(); return(remoteBullet); } if (entityInfo.Type == EntityDefine.GetTypeId <Monster>()) { var remote = MongoHelper.FromBson <Monster>(entityInfo.BsonBytes.bytes); Log.Debug($"HealthComponent HP{remote.GetComponent<HealthComponent>().HP}"); remote.Awake(); remote.Domain = ETModel.Game.Scene; remote.BodyView = GameObject.Instantiate(PrefabHelper.GetUnitPrefab("Monster")); GameObject.DontDestroyOnLoad(remote.BodyView); //var monster = MonsterFactory.Create(ETModel.Game.Scene, remote.Id); MonsterComponent.Instance.Add(remote); //remote.Position = remote.Position; //remote.Dispose(); return(remote); } } catch (System.Exception e) { Log.Error(e); } return(null); }
public override async ETTask C2M_SetEntityPropertyHandler(Unit unit, C2M_SetEntityProperty message) { Log.Msg(message); var propertyCollection = EntityDefine.PropertyCollectionMap[EntityDefine.GetTypeId <Unit>()]; if (propertyCollection.ContainsKey((ushort)message.PropertyId)) { unit.SetPropertyValue((ushort)message.PropertyId, message.PropertyValue.bytes); } await ETTask.CompletedTask; }
public override async ETTask M2C_OnEntityChangedHandler(ETModel.Session session, M2C_OnEntityChanged message) { Log.Debug($"M2C_OnEntityChangedHandler {Dumper.DumpAsString(message)}"); var entityType = EntityDefine.TypeIds.GetKeyByValue((ushort)message.EntityType); ETModel.Entity entity = null; if (entityType == typeof(Unit)) { entity = UnitComponent.Instance.Get(message.EntityId); } if (entityType == typeof(Bullet)) { var bullet = BulletComponent.Instance.Get(message.EntityId); entity = bullet; if (entity is null) { return; } if (bullet.BodyView != null) { bullet.BodyView.transform.DOMove(new Vector3(message.X / 100f, message.Y / 100f, message.Z / 100f), 0.2f); } } if (entityType == typeof(Monster)) { var monster = MonsterComponent.Instance.Get(message.EntityId); entity = monster; if (entity is null) { return; } if (monster.BodyView != null) { monster.BodyView.transform.DOMove(new Vector3(message.X / 100f, message.Y / 100f, message.Z / 100f), 0.2f); } } if (entity is null) { return; } if (message.ComponentType > 0) { entity = entity.GetComponent(EntityDefine.GetType(message.ComponentType)); } var propertyCollection = EntityDefine.PropertyCollectionMap[EntityDefine.GetTypeId(entity.GetType())]; if (propertyCollection.ContainsKey((ushort)message.PropertyId)) { entity.SetPropertyValue((ushort)message.PropertyId, message.PropertyValue.bytes); } await ETTask.CompletedTask; }
public override async ETTask M2C_OnLeaveViewHandler(ETModel.Session session, M2C_OnLeaveView message) { if (message.EntityType == EntityDefine.GetTypeId <Unit>()) { UnitComponent.Instance.Remove(message.LeaveEntity); } if (message.EntityType == EntityDefine.GetTypeId <Bullet>()) { var bullet = BulletComponent.Instance.Get(message.LeaveEntity); if (bullet != null) { BulletComponent.Instance.Remove(message.LeaveEntity); } } await ETTask.CompletedTask; }
public static void Update(this EntitySyncComponent self) { if (TimeHelper.Now() - self.Timer > self.Interval) { self.Timer = TimeHelper.Now(); var transform = self.Parent.GetComponent <TransformComponent>(); var lp = transform.lastPosition; var p = transform.position; if (Vector3.Distance(lp, p) < 0.1f) { return; } transform.lastPosition = p; var msg = new M2C_OnEntityChanged(); msg.EntityId = self.Id; msg.EntityType = EntityDefine.GetTypeId(self.Parent.GetType()); msg.X = (int)(p.x * 100); msg.Y = (int)(p.y * 100); msg.Z = (int)(p.z * 100); MessageHelper.Broadcast(self.Domain, msg); } }
public static void OnPropertyChanged(Entity entity, string propertyName, byte[] valueBytes) { var type = entity.GetType(); Entity component = null; if (EntityDefine.IsComponent(type)) { component = entity; entity = entity.GetParent <Entity>(); } var entityTypeId = EntityDefine.GetTypeId(entity.GetType()); //if (component != null) //{ //} //if (!EntityDefine.PropertyDefineCollectionMap.ContainsKey(entityTypeId)) //{ // Log.Error($"EntityDefine.PropertyDefineCollectionMap has no key {entityTypeId}"); // return; //} //if (!EntityDefine.PropertyDefineCollectionMap[entityTypeId].ContainsKey(propertyName)) //{ // Log.Error($"EntityDefine.PropertyDefineCollectionMap[{entityTypeId}] has no key {propertyName}"); // return; //} var msg = new M2C_OnEntityChanged(); PropertyDefineAttribute attr; if (component != null) { msg.ComponentType = EntityDefine.GetTypeId(component.GetType()); attr = EntityDefine.PropertyDefineCollectionMap[msg.ComponentType][propertyName]; } else { attr = EntityDefine.PropertyDefineCollectionMap[entityTypeId][propertyName]; } msg.EntityId = entity.Id; msg.EntityType = entityTypeId; msg.PropertyId = attr.Id; msg.PropertyValue.bytes = valueBytes; Log.Error($"MessageHelper OnPropertyChanged {type} {propertyName} flag={attr.Flag} "); if (attr.Flag == SyncFlag.AllClients) { Broadcast(entity.Domain, msg); } if (attr.Flag == SyncFlag.OtherClients) { if (entity is Unit u) { BroadcastToOther(u, msg); } } if (attr.Flag == SyncFlag.OwnClient) { if (entity is Unit u) { Send(u, msg); } } }
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(); }
public static void Start() { #if ILRuntime if (!Define.IsILRuntime) { Log.Error("mono层是mono模式, 但是Hotfix层是ILRuntime模式"); } #else if (Define.IsILRuntime) { Log.Error("mono层是ILRuntime模式, Hotfix层是mono模式"); } #endif try { // 注册热更层回调 ETModel.Game.Hotfix.Update = () => { Update(); }; ETModel.Game.Hotfix.LateUpdate = () => { LateUpdate(); }; ETModel.Game.Hotfix.OnApplicationQuit = () => { OnApplicationQuit(); }; OpcodeHelper.ignoreDebugLogMessageSet.Add(HotfixOpcode.UnitOperation); OpcodeHelper.ignoreDebugLogMessageSet.Add(HotfixOpcode.M2C_OnEntityChanged); Game.Scene.AddComponent <UIComponent>(); Game.Scene.AddComponent <OpcodeTypeComponent>(); Game.Scene.AddComponent <MessageDispatcherComponent>(); // 加载热更配置 ETModel.Game.Scene.GetComponent <ResourcesComponent>().LoadBundle("config.unity3d"); Game.Scene.AddComponent <ConfigComponent>(); ETModel.Game.Scene.GetComponent <ResourcesComponent>().UnloadBundle("config.unity3d"); UnitConfig unitConfig = (UnitConfig)Game.Scene.GetComponent <ConfigComponent>().Get(typeof(UnitConfig), 1001); Log.Debug($"config {JsonHelper.ToJson(unitConfig)}"); Game.EventSystem.Run(EventIdType.InitSceneStart); HandlersHelper.Instance = new HandlersHelper(); EntityDefine.OnPropertyChanged += (entity, name, value) => { Log.Debug($"OnPropertyChanged {name}"); var propertyDefineCollection = EntityDefine.PropertyDefineCollectionMap[EntityDefine.GetTypeId <Unit>()]; var attr = propertyDefineCollection[name]; var msg = new C2M_SetEntityProperty(); msg.PropertyId = attr.Id; msg.PropertyValue.bytes = value; SessionHelper.HotfixSend(msg); }; } catch (Exception e) { Log.Error(e); } }