Esempio n. 1
0
    public static GameObject SpawnNewElement(int catagory, int type, int facing, int gridX, int gridY)
    {
        // create the holder
        GameObject  newEle  = new GameObject("new element");
        ElementInfo eleInfo = ElementInfos.First(ei => ei.Catagory == catagory && ei.Type == type);
        EleComp     eleComp = newEle.AddComponent <EleComp>();

        eleComp.ElementInfo = eleInfo;
        eleComp.SetGridPos(new Vector2(gridX, gridY));
        newEle.transform.rotation = Quaternion.Euler(0, 0, facing * Mathf.PI / 2.0f);

        // create sprite in left world
        GameObject spriteL = new GameObject("sprite");

        spriteL.transform.parent = newEle.transform;
        spriteL.layer            = LayerMask.NameToLayer("ThingL");
        spriteL.AddComponent <SpriteRenderer>().sprite = Resources.Load <Sprite>("Elements/" + eleInfo.AssetL);
        Vector3 offsetL = eleInfo.OffsetL;

        spriteL.transform.localPosition = new Vector3(offsetL.x * GameManager.GridWorldSize, offsetL.y * GameManager.GridWorldSize, 0);

        PolygonCollider2D polyColl2DL = spriteL.AddComponent <PolygonCollider2D>();
        List <Vector2>    collPointsL = new List <Vector2>();

        foreach (Vector2 p in eleInfo.CollidPointsL)
        {
            collPointsL.Add(new Vector2(p.x * GameManager.GridWorldSize, p.y * GameManager.GridWorldSize));
        }
        polyColl2DL.SetPath(0, collPointsL.ToArray());

        // create sprite in right world
        GameObject spriteR = new GameObject("sprite");

        spriteR.transform.parent = newEle.transform;
        spriteR.layer            = LayerMask.NameToLayer("ThingR");
        spriteR.AddComponent <SpriteRenderer>().sprite = Resources.Load <Sprite>("Elements/" + eleInfo.AssetR);
        Vector3 offsetR = eleInfo.OffsetR;

        spriteR.transform.localPosition = new Vector3(offsetR.x * GameManager.GridWorldSize, offsetR.y * GameManager.GridWorldSize, 0);

        PolygonCollider2D polyColl2DR = spriteR.AddComponent <PolygonCollider2D>();
        List <Vector2>    collPointsR = new List <Vector2>();

        foreach (Vector2 p in eleInfo.CollidPointsR)
        {
            collPointsR.Add(new Vector2(p.x * GameManager.GridWorldSize, p.y * GameManager.GridWorldSize));
        }
        polyColl2DR.SetPath(0, collPointsR.ToArray());

        GameManager.Instance.EleCompList.Add(eleComp);

        if (catagory == 2)
        {
            GameManager.Instance.EleCompGreen = eleComp;
        }

        if (catagory == 3)
        {
            GameManager.Instance.EleCompRed = eleComp;
        }



        return(newEle);
    }
