コード例 #1
0
        public IToolImpact Use(IDynamicEntity owner)
        {
            IToolImpact checkImpact;

            if (!CanDoEntityAction(owner, out checkImpact))
            {
                return(checkImpact);
            }

            var impact = new EntityToolImpact();

            if (owner.EntityState.PickedEntityLink.IsDynamic)
            {
                impact.Message = "Only static entities allowed to use";
                return(impact);
            }

            var entity = owner.EntityState.PickedEntityLink.ResolveStatic(LandscapeManager);

            if (entity == null)
            {
                impact.Message = "There is no entity by this link";
                return(impact);
            }

            //Trigger item activation (Make it play sound, open, ...)
            if (entity is IUsableEntity)
            {
                var usable = (IUsableEntity)entity;
                usable.Use();
                impact.Success  = true;
                impact.EntityId = entity.StaticId;
                return(impact);
            }

            var cursor = LandscapeManager.GetCursor(entity.Position);

            if (cursor == null)
            {
                impact.Dropped = true;
                return(impact);
            }

            if (!entity.IsPickable)
            {
                impact.Message = "You need a special tool to pick this item";
                return(impact);
            }

            cursor.OwnerDynamicId = owner.DynamicId;
            return(TakeImpact(owner, entity, impact, cursor, this));
        }
コード例 #2
0
        public IToolImpact Use(IDynamicEntity owner)
        {
            IToolImpact checkImpact;

            if (!CanDoEntityAction(owner, out checkImpact))
            {
                return(checkImpact);
            }

            var impact = new EntityToolImpact {
                SrcBlueprintId = BluePrintId
            };

            if (owner.EntityState.PickedEntityLink.IsDynamic)
            {
                impact.Message = "Only static entities allowed to use";
                return(impact);
            }

            var entity = owner.EntityState.PickedEntityLink.ResolveStatic(LandscapeManager);

            if (entity == null)
            {
                impact.Message = "Unable to resolve the link";
                return(impact);
            }

            var cursor = LandscapeManager.GetCursor(entity.Position);

            if (cursor == null)
            {
                impact.Dropped = true;
                return(impact);
            }

            cursor.OwnerDynamicId = owner.DynamicId;
            return(HandTool.TakeImpact(owner, entity, impact, cursor, this));
        }
コード例 #3
0
        /// <summary>
        /// Damage handling
        /// </summary>
        /// <param name="change">Use negative value to do the damage, and positive to heal</param>
        /// <param name="sourceEntity">Entity that hits us (or heal)</param>
        /// <returns></returns>
        public IToolImpact HealthImpact(float change, IDynamicEntity sourceEntity = null)
        {
            var impact = new EntityToolImpact();

            if (HealthState == DynamicEntityHealthState.Dead)
            {
                impact.Message = "The player is dead, cannot be subject to health change";
                return(impact);
            }

            Health.CurrentValue += change;

            //If change > some Trigger ==> Risk of Afflication change like Stunt !

            if (Health.CurrentValue <= 0 && HealthState != DynamicEntityHealthState.Dead)
            {
                impact = ActivateDead();
            }

            //Raise trigger here : CharacterEntityHealthChange (Health energy + Contact point + Normal vector for the damage)
            //Will be subscribed by client to play Hurt sound, show animation on hit point, ...
            var e = new EntityHealthChangeEventArgs
            {
                Change                        = change,
                Health                        = Health,
                ImpactedEntity                = this,
                SourceEntity                  = sourceEntity,
                HealthChangeHitLocation       = sourceEntity == null ? default(Vector3) : sourceEntity.EntityState.PickPoint,
                HealthChangeHitLocationNormal = sourceEntity == null ? default(Vector3I) : sourceEntity.EntityState.PickPointNormal
            };

            OnHealthChanged(e);
            impact.Success = true;


            return(impact);
        }
