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.MyMouseListener;
- 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.Memento.Caretaker;
- import eg.edu.alexu.csd.oop.game.sample.Memento.Originator;
- import eg.edu.alexu.csd.oop.game.sample.ShapeFactory.ShapeFactory;
- import eg.edu.alexu.csd.oop.game.sample.State.FallingState;
- import eg.edu.alexu.csd.oop.game.sample.State.ShelfState;
- import java.awt.*;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- import java.sql.SQLClientInfoException;
- import java.util.LinkedList;
- import java.util.List;
- public class Game implements World {
- private static int MAX_TIME = 1 * 60 * 1000; // 1 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>();
- ShapeFactory sF ;
- public Game(int screenWidth, int screenHeight) {
- //moving.add(new ImageObject((int)(Math.random() * screenWidth), -1 * (int)(Math.random() * screenHeight), "/star.png"));
- sF = new ShapeFactory();
- sF.setWorld(this);
- width = screenWidth;
- height = screenHeight;
- control.add(Clown.getInstance());
- constant.add(new Shelf(10,70,500,true, Color.RED));
- constant.add(new Shelf(750,70,500,true, Color.RED));
- constant.add(new Shelf(10,70+100,300,true, Color.BLUE));
- constant.add(new Shelf(950,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(10, 70));
- sF.setGate(0);
- moving.add(sF.getShape());
- // GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(1).getX(), -250));
- // sF.setGate(1);
- // moving.add(sF.getShape());
- // GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(0).getX()-ShelfDirector.getInstance().showShelfeDetails(0).getWidth(), -250+100));
- // sF.setGate(2);
- // moving.add(sF.getShape());
- // GateDirector.getInstance().addGate(new Gate(ShelfDirector.getInstance().showShelfeDetails(1).getX(), -250+100));
- // sF.setGate(3);
- // moving.add(sF.getShape());
- }
- private boolean intersect(GameObject o1, GameObject o2){
- return (Math.abs((o1.getX()+o1.getWidth()/2) - (o2.getX()+o2.getWidth()/2)) <= o1.getWidth()) && (Math.abs((o1.getY()+o1.getHeight()/2) - (o2.getY()+o2.getHeight()/2)) <= o1.getHeight());
- }
- boolean flag = true;
- @Override
- public boolean refresh() {
- 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;
- }
- // Originator originator = new Originator();
- // Caretaker careTaker = new Caretaker();
- // originator.setState(this);
- // careTaker.addMemento(originator.save());
- for(int i=0;i<moving.size();i++) {
- // if (!moving.get(i).isVisible())
- // moving.remove(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)));
- // System.out.println(moving.get(i).getX());
- // if (moving.get(i).getX() + 489 < 1100 && !((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() * -1 < 360 && !((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);
- // }
- }
- }
- // for(GameObject m : moving){ // change ball position
- // BubleObject ball = (BubleObject)m;
- // ball.setX(ball.getX() + (ball.isLeft() ? 1 : -1));
- // ball.setY(ball.getY() + (ball.isTop() ? 1 : -1));
- // boolean hitBar = false;
- // if(!timeout)
- // for(GameObject bar : control)
- // if(intersect(bar, ball)){ // hit the ball
- // score++; // acquire score
- // hitBar = true;
- // if(!ball.isTop())
- // ball.setY(Math.min(getHeight() - 2 * ball.getHeight(), ball.getY() + ball.getHeight()/2));
- // else
- // ball.setY(Math.max(0, ball.getY() - ball.getHeight()/2));
- // }
- // if(hitBar || ball.getY() < 0 || ball.getY()+ball.getHeight()+20 > getHeight())
- // ball.setTop(!ball.isTop()); // reverse vertical direction
- // if(ball.getX() < 0 || ball.getX()+ball.getWidth() > getWidth())
- // ball.setLeft(!ball.isLeft()); // reverse horizontal direction
- // }
- // for(GameObject bar : control)
- // for(GameObject c : constant)
- // if(intersect(bar, c)) // hit a cross
- // score = Math.max(0, score-1); // lose score
- return !timeout;
- }
- @Override
- public int getSpeed() {
- return 10;
- }
- @Override
- public int getControlSpeed() {
- return 10;
- }
- @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
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement