예제 #1
0
    void OnMouseDown()
    {
        MouseFollowHandler mfhandler = EventSystem.GetComponent <MouseFollowHandler>();

        if (mfhandler.followingSprite == null)
        {
            gameObject.GetComponent <ShipInfo>().lastTransformPos = gameObject.transform.position;

            //Reset old occupied grid data if ship is deployed
            ShipInfo   ship       = GetComponent <ShipInfo>();
            GridLayout gridLayout = GameObject.Find("Grid System").GetComponent <GridLayout>();
            if (ship.isDeployed)
            {
                for (int i = (int)ship.startPos.x; i <= (int)ship.endPos.x; i++)
                {
                    for (int j = (int)ship.startPos.y; j <= (int)ship.endPos.y; j++)
                    {
                        CellInfo c = gridLayout.Grid[i][j].GetComponent <CellInfo>();
                        c.hasShip      = false;
                        c.occupiedShip = null;
                    }
                }
                ship.isDeployed = false;
                ship.startPos   = ship.endPos = new Vector2(-1, -1);
            }

            mfhandler.followingSprite = this.gameObject;
            GetComponent <BoxCollider2D>().enabled = false;
        }
    }
예제 #2
0
    void OnMouseDown()
    {
        bool occupy = true;
        //Check condition to put ship in
        MouseFollowHandler mfhandler  = EventSystem.GetComponent <MouseFollowHandler>();
        GameObject         shipSprite = mfhandler.followingSprite;

        if (shipSprite != null)
        {
            //Checking before deploying ship into the selected cell

            ShipInfo ship   = shipSprite.GetComponent <ShipInfo>();
            CellInfo cell   = gameObject.GetComponent <CellInfo>();
            Vector2  endPos = new Vector2(cell.rowId + ship.orientation * (ship.length - 1), cell.colId + (1 - ship.orientation) * (ship.length - 1));
            for (int i = cell.rowId; i <= endPos.x; i++)
            {
                for (int j = cell.colId; j <= endPos.y; j++)
                {
                    if (!valid(i, j))
                    {
                        occupy = false;
                        break;
                    }
                }
                if (!occupy)
                {
                    break;
                }
            }

            if (occupy)
            {
                //Update grid data and deploy the ship
                Debug.Log("Ship deployed !!!");
                ship.startPos = new Vector2(cell.rowId, cell.colId);
                ship.endPos   = endPos;
                SetGridData(ship.startPos, ship.endPos, true, shipSprite);
                ship.isDeployed = true;

                //Release mouse following and restore clickable feature
                Vector2 cellPos    = this.gameObject.transform.position;
                Vector2 offsetShip = new Vector2((1 - ship.orientation) * (shipSprite.GetComponent <SpriteRenderer>().bounds.size.x / 2 - GetComponent <SpriteRenderer>().bounds.size.x / 2),
                                                 ship.orientation * (-shipSprite.GetComponent <SpriteRenderer>().bounds.size.y / 2 + GetComponent <SpriteRenderer>().bounds.size.y / 2));
                shipSprite.transform.position = cellPos + offsetShip;
                shipSprite.GetComponent <BoxCollider2D>().enabled = true;
                mfhandler.followingSprite = null;
            }
            else
            {
                Debug.Log("Nooo, can't put the ship thereee!!!");
            }
        }
    }