コード例 #4
0
        public IToolImpact Use(IDynamicEntity owner)
        {
            var impact = new EntityToolImpact();

            if (!owner.EntityState.IsEntityPicked)
            {
                impact.Success = true;
                return(impact);
            }

            var entity = owner.EntityState.PickedEntityLink.Resolve(EntityFactory) as CharacterEntity;

            if (entity == null)
            {
                impact.Success = true;
                return(impact);
            }

            var imp = entity.HealthImpact(-Damage, owner);

            imp.Success = true;

            return(imp);
        }
コード例 #5
0
        public IToolImpact Use(IDynamicEntity owner)
        {
            var impact = new EntityToolImpact();

            var charEntity = owner as CharacterEntity;

            if (charEntity != null)
            {
                var slot = charEntity.Inventory.FirstOrDefault(s => s.Item.StackType == StackType);

                if (slot == null)
                {
                    // we have no more items in the inventory, remove from the hand
                    slot           = charEntity.Equipment[EquipmentSlotType.Hand];
                    impact.Success = charEntity.Equipment.TakeItem(slot.GridPosition);
                }
                else
                {
                    impact.Success = charEntity.Inventory.TakeItem(slot.GridPosition);
                }

                if (!impact.Success)
                {
                    impact.Message = "Unable to take an item from the inventory";
                    return(impact);
                }

                charEntity.Health.CurrentValue += Calories;
                if (SoundEngine != null && UseSound != null)
                {
                    SoundEngine.StartPlay3D(UseSound, owner.Position.AsVector3());
                }
            }

            return(impact);
        }
コード例 #6
0
        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);
        }
コード例 #7
0
        private EntityToolImpact ActivateDead()
        {
            HealthState      = DynamicEntityHealthState.Dead; //Set Dead state to the player
            DisplacementMode = EntityDisplacementModes.Dead;  //Set character displacement mode

            //Create grave logic
            var impact = new EntityToolImpact();

            var graveBp = EntityFactory.Config.GraveBlueprint;

            if (graveBp < 256)
            {
                logger.Warn("Unable to create grave entity");
                return(impact);
            }

            // first we will find a place for a grave

            var blockPos = Position.ToCubePosition();
            var cursor   = EntityFactory.LandscapeManager.GetCursor(blockPos);

            if (cursor == null)
            {
                impact.Dropped = true;
                return(impact);
            }

            cursor.OwnerDynamicId = DynamicId;

            while (cursor.GlobalPosition.Y > 0)
            {
                if (cursor.PeekValue(new Vector3I(0, -1, 0)) != WorldConfiguration.CubeId.Air)
                {
                    break;
                }
                cursor.Move(new Vector3I(0, -1, 0));
            }

            // check if the grave already exists

            var chunk = EntityFactory.LandscapeManager.GetChunkFromBlock(cursor.GlobalPosition);

            Entity grave = null;

            foreach (var staticEntity in chunk.Entities)
            {
                var entity = (Entity)staticEntity;
                if (entity.BluePrintId == graveBp && entity.Position == cursor.GlobalPosition)
                {
                    grave = entity;
                }
            }

            var graveCreated    = false;
            var itemsTransfered = false;

            // create new if not exists
            if (grave == null)
            {
                grave          = EntityFactory.CreateFromBluePrint(graveBp);
                grave.Position = cursor.GlobalPosition;
                graveCreated   = true;
            }

            var graveContainer = grave as Container;

            if (graveContainer != null)
            {
                foreach (var containedSlot in Slots())
                {
                    if (!containedSlot.Item.IsDestroyedOnDeath)
                    {
                        if (!graveContainer.PutItems(containedSlot.Item, containedSlot.ItemsCount))
                        {
                            logger.Warn("Can't put all items to the container, it is too small!");
                            break;
                        }
                        itemsTransfered = true;
                    }
                }
            }

            if (graveCreated && itemsTransfered)
            {
                cursor.AddEntity((IStaticEntity)grave);
            }

            // remove all items from the character

            foreach (var containedSlot in Slots().ToList())
            {
                TakeItems(containedSlot.Item.BluePrintId, containedSlot.ItemsCount);
            }

            impact.Success = true;
            return(impact);
        }