Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package eg.edu.alexu.csd.oop.game.sample.world;
- import java.awt.Color;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Stack;
- import eg.edu.alexu.csd.oop.game.GameObject;
- import eg.edu.alexu.csd.oop.game.World;
- import eg.edu.alexu.csd.oop.game.sample.State.State_2;
- import eg.edu.alexu.csd.oop.game.sample.levels.Strategy;
- import eg.edu.alexu.csd.oop.game.sample.object.Clown;
- import eg.edu.alexu.csd.oop.game.sample.object.ConnectionPool;
- import eg.edu.alexu.csd.oop.game.sample.object.Plate;
- import eg.edu.alexu.csd.oop.game.sample.object.Shape;
- import eg.edu.alexu.csd.oop.game.sample.object.StickLeft;
- import eg.edu.alexu.csd.oop.game.sample.object.StickRight;
- public class Game implements World{
- private static int MAX_TIME;
- private int score=0;
- private long startTime = System.currentTimeMillis();
- private final int width;
- private final int height;
- private List<GameObject> constant = new LinkedList<GameObject>();
- private List<GameObject> moving = new LinkedList<GameObject>();
- private List<GameObject> control = new LinkedList<GameObject>();
- private Stack<Plate> RStack = new Stack<Plate>();
- private Stack<Plate> LStack = new Stack<Plate>();
- private List<Plate> R = new LinkedList<Plate>();
- private List<Plate> L = new LinkedList<Plate>();
- private Strategy level;
- private int limitheight;
- ConnectionPool son = new ConnectionPool();
- public Game(int screenWidth, int screenHeight,Strategy level) {
- this.level = level;
- Game.MAX_TIME = this.level.getMaxTime();
- this.limitheight = this.level.maxHeightOfPlate();
- score = 0;
- width = screenWidth;
- height = screenHeight;
- control = State_2.getControl();
- moving = State_2.getMoving();
- constant = State_2.getConstant();
- /*
- * control.add(new StickRight(440, 570, "/right.png", true));
- * control.add(new StickLeft(330, 535, "/left.png", true));
- * control.add(new Clown(270, 530,"/Clown.png", true));
- */
- for (int i = 3; i < 7; i++) {
- moving.add((GameObject) son.checkOut());
- moving.add((GameObject) son.checkOut());
- }
- System.out.println();
- }
- public void moveToLimit() {
- if (this.control.get(0).getX() == 855) {
- this.control.get(2).setX(700);
- this.control.get(0).setX(855);
- this.control.get(1).setX(730);
- // objectsLimit();
- } else if (this.control.get(0).getX() > 855) {
- this.control.get(2).setX(700);
- this.control.get(0).setX(855);
- this.control.get(1).setX(730);
- // objectsLimit();
- }
- if (this.control.get(1).getX() == 35) {
- this.control.get(2).setX(5);
- this.control.get(0).setX(160);
- this.control.get(1).setX(35);
- // objectsLimit();
- }
- else if (this.control.get(1).getX() < 35) {
- this.control.get(2).setX(5);
- this.control.get(0).setX(160);
- this.control.get(1).setX(35);
- // objectsLimit();
- }
- }
- private boolean intersect(GameObject o1, GameObject o2) {
- //return (o1.getY()+o1.getHeight()==o2.getY()&&o1.getX()==o2.getX());
- 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());
- //return (( o1.getX()>=o2.getX() )&& (o1.getX()<=o2.getX()+o2.getWidth())
- // && (o2.getY()==o1.getY() ) );
- }
- @Override
- public boolean refresh() {
- moveToLimit();
- boolean timeout = score == constant.size() || System.currentTimeMillis() - startTime > MAX_TIME;
- // randomly hide constant objects
- //for (GameObject n : constant)
- //if (n.isVisible() && Math.random() < 0.0002)
- // ((Plate) n).setVisible(false);
- // change position of moving objects
- boolean direction = false;
- for (GameObject m : moving) {
- m.setY((m.getY() + 1));
- if (m.getY() == getHeight()) {
- // reuse the star in another position
- m.setY(-1 * (int) (Math.random() * getHeight()));
- m.setX((int) (Math.random() * getWidth()));
- }
- m.setX(m.getX());
- }
- // for(GameObject c : control)
- // c.setY(c.getHeight());
- // check intersection with constant
- // check intersection with moving objects
- if (false &&!RStack.isEmpty()) {
- Stack<Plate>Rtemp = RStack;
- Plate[] R=new Plate[RStack.size()];
- int i=0;
- while(Rtemp.peek()!=null)
- R[i++]=Rtemp.pop();
- R[R.length-1].setX(control.get(0).getX());
- for ( i=R.length-2;i<=0;i--)
- R[i].setX(R[i+1].getX());
- } if (false &&!LStack.isEmpty()) {
- Stack<Plate>Ltemp = LStack;
- Plate[] L=new Plate[LStack.size()];
- int i=0;
- System.out.println(Ltemp.size());
- while(Ltemp.peek()!=null)
- L[i++]=Ltemp.pop();
- L[L.length-1].setX(control.get(0).getX());
- for ( i=L.length-2;i<=0;i--)
- L[i].setX(L[i+1].getX());
- }
- if (!L.isEmpty()) {
- L.get(0).setX(control.get(1).getX());
- for (int i=1;i<L.size();i++)
- L.get(i).setX(L.get(i-1).getX());
- }
- if (!R.isEmpty()) {
- R.get(0).setX(control.get(0).getX());
- for (int i=1;i<R.size();i++)
- R.get(i).setX(R.get(i-1).getX());
- }
- for(Plate m :moving.toArray(new Plate[moving.size()])){
- boolean flag=false;
- // System.out.println("done");
- if (RStack.isEmpty()) {
- if (intersect(control.get(0), m) ) {
- m.setX(control.get(0).getX());
- m.setY(m.getY());
- RStack.push(m); R.add(m);
- if (m.getShape()==Shape.Crown)
- m.setY(m.getY()+10);
- else m.setY(m.getY());
- control.add(m);
- moving.remove(m); flag=true;
- }
- }
- if (!RStack.isEmpty()&&intersect(R.get(R.size()-1), m) ) {
- m.setX(RStack.peek().getX());
- m.setY(m.getY());
- if (( R.get(R.size()-1)).getShape()==Shape.Lantern&&(m.getShape()==Shape.Crown))
- m.setY(m.getY()+15);
- if (( R.get(R.size()-1)).getShape()==Shape.Crown&&(m.getShape()==Shape.Lantern))
- m.setY(m.getY()-15);
- RStack.push(m); R.add(m);
- control.add(m);
- moving.remove(m); flag=true;
- }
- if (LStack.isEmpty()) {
- if (intersect(control.get(1), m) ) {
- m.setX(control.get(1).getX()-15);
- m.setY(m.getY());
- LStack.push(m); L.add(m);
- if (m.getShape()==Shape.Crown)
- m.setY(m.getY()+10);
- else m.setY(m.getY());
- control.add(m);
- moving.remove(m); flag=true;
- }
- }
- if (!LStack.isEmpty()&&intersect(L.get(L.size()-1), m) ) {
- m.setX(LStack.peek().getX()-15);
- m.setY(m.getY());
- if (( L.get(L.size()-1)).getShape()==Shape.Lantern&&(m.getShape()==Shape.Crown))
- m.setY(m.getY()+15);
- if (( L.get(L.size()-1)).getShape()==Shape.Crown&&(m.getShape()==Shape.Lantern))
- m.setY(m.getY()-15);
- LStack.push(m); L.add(m);
- control.add(m);
- moving.remove(m); flag=true;
- }
- //if (flag==true) m.setX(m.getX()-15);
- /*
- if (intersect(LStack.peek(), m) ) {
- m.setX(LStack.peek().getX());
- if (( RStack.peek()).getShape()==Shape.Crown&&(m.getShape()==Shape.Lantern))
- m.setY(m.getY()-10);
- LStack.push(m);
- m.setY(m.getY());
- }
- if (hR>400&&intersect(RStack.peek(), m)) {
- if (((Plate)m).getShape()==Shape.Crown)
- m.setY(m.getY()+30);
- if (((Plate)m).getShape()==Shape.Lantern)
- m.setY(m.getY()+15);
- hR-=100;
- }
- if (hL>400&&intersect(LStack.peek(), m)) {
- if (((Plate)m).getShape()==Shape.Crown)
- m.setY(m.getY()+30);
- if (((Plate)m).getShape()==Shape.Lantern)
- m.setY(m.getY()+15);
- hL-=100;
- }
- */
- // m.setX(m.getX() + (Math.random() > 0.5 ? 1 : -1));
- }
- //return true; // game over (lose)
- // check if any constant object still visible
- boolean foundVisible = false;
- for (GameObject n : constant)
- foundVisible |= n.isVisible();
- /*
- * if(!foundVisible) return false; // game ends (win)
- */
- return true;
- }
- @Override
- public int getSpeed() {
- return level.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 "Please Use Arrows To Move | " + "Score=" + score + " | Time="
- + Math.max(0, (MAX_TIME - (System.currentTimeMillis() - startTime)) / 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement