Пример #1
0
    ElementBehaviour CreateCurrentLinkManually(Vector3 curPos, int index, Vector3[] connectPts, int lastIndex, out int newIndex)
    {
        newIndex = 0;                   //reminder: it will change in ANY way, just to get rid of error
        GameObject       link = (GameObject)Instantiate(wires[1], curPos, Quaternion.identity);
        ElementBehaviour elt  = link.GetComponent <ElementBehaviour>();

        elements[(int)(curPos.x) + 3, (int)(curPos.y) + 3] = elt;
        bool isPlaced = false;

        for (int k = 0; k < 4; k++)
        {
            link.transform.Rotate(0f, 0f, 90f, Space.Self);
            elt.FindConnectionPoints();
            for (int i = 0; i < elt.connectionsPts.Length; i++)
            {
                if (UniMath.ApproximatelyEqual(elt.connectionsPts[i], connectPts[lastIndex]))
                {
                    if (elt.connectionsPts[1 - i].x > -3.5f && elt.connectionsPts[1 - i].x <4.5f && elt.connectionsPts[1 - i].y> -3.5f && elt.connectionsPts[1 - i].y < 4.5f)                   //if we're not out of bounds
                    {
                        isPlaced = true;
                        newIndex = 1 - i;
                        break;
                    }
                }
            }
            if (isPlaced)
            {
                break;
            }
        }
        link.name = link.name.Remove(link.name.Length - 7);
        link.transform.SetParent(elementsRoot);
        return(elt);
    }
