Пример #1
0
    private bool SetupDestination()
    {
        if (stateMachine.currentWantedProduct == StockTypes.None)
        {
            // Decide on a random product to get
            stateMachine.currentWantedProduct = EssentialFunctions.GetRandomFromArray(stateMachine.mapManager.GetStockTypesAvailable());

            // Grab random shelf component on map with wanted product
            shelf = stateMachine.mapManager.GetRandomShelvingUnit(stateMachine.currentWantedProduct);
        }
        else if (stateMachine.taskDestination)
        {
            // Get shelf container if task destination exists already
            shelf = stateMachine.taskDestination.GetComponent <ShelfContainer>();
        }

        // Check if shelf exists
        if (!shelf)
        {
            return(false);
        }

        // Get shelf transform
        stateMachine.taskDestination = shelf.transform;

        // Get pickup position
        stateMachine.taskDestinationPosition = shelf.GetPickupPositions().Length == 1
            ? shelf.GetPickupPositions()[0]
            : EssentialFunctions.GetRandomFromArray(shelf.GetPickupPositions());

        return(true);
    }
Пример #2
0
    public ShelfContainer GetRandomShelvingUnit(StockTypes selectedType = StockTypes.None)
    {
        // Check if there are shelves in the map
        if (shelvingUnits.Count == 0)
        {
            Debug.LogWarning(!isDoneLoading ? "MapManager is not done loading!" : "There are no shelves in the map!");

            return(null);
        }

        // Filter out shelves that are needed
        List <ShelfContainer> sortedShelfList = new List <ShelfContainer>();

        if (selectedType != StockTypes.None)
        {
            for (int i = 0; i < shelvingUnits.Count; i++)
            {
                ShelfContainer currentShelf = shelvingUnits[i];

                if (currentShelf.ShelfStockType == selectedType)
                {
                    sortedShelfList.Add(shelvingUnits[i]);
                }
            }
        }
        else
        {
            CopyData.CopyObjectData(shelvingUnits, sortedShelfList);
        }

        if (sortedShelfList.Count == 0)
        {
            Debug.LogWarning("There are no shelves that have this type of stock: " + selectedType.ToString());
            return(null);
        }

        // Filter out empty shelves
        List <ShelfContainer> stockedShelves = new List <ShelfContainer>();

        for (int s = 0; s < sortedShelfList.Count; s++)
        {
            ShelfContainer shelf = sortedShelfList[s];
            if (!shelf.IsEmpty())
            {
                stockedShelves.Add(shelf);
            }
        }

        if (stockedShelves.Count == 0)
        {
            Debug.Log("All shelves of " + selectedType.ToString() + " is empty!");
            return(EssentialFunctions.GetRandomFromArray(sortedShelfList));;
        }
        else
        {
            return(EssentialFunctions.GetRandomFromArray(stockedShelves));
        }
    }
    //*************************************************************************
    // Interaction methods

    public void Interact()
    {
        //// Raycast to see what's infront of AI

        //RaycastHit[] hits = Physics.RaycastAll(transform.position, transform.forward, maxPickupDistance, LayerMask.GetMask("Interactive"));

        //if (hits.Length > 0)
        //{
        //    Transform rootTransform = null;

        //    foreach (RaycastHit hit in hits)
        //    {
        //        rootTransform = hit.transform.root;
        //        if (EssentialFunctions.CompareTags(rootTransform, new string[] { "Shelf", "Register" }))
        //        {
        //            break;
        //        }
        //    }

        //    if (rootTransform)
        //    {
        //        ShelfContainer shelfContainer = rootTransform.GetComponent<ShelfContainer>();

        //        if (!shelfContainer)
        //        {
        //            Debug.LogError("Shelf \"" + rootTransform + "\" is tagged as a shelf, but doesn't have a ShelfContainer component.");
        //            return;
        //        }

        //        if (equippedItem)
        //        {
        //            AddStockToShelf(shelfContainer);
        //        }
        //        else
        //        {
        //            GetStockFromShelf(shelfContainer);
        //        }
        //    }
        //}

        ShelfContainer interactShelf = stateMachine.TaskDestinationAsShelf();

        if (interactShelf)
        {
            if (stateMachine.equippedItem)
            {
                AddStockToShelf(interactShelf);
            }
            else
            {
                GetStockFromShelf(interactShelf);
            }
        }
    }
    public void GetStockFromShelf(ShelfContainer shelf)
    {
        StockTypes stockType = shelf.ShelfStockType;
        int        amount    = shelf.GetStock();

        if (amount != 0)
        {
            GameObject newItem = Object.Instantiate(stateMachine.mapManager.GetStockTypePrefab(stockType)) as GameObject;
            EquipItem(newItem.transform);
        }
    }
Пример #5
0
    public void GetStockFromShelf(ShelfContainer shelf)
    {
        StockTypes stockType = shelf.ShelfStockType;
        int        amount    = shelf.GetStock();

        if (amount == 0)
        {
            return;
        }

        GameObject newItem = Instantiate(mapManager.GetStockTypePrefab(stockType));

        EquipItem(newItem.transform);
    }
Пример #6
0
    ShelfContainer GetShelfDestination()
    {
        if (!stateMachine.mapManager.isDoneLoading)
            return GetShelfDestination();

        // Get a random stock type
        StockTypes selectedType = GetRandomStockType(stateMachine.currentWantedProduct);
        stateMachine.currentWantedProduct = selectedType;

        // Get random shelf with wanted stocktype
        ShelfContainer selectedShelf = stateMachine.mapManager.GetRandomShelvingUnit(stateMachine.currentWantedProduct);

        return selectedShelf;
    }
Пример #7
0
    public void Interact()
    {
        ShelfContainer interactShelf = TaskDestinationAsShelf();

        // Check if task destination is a shelf
        if (!interactShelf)
        {
            return;
        }

        // Is not holding a product in hand
        if (!equippedItem)
        {
            GetStockFromShelf(interactShelf);
        }
    }
    public void AddStockToShelf(ShelfContainer shelf)
    {
        // Check if there is something equipped
        if (!stateMachine.equippedItem)
        {
            return;
        }

        // Get stock type that is equipped
        StockTypes equippedType = stateMachine.equippedItem.GetComponent <StockItem>().GetStockType();

        int result = shelf.AddStock(equippedType);

        if (result == 0)
        {
            Object.Destroy(stateMachine.equippedItem);
        }
    }
Пример #9
0
    public void UpdateState()
    {
        if (decided)
        {
            // Change state to walk state
            ToWalkToPositionState();
        }

        // Get random shelf wanted
        ShelfContainer selectedShelf = GetShelfDestination();

        stateMachine.taskDestination = selectedShelf.transform;

        Vector3[] pickupPositions = selectedShelf.GetPickupPositions();

        // Set AI destination to selected shelf
        stateMachine.taskDestinationPosition = pickupPositions[Random.Range(0, pickupPositions.Length - 1)];

        decided = true;
    }
Пример #10
0
    public void UpdateAvailableStockTypes()
    {
        List <StockTypes> updatedList = new List <StockTypes>();

        for (int s = 0; s < shelvingUnits.Count; s++)
        {
            ShelfContainer shelves = shelvingUnits[s];

            if (stockTypesAvailable.Count == 0 || shelves.ShelfStockType != StockTypes.None)
            {
                updatedList.Add(shelves.ShelfStockType);
            }
            else
            if (!stockTypesAvailable.Contains(shelves.ShelfStockType) && shelves.ShelfStockType != StockTypes.None)
            {
                updatedList.Add(shelves.ShelfStockType);
            }
        }

        // Replace old list with updated one
        stockTypesAvailable = updatedList;
    }
    private void AddStockToShelf(ShelfContainer shelf)
    {
        // Check if there is something equipped
        if (!_equippedItem)
        {
            return;
        }

        int result = 0;

        switch (_equippedItem.tag)
        {
        case "Product":
        {
            // Get stock type that is equipped
            StockTypes equippedType = _equippedItem.GetComponent <StockItem>().GetStockType();

            result = shelf.AddStock(equippedType);
            break;
        }

        case "StockCrate":
        {
            StockCrate crateComponent = _equippedItem.GetComponent <StockCrate>();
            if (crateComponent)
            {
                result = shelf.AddCrate(crateComponent);
            }
            break;
        }
        }

        if (result == 0)
        {
            Destroy(_equippedItem);
        }
    }
Пример #12
0
    public ShelfContainer TaskDestinationAsShelf()
    {
        ShelfContainer result = taskDestination.GetComponent <ShelfContainer>();

        return(result);
    }
