Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.game.sample.ShapeFactory;
- import eg.edu.alexu.csd.oop.game.GameObject;
- import eg.edu.alexu.csd.oop.game.World;
- import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.Gate;
- import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.GateDirector;
- import eg.edu.alexu.csd.oop.game.sample.GameObjects.Plates;
- import eg.edu.alexu.csd.oop.game.sample.Logger.LoggerObject;
- import eg.edu.alexu.csd.oop.game.sample.State.ShelfState;
- import eg.edu.alexu.csd.oop.game.sample.State.State;
- import org.apache.log4j.Logger;
- import java.util.List;
- import java.util.ListIterator;
- public class ShapeFactory {
- private Logger logger = LoggerObject.getLogger();
- private World world;
- List<GameObject> shapes;
- private Object ShelfState = new ShelfState();
- private int x;
- private int y;
- private static int gate;
- static ShapeFactory factory;
- static int TurnTaker=0;
- private ShapeFactory(){
- }
- public static ShapeFactory getFactory(){
- if (factory==null)factory =new ShapeFactory();
- return factory;
- }
- public World getWorld() {
- return world;
- }
- public void setWorld(World world) {
- this.world = world;
- shapes = world.getMovableObjects();
- }
- public void setGate(int gate) {
- this.gate = gate;
- TurnTaker++;
- TurnTaker=TurnTaker%4;
- // logger.info("Turn Taker Value : "+TurnTaker+" And Gate :"+this.gate);
- x = GateDirector.getInstance().showGateDetails(this.gate).getX();
- y = GateDirector.getInstance().showGateDetails(this.gate).getY();
- }
- String[] Colors = {"Purple","Red","Blue","Yellow"};
- String[] Images = {"Ball"};// We change this!
- public Plates getShape(){
- String color = getRandomColor() ;
- String type = color +getRandomShape();
- ListIterator<GameObject> iterator= shapes.listIterator();
- while (iterator.hasNext()){
- Plates plate = (Plates)iterator.next();
- String PlateType = plate.getPath();
- if(type.equalsIgnoreCase(PlateType)&& ! plate.isVisible()){
- logger.info("(Flyweight) Using old shape of type "+PlateType);
- plate.setGate(gate);
- plate.setX(x);
- plate.setY(y);
- plate.setState((State)ShelfState);
- plate.setVisible(true);
- return plate;
- }
- }
- // logger.info("Creating new plate of type :"+type);
- Plates plate = new Plates(x,y,"/"+type+".png");
- logger.info("Set Shape at Gate : "+this.gate);
- plate.setGate(gate);
- plate.setVisible(true);
- plate.setState((State)ShelfState);
- plate.setColor(color);
- shapes.add(plate);
- return plate;
- }
- private String getRandomColor(){
- return Colors[(int) (Math.random()* Colors.length)];
- }
- private String getRandomShape(){
- return Images[(int) (Math.random()*Images.length)];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement