// ------------------------------------------------------------------------------------------------------------------ private void GenerateAssets() { // generate all needed assets so that I do not need anything else but this script in the project folder textures = new Texture2D[2]; Color32 cBlack = new Color32(0, 0, 0, 255); Color32 cWhite = new Color32(255, 255, 255, 255); Color32 cGrey = new Color32(128, 128, 128, 255); // create the background textures[0] = new Texture2D(4, 4, TextureFormat.RGBA32, false); EdGameUtil.TextureFill(textures[0], cBlack); EdGameUtil.TextureRect(textures[0], cWhite, 0, 0, 3, 3); // create the cell/block texture textures[1] = new Texture2D(CellSize, CellSize, TextureFormat.RGBA32, false); EdGameUtil.TextureFill(textures[1], cWhite); EdGameUtil.TextureFillRect(textures[1], cGrey, CellSize / 2 - 2, CellSize / 2 - 2, 4, 4); EdGameUtil.TextureRect(textures[1], cGrey, 0, 0, CellSize - 1, CellSize - 1); // apply common texture settings foreach (Texture2D t in textures) { t.wrapMode = TextureWrapMode.Clamp; t.filterMode = FilterMode.Point; t.hideFlags = HideFlags.HideAndDontSave; } // create the styles backStyle = new GUIStyle() { border = new RectOffset(1, 1, 1, 1), normal = { background = textures[0] } }; }
private bool IsOverFilledCells(bool below) { int ox, oy; EdGameUtil.CalcCellPos(boardRect, blockPos, CellSize, below, out ox, out oy); if (below) { oy++; } for (int x = 0; x < blockWH; x++) { for (int y = 0; y < blockWH; y++) { if (block[y, x] == 1) { // idx can be smaller than 0 for new blocks, which starts above board and idx could // be bigger than grid.Length because of empty cells reaching below board int idx = (x + ox) + ((y + oy) * GridWidth); if (idx >= 0 && idx < grid.Length && grid[idx] >= 0) { return(true); } } } } return(false); }
private void RotateBlock() { if (blockType == 0) { return; // no need to rotate this one } Vector2 oldPos = blockPos; int[,] oldShape = block; block = EdGameUtil.RotateMatrix(block, blockWH); // check if rotated while next to a wall and if block is now outside of board int res = 0; if ((res = IsOutsideEdge()) != 0) { blockPos.x += (res * CellSize); if (blockType == 1) { // this one might have two cells outside of border and need another kick if (blockPos.x < boardRect.x || blockPos.x + blockWH * CellSize > boardRect.xMax) { blockPos.x += (res * CellSize); } } } // check if over any filled cells now and revert if so if (IsOverFilledCells(false)) { blockPos = oldPos; block = oldShape; } }
private void DrawIndicator(Event ev) { if (clickAreaRect.Contains(ev.mousePosition)) { lastMousePos = Event.current.mousePosition; } EdGameUtil.DrawTexture(textures[2], pivotPoint, lastMousePos, -360, +360); }
private void MoveBubble(float dt) { bubblePos += moveDir * dt * bubbleMoveSpeed; // check if should stick to other bubbles int x, y; // this is buggy. sometimes a bubble will stop in the air cause not checking for the offset. Vector2 p = bubblePos; p = p + (moveDir.normalized * CellSizeHalf); EdGameUtil.CalcCellPos(boardRect, p, CellSize, true, out x, out y); if (y >= 0 && y < GridHeight && grid[y * GridWidth + x] >= 0) { EdGameUtil.CalcCellPos(boardRect, bubblePos, CellSize, true, out x, out y); if (y >= GridHeight || y < 0) { gameActive = false; return; } if (grid[y * GridWidth + x] < 0) { grid[y * GridWidth + x] = bubbleType; CheckAndBreakCells(x, y, bubbleType); SelectBubble(); return; } } // bounce bubble off left/right edge if (bubblePos.x - CellSizeHalf < boardRect.x) { bubblePos.x = boardRect.x + CellSizeHalf + 1; moveDir.x *= -1; } else if (bubblePos.x + CellSizeHalf > boardRect.xMax) { bubblePos.x = boardRect.xMax - (CellSizeHalf + 1); moveDir.x *= -1; } // stick to top edge if (bubblePos.y - CellSizeHalf < boardRect.y) { bubblePos.y = boardRect.y + CellSizeHalf + 1; EdGameUtil.CalcCellPos(boardRect, bubblePos, CellSize, true, out x, out y); if (grid[y * GridWidth + x] < 0) { grid[y * GridWidth + x] = bubbleType; CheckAndBreakCells(x, y, bubbleType); } SelectBubble(); } }
private void CheckAndBreakCells(int sx, int sy, int bubbleType) { // check if 3 or more of same colour are touching the newly placed cell int ox, oy; List <int> collected = new List <int>(); AddCellToList(collected, sx, sy, bubbleType); if (collected.Count >= 3) { foreach (int i in collected) { grid[i] = -1; EdGameUtil.IndexToXY(GridWidth, i, out ox, out oy); IncScore(1, new Vector2(ox, oy)); } // collect all cells attached to walls and ceiling or cells which are int idx; for (int x = 0; x < GridWidth; x++) { idx = GridWidth + x; if (grid[idx] >= 0) { AddCellToList(collected, x, 0); } } for (int y = 0; y < GridHeight; y++) { idx = y * GridWidth; if (grid[idx] >= 0) { AddCellToList(collected, 0, y); } idx = y * GridWidth + (GridWidth - 1); if (grid[idx] >= 0) { AddCellToList(collected, GridWidth - 1, y); } } // now run through grid and check what was not collected and remove it for (int i = 0; i < grid.Length; i++) { if (grid[i] >= 0 && !collected.Contains(i)) { grid[i] = -1; EdGameUtil.IndexToXY(GridWidth, i, out ox, out oy); IncScore(2, new Vector2(ox, oy)); } } } }
// ------------------------------------------------------------------------------------------------------------------ private void GenerateAssets() { // generate all needed assets so that I do not need any texture resources in the project textures = new Texture2D[3]; Color32 cBlack = new Color32(0, 0, 0, 255); Color32 cWhite = new Color32(255, 255, 255, 255); Color32 cGrey = new Color32(128, 128, 128, 255); Color32 cTrans = new Color32(0, 0, 0, 0); // create the background textures[0] = new Texture2D(4, 4, TextureFormat.RGBA32, false); EdGameUtil.TextureFill(textures[0], cBlack); EdGameUtil.TextureRect(textures[0], cWhite, 0, 0, 3, 3); // create the cell/bubble texture textures[1] = new Texture2D(CellSize, CellSize, TextureFormat.RGBA32, false); EdGameUtil.TextureFill(textures[1], cTrans); EdGameUtil.TextureDrawCircleFilled(textures[1], cWhite, CellSizeHalf, CellSizeHalf, CellSizeHalf); EdGameUtil.TextureDrawCircle(textures[1], cGrey, CellSizeHalf, CellSizeHalf, CellSizeHalf); EdGameUtil.TextureDrawCircleFilled(textures[1], cGrey, CellSizeHalf, CellSizeHalf, CellSizeHalf / 3); // create indicator texture textures[2] = new Texture2D(CellSizeHalf, CellSize * 3, TextureFormat.RGBA32, false); EdGameUtil.TextureFill(textures[2], cTrans); EdGameUtil.TextureDrawVLine(textures[2], cGrey, textures[2].width / 2, 0, textures[2].height - 1); EdGameUtil.TextureDrawLine(textures[2], cGrey, textures[2].width / 2, 0, 0, textures[2].height / 4); EdGameUtil.TextureDrawLine(textures[2], cGrey, textures[2].width / 2, 0, textures[2].width - 1, textures[2].height / 4); // apply common texture settings foreach (Texture2D t in textures) { t.wrapMode = TextureWrapMode.Clamp; t.filterMode = FilterMode.Point; t.hideFlags = HideFlags.HideAndDontSave; } // create the styles backStyle = new GUIStyle() { border = new RectOffset(1, 1, 1, 1), normal = { background = textures[0] } }; scoreStyle = new GUIStyle() { normal = { textColor = Color.green } }; }
private bool SnapBlock() { int ox, oy; EdGameUtil.CalcCellPos(boardRect, blockPos, CellSize, true, out ox, out oy); for (int x = 0; x < blockWH; x++) { for (int y = 0; y < blockWH; y++) { if (block[y, x] == 1) { int idx = (x + ox) + ((y + oy) * GridWidth); if (idx < 0) { return(true); // if idx invalid then reached top and game over } grid[idx] = blockType; } } } return(false); }