private void InitTileMap() { // setup overall container and place it in the middle of the screen boardContainer = new FContainer(); boardContainer.x = Futile.screen.halfWidth - tileMapWidth * tileSize / 2f; boardContainer.y = Futile.screen.halfHeight - tileMapHeight * tileSize / 2f; AddChild(boardContainer); // create the blank background tiles for (int i = 0; i < tileMapWidth; i++) { for (int j = 0; j < tileMapHeight; j++) { FSprite backgroundTile = new FSprite("WhiteBox"); backgroundTile.width = backgroundTile.height = tileSize; backgroundTile.color = new Color(0.2f, 0.2f, 0.2f); backgroundTile.x = (i + 0.5f) * tileSize; backgroundTile.y = (j + 0.5f) * tileSize; boardContainer.AddChild(backgroundTile); } } // create random tiles and fill the board with them tileMap = new LineTile[tileMapWidth][]; for (int i = 0; i < tileMapWidth; i++) { tileMap[i] = new LineTile[tileMapHeight]; for (int j = 0; j < tileMapHeight; j++) { LineTileType randomTileType = (LineTileType)Random.Range(0, (int)LineTileType.MAX); RotationType randomRotationType = (RotationType)Random.Range(0, (int)RotationType.MAX); LineTile newTile = new LineTile(randomTileType, randomRotationType); newTile.tileIndex.xIndex = i; newTile.tileIndex.yIndex = j; newTile.sprite.width = newTile.sprite.height = tileSize; tileMap[i][j] = newTile; newTile.x = (i + 0.5f) * tileSize; newTile.y = (j + 0.5f) * tileSize; boardContainer.AddChild(newTile); } } // create the tile outlines for (int i = 0; i < tileMapWidth; i++) { for (int j = 0; j < tileMapHeight; j++) { FSprite backgroundTile = new FSprite("lineTileOutline"); backgroundTile.width = backgroundTile.height = tileSize; backgroundTile.color = new Color(0.2f, 0.2f, 0.2f); backgroundTile.x = (i + 0.5f) * tileSize; backgroundTile.y = (j + 0.5f) * tileSize; boardContainer.AddChild(backgroundTile); } } }
public TileIndex tileIndex = new TileIndex(); // the tile's location on the board public LineTile(LineTileType lineTileType, RotationType rotationType) { this.lineTileType = lineTileType; this.rotationType = rotationType; // set up sprite switch (lineTileType) { case LineTileType.Nub: sprite = new FSprite("lineTileNub"); break; case LineTileType.Line: sprite = new FSprite("lineTileLine"); break; case LineTileType.Corner: sprite = new FSprite("lineTileCorner"); break; case LineTileType.Threeway: sprite = new FSprite("lineTileThreeway"); break; case LineTileType.Cross: sprite = new FSprite("lineTileCross"); break; default: throw new FutileException("invalid line tile type"); } AddChild(sprite); // set up sprite rotation switch (rotationType) { case RotationType.Rotation0: sprite.rotation = 0; break; case RotationType.Rotation90: sprite.rotation = 90; break; case RotationType.Rotation180: sprite.rotation = 180; break; case RotationType.Rotation270: sprite.rotation = 270; break; default: throw new FutileException("invalid rotation type"); } // set up bitmask by doing bitwise OR with each side that is included in the shape // so for example, a tile that has all four sides solid (e.g. the cross tile) would be // 1 | 2 | 4 | 8 = 15, which is the same as 0001 | 0010 | 0100 | 1000 = 1111 in binary if (lineTileType == LineTileType.Nub) { if (rotationType == RotationType.Rotation0) { bitmask = kBitmaskTop; } if (rotationType == RotationType.Rotation90) { bitmask = kBitmaskRight; } if (rotationType == RotationType.Rotation180) { bitmask = kBitmaskBottom; } if (rotationType == RotationType.Rotation270) { bitmask = kBitmaskLeft; } } if (lineTileType == LineTileType.Line) { if (rotationType == RotationType.Rotation0 || rotationType == RotationType.Rotation180) { bitmask = kBitmaskTop | kBitmaskBottom; } if (rotationType == RotationType.Rotation90 || rotationType == RotationType.Rotation270) { bitmask = kBitmaskRight | kBitmaskLeft; } } if (lineTileType == LineTileType.Corner) { if (rotationType == RotationType.Rotation0) { bitmask = kBitmaskTop | kBitmaskRight; } if (rotationType == RotationType.Rotation90) { bitmask = kBitmaskRight | kBitmaskBottom; } if (rotationType == RotationType.Rotation180) { bitmask = kBitmaskBottom | kBitmaskLeft; } if (rotationType == RotationType.Rotation270) { bitmask = kBitmaskLeft | kBitmaskTop; } } if (lineTileType == LineTileType.Threeway) { if (rotationType == RotationType.Rotation0) { bitmask = kBitmaskTop | kBitmaskRight | kBitmaskBottom; } if (rotationType == RotationType.Rotation90) { bitmask = kBitmaskRight | kBitmaskBottom | kBitmaskLeft; } if (rotationType == RotationType.Rotation180) { bitmask = kBitmaskBottom | kBitmaskLeft | kBitmaskTop; } if (rotationType == RotationType.Rotation270) { bitmask = kBitmaskLeft | kBitmaskTop | kBitmaskRight; } } if (lineTileType == LineTileType.Cross) { bitmask = kBitmaskTop | kBitmaskRight | kBitmaskBottom | kBitmaskLeft; } }
public TileIndex tileIndex = new TileIndex(); // the tile's location on the board #endregion Fields #region Constructors public LineTile(LineTileType lineTileType, RotationType rotationType) { this.lineTileType = lineTileType; this.rotationType = rotationType; // set up sprite switch (lineTileType) { case LineTileType.Nub: sprite = new FSprite("lineTileNub"); break; case LineTileType.Line: sprite = new FSprite("lineTileLine"); break; case LineTileType.Corner: sprite = new FSprite("lineTileCorner"); break; case LineTileType.Threeway: sprite = new FSprite("lineTileThreeway"); break; case LineTileType.Cross: sprite = new FSprite("lineTileCross"); break; default: throw new FutileException("invalid line tile type"); } AddChild(sprite); // set up sprite rotation switch (rotationType) { case RotationType.Rotation0: sprite.rotation = 0; break; case RotationType.Rotation90: sprite.rotation = 90; break; case RotationType.Rotation180: sprite.rotation = 180; break; case RotationType.Rotation270: sprite.rotation = 270; break; default: throw new FutileException("invalid rotation type"); } // set up bitmask by doing bitwise OR with each side that is included in the shape // so for example, a tile that has all four sides solid (e.g. the cross tile) would be // 1 | 2 | 4 | 8 = 15, which is the same as 0001 | 0010 | 0100 | 1000 = 1111 in binary if (lineTileType == LineTileType.Nub) { if (rotationType == RotationType.Rotation0) bitmask = kBitmaskTop; if (rotationType == RotationType.Rotation90) bitmask = kBitmaskRight; if (rotationType == RotationType.Rotation180) bitmask = kBitmaskBottom; if (rotationType == RotationType.Rotation270) bitmask = kBitmaskLeft; } if (lineTileType == LineTileType.Line) { if (rotationType == RotationType.Rotation0 || rotationType == RotationType.Rotation180) bitmask = kBitmaskTop | kBitmaskBottom; if (rotationType == RotationType.Rotation90 || rotationType == RotationType.Rotation270) bitmask = kBitmaskRight | kBitmaskLeft; } if (lineTileType == LineTileType.Corner) { if (rotationType == RotationType.Rotation0) bitmask = kBitmaskTop | kBitmaskRight; if (rotationType == RotationType.Rotation90) bitmask = kBitmaskRight | kBitmaskBottom; if (rotationType == RotationType.Rotation180) bitmask = kBitmaskBottom | kBitmaskLeft; if (rotationType == RotationType.Rotation270) bitmask = kBitmaskLeft | kBitmaskTop; } if (lineTileType == LineTileType.Threeway) { if (rotationType == RotationType.Rotation0) bitmask = kBitmaskTop | kBitmaskRight | kBitmaskBottom; if (rotationType == RotationType.Rotation90) bitmask = kBitmaskRight | kBitmaskBottom | kBitmaskLeft; if (rotationType == RotationType.Rotation180) bitmask = kBitmaskBottom | kBitmaskLeft | kBitmaskTop; if (rotationType == RotationType.Rotation270) bitmask = kBitmaskLeft | kBitmaskTop | kBitmaskRight; } if (lineTileType == LineTileType.Cross) { bitmask = kBitmaskTop | kBitmaskRight | kBitmaskBottom | kBitmaskLeft; } }