/* * GameHandler.java * Karl-Adam Karlsson * 06-10-17 * * Hanterar wormspelet. * */ import java.awt.Point; import java.util.LinkedList; import javax.swing.*; public class Gamehandler implements Runnable{ Direction myDirection; Direction enemyDirection; Point myPos, enemyPos; Paper p; LinkedList blackPoints; Thread theThread; Draw myDraw; public boolean run=true; Communicator acomm; /* * Konstruktor, sätter upp directions, en upp, en ner. * Inte mycket mer. */ public Gamehandler(int aDirection, Paper aPaper, Thread aThread, Draw aDraw, Communicator comm){ myDirection = new Direction(aDirection); p = aPaper; theThread = aThread; myDraw = aDraw; blackPoints = new LinkedList(); acomm = comm; if(myDirection.getDir()==1){ myDirection = new Direction(1); enemyDirection = new Direction(2); myPos = new Point(180,10); enemyPos = new Point(180,350); } else{ myDirection = new Direction(2); enemyDirection = new Direction(1); enemyPos = new Point(180,10); myPos = new Point(180,350); } } public void setMyDir(Direction aDir){ myDirection = aDir; } public void setEnemyDir(Direction aDir){ enemyDirection = aDir; } /** * RUN sköter maskrörelse ioch letar efter kollision * */ public void run(){ while(run){ //MIN RÖRELSE if(myDirection.getDir() == Direction.UP){ myPos.setLocation( myPos.getX(), myPos.getY()+1); }else if(myDirection.getDir() == Direction.DOWN){ myPos.setLocation( myPos.getX(), myPos.getY()-1); }else if(myDirection.getDir() ==Direction.LEFT){ myPos.setLocation( myPos.getX()-1, myPos.getY()); }else if(myDirection.getDir() ==Direction.RIGHT){ myPos.setLocation( myPos.getX()+1, myPos.getY()); } //FIENDENS RÖRELSE if(enemyDirection.getDir() == Direction.UP){ enemyPos.setLocation( enemyPos.getX(), enemyPos.getY()+1); }else if(enemyDirection.getDir() == Direction.DOWN){ enemyPos.setLocation( enemyPos.getX(), enemyPos.getY()-1); }else if(enemyDirection.getDir() ==Direction.LEFT){ enemyPos.setLocation( enemyPos.getX()-1, enemyPos.getY()); }else if(enemyDirection.getDir() ==Direction.RIGHT){ enemyPos.setLocation( enemyPos.getX()+1, enemyPos.getY()); } //KROCKAR VI MED NGT??? if( blackPoints.contains(myPos) ||myPos.getX()<1||myPos.getX()>399||myPos.getY()<1||myPos.getY()>399 ){ acomm.setRun(false); run=false; myDraw.setPlay(false); myDraw.dispose(); JOptionPane.showMessageDialog(myDraw, "YOU LOOSE!"); //KROCKAR FIENDEN? }else if(blackPoints.contains(enemyPos)||enemyPos.getX()<1||enemyPos.getX()>399||enemyPos.getY()<1||enemyPos.getY()>399 ){ acomm.setRun(false); run=false; myDraw.setPlay(false); myDraw.dispose(); JOptionPane.showMessageDialog(myDraw, "YOU WIN!"); } else{//NEHEJ; DÅ FLYTTAR VI p.drawPoint(myPos); p.drawEnemyPoint(enemyPos); blackPoints.addLast(new Point( (int)myPos.getX(), (int)myPos.getY())); blackPoints.addLast(new Point( (int)enemyPos.getX(), (int)enemyPos.getY())); } try{ Thread.sleep(100); }catch(InterruptedException ie){ ie.printStackTrace(); } //p.repaint(); } } }