Ejemplo n.º 1
0
        public override bool InteractWithFoundColliders(Action onInteractionAction = null)
        {
            bool interacted = false;

            if (detectedColliders.Count > 0)
            {
                bool searchHappened = false;
                foreach (ISearchable searchable in detectedColliders)
                {
                    if (searchable.IsSearched)
                    {
                        continue;
                    }

                    List <InventoryItem> enemyItems = searchable.Search();
                    if (enemyItems.Count > 0)
                    {
                        foreach (InventoryItem item in enemyItems)
                        {
                            inventory.AddItem(item);
                        }
                        searchHappened = true;
                    }
                }
                interacted = true;
                if (searchHappened)
                {
                    onInteractionAction?.Invoke();
                }
            }
            return(interacted);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Craft an item based on player inventory
        /// </summary>
        /// <param name="inventory">Player inventory</param>
        public void CraftItem(Inventory.Inventory inventory)
        {
            if (foundIngredients == null || foundIngredients.Count < ingredients.Count)
            {
                return;
            }

            for (var i = 0; i < ingredients.Count; ++i)
            {
                var ingredientsUsedCount = ingredients[i].amount;
                for (var j = 0; j < foundIngredients[i].Length; ++j)
                {
                    ingredientsUsedCount -= foundIngredients[i][j].amount;
                    if (ingredientsUsedCount <= 0)
                    {
                        inventory.RemoveItem(foundIngredients[i][j], foundIngredients[i][j].amount + ingredientsUsedCount);
                        break;
                    }
                    inventory.RemoveItem(foundIngredients[i][j], foundIngredients[i][j].amount);
                }
            }

            var indexToAddTo = inventory.FindFreeCellToAdd(result.item);

            if (indexToAddTo != -1)
            {
                inventory.AddItem(result.item, indexToAddTo);
            }

            result.amount--;
        }
 public void Start()
 {
     ToolBar            = new ItemStack[10];
     Inventory          = new Inventory.Inventory();
     inventoryUI.Holder = this;
     Inventory.AddItem(Database <Item> .Items["Apple"], 1);
 }
Ejemplo n.º 4
0
        private void DataReceived(byte[] data)
        {
            int startingIndex = 0;

            while (startingIndex < data.Length)
            {
                switch ((CommandType)data[startingIndex + 1])
                {
                case CommandType.Tick:
                {
                    float deltaTime = BitConverter.ToSingle(data.SubArray(startingIndex + 2, 4), 0);
                    worldGrid.UpdateCrops(deltaTime);
                    worldGrid.UpdateStructures(deltaTime);
                    break;
                }

                case CommandType.AddCrop:
                {
                    byte     y        = data[startingIndex + 2];
                    byte     x        = data[startingIndex + 3];
                    CropType cropType = (CropType)data[startingIndex + 4];
                    worldGrid.AddCrop(y, x, cropType);
                    break;
                }

                case CommandType.RemoveCrop:
                {
                    byte y = data[startingIndex + 2];
                    byte x = data[startingIndex + 3];
                    worldGrid.RemoveCrop(y, x, out _);
                    break;
                }

                case CommandType.AddStructure:
                {
                    byte          y             = data[startingIndex + 2];
                    byte          x             = data[startingIndex + 3];
                    StructureType structureType = (StructureType)data[startingIndex + 4];
                    worldGrid.AddStructure(y, x, structureType);
                    break;
                }

                case CommandType.RemoveStructure:
                {
                    byte y = data[startingIndex + 2];
                    byte x = data[startingIndex + 3];
                    worldGrid.RemoveStructure(y, x, out _);
                    break;
                }

                case CommandType.AddItemToInventory:
                {
                    byte playerId = data[startingIndex + 2];
                    if (this.playerId != playerId)
                    {
                        break;
                    }
                    ItemType itemType = (ItemType)data[startingIndex + 3];
                    int      amount   = BitConverter.ToInt32(data.SubArray(startingIndex + 4, 4), 0);
                    playerInventory.AddItem(itemType, amount);
                    break;
                }

                case CommandType.RemoveItemFromInventory:
                {
                    byte playerId = data[startingIndex + 2];
                    if (this.playerId != playerId)
                    {
                        break;
                    }
                    ItemType itemType = (ItemType)data[startingIndex + 3];
                    int      amount   = BitConverter.ToInt32(data.SubArray(startingIndex + 4, 4), 0);
                    playerInventory.RemoveItem(itemType, amount);
                    break;
                }

                case CommandType.SpawnItemDrop:
                {
                    float    y        = BitConverter.ToSingle(data.SubArray(startingIndex + 2, 4), 0);
                    float    x        = BitConverter.ToSingle(data.SubArray(startingIndex + 6, 4), 0);
                    ItemType itemType = (ItemType)data[startingIndex + 10];
                    byte     amount   = data[startingIndex + 11];
                    itemDrops.Add(y, x, itemType, amount);
                    break;
                }

                case CommandType.RemoveItemDrop:
                {
                    int index = BitConverter.ToInt32(data.SubArray(startingIndex + 2, 4), 0);
                    itemDrops.Remove(index);
                    break;
                }

                case CommandType.UpdatePlayerLocation:
                {
                    byte  playerId  = data[startingIndex + 2];
                    float y         = BitConverter.ToSingle(data.SubArray(startingIndex + 3, 4), 0);
                    float x         = BitConverter.ToSingle(data.SubArray(startingIndex + 7, 4), 0);
                    float yVelocity = BitConverter.ToSingle(data.SubArray(startingIndex + 11, 4), 0);
                    float xVelocity = BitConverter.ToSingle(data.SubArray(startingIndex + 15, 4), 0);
                    players.SetPlayerPositionAndVelocity(playerId, y, x, yVelocity, xVelocity);
                    break;
                }

                case CommandType.SetPlayerId:
                {
                    playerId = data[startingIndex + 2];

                    break;
                }
                }

                startingIndex += data[startingIndex];
            }
        }