public void Attach(int x, int y, TriBlock shape) { attachHistories.Add(new AttachHistory() { offsetX = x, offsetY = y, block = shape }); triGrid.Attach(x, y, shape); }
public TriBlock AddBlock(int offsetX, int offsetY, TriBlock block) { TriBlock result = new TriBlock(); result.mask = this.mask; TriBlock shifted = block.Shift(offsetX, offsetY); result.mask |= shifted.mask; return(result); }
public TriBlock Shift(int offsetX, int offsetY) { TriBlock result = new TriBlock(); bool left = offsetX < 0; bool down = offsetY < 0; offsetX = System.Math.Abs(offsetX) * 2; offsetY = System.Math.Abs(offsetY); result.mask = this.mask; if (offsetY >= 4) { result.mask = 0; return(result); } // Y offset if (down) { result.mask = result.mask >> (offsetY * 8); } else { result.mask = result.mask << (offsetY * 8); } // X offset uint row0 = result.mask & 255; uint row1 = (result.mask >> 8) & 255; uint row2 = (result.mask >> 16) & 255; uint row3 = (result.mask >> 24) & 255; if (left) { row0 = row0 >> offsetX; row1 = row1 >> offsetX; row2 = row2 >> offsetX; row3 = row3 >> offsetX; } else { row0 = (row0 << offsetX) & 255; row1 = (row1 << offsetX) & 255; row2 = (row2 << offsetX) & 255; row3 = (row3 << offsetX) & 255; } result.mask = row0 | (row1 << 8) | (row2 << 16) | (row3 << 24); return(result); }
public void Attach(int offsetX, int offsetY, TriBlock triBlock) { for (int y = 0; y < gridSize.y; y++) { for (int x = 0; x < gridSize.x; x++) { int index = y * gridSize.x + x; TriBlock block = blocks[index]; int blockOffsetX = offsetX - x * 4; int blockOffsetY = offsetY - y * 4; blocks[index].mask = block.AddBlock(blockOffsetX, blockOffsetY, triBlock).mask; } } }
public bool CanFit(int offsetX, int offsetY, TriBlock toFit) { for (int y = 0; y < gridSize.y; y++) { for (int x = 0; x < gridSize.x; x++) { int index = y * gridSize.x + x; TriBlock block = blocks[index]; int blockOffsetX = offsetX - x * 4; int blockOffsetY = offsetY - y * 4; Debug.Log(block + "-------\n" + toFit.Shift(blockOffsetX, blockOffsetY)); if (!block.CanFit(toFit.Shift(blockOffsetX, blockOffsetY))) { return(false); } } } return(true); }
void Update() { float blockSize = cellSize * 4; for (int y = 0; y < gridSize.y; y++) { for (int x = 0; x < gridSize.x; x++) { Vector2 origin = new Vector2(x, y) * blockSize; int index = y * gridSize.x + x; TriBlock block = blocks[index]; for (int i = 0; i < 16; i++) { int ix = i % 4; int iy = i / 4; uint mask = invert ? ~block.mask : block.mask; uint cellMask = mask >> (i * 2) & 3; var triCell = triCells[index, i]; triCell.UpdatePresentation(cellMask); triCells[index, i] = triCell; } } } }
void Start() { triCells = new TriCell[blocks.Length, 16]; float blockSize = cellSize * 4; for (int y = 0; y < gridSize.y; y++) { for (int x = 0; x < gridSize.x; x++) { Vector2 origin = new Vector2(x, y) * blockSize; origin = CoordsUtils.OrthoToSlope(origin); GameObject blockObj = new GameObject(); blockObj.name = "block" + x + "," + y; blockObj.transform.SetParent(transform, false); blockObj.transform.localPosition = origin; blockObj.hideFlags = HideFlags.DontSave; int index = y * gridSize.x + x; TriBlock block = blocks[index]; for (int i = 0; i < 16; i++) { int ix = i % 4; int iy = i / 4; uint mask = invert ? ~block.mask : block.mask; uint cellMask = mask >> (i * 2) & 3; GameObject cell = Instantiate <GameObject>(cellPrefab); cell.name = "cell" + i; cell.transform.SetParent(blockObj.transform, false); var cellPos = CoordsUtils.OrthoToSlope(new Vector2(ix, iy) * cellSize); cell.transform.localPosition = cellPos; var triCell = cell.GetComponent <TriCell>(); triCell.UpdatePresentation(cellMask); triCells[index, i] = triCell; } } } }
public bool CanFit(TriBlock block) { var inverted = ~this.mask; return((inverted | block.mask) == inverted); }