Пример #13
0
    private void LoadMap()
    {
        UpdateSegments();

        //************************************************************************/
        currentLoadingTask = "Collecting shelves...";

        GameObject[] foundShelves = GameObject.FindGameObjectsWithTag("Shelf");

        _tasksToDo = foundShelves.Length;

        //************************************************************************/
        currentLoadingTask = "Validating shelves...";

        List <ShelfContainer> validShelves = new List <ShelfContainer>();

        for (int s = 0; s < foundShelves.Length; s++)
        {
            GameObject shelf = foundShelves[s];

            ShelfContainer result = shelf.GetComponent <ShelfContainer>();

            if (result)
            {
                validShelves.Add(result);
            }
            else
            {
                Debug.LogWarning("Shelf \"" + shelf + "\" is tagged as a shelf, but doesn't have a ShelfContainer compnent!");
            }

            _tasksDone++;
        }

        shelvingUnits.AddRange(validShelves);

        //************************************************************************/
        currentLoadingTask = "Detecting available types of stock...";

        UpdateAvailableStockTypes();

        //************************************************************************/
        currentLoadingTask = "Map building done!";

        //************************************************************************/
        currentLoadingTask = "Checking stock prefabs...";

        if (stockPrefabs.Length != EnumConverter.ToNameArray <StockTypes>().Length)
        {
            StockTypes[] allStocks = EnumConverter.ToListOfValues <StockTypes>().ToArray();

            List <string> missingStockTypes = new List <string>();

            for (int stock = 0; stock < allStocks.Length; stock++)
            {
                // Skip None enum
                if (allStocks[stock] == StockTypes.None)
                {
                    continue;
                }

                bool isMissing = true;

                for (int p = 0; p < stockPrefabs.Length; p++)
                {
                    if (stockPrefabs[p].GetStockType() == allStocks[stock])
                    {
                        isMissing = false;
                    }
                }

                if (isMissing)
                {
                    missingStockTypes.Add(allStocks[stock].ToString());
                }
            }

            Debug.LogWarning("The following prefabs are missing for stock types: " + string.Join(", ", missingStockTypes));
        }

        //************************************************************************/
        currentLoadingTask = "Collecting registers...";

        GameObject[] foundRegisters = GameObject.FindGameObjectsWithTag("Register");

        _tasksToDo = foundRegisters.Length;

        //************************************************************************/
        currentLoadingTask = "Validating registers...";

        List <CashRegister> validRegisters = new List <CashRegister>();

        for (int r = 0; r < foundRegisters.Length; r++)
        {
            GameObject register = foundRegisters[r];

            CashRegister result = register.GetComponent <CashRegister>();

            if (result)
            {
                validRegisters.Add(result);
            }
            else
            {
                Debug.LogWarning("Register \"" + register + "\" is tagged as a Register, but doesn't have a CashRegister compnent!");
            }

            _tasksDone++;
        }

        cashRegisters.AddRange(validRegisters);

        //************************************************************************/
        currentLoadingTask = "Collecting customer spawn locations...";

        GameObject[] tempCustomerSpawner = GameObject.FindGameObjectsWithTag("CustomerSpawn");

        for (int customerSpawner = 0; customerSpawner < tempCustomerSpawner.Length; customerSpawner++)
        {
            if (customerSpawners.Contains(tempCustomerSpawner[customerSpawner].GetComponent <CustomerSpawner>()))
            {
                continue;
            }
            customerSpawners.Add(tempCustomerSpawner[customerSpawner].GetComponent <CustomerSpawner>());
        }

        //************************************************************************/
        currentLoadingTask = "Collecting exit points...";

        GameObject[] tempExitPoints = GameObject.FindGameObjectsWithTag("MapExitPoint");

        for (int point = 0; point < tempExitPoints.Length; point++)
        {
            if (exitPoints.Contains(tempExitPoints[point].transform))
            {
                continue;
            }
            exitPoints.Add(tempExitPoints[point].transform);
        }

        LoadPlayerData();

        isDoneLoading = true;
    }
    private void PickupObject()
    {
        string[]     tagsToScan          = { "Product", "StockCrate", "Shelf" };
        GameObject[] excludedGameObjects = { gameObject, _equippedItem };

        GameObject closestInteractable = EssentialFunctions.GetClosestInteractableInFOV(transform, pickupArea, pickupAngle, maxPickupDistance, tagsToScan, excludedGameObjects);

        Vector3 org = EssentialFunctions.GetMaxBounds(gameObject).center;

        if (!closestInteractable)
        {
            return;
        }

        Vector3 tar = EssentialFunctions.GetMaxBounds(closestInteractable).center;

        Vector3 dirToInteractable = org - tar;

        Debug.DrawRay(org, dirToInteractable, Color.red, 1.0f);

        if (Physics.Raycast(org, dirToInteractable, out RaycastHit hit, 5.0f) && hit.transform.root != closestInteractable.transform)
        {
            return;
        }

        //Debug.Log(hit.transform);

        Debug.DrawLine(EssentialFunctions.GetMaxBounds(gameObject).center, EssentialFunctions.GetMaxBounds(closestInteractable).center, Color.red);

        switch (closestInteractable.tag)
        {
        case "Product":
        {
            // Fill equip slot
            EquipItem(closestInteractable.transform);

            break;
        }

        case "StockCrate":
        {
            EquipItem(closestInteractable.transform);

            break;
        }

        case "Shelf":
        {
            ShelfContainer shelfComponent = closestInteractable.GetComponent <ShelfContainer>();
            if (shelfComponent)
            {
                if (_equippedItem)
                {
                    AddStockToShelf(shelfComponent);
                }
                else
                {
                    GetStockFromShelf(shelfComponent);
                }
            }
            else
            {
                Debug.LogError("Shelf \"" + shelfComponent.gameObject + "\" is missing a ShelfContainer component or wrongly tagged!");
            }
            break;
        }
        }

        _justPickedUp = true;
    }
Пример #15
0
 // Use this for initialization
 void Start()
 {
     shelves        = GameObject.Find("_Shelves");
     shelfContainer = shelves.GetComponent <ShelfContainer>();
 }