예제 #1
0
 // Update is called once per frame
 void Update()
 {
     for (int index = 0; index < motionComponents.Length; index++)
     {
         MotionComponent motionComponent = motionComponents[index];
         Transform       trans           = transformComponents[index];
         trans.position += Time.deltaTime * (Vector3)motionComponent.direction.normalized * motionComponent.speed;
     }
 }
예제 #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);
            }
        }