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 virtual async ETTask M2C_OnEntityChangedHandler(ETModel.Session session, M2C_OnEntityChanged message) { }
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 virtual async ETTask M2C_OnEntityChangedHandler(Unit unit, M2C_OnEntityChanged message) { }