Пример #1
0
    public void CollectResource(ResourcePickup resource)
    {
        int stackCount    = resource.gameObjects.Count;
        int countToPickUp = stackCount;

        float carriedResourceCount = GetCarriedResourceCount();

        if (carriedResourceCount + stackCount > curInventorySize)
        {
            int remainingSpace = Mathf.FloorToInt(curInventorySize - carriedResourceCount);
            if (remainingSpace <= 0)
            {
                return;
            }
            else
            {
                countToPickUp = remainingSpace;
            }
        }

        if (countToPickUp >= stackCount)
        {
            resource.island.pickups.Remove(resource);
            tm.pickups.Remove(resource.position);
        }

        inventory [resource.info.ToIndex()] += (float)countToPickUp;
        List <ResourcePickup> affectedResources = new List <ResourcePickup> {
            resource
        };
        List <int> countsToPickUp = new List <int> {
            countToPickUp
        };

        tm.ConsumeResources(affectedResources, countsToPickUp);

        GameManager.instance.SaveThisTurn();
        UpdateInventoryUI();
    }
Пример #2
0
    Craftable GetCraftableForResource(ResourcePickup anchorResource, out Vector3 spawnPos)
    {
        ResourceInfo.ResourceType tileType        = tm.tiles[anchorResource.position].resourceType;
        List <Crafting.Recipe>    possibleRecipes = GetPossibleRecipes(new Stack(anchorResource.info.type, tileType, anchorResource.gameObjects.Count));

        bool hasRecipe = false;

        Crafting.Recipe       confirmedRecipe   = new Crafting.Recipe();
        List <ResourcePickup> affectedResources = new List <ResourcePickup> ();

        foreach (var recipe in possibleRecipes)
        {
            int correct = 0;
            for (int y = 0; y < recipe.resources.GetLength(1); y++)
            {
                for (int x = 0; x < recipe.resources.GetLength(0); x++)
                {
                    Vector2 posToCheck = new Vector2(anchorResource.position.x + x, anchorResource.position.y + y);
                    if (ResourcePickup.IsAtPosition(posToCheck) && tm.PadAtPosition(posToCheck) == null)
                    {
                        ResourcePickup resourceAtPos = ResourcePickup.GetAtPosition(posToCheck);
                        if (resourceAtPos.island.buildable)
                        {
                            if (resourceAtPos.info.type == recipe.resources [x, y].resourceType && resourceAtPos.gameObjects.Count == recipe.resources [x, y].count)
                            {
                                // check tile
                                ResourceInfo.ResourceType curTileType = tm.tiles [posToCheck].resourceType;
                                if (curTileType == recipe.resources [x, y].tileType)
                                {
                                    affectedResources.Add(resourceAtPos);
                                    correct++;
                                }
                            }
                        }
                    }
                }
            }

            if (correct >= recipe.resources.Length)
            {
                hasRecipe       = true;
                confirmedRecipe = recipe;
                break;
            }
        }

        if (hasRecipe)
        {
            Craftable craftableInfo = craftableInfos [confirmedRecipe.name];
            spawnPos = anchorResource.gameObjects[0].transform.position + new Vector3(craftableInfo.anchorOffset.x, 0, craftableInfo.anchorOffset.y);

            if (confirmedRecipe.type != Recipe.RecipeType.Building)
            {
                tm.ConsumeResources(affectedResources);
            }

            return(craftableInfo);
        }
        else
        {
            spawnPos = Vector3.zero;
            return(null);
        }
    }