public void RemoveEntity(ICharacterEntity entity) { if (_dynamicEntitiesDico.ContainsKey(entity.DynamicId)) { ModelAndInstances instances; if (!_models.TryGetValue(entity.ModelInstance.VoxelModel.Name, out instances)) { throw new InvalidOperationException("we have no such model"); } instances.Instances.Remove(entity.DynamicId); VisualDynamicEntity visualEntity = _dynamicEntitiesDico[entity.DynamicId]; DynamicEntities.Remove(visualEntity); _dynamicEntitiesDico.Remove(entity.DynamicId); entity.HealthStateChanged -= EntityHealthStateChanged; entity.HealthChanged -= entity_HealthChanged; entity.AfflictionStateChanged -= EntityAfflictionStateChanged; visualEntity.Dispose(); OnEntityRemoved(new DynamicEntityEventArgs { Entity = entity }); } }
public void RemoveEntityById(uint entityId, bool dispose = true) { VisualDynamicEntity entity; if (_dynamicEntitiesDico.TryGetValue(entityId, out entity)) { ModelAndInstances instances; if (!_models.TryGetValue(entity.VoxelEntity.ModelInstance.VoxelModel.Name, out instances)) { throw new InvalidOperationException("we have no such model"); } instances.Instances.Remove(entityId); VisualDynamicEntity visualEntity = _dynamicEntitiesDico[entityId]; DynamicEntities.Remove(_dynamicEntitiesDico[entityId]); _dynamicEntitiesDico.Remove(entityId); visualEntity.DynamicEntity.HealthStateChanged -= EntityHealthStateChanged; entity.DynamicEntity.HealthChanged -= entity_HealthChanged; visualEntity.DynamicEntity.AfflictionStateChanged -= EntityAfflictionStateChanged; if (dispose) { visualEntity.Dispose(); } OnEntityRemoved(new DynamicEntityEventArgs { Entity = visualEntity.DynamicEntity }); } }
/// <summary> /// Add an entity to the dynamic entity collection, all visual part of the entity (voxel) will be created here /// </summary> /// <param name="entity">the entity</param> /// <param name="withNetworkInterpolation">the entity movement will be interpolated between each move/rotate changes</param> public void AddEntity(ICharacterEntity entity, bool withNetworkInterpolation) { string entityVoxelName = entity.ModelName; if (entity.HealthState == DynamicEntityHealthState.Dead) { entityVoxelName = "Ghost"; } //Do we already have this entity ?? if (_dynamicEntitiesDico.ContainsKey(entity.DynamicId) == false) { //subscribe to entity state/afllication changes entity.HealthStateChanged += EntityHealthStateChanged; entity.HealthChanged += entity_HealthChanged; entity.AfflictionStateChanged += EntityAfflictionStateChanged; ModelAndInstances modelWithInstances; //Does the entity model exist in the collection ? //If yes, will send back the instance Ids registered to this Model if (!_models.TryGetValue(entityVoxelName, out modelWithInstances)) { //Model not existing ==== // load a new model modelWithInstances = new ModelAndInstances { VisualModel = _voxelModelManager.GetModel(entityVoxelName), //Get the model from the VoxelModelManager Instances = new Dictionary <uint, VoxelModelInstance>() //Create a new dico of modelInstance }; //If the voxel model was send back by the manager, create the Mesh from it (Vx and idx buffers) if (modelWithInstances.VisualModel != null) { // todo: probably do this in another thread modelWithInstances.VisualModel.BuildMesh(); } _models.Add(entityVoxelName, modelWithInstances); } VoxelModelInstance instance; //If the Model has a Voxel Model (Search by Name) if (modelWithInstances.VisualModel != null) { //Create a new Instance of the Model instance = new VoxelModelInstance(modelWithInstances.VisualModel.VoxelModel); entity.ModelInstance = instance; modelWithInstances.Instances.Add(entity.DynamicId, instance); } else { //Add a new instance for the model, but without Voxel Body (=null instance) modelWithInstances.Instances.Add(entity.DynamicId, null); } VisualDynamicEntity newEntity = CreateVisualEntity(entity); newEntity.WithNetworkInterpolation = withNetworkInterpolation; _dynamicEntitiesDico.Add(entity.DynamicId, newEntity); DynamicEntities.Add(newEntity); OnEntityAdded(new DynamicEntityEventArgs { Entity = entity }); } }