Пример #2
0
    public void CheckForCurcuit(Vector3 lastPt)
    {
        bool pathWasFound = false;

        for (int i = 0; i < elements.Length; i++)                                                         //for every element in level
        {
            if (elements[i].GetInstanceID() != this.GetInstanceID() && elementCanBeUsed[i])               //if this element wasn't used and it is not the same element that has this script
            {
                for (int j = 0; j < elements[i].connectionsPts.Length; j++)                               //we need to check all connection points of that element
                {
                    for (int k = 0; k < connectionsPts.Length; k++)                                       //and compare with points of element that has this script
                    {
                        if (UniMath.ApproximatelyEqual(elements[i].connectionsPts[j], connectionsPts[k])) //float equality
                        {
                            if (!UniMath.ApproximatelyEqual(lastPt, connectionsPts[k]))                   //check if we're not "going backwards" to the same point that we found before
                            {
                                if (elements[i].isDisplay)                                                //if it is a display, turn it on
                                {
                                    elements[i].GetComponent <SpriteRenderer>().sprite = displayOn;
                                    winlose.EnableDisplay();
                                    StartCoroutine(elements[i].MoveElectron(lastPt, elements[i].connectionsPts[j], null, this.transform.position));
                                    this.enabled = false;
                                    pathWasFound = true;
                                }
                                else if (elements[i].isPower)
                                {
                                    elements[i].GetComponent <SpriteRenderer>().sprite = powerOn;
                                    StartCoroutine(elements[i].MoveElectron(lastPt, elements[i].connectionsPts[j], null, this.transform.position));
                                    this.enabled = false;
                                    pathWasFound = true;
                                }
                                else                                                                    //continue search
                                {
                                    elementCanBeUsed[i] = false;                                        //we can't use founded element no more (to prevent short curcuits (and stack overflows :D))
                                    StartCoroutine(elements[i].MoveElectron(lastPt, elements[i].connectionsPts[j], elements[i], this.transform.position));
                                    this.enabled = false;
                                    pathWasFound = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!pathWasFound)
        {
            if (UniMath.ApproximatelyEqual(lastPt, connectionsPts[0]))
            {
                StartCoroutine(MoveElectron(lastPt, connectionsPts[1], null, this.transform.position));
            }
            else
            {
                StartCoroutine(MoveElectron(lastPt, connectionsPts[0], null, this.transform.position));
            }
        }
    }
Пример #3
0
    IEnumerator MoveToPosition(float y, Transform elt)
    {
        float curvel = 0f;

        while (!UniMath.ApproximatelyEqual(elt.position.y, y, 0.001f))
        {
            elt.position = new Vector3(elt.position.x, Mathf.SmoothDamp(elt.position.y, y, ref curvel, 0.2f));
            yield return(new WaitForFixedUpdate());
        }
        elt.GetComponent <GridEltBehaviour>().needToFindConnectionPts = true;
    }
Пример #4
0
 public void OnMouseUpAsButton()
 {
     if (!ui.winloseGroup.activeInHierarchy)
     {
         for (int j = 0; j < powerSources.Length; j++)
         {
             if (UniMath.ApproximatelyEqual(transform.position, powerSources[j].transform.position, 0.5f))
             {
                 if (!powerSourceIsEnabled[j])
                 {
                     powerSourceIsEnabled[j] = true;
                     ui.FadeButton(this.GetComponent <SpriteRenderer>());
                     EnablePower(j);
                     break;
                 }
             }
         }
     }
 }
Пример #5
0
    IEnumerator MoveElectron(Vector3 start, Vector3 end, GridEltBehaviour element, Vector3 center)
    {
        GameObject newElectron = (GameObject)Instantiate(electron, start, Quaternion.identity);

        GridGenerator.checkGrid = true;
        //elementsUsed++;
        int i = 1;

        if (!UniMath.ApproximatelyEqual(start.x, end.x) && !UniMath.ApproximatelyEqual(start.y, end.y))
        {
            while (newElectron.transform.position != center)
            {
                newElectron.transform.position = Vector3.Lerp(start, center, 0.4f * i);
                i++;
                yield return(new WaitForFixedUpdate());
            }
            i = 1;
            while (newElectron.transform.position != end)
            {
                newElectron.transform.position = Vector3.Lerp(center, end, 0.4f * i);
                i++;
                yield return(new WaitForFixedUpdate());
            }
        }
        else
        {
            while (newElectron.transform.position != end)
            {
                newElectron.transform.position = Vector3.Lerp(start, end, 0.2f * i);
                i++;
                yield return(new WaitForFixedUpdate());
            }
        }
        if (element != null)
        {
            element.CheckForCurcuit(end);
        }

        Destroy(newElectron);
    }
Пример #6
0
    IEnumerator DisplayReplace(Transform disp)
    {
        Vector3 oldPos = disp.position;

        yield return(new WaitForSeconds(0.3f));

        StartCoroutine(Fading.FadeSprite(1f, 0f, 0.15f, disp.GetComponent <SpriteRenderer>()));
        while (!UniMath.ApproximatelyEqual(disp.position.x, 5.5f, 0.1f))
        {
            disp.Translate(Vector3.right * 0.1f, Space.World);
            yield return(new WaitForFixedUpdate());
        }
        yield return(new WaitUntil(() => GridGenerator.checkGrid == false));                    //this will execute only when all electrons will have died

        disp.GetComponent <SpriteRenderer>().color  = Color.white;
        disp.GetComponent <SpriteRenderer>().sprite = displayOff;
        float curvel = 0f;

        while (!UniMath.ApproximatelyEqual(disp.position.x, 4.5f, 0.001f))
        {
            disp.position = new Vector3(Mathf.SmoothDamp(disp.position.x, oldPos.x, ref curvel, 0.2f), disp.position.y);
            yield return(new WaitForFixedUpdate());
        }
    }
Пример #7
0
    void CreateInitialPowersource()
    {
        bool isPlaced = false;

        while (!isPlaced)
        {
            int x = Random.Range(-3, 5);
            int y = Random.Range(-3, 5);
            if (elements[x + 3, y + 3] == null)
            {
                GameObject       go  = (GameObject)Instantiate(powerSource, new Vector3(x, y), Quaternion.identity);                    //instantiate and move to position
                ElementBehaviour elt = go.GetComponent <ElementBehaviour>();
                elements[x + 3, y + 3] = elt;                                                                                           //add to the matrix
                go.transform.Rotate(0f, 0f, 90f * Random.Range(0, 4), Space.Self);
                for (int i = 0; i < 4; i++)
                {
                    go.transform.Rotate(0f, 0f, 90f, Space.Self);
                    elt.FindConnectionPoints();
                    if (elt.connectionsPts[0].x > -3.5f && elt.connectionsPts[0].x <4.5f && elt.connectionsPts[0].y> -3.5f && elt.connectionsPts[0].y < 4.5f)                           //if we're not out of bounds
                    //calculate position for next link
                    {
                        Vector3 offset = Vector3.zero;
                        if (UniMath.ApproximatelyEqual(elt.connectionsPts[0].x, elt.transform.position.x))
                        {
                            if (elt.connectionsPts[0].y > elt.transform.position.y)
                            {
                                offset = Vector3.up;                                            //above the center of current link
                            }
                            else
                            {
                                offset = Vector3.down;                                          //below the center
                            }
                        }
                        else if (UniMath.ApproximatelyEqual(elt.connectionsPts[0].y, elt.transform.position.y))
                        {
                            if (elt.connectionsPts[0].x > elt.transform.position.x)
                            {
                                offset = Vector3.right;                                         //to the right to the center
                            }
                            else
                            {
                                offset = Vector3.left;                                          //to the left to the center
                            }
                        }
                        if (elements[x + 3 + (int)offset.x, y + 3 + (int)offset.y] == null)
                        {
                            isPlaced = true;
                            break;
                        }
                    }
                }
                if (!isPlaced)                                          //if we fail to place our new powersource at given (x, y) coords
                {
                    Destroy(go);
                    continue;
                }
                go.name = go.name.Remove(go.name.Length - 7);                                                                                                                           //edit name
                go.transform.SetParent(elementsRoot);                                                                                                                                   //edit hierachy
                GenerateVoltageButton();                                                                                                                                                //make voltage button

                GameObject lightning = (GameObject)Instantiate(powerSourceLightning, go.transform.position, Quaternion.identity);
                elt.lightningSprite = lightning.GetComponent <SpriteRenderer>();

                CreateChainLink(null, elt, 0, 0);
            }
        }
    }
Пример #8
0
    void CreateChainLink(ElementBehaviour lastLink, ElementBehaviour curLink, int lastIndex, int index)
    {
        int  eltIndex = 0, newIndex = 0;
        bool isPlaced = false;

        if (!(curLink.connectionsPts[index].x > -3.5f && curLink.connectionsPts[index].x <4.5f && curLink.connectionsPts[index].y> -3.5f && curLink.connectionsPts[index].y < 4.5f))                            //if we're out of bounds
        {
            if (UniMath.ApproximatelyEqual((Vector2)curLink.transform.position, new Vector2(-3f, -3f)) ||
                UniMath.ApproximatelyEqual((Vector2)curLink.transform.position, new Vector2(4f, -3f)) ||
                UniMath.ApproximatelyEqual((Vector2)curLink.transform.position, new Vector2(-3f, 4f)) ||
                UniMath.ApproximatelyEqual((Vector2)curLink.transform.position, new Vector2(4f, 4f)))                           //corner?
            //here we need to REPLACE curLink with indicator
            {
                Vector3 curPos = curLink.transform.position;
                Destroy(curLink.gameObject);
                CreateDisplay(curPos);
                return;
            }
            else
            {
                //here we need to REPLACE curLink with a turn
                Vector3 curPos = curLink.transform.position;
                Destroy(curLink.gameObject);
                curLink = CreateCurrentLinkManually(curPos, 1, lastLink.connectionsPts, lastIndex, out index);
            }
        }                       //if we're not out of bounds, just ignore and go further

        //Calculate position for new link
        Vector3 offset = Vector3.zero;

        if (UniMath.ApproximatelyEqual(curLink.connectionsPts[index].x, curLink.transform.position.x))
        {
            if (curLink.connectionsPts[index].y > curLink.transform.position.y)
            {
                offset = Vector3.up;                            //above the center of current link
            }
            else
            {
                offset = Vector3.down;                          //below the center
            }
        }
        else if (UniMath.ApproximatelyEqual(curLink.connectionsPts[index].y, curLink.transform.position.y))
        {
            if (curLink.connectionsPts[index].x > curLink.transform.position.x)
            {
                offset = Vector3.right;                         //to the right to the center
            }
            else
            {
                offset = Vector3.left;                          //to the left to the center
            }
        }

        int ix = (int)((curLink.transform.position + offset).x) + 3;
        int iy = (int)((curLink.transform.position + offset).y) + 3;

        if (elements[ix, iy] != null)                   //if there's already an element
        {
            if (elements[ix, iy].isPower)               //is it a powersource?
            //here we need to REPLACE curLink with indicator
            {
                Vector3 curPos = curLink.transform.position;
                Destroy(curLink.gameObject);
                CreateDisplay(curPos);
                return;
            }
            else
            {
                int iix = (int)((curLink.transform.position + offset * 2).x);                           //индекс элемента через элемент в том же направлении
                int iiy = (int)((curLink.transform.position + offset * 2).y);                           // --||--
                if (iix >= -3 && iix <= 4 && iiy >= -3 && iiy <= 4)                                     //if we're still in bounds
                //here we need to REPLACE curLink with indicator
                {
                    Vector3 curPos = curLink.transform.position;
                    Destroy(curLink.gameObject);
                    CreateDisplay(curPos);
                    return;
                }
                else
                {
                    //here we need to REPLACE curLink with indicator
                    Vector3 curPos = curLink.transform.position;
                    Destroy(curLink.gameObject);
                    CreateDisplay(curPos);
                    return;
                }
            }
        }
        else                    //else if the place is free
        //new element will be turn or straight
        {
            eltIndex = Random.Range(0, 2);
        }

        //create new element
        GameObject       link = (GameObject)Instantiate(wires[eltIndex], curLink.transform.position + offset, Quaternion.identity);
        ElementBehaviour elt  = link.GetComponent <ElementBehaviour>();

        elements[ix, iy] = elt;
        isPlaced         = false;
        for (int k = 0; k < 4; k++)
        {
            link.transform.Rotate(0f, 0f, 90f, Space.Self);
            elt.FindConnectionPoints();
            for (int i = 0; i < elt.connectionsPts.Length; i++)
            {
                if (UniMath.ApproximatelyEqual(elt.connectionsPts[i], curLink.connectionsPts[index]))
                {
                    isPlaced = true;
                    newIndex = 1 - i;
                    break;
                }
            }
            if (isPlaced)
            {
                break;
            }
        }
        link.name = link.name.Remove(link.name.Length - 7);
        link.transform.SetParent(elementsRoot);
        CreateChainLink(curLink, elt, index, newIndex);
    }
Пример #9
0
    IEnumerator MoveElectron(Vector3 start, Vector3 end, ElementBehaviour element, Vector3 center)
    {
        GameObject newElectron = (GameObject)Instantiate(electron, start, Quaternion.identity);

        WinLoseManager.nowtime = 0.4f;
        elementsUsed++;
        int i = 1;

        if (!UniMath.ApproximatelyEqual(start.x, end.x) && !UniMath.ApproximatelyEqual(start.y, end.y))
        {
            while (newElectron.transform.position != center)
            {
                newElectron.transform.position = Vector3.Lerp(start, center, 0.2f * i);
                i++;
                yield return(new WaitForFixedUpdate());
            }
            i = 1;
            while (newElectron.transform.position != end)
            {
                newElectron.transform.position = Vector3.Lerp(center, end, 0.2f * i);
                i++;
                yield return(new WaitForFixedUpdate());
            }
        }
        else
        {
            while (newElectron.transform.position != end)
            {
                newElectron.transform.position = Vector3.Lerp(start, end, 0.1f * i);
                i++;
                yield return(new WaitForFixedUpdate());
            }
        }
        if (element != null)                    //if we need to check further
        {
            element.CheckForCurcuit(end);
        }

        while (true)
        {
            i = 1;
            newElectron.transform.position = start;
            if (!UniMath.ApproximatelyEqual(start.x, end.x) && !UniMath.ApproximatelyEqual(start.y, end.y))
            {
                while (newElectron.transform.position != center)
                {
                    newElectron.transform.position = Vector3.Lerp(start, center, 0.2f * i);
                    i++;
                    yield return(new WaitForFixedUpdate());
                }
                i = 1;
                while (newElectron.transform.position != end)
                {
                    newElectron.transform.position = Vector3.Lerp(center, end, 0.2f * i);
                    i++;
                    yield return(new WaitForFixedUpdate());
                }
            }
            else
            {
                while (newElectron.transform.position != end)
                {
                    newElectron.transform.position = Vector3.Lerp(start, end, 0.1f * i);
                    i++;
                    yield return(new WaitForFixedUpdate());
                }
            }
        }
    }
Пример #10
0
    public void CheckForCurcuit(Vector3 lastPt)
    {
        bool pathWasFound = false;

        for (int i = 0; i < elements.Length; i++)                                                               //for every element in level
        {
            if (elements[i].GetInstanceID() != this.GetInstanceID() && elementCanBeUsed[i])                     //if this element wasn't used and it is not the same element that has this script
            {
                for (int j = 0; j < elements[i].connectionsPts.Length; j++)                                     //we need to check all connection points of that element
                {
                    for (int k = 0; k < connectionsPts.Length; k++)                                             //and compare with points of element that has this script
                    {
                        if (UniMath.ApproximatelyEqual(elements[i].connectionsPts[j], connectionsPts[k], 0.1f)) //float equality
                        {
                            if (!UniMath.ApproximatelyEqual(lastPt, connectionsPts[k], 0.1f))                   //check if we're not "going backwards" to the same point that we found before
                            {
                                if (elements[i].isDisplay)                                                      //if it is a display, turn it on
                                {
                                    elements[i].GetComponent <SpriteRenderer>().sprite = displayOn;
                                    ui.displaysRemain--;
                                    ui.AddComboDisplay();
                                    StartCoroutine(DisplayReplace(elements[i].transform));
                                    StartCoroutine(elements[i].MoveElectron(lastPt, elements[i].connectionsPts[j], null, this.transform.position));
                                    pathWasFound = true;
                                }
                                else if (elements[i].isPower)
                                {
                                    elements[i].GetComponent <SpriteRenderer>().sprite = powerOn;
                                    StartCoroutine(elements[i].PowersourcesCooldown());
                                    StartCoroutine(elements[i].MoveElectron(lastPt, elements[i].connectionsPts[j], null, this.transform.position));
                                    pathWasFound = true;
                                }
                                else                                                                    //continue search
                                {
                                    elementCanBeUsed[i] = false;                                        //we can't use founded element no more (to prevent short curcuits (and stack overflows :D))
                                    StartCoroutine(elements[i].MoveElectron(lastPt, elements[i].connectionsPts[j], elements[i], this.transform.position));
                                    if (!elements[i].isPower)
                                    {
                                        StartCoroutine(Fading.FadeSprite(1f, 0f, 0.15f, elements[i].GetComponent <SpriteRenderer>(), 0.5f, false));
                                        GridGenerator.grid[(int)Mathf.Round(elements[i].transform.position.x + 2.5f), (int)Mathf.Round(elements[i].transform.position.y + 4f)] = null;
                                    }
                                    pathWasFound = true;
                                }
                            }
                        }
                    }
                }
            }
        }
        if (!pathWasFound)
        {
            if (UniMath.ApproximatelyEqual(lastPt, connectionsPts[0]))
            {
                StartCoroutine(MoveElectron(lastPt, connectionsPts[1], null, this.transform.position));
            }
            else
            {
                StartCoroutine(MoveElectron(lastPt, connectionsPts[0], null, this.transform.position));
            }
        }
    }