Exemplo n.º 1
0
        void SendDwarfsToWork()
        {
            for (int index = 0; index < pathComponents.Length; index++)
            {
                PathComponent             pathComponent             = pathComponents[index];
                WorkingSlotIndexComponent workingSlotIndexComponent = workingSlotIndexComponents[index];
                Transform          transformComponent = dwarfsTransformComponents[index];
                InventoryComponent inventoryComponent = inventoryComponents[index];

                if (workingSlotIndexComponent.dwarfsSlots == null)
                {
                    continue;
                }

                if (pathComponent.index != 0 && pathComponent.dwarfsSlots[pathComponent.index - 1] != null)
                {
                    pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsAlreadyIn--;
                    pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsInside
                    .Remove(inventoryComponent);
                }

                pathComponent.nodes       = aStarSystem.GetPath(transformComponent, workingSlotIndexComponent.dwarfsSlots.transform);
                pathComponent.index       = 0;
                pathComponent.dwarfsSlots = new DwarfsSlots[pathComponent.nodes.Length];
                pathComponent.dwarfsSlots[pathComponent.nodes.Length - 1] = workingSlotIndexComponent.dwarfsSlots;
            }
        }
Exemplo n.º 2
0
        // Update is called once per frame
        void Update()
        {
            for (int index = 0; index < motionComponents.Length; index++)
            {
                MotionComponent    motionComponent    = motionComponents[index];
                PathComponent      pathComponent      = pathComponents[index];
                Transform          transformComponent = transformComponents[index];
                InventoryComponent inventoryComponent = inventoryComponents[index];

                if (pathComponent.nodes == null || pathComponent.nodes.Length == 0)
                {
                    continue;
                }

                //Test distance
                if (Vector2.Distance(transformComponent.position,
                                     pathComponent.nodes[pathComponent.index].position.position) < 0.2f)
                {
                    if (pathComponent.index != 0 && pathComponent.dwarfsSlots[pathComponent.index - 1] != null)
                    {
                        pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsAlreadyIn--;
                        if (inventoryComponent != null)
                        {
                            pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsInside.Remove(inventoryComponent);
                        }
                    }
                    if (pathComponent.index != 0 && pathComponent.dwarfsSlots[pathComponent.index] != null)
                    {
                        pathComponent.dwarfsSlots[pathComponent.index].dwarfsAlreadyIn++;
                        if (inventoryComponent != null)
                        {
                            pathComponent.dwarfsSlots[pathComponent.index].dwarfsInside.Add(inventoryComponent);
                        }
                    }
                    pathComponent.index++;
                    if (pathComponent.index >= pathComponent.nodes.Length)
                    {
                        pathComponent.nodes       = new GraphNodeComponent[0];
                        motionComponent.direction = Vector2.zero;
                        continue;
                    }
                }

                motionComponent.direction = (pathComponent.nodes[pathComponent.index].position.position - transformComponent.position).normalized;

                float maxCost = 5;
                float speed   = motionComponent.maxSpeed / ((Mathf.Lerp(0, maxCost, pathComponent.nodes[pathComponent.index].cost) / maxCost) * 2);

                if (pathComponent.index > 0)
                {
                    if (Vector2.Distance(transformComponent.position,
                                         pathComponent.nodes[pathComponent.index].position.position) >
                        Vector2.Distance(transformComponent.position,
                                         pathComponent.nodes[pathComponent.index - 1].position.position))
                    {
                        speed = motionComponent.maxSpeed / ((Mathf.Lerp(0, maxCost, pathComponent.nodes[pathComponent.index - 1].cost) / maxCost) * 2);
                    }
                    else
                    {
                        speed = motionComponent.maxSpeed / ((Mathf.Lerp(0, maxCost, pathComponent.nodes[pathComponent.index].cost) / maxCost) * 2);
                    }
                }
                else
                {
                    speed = motionComponent.maxSpeed / ((Mathf.Lerp(0, maxCost, pathComponent.nodes[pathComponent.index].cost) / maxCost) * 2);
                }

                motionComponent.speed = Mathf.Clamp(speed, 0, motionComponent.maxSpeed);
            }
        }
