Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.game.sample;
- 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.Clown;
- 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.Composite.ShelfDirector;
- import eg.edu.alexu.csd.oop.game.sample.GameObjects.ImageObject;
- import eg.edu.alexu.csd.oop.game.sample.GameObjects.Plates;
- import eg.edu.alexu.csd.oop.game.sample.GameObjects.Composite.Shelf;
- import eg.edu.alexu.csd.oop.game.sample.GameObjects.UndoButton;
- import eg.edu.alexu.csd.oop.game.sample.Logger.LoggerObject;
- import eg.edu.alexu.csd.oop.game.sample.Memento.Caretaker;
- import eg.edu.alexu.csd.oop.game.sample.Memento.Originator;
- import eg.edu.alexu.csd.oop.game.sample.Observer.Change;
- import eg.edu.alexu.csd.oop.game.sample.Observer.Subject;
- import eg.edu.alexu.csd.oop.game.sample.ShapeFactory.ShapeFactory;
- import eg.edu.alexu.csd.oop.game.sample.State.*;
- import org.apache.log4j.Logger;
- import java.awt.*;
- import java.lang.reflect.Array;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Timer;
- import java.util.Stack;
- import java.util.concurrent.TimeUnit;
- public class Game implements World {
- Logger logger = LoggerObject.getLogger();
- private static int MAX_TIME = 1 * 60 * 1000; // 11 minute
- private int score = 0;
- private long startTime = System.currentTimeMillis();
- private final int width;
- private final int height;
- private final List<GameObject> constant = new LinkedList<GameObject>();
- private final List<GameObject> moving = new LinkedList<GameObject>();
- private final List<GameObject> control = new LinkedList<GameObject>();
- private ImageObject LeftHand;
- private ImageObject RightHand;
- private final Stack<Plates> RightHandStack= new Stack<>();
- private final Stack<Plates> LeftHandStack= new Stack<>();
- ShapeFactory sF ;
- Caretaker careTaker = new Caretaker();
- boolean flag = true;
- private int Speed =0;
- private Subject subject = new Subject();
- private Object CaughtState = new CaughtState();
- private boolean intersect(ImageObject o1, ImageObject o2,char Ch){
- if(o2 instanceof Clown) {
- if ((Math.abs(o1.getX()-o2.getX())<o1.getWidth()/2) && (o1.getY() - o2.getY()+o1.getHeight() == 0 && Ch=='L')) {
- logger.info("Left hand Clown Intersection/Object Type : "+((Plates)o1).getPath());
- //System.exit(0);
- return true;
- }
- else if ((Math.abs(o1.getX()-o2.getX()-(o2.getWidth()/2))<o1.getWidth()) && (o1.getY() - o2.getY()+o1.getHeight() == 0&&Ch=='R')){
- logger.info("Right hand Clown Intersection/Object Type : "+((Plates)o1).getPath());
- return true;
- }
- return false;
- }
- else {
- if ((Math.abs(o1.getX()-o2.getX())<o1.getWidth()/2) && (o1.getY() - o2.getY() +o1.getHeight()==0)) {
- logger.info("Object intersection of type "+((Plates)o1).getPath());
- return true;
- }
- return false;
- }
- }
- private GameObjectsSave Clone() throws CloneNotSupportedException {
- ArrayList<GameObject> Mgame = new ArrayList<>();
- for (int i =0;i<moving.size();i++){
- // if (moving.get(i) instanceof Plates)
- Mgame.add(((Plates)moving.get(i)).clone());
- }
- ArrayList<GameObject> Congame = new ArrayList<>();
- for (int i =0;i<constant.size();i++){
- if (constant.get(i) instanceof Plates)
- Congame.add(((Plates)constant.get(i)).clone());
- else Congame.add(constant.get(i));
- }
- ArrayList<GameObject> Cgame = new ArrayList<>();
- Cgame.add(Clown.getInstance().clone());
- for (int i =0;i<control.size();i++){
- if (control.get(i) instanceof Plates)
- Congame.add(((Plates)control.get(i)).clone());
- else if (control.get(i) instanceof Clown)
- Congame.add (Clown.getInstance().clone());
- }
- ArrayList<GameObject> LHand = new ArrayList<>(LeftHandStack);
- ArrayList<GameObject> Lgame = new ArrayList<>();
- // for (int i =0;i<LHand.size()&&LeftHandStack.size()!=0;i++){
- // if (LeftHandStack.get(i) instanceof Plates)
- // Lgame.add(((Plates)LHand.get(i)).clone());
- // }
- ArrayList<GameObject> RHand = new ArrayList<>(RightHandStack);
- ArrayList<GameObject> Rgame = new ArrayList<>();
- // for (int i =0;i<RHand.size()&&RightHandStack.size()!=0;i++){
- // if (RightHandStack.get(i) instanceof Plates)
- // Rgame.add(((Plates)RHand.get(i)).clone());
- // }
- Stack R = new Stack<>();R.addAll(Rgame);
- Stack L = new Stack<>();L.addAll(Lgame);
- GameObjectsSave gS = new GameObjectsSave(Congame,Mgame,Clown.getInstance().clone(),LeftHand,RightHand,score,Cgame,R,L);
- return gS;
- }
- public Game(GameObjectsSave GO){
- // this.sF = ShapeFactory.getFactory();
- // width = ShapeFactory.getFactory().getWorld().getWidth();
- // height = ShapeFactory.getFactory().getWorld().getHeight();
- // System.out.println(GO.getControl());
- // LeftHand = GO.getLeftHand();
- // RightHand = GO.getRightHand();
- // this.control.add(GO.getCl());
- //// this.moving.addAll(GO.getControl());
- // this.constant.addAll(ShapeFactory.getFactory().getWorld().getConstantObjects());
- // this.moving.addAll(ShapeFactory.getFactory().getWorld().getMovableObjects());
- //
- ////// try {
- // if(GO.getRightHandStack().size()!=0) {
- // for (int i=0;i<GO.getRightHandStack().size();i++){
- // moving.remove(GO.getRightHandStack().get(i));
- // GO.getRightHandStack().get(i).setX(RightHand.getX());
- // }
- //// this.RightHandStack.addAll(GO.getRightHandStack());
- //// this.control.addAll(GO.getRightHandStack());
- // System.out.println("Right hand stack "+this.RightHandStack.size());
- // System.out.println("Control Size "+this.control.size());
- // }
- // if (GO.getLeftHandStack().size()!=0) {
- // for (int i=0;i<GO.getLeftHandStack().size();i++){
- // moving.remove(GO.getLeftHandStack().get(i));
- // GO.getLeftHandStack().get(i).setX(LeftHand.getX());
- // }
- // System.out.println("Left Hand stack "+(GO.getLeftHandStack()));
- //// this.LeftHandStack.addAll(GO.getLeftHandStack());
- // System.out.println("Left hand stack " + this.LeftHandStack.size());
- //// this.control.addAll(LeftHandStack);
- // System.out.println("Control Size "+this.control.size());
- // }
- // sF.setWorld(this);
- this.sF = ShapeFactory.getFactory();
- width = ShapeFactory.getFactory().getWorld().getWidth();
- height = ShapeFactory.getFactory().getWorld().getHeight();
- LeftHand = GO.getLeftHand();
- RightHand = GO.getRightHand();
- this.control.addAll(GO.getControl());
- this.score = GO.getScore();
- this.moving.addAll(GO.getMoving());
- this.constant.addAll(ShapeFactory.getFactory().getWorld().getConstantObjects());
- try {
- TimeUnit.MILLISECONDS.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- this.score = GO.getScore();
- DGameController.getInstance().pause();
- // System.out.println("AHO SA&AA");
- // DGameController.getInstance().resume();
- //// try {
- // if(GO.getRightHandStack().size()!=0) {
- // for (int i=0;i<GO.getRightHandStack().size();i++){
- // moving.remove(GO.getRightHandStack().get(i));
- // GO.getRightHandStack().get(i).setX(RightHand.getX());
- // }
- //// this.RightHandStack.addAll(GO.getRightHandStack());
- //// this.control.addAll(GO.getRightHandStack());
- // System.out.println("Right hand stack "+this.RightHandStack.size());
- // System.out.println("Control Size "+this.control.size());
- // }
- // if (GO.getLeftHandStack().size()!=0) {
- // for (int i=0;i<GO.getLeftHandStack().size();i++){
- // moving.remove(GO.getLeftHandStack().get(i));
- // GO.getLeftHandStack().get(i).setX(LeftHand.getX());
- // }
- // System.out.println("Left Hand stack "+(GO.getLeftHandStack()));
- //// this.LeftHandStack.addAll(GO.getLeftHandStack());
- // System.out.println("Left hand stack " + this.LeftHandStack.size());
- //// this.control.addAll(LeftHandStack);
- // System.out.println("Control Size "+this.control.size());
- // }
- }
- public Game(int screenWidth, int screenHeight) {
- //moving.add(new ImageObject((int)(Math.random() * screenWidth), -1 * (int)(Math.random() * screenHeight), "/star.png"));
- sF = ShapeFactory.getFactory();
- sF.setWorld(this);
- width = screenWidth;
- height = screenHeight;
- control.add(Clown.getInstance());
- LeftHand=(ImageObject)control.get(0);
- RightHand=(ImageObject)control.get(0);
- constant.add(new Shelf(10,70,450,true, Color.RED));
- constant.add(new Shelf(800,70,500,true, Color.RED));
- constant.add(new Shelf(10,70+100,250,true, Color.BLUE));
- constant.add(new Shelf(1000,70+100,300,true, Color.BLUE));
- for(int i = 0;i<constant.size();i++)
- ShelfDirector.getInstance().addShelf((Shelf)constant.get(i));
- GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(0).getX(), 0));
- sF.setGate(0);
- moving.add(sF.getShape());
- GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(1).getX()+ShelfDirector.getInstance().showShelfeDetails(1).getWidth(), 0));
- sF.setGate(1);
- moving.add(sF.getShape());
- GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(2).getX(), 0+100));
- sF.setGate(2);
- moving.add(sF.getShape());
- GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(3).getX()+ShelfDirector.getInstance().showShelfeDetails(3).getWidth(), 0+100));
- sF.setGate(3);
- moving.add(sF.getShape());
- logger.info("Starting game of Dimensions \n Height : "+this.getHeight()+" And width : "+this.getWidth()+"\n Current Speed : "+this.getSpeed()+" Max Time : "+MAX_TIME/(1000*60) +" Minute And " + MAX_TIME%(1000*60)+" Seconds." );
- }
- // boolean flag = true;
- private static int i =0;
- @Override
- public boolean refresh() {
- i++;
- if(i%50 == 0){
- Originator originator = new Originator();
- try {
- originator.setState(Clone());
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- }
- careTaker.addMemento(originator.save());
- }
- boolean timeout = System.currentTimeMillis() - startTime > MAX_TIME; // time end and game over
- if(flag) {
- Frame.getFrames()[Frame.getFrames().length-1].add(UndoButton.getInstance());
- constant.add(UndoButton.getInstance());
- flag = false;
- }
- for(int i=0;i<moving.size();i++) {
- if (((Plates) moving.get(i)).getState() instanceof ShelfState || ((Plates) moving.get(i)).getState() instanceof FallingState) {
- ((Plates) moving.get(i)).getState().doAction(((Plates) moving.get(i)));
- if (moving.get(i).getX() < getWidth()-200 && !((Plates) moving.get(i)).isUsed() && ((((Plates) moving.get(i)).getGate() == 1 ) || (((Plates) moving.get(i)).getGate() == 3 ))) {
- sF.setGate(((Plates) moving.get(i)).getGate());
- moving.add(sF.getShape());
- ((Plates) moving.get(i)).setUsed(true);
- }
- else if (moving.get(i).getX() > 200 && !((Plates) moving.get(i)).isUsed() && (((Plates) moving.get(i)).getGate() == 0 || (((Plates) moving.get(i)).getGate() == 2))) {
- sF.setGate(((Plates) moving.get(i)).getGate());
- moving.add(sF.getShape());
- ((Plates) moving.get(i)).setUsed(true);
- }
- }
- // else if (((Plates)moving.get(i)).getState() instanceof FloorState)((Plates)moving.get(i)).getState().doAction((Plates) moving.get(i));
- if (((Plates) moving.get(i)).getState() instanceof FallingState) {
- Plates plateTest = (Plates) moving.get(i);
- if (intersect((ImageObject) moving.get(i), LeftHand,'L')) {
- if(! (LeftHand instanceof Clown))
- logger.info("Left hand intersection");
- control.add(moving.get(i));
- LeftHand = (ImageObject) moving.get(i);
- LeftHandStack.push((Plates)LeftHand);
- System.out.println("Moving size "+moving.size());
- ((Plates) moving.get(i)).setState((State) CaughtState);
- moving.remove(i);
- System.out.println("Moving size "+moving.size());
- plateTest.setState(new CaughtState());
- getScoreFrom(LeftHandStack);
- if(!LeftHandStack.empty())
- LeftHand=(Plates)LeftHandStack.peek();
- else
- LeftHand=Clown.getInstance();
- logger.info("Current Peek of Left Hand : "+LeftHand.getPath());
- } else if (intersect((ImageObject) moving.get(i), RightHand,'R')) {
- if(! (RightHand instanceof Clown))
- logger.info("Right hand intersection");
- control.add(moving.get(i));
- RightHandStack.push((Plates) moving.get(i));
- RightHand = (ImageObject) moving.get(i);
- moving.remove(i);
- plateTest.setState(new CaughtState());
- getScoreFrom(RightHandStack);
- if(!RightHandStack.empty())
- RightHand=(Plates)RightHandStack.peek();
- else
- RightHand=Clown.getInstance();
- logger.info("Current Peek of Right Hand : "+RightHand.getPath());
- }
- }
- }
- return !timeout;
- }
- @Override
- public int getSpeed() {
- return Speed;
- }
- @Override
- public int getControlSpeed() {
- return 20;
- }
- @Override
- public List<GameObject> getConstantObjects() {
- return constant;
- }
- @Override
- public List<GameObject> getMovableObjects() {
- return moving;
- }
- @Override
- public List<GameObject> getControlableObjects() {
- return control;
- }
- @Override
- public int getWidth() {
- return width;
- }
- @Override
- public int getHeight() {
- return height;
- }
- @Override
- public String getStatus() {
- // return "";
- return "Please Use Arrows To Move | Location = " + control.get(0).getX() + "," + control.get(0).getY() + " | Score=" + score + " | Time=" + Math.max(0, (MAX_TIME - (System.currentTimeMillis()-startTime))/1000); // update status
- }
- public void setSpeed(int speed) {
- Speed = speed;
- }
- public int getScore() {
- return score;
- }
- public void setScore( int NewScore) {
- this.score = NewScore ;
- }
- public void getScoreFrom ( Stack <Plates> HandStack ){
- subject.setWorld(this);
- new Change(subject , subject.getWorld() , HandStack );
- subject.setState(this.getScore());
- setScore(subject.getState()) ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement