BlockFactory.java (1287B)
1 package game; 2 3 import blocks.I_Block; 4 5 import java.util.Random; 6 7 import blocks.*; 8 9 public class BlockFactory { 10 11 private static Random random; 12 private int[] spawnPos; 13 14 /** 15 * Initiates the blockFactory. 16 */ 17 public BlockFactory() { 18 random = new Random(); 19 } 20 21 /** 22 * Generates a Poly of given type and spawns it at desired position. 23 * Note: the spawn needs to be set before calling this function. 24 * 25 * @param type 26 * @return Poly. 27 */ 28 public Poly generatePoly(int type) { 29 switch (type) { 30 case 0: 31 return new Z_Block(spawnPos[0], spawnPos[1]); 32 case 1: 33 return new I_Block(spawnPos[0], spawnPos[1]); 34 case 2: 35 return new J_Block(spawnPos[0], spawnPos[1]); 36 case 3: 37 return new L_Block(spawnPos[0], spawnPos[1]); 38 case 4: 39 return new S_Block(spawnPos[0], spawnPos[1]); 40 case 5: 41 return new O_Block(spawnPos[0], spawnPos[1]); 42 case 6: 43 return new T_Block(spawnPos[0], spawnPos[1]); 44 default: 45 return new O_Block(spawnPos[0], spawnPos[1]); 46 } 47 } 48 49 /** 50 * Sets the spawnpoint for newly created Polys. 51 * @param spawnPos 52 */ 53 public void setSpawn(int[] spawnPos) { 54 this.spawnPos = spawnPos; 55 } 56 57 /** 58 * Return a Poly of random type. 59 * @return Poly. 60 */ 61 public Poly generateRandomPoly() { 62 return generatePoly(random.nextInt(0, 7)); 63 } 64 }