Exemplo n.º 3
0
        // Update is called once per frame
        void Update()
        {
            for (int index = 0; index < dwarfsSlotsComponents.Length; index++)
            {
                DwarfsSlots dwarfsSlotsComponent = dwarfsSlotsComponents[index];

                if (dwarfsSlotsComponent.dwarfsInside.Count <= 0)
                {
                    continue;
                }

                for (int i = 0; i < dwarfsSlotsComponent.dwarfsInside.Count; i++)
                {
                    InventoryComponent inventoryComponent = dwarfsSlotsComponent.dwarfsInside[i];
                    bool found = false;

                    foreach (InventoryComponent component in dwarfsSlotsComponent.dwarfsInside)
                    {
                        if (component == inventoryComponent)
                        {
                            found = true;
                            break;
                        }
                    }

                    if (!found)
                    {
                        return;
                    }

                    GiverComponent[]            giverComponent            = giverComponents[index];
                    ReceiverComponent[]         receiverComponent         = receiverComponents[index];
                    PassiveInventoryComponent[] passiveInventoryComponent = passiveInventoryComponents[index];

                    if (giverComponent != null && giverComponent.Length > 0)
                    {
                        foreach (GiverComponent component in giverComponent)
                        {
                            if (component.nbDwarfsAttributed > 0 &&
                                component.resourceType == inventoryComponent.resourceType)
                            {
                                if (inventoryComponent.amount >= inventoryComponent.maxCapacity)
                                {
                                    continue;
                                }

                                if (component.amount < inventoryComponent.maxCapacity)
                                {
                                    inventoryComponent.amount = component.amount;
                                }
                                else
                                {
                                    inventoryComponent.amount = inventoryComponent.maxCapacity;
                                }

                                component.amount -= inventoryComponent.amount;
                                component.nbDwarfsAttributed--;
                                break;
                            }
                        }
                    }

                    if (receiverComponent != null && receiverComponent.Length > 0)
                    {
                        foreach (ReceiverComponent component in receiverComponent)
                        {
                            if (component.nbDwarfsAttributed > 0 &&
                                component.resourceType == inventoryComponent.resourceType)
                            {
                                component.amount         += inventoryComponent.amount;
                                inventoryComponent.amount = 0;
                                component.nbDwarfsAttributed--;
                                break;
                            }
                        }
                    }

                    if (passiveInventoryComponent != null && passiveInventoryComponent.Length > 0)
                    {
                        foreach (PassiveInventoryComponent component in passiveInventoryComponent)
                        {
                            if (component.resourceType == inventoryComponent.resourceType)
                            {
                                if (inventoryComponent.amount == 0)
                                {
                                    inventoryComponent.amount = inventoryComponent.maxCapacity;
                                    component.amount         -= inventoryComponent.amount;
                                }
                                else
                                {
                                    component.amount         += inventoryComponent.amount;
                                    inventoryComponent.amount = 0;
                                }

                                dwarfsSlotsComponent.dwarfsInside.Remove(inventoryComponent);
                                break;
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        void Update()
        {
            if (dayCycleSystem.state != DayCycleSystem.State.WORK)
            {
                return;
            }

            //Get ressource from giver then go the passiveInventory
            for (int index = 0; index < giverComponents.Length; index++)
            {
                GiverComponent giverComponent = giverComponents[index];
                if (giverComponent.nbDwarfsAttributed >= 1 || giverComponent.amount < 10)
                {
                    continue;
                }

                for (int i = 0; i < pathComponents.Length; i++)
                {
                    PathComponent pathComponent = pathComponents[i];
                    if (pathComponent.nodes.Length != 0)
                    {
                        continue;
                    }

                    Transform   giverBuildingTransformsComponent = giverTransformComponents[index];
                    DwarfsSlots giverDwarfsSlots = giverDwarfsSlotComponents[index];

                    Transform dwarfsTransform = dwarfsTransformComponents[i];

                    //Then go to closet storage
                    int indexPassiveInventory = GetClosestStorageIndexFromBuilding(giverBuildingTransformsComponent.position, giverComponent.resourceType);

                    if (indexPassiveInventory == -1)
                    {
                        continue;
                    }
                    InventoryComponent dwarfsInventoryComponent = dwarfsInventoryComponents[i];

                    if (pathComponent.index != 0 && pathComponent.dwarfsSlots[pathComponent.index - 1] != null)
                    {
                        pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsAlreadyIn--;
                        pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsInside
                        .Remove(dwarfsInventoryComponent);
                    }

                    giverComponent.nbDwarfsAttributed++;

                    dwarfsInventoryComponent.resourceType = giverComponent.resourceType;

                    //Go to giver
                    pathComponent.nodes       = aStarSystem.GetPath(dwarfsTransform, giverBuildingTransformsComponent);
                    pathComponent.dwarfsSlots = new DwarfsSlots[pathComponent.nodes.Length];
                    pathComponent.dwarfsSlots[pathComponent.nodes.Length - 1] = giverDwarfsSlots;
                    pathComponent.index = 0;

                    //Then go to closet storage
                    GraphNodeComponent[] path2  = aStarSystem.GetPath(giverBuildingTransformsComponent, passiveInventoryTransformsComponents[indexPassiveInventory]);
                    DwarfsSlots[]        slots2 = new DwarfsSlots[path2.Length];
                    slots2[path2.Length - 1] = passiveInventoryDwarfsSlotsComponents[indexPassiveInventory];

                    pathComponent.nodes       = pathComponent.nodes.Concat(path2).ToArray();
                    pathComponent.dwarfsSlots = pathComponent.dwarfsSlots.Concat(slots2).ToArray();
                    break;
                }
            }

            //Get ressource from storage then go the receiver
            for (int index = 0; index < receiverComponents.Length; index++)
            {
                ReceiverComponent receiverComponent = receiverComponents[index];
                if (receiverComponent.nbDwarfsAttributed >= 1 || receiverComponent.amount >= receiverComponent.maxCapacity)
                {
                    continue;
                }

                for (int i = 0; i < pathComponents.Length; i++)
                {
                    PathComponent pathComponent = pathComponents[i];
                    if (pathComponent.nodes.Length != 0)
                    {
                        continue;
                    }

                    Transform   receiverBuildingTransformsComponent = receiverTransformComponents[index];
                    DwarfsSlots receiverDwarfsSlots = receiverDwarfsSlotComponents[index];

                    Transform dwarfsTransform = dwarfsTransformComponents[i];

                    //Then go to closet storage
                    int indexPassiveInventory =
                        GetClosestStorageIndexFromBuilding(receiverBuildingTransformsComponent.position,
                                                           receiverComponent.resourceType);

                    if (indexPassiveInventory == -1)
                    {
                        continue;
                    }

                    InventoryComponent dwarfsInventoryComponent = dwarfsInventoryComponents[i];

                    if (pathComponent.index != 0 && pathComponent.dwarfsSlots[pathComponent.index - 1] != null)
                    {
                        pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsAlreadyIn--;
                        pathComponent.dwarfsSlots[pathComponent.index - 1].dwarfsInside
                        .Remove(dwarfsInventoryComponent);
                    }

                    receiverComponent.nbDwarfsAttributed++;

                    dwarfsInventoryComponent.resourceType = receiverComponent.resourceType;

                    //Go to closet storage
                    pathComponent.nodes = aStarSystem.GetPath(dwarfsTransform,
                                                              passiveInventoryTransformsComponents[indexPassiveInventory]);
                    pathComponent.dwarfsSlots = new DwarfsSlots[pathComponent.nodes.Length];
                    pathComponent.dwarfsSlots[pathComponent.nodes.Length - 1] = passiveInventoryDwarfsSlotsComponents[indexPassiveInventory];
                    pathComponent.index = 0;

                    //Then go to giver
                    GraphNodeComponent[] path2  = aStarSystem.GetPath(passiveInventoryTransformsComponents[indexPassiveInventory], receiverBuildingTransformsComponent);
                    DwarfsSlots[]        slots2 = new DwarfsSlots[path2.Length];
                    slots2[path2.Length - 1] = receiverDwarfsSlots;

                    pathComponent.nodes       = pathComponent.nodes.Concat(path2).ToArray();
                    pathComponent.dwarfsSlots = pathComponent.dwarfsSlots.Concat(slots2).ToArray();
                    break;
                }
            }
        }