예제 #1
0
    // Transmit the unit on this node toward the relevant connexion's node
    bool SendUnit(Connexion relevantConnexion)
    {
        // Check if there is a unit to send
        if (_unitOnNode == null)
        {
            // Otherwise,
            // Return that it couldn't succesfully send the inexisting unit
            Debug.Log("Error - " + this + " : SendUnit() called with no unit to send.");
            return(false);
        }

        // Check if the unit can start traveling to the relevant node
        if (!_unitOnNode.IsReadyToExecuteOrder())
        {
            // Otherwise,
            // Return that it couldn't succesfully send the inexisting unit
            Debug.Log("Error - " + this + " : SendUnit() called but the unit isnt't ready to travel to it's new destination.");
            return(false);
        }

        // Transmit the unit on this node to the target node system
        if (!relevantConnexion._connectedNode.RecieveUnit(_unitOnNode))
        {
            // If the target node couldn't recive the unit,
            // Return that it couldn't succesfully send the unit
            Debug.Log("Error - " + this + " : SendUnit() called but couldn't successfully transmit the unit to it's destination node.");
            return(false);
        }

        // Make the unit travel to it's relevant node
        _unitOnNode.RecieveOrder(relevantConnexion._path);


        // Set the target node on unit
        _unitOnNode._currentNode = relevantConnexion._connectedNode;

        // Set the last node on unit
        _unitOnNode._lastNode = this;

        // Check if the unit has a child
        if (_unitOnNode._childCount > 0)
        {
            // If it does, spawn it on this node
            _unitOnNode._childCount--;
            _unitOnNode = SpawnUnitOnNode();
            // Calculate relevant destination
            _relevantConnexionFound = CalculateRelevantConnexion(ref _relevantConnexion);
        }
        else
        {
            // Otherwise, remove the stored unit from this node if it was sucessfully sent
            _unitOnNode = null;
            // Reset relevant connexion
            _relevantConnexionFound = false;
        }

        // Return that it succesfully send the unit
        return(true);
    }
예제 #2
0
    IEnumerator CreateMouseOrder()
    {
        // Get order type
        RaycastHit2D hit = Physics2D.Raycast(new Vector2(_debug_mainCamera.ScreenToWorldPoint(Input.mousePosition).x, _debug_mainCamera.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0f);

        if (hit.collider != null && hit.collider.tag == "unit")
        {
            // Impulse or link
            // Debug.Log("Impulse/Link order started");

            UnitSystem startUnit = hit.collider.gameObject.GetComponent <UnitSystem>();
            if (startUnit == null)
            {
                startUnit = hit.collider.transform.GetComponentInParent <UnitSystem>();
            }
            _drag[0]   = startUnit.transform.position;
            _drag[0].z = 1;
            Feedback_SetDragColor(startUnit);

            // Check if the start unit can execute an order
            if (!startUnit.IsReadyToExecuteOrder())
            {
                // Otherwise, cancel order creation

                // Reset input feedback
                _drag = new Vector3[2] {
                    Vector3.zero, Vector3.zero
                };
                yield break;
            }

            for (;;)
            {
                // Check if start unit is still aviable
                if (startUnit == null)
                {
                    // Otherwise, cancel order creation

                    // Reset input feedback
                    _drag = new Vector3[2] {
                        Vector3.zero, Vector3.zero
                    };
                    yield break;
                }
                //
                Feedback_UpdateDragColor(startUnit);

                //
                _drag[1]   = _debug_mainCamera.ScreenToWorldPoint(Input.mousePosition);
                _drag[1].z = _drag[0].z;
                _drag[1]   = _drag[0] + (_drag[1] - _drag[0]).normalized * Mathf.Clamp(Vector3.Distance(_drag[1], _drag[0]), 0, startUnit._currentMaxImpulse * 0.5f);

                if (Input.GetMouseButtonUp(0))
                {
                    hit = Physics2D.Raycast(new Vector2(_debug_mainCamera.ScreenToWorldPoint(Input.mousePosition).x, _debug_mainCamera.ScreenToWorldPoint(Input.mousePosition).y), Vector2.zero, 0f);
                    if (/*debug*/ false /* hit.collider != null && hit.collider.tag == "unit" */)
                    {
                        // Link
                    }
                    else
                    {
                        // Impulse
                        Vector3 dragEnd = _debug_mainCamera.ScreenToWorldPoint(Input.mousePosition);
                        dragEnd = startUnit.transform.position + (startUnit.transform.position - dragEnd).normalized * Mathf.Clamp(Vector3.Distance(startUnit.transform.position, dragEnd), startUnit._minImpulse, startUnit._currentMaxImpulse);
                        Vector3 dragDelta = Vector2.zero;
                        Vector2 impulse   = -new Vector2((startUnit.transform.position - dragEnd).x, (startUnit.transform.position - dragEnd).y);

                        // Check if the start unit can execute an order
                        if (!startUnit.IsReadyToExecuteOrder())
                        {
                            // Otherwise, cancel order creation

                            // Reset input feedback
                            _drag = new Vector3[2] {
                                Vector3.zero, Vector3.zero
                            };
                            yield break;
                        }

                        startUnit.RecieveDirectOrder(new OrderImpulse(impulse * _debug_speed));

                        // Debug.Log("Impulse sent"+ impulse);
                    }

                    // Reset input feedback
                    _drag = new Vector3[2] {
                        Vector3.zero, Vector3.zero
                    };
                    yield break;
                }
                yield return(null);
            }
        }
        else
        {
            // Camera drag
            Vector3 dragStart   = _debug_mainCamera.ScreenToWorldPoint(Input.mousePosition);
            Vector3 cameraStart = _debug_mainCamera.transform.position;
            // Debug.Log("Camera drag order started");
            for (;;)
            {
                Vector3 dragEnd   = _debug_mainCamera.ScreenToWorldPoint(Input.mousePosition);
                Vector3 dragDelta = dragStart - dragEnd;
                dragDelta.z = 0;
                _debug_mainCamera.transform.position = cameraStart + dragDelta * _debug_cameraDragSpeed;

                if (Input.GetMouseButtonUp(0))
                {
                    yield break;
                }
                yield return(null);
            }
            yield return(null);
        }
    }