Esempio n. 2
0
    void Update()
    {
        if (GameState == EGameState.Gaming)
        {
            if (!SelEleComp)
            {
                CheckWin();
            }

            if (Input.GetMouseButtonDown(0) && !SelEleComp)
            {
                int          layerMask = LayerMask.GetMask(new string[] { "Sensor", "ThingL", "ThingR" });
                Collider2D[] colliders = Physics2D.OverlapPointAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), layerMask);
                if (colliders.Length > 0)
                {
                    SelEleComp             = colliders[0].transform.parent.gameObject.GetComponent <EleComp>();
                    LastSelEleComp         = SelEleComp;
                    SelEleCompBeginMovePos = SelEleComp.transform.position;
                    MouseDragBeginPos      = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                }
            }

            if (Input.GetMouseButtonUp(0))     // todo change back to !button after debugging
            {
                if (SelEleComp)
                {
                    // snap to grid
                    SelEleComp.SetGridPos(SelEleComp.GetNearestGridPos());

                    bool posConflict = false;

                    // you can insert element into others, so fix the y again
                    foreach (Vector2 relaPos in SelEleComp.ElementInfo.OccupyGridsL)
                    {
                        int checkX = (int)(SelEleComp.Grid.x + relaPos.x);
                        int checkY = (int)(SelEleComp.Grid.y + relaPos.y);
                        if (GetGridWorldIndex(checkX) == 0)
                        {
                            if (GridStats[checkY, checkX].State == EGridState.Occupied)
                            {
                                posConflict = true;
                                break;
                            }
                        }
                    }

                    foreach (Vector2 relaPos in SelEleComp.ElementInfo.OccupyGridsR)
                    {
                        int checkX = (int)(SelEleComp.Grid.x + relaPos.x);
                        int checkY = (int)(SelEleComp.Grid.y + relaPos.y);
                        if (GetGridWorldIndex(checkX) == 1)
                        {
                            if (GridStats[checkY, checkX].State == EGridState.Occupied)
                            {
                                posConflict = true;
                                break;
                            }
                        }
                    }

                    if (posConflict)
                    {
                        Vector3 realPos = SelEleComp.transform.position;
                        // note hack
                        Vector3 raisePos   = new Vector3(realPos.x, GridWorldSize * 8, 0);
                        Vector2 stableGrid = SelEleComp.FindStableGrid(raisePos);
                        SelEleComp.SetGridPos(stableGrid);
                        SelEleComp.State = EEleCompState.Stable;
                    }
                    else
                    {
                        SelEleComp.State = EEleCompState.Falling;
                    }

                    SelEleComp = null;
                }
            }

            if (Input.GetMouseButton(0) && SelEleComp)
            {
                Vector3 mouseWorldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
                Vector3 newPos        = new Vector3(SelEleCompBeginMovePos.x + mouseWorldPos.x - MouseDragBeginPos.x, SelEleCompBeginMovePos.y + mouseWorldPos.y - MouseDragBeginPos.y, 0);
                // todo fixPos
                Vector3 fixedNewPos = SelEleComp.GetFixedLimitPos(newPos);

                SelEleComp.transform.position = fixedNewPos;
            }

            // todo check and update grid board states (accoirding to all EleComp Status)

            // cleanup
            for (int i = 0; i < GridRowCount; i++)
            {
                for (int j = 0; j < GridColCount; j++)
                {
                    GridStats[i, j].EleComp   = null;
                    GridStats[i, j].PartIndex = 0;
                }
            }

            foreach (EleComp eleComp in EleCompList)
            {
                if (eleComp == SelEleComp || eleComp.State != EEleCompState.Stable)
                {
                    continue;
                }

                // left world
                for (int i = 0; i < eleComp.ElementInfo.OccupyGridsL.Length; i++)
                {
                    Vector2 relaGridPos = eleComp.ElementInfo.OccupyGridsL[i];
                    int     gridX       = (int)(eleComp.Grid.x + relaGridPos.x);
                    int     gridY       = (int)(eleComp.Grid.y + relaGridPos.y);

                    if (GetGridWorldIndex(gridX) == 0 && CheckGridInBoard(gridX, gridY))
                    {
                        GridStats[gridY, gridX].EleComp   = eleComp;
                        GridStats[gridY, gridX].PartIndex = i;
                    }
                }

                // right world
                for (int i = 0; i < eleComp.ElementInfo.OccupyGridsR.Length; i++)
                {
                    Vector2 relaGridPos = eleComp.ElementInfo.OccupyGridsR[i];
                    int     gridX       = (int)(eleComp.Grid.x + relaGridPos.x);
                    int     gridY       = (int)(eleComp.Grid.y + relaGridPos.y);

                    if (GetGridWorldIndex(gridX) == 1 && CheckGridInBoard(gridX, gridY))
                    {
                        GridStats[gridY, gridX].EleComp   = eleComp;
                        GridStats[gridY, gridX].PartIndex = i;
                    }
                }
            }
        }
        else
        {
            if (Input.GetMouseButtonDown(0) && !SelEleComp)
            {
                int          layerMask = LayerMask.GetMask(new string[] { "GUIButton" });
                Collider2D[] colliders = Physics2D.OverlapPointAll(Camera.main.ScreenToWorldPoint(Input.mousePosition), layerMask);
                if (colliders.Length > 0)
                {
                    GUIButton button = colliders[0].gameObject.GetComponent <GUIButton>();
                    if (button)
                    {
                        HandleButtonEvent(button.FuncName);
                    }
                }
            }
        }
    }