public void AddEntity(IStaticEntity entity, uint sourceDynamicId = 0) { Vector3I entityBlockPosition; //If the entity is of type IBlockLinkedEntity, then it needs to be store inside the chunk where the LinkedEntity belong. var blockLinkedItem = entity as IBlockLinkedEntity; if (blockLinkedItem != null && blockLinkedItem.Linked) { entityBlockPosition = ((IBlockLinkedEntity)entity).LinkedCube; } else { entityBlockPosition = (Vector3I)entity.Position; } var impactedChunk = GetChunkFromBlock(entityBlockPosition); if (impactedChunk == null) { return; } impactedChunk.Entities.Add(entity, sourceDynamicId); //Raise event (Playing sound) OnStaticEntityAdd(new StaticEventArgs() { Entity = entity }); // Save the modified Chunk in local buffer DB SendChunkForBuffering(impactedChunk); }
/// <summary> /// Finds exact static entity that links points to /// Supports container-in-container extraction /// </summary> /// <param name="landscapeManager"></param> /// <returns></returns> public IStaticEntity ResolveStatic(ILandscapeManager landscapeManager) { if (IsDynamic) { throw new InvalidOperationException(); } var chunk = landscapeManager.GetChunk(ChunkPosition); if (chunk == null) { return(null); } var collection = (IStaticContainer)chunk.Entities; IStaticEntity sEntity = null; for (int i = 0; i < Tail.Length; i++) { if (!collection.ContainsId(Tail[i], out sEntity)) { return(null); } if (sEntity is IStaticContainer) { collection = sEntity as IStaticContainer; } } return(sEntity); }
/// <summary> /// Adds static entity to the world /// </summary> /// <param name="entity"></param> /// <param name="sourceDynamicId">Parent entity that issues adding</param> public void AddEntity(IStaticEntity entity, uint sourceDynamicId = 0) { Vector3I entityBlockPosition; //If the entity is of type IBlockLinkedEntity, then it needs to be store inside the chunk where the LinkedEntity belong. var blockLinkedEntity = entity as IBlockLinkedEntity; if (blockLinkedEntity != null && blockLinkedEntity.Linked) { entityBlockPosition = blockLinkedEntity.LinkedCube; // you probably forget to set LinkedCube property of the entity if (Vector3D.DistanceSquared(entity.Position, blockLinkedEntity.LinkedCube) > 256) { throw new InvalidOperationException("Invalid linked block"); } } else { entityBlockPosition = (Vector3I)entity.Position; } var entityChunk = _manager.GetChunkFromBlock(entityBlockPosition); entityChunk.Entities.Add(entity, sourceDynamicId == 0 ? OwnerDynamicId : sourceDynamicId); }
/// <summary> /// Checks if entity inside /// </summary> /// <param name="entity"></param> /// <returns></returns> public bool Contains(IStaticEntity entity) { for (int x = 0; x < _gridSize.X; x++) { for (int y = 0; y < _gridSize.Y; y++) { if (_items[x, y] != null) { if (_items[x, y].Item.StaticId == entity.StaticId) { return(true); } } } } return(false); }
public IRecord CreateRecord(IStaticEntity entity, String key, String label, IESpace espace) { IEntityAttribute GetAttribute(string name) => entity.Attributes.Single(a => a.Name == name); var rec = entity.Records; var exists = rec.SingleOrDefault(s => s.ToString().Contains(key)); if (exists == default) { var record = entity.CreateRecord(); record.Identifier = key; var keyatt = GetAttribute("Key"); record.SetAttributeValue(keyatt, $"\"{key}\""); var labelatt = GetAttribute("Label"); record.SetAttributeValue(labelatt, $"\"{label}\""); return(record); } return(null); }
public static IToolImpact TakeImpact(IDynamicEntity owner, IStaticEntity entity, EntityToolImpact impact, ILandscapeCursor cursor, Item hostItem) { var charEntity = owner as CharacterEntity; if (charEntity != null) { var item = (IItem)entity; impact.EntityId = entity.StaticId; var playerBindedItem = entity as IOwnerBindable; if (playerBindedItem != null && playerBindedItem.DynamicEntityOwnerID != charEntity.DynamicId) { impact.Message = "This item is not binded to you !"; return(impact); } if (item.IsDestroyedOnWorldRemove) { item.BeforeDestruction(charEntity); cursor.RemoveEntity(owner.EntityState.PickedEntityLink); impact.Success = true; item.ModelInstance = null; impact.Message = "Item has been destroyed"; return(impact); } var treeItem = item as TreeGrowingEntity; if (treeItem != null) { if (treeItem.Scale > 0.2) { impact.Message = "The tree is growing, you can't remove it by hands"; return(impact); } treeItem.Scale = 0; treeItem.LastGrowUpdate = new UtopiaTime(); } var putItems = new List <KeyValuePair <IItem, int> >(); putItems.Add(new KeyValuePair <IItem, int>(item, 1)); var growing = item as PlantGrowingEntity; if (growing != null) { putItems.Clear(); foreach (var slot in growing.CurrentGrowLevel.HarvestSlots) { if (slot.BlueprintId != 0) { putItems.Add( new KeyValuePair <IItem, int>((Item)hostItem.EntityFactory.CreateFromBluePrint(slot.BlueprintId), slot.Count)); } } } if (item.Transformations != null) { var random = new FastRandom(owner.EntityState.PickedEntityPosition.GetHashCode() ^ owner.EntityState.Entropy); bool transformed = false; foreach (var itemTransformation in item.Transformations) { if (random.NextDouble() < itemTransformation.TransformChance) { putItems.Clear(); item.Transformations = null; foreach (var slot in itemTransformation.GeneratedItems) { putItems.Add( new KeyValuePair <IItem, int>((Item)hostItem.EntityFactory.CreateFromBluePrint(slot.BlueprintId), slot.Count)); transformed = true; } break; } } if (!transformed) { // don't allow future transforms item.Transformations = null; } } //Try to put the item into the inventory if (charEntity.Inventory.PutMany(putItems)) { //If inside the inventory, then remove it from the world var removedEntity = (Item)cursor.RemoveEntity(owner.EntityState.PickedEntityLink); impact.Success = true; // entity should lose its voxel intance if put into the inventory removedEntity.ModelInstance = null; if (hostItem.SoundEngine != null && hostItem.EntityFactory.Config.EntityTake != null) { hostItem.SoundEngine.StartPlay3D(hostItem.EntityFactory.Config.EntityTake, removedEntity.Position.AsVector3()); } return(impact); } impact.Message = "Unable to put item to the inventory, is it full?"; return(impact); } impact.Message = "Expected CharacterEntity owner"; return(impact); }
/// <summary> /// Adds static entity to the world /// </summary> /// <param name="entity"></param> /// <param name="sourceDynamicId">Parent entity that issues adding</param> public void AddEntity(IStaticEntity entity, uint sourceDynamicId = 0) { _landscapeManager.AddEntity(entity, sourceDynamicId == 0 ? OwnerDynamicId : sourceDynamicId); }
/// <summary> /// This method is not supported. Use TakeItem instead. /// </summary> /// <param name="entity"></param> public void Remove(IStaticEntity entity) { throw new NotSupportedException("This method is not supported. Use TakeItem instead."); }
/// <summary> /// This method is not supported. Use PutItem instead. /// </summary> /// <param name="entity"></param> public void Add_(IStaticEntity entity) { throw new NotSupportedException("This method is not supported. Use PutItem instead."); }