public virtual List<ChocolatePiece> CreateBar(ChocolateBar bar, GameObject prefab) { List<ChocolatePiece> pieces = new List<ChocolatePiece>(); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { //Instantiate the prefab for chocolate piece. Then get the script component to pass to conststructor //for the class that controls the piece. then add to the list. GameObject hooksGO = GameObject.Instantiate(prefab, Vector3.zero, Quaternion.identity) as GameObject; ChocolatePieceHooks hooks = hooksGO.GetComponent<ChocolatePieceHooks>(); ChocolatePiece chocolatePiece = new ChocolatePiece(hooks); chocolatePiece.Initialize(); float width = chocolatePiece.SpriteRenderer.sprite.bounds.size.x; float height = chocolatePiece.SpriteRenderer.sprite.bounds.size.y; float offsetX = gridOffsets[i, j].x * width; float offsetY = gridOffsets[i, j].y * height; Vector3 offset = new Vector3(offsetX, offsetY); chocolatePiece.Position = bar.Position + offset; pieces.Add(chocolatePiece); } } return pieces; }
//create the generator according to no of pieces. public ChocolateBarGenerator Create(ChocolateBar bar, int noOfPieces) { switch (noOfPieces) { case 4: return new ChocolateBarGenerator4Piece(); case 6: return new ChocolateBarGenerator6Piece(); case 8: return new ChocolateBarGenerator8Piece(); case 9: return new ChocolateBarGenerator9Piece(); case 12: return new ChocolateBarGenerator12Piece(); } Assert.IsTrue(false); return null; }