PlayArea.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package model.objects;
  2. import java.awt.Graphics2D;
  3. import java.awt.Polygon;
  4. import java.awt.geom.Line2D;
  5. import java.awt.geom.Point2D;
  6. import java.awt.geom.Rectangle2D;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import model.gameState.PlayState;
  10. public class PlayArea {
  11. public List<Path> paths;
  12. private Polygon octagon,hitArea;
  13. public Rectangle2D[] hitAreas; //de area voor elk path die de enemy moet raken
  14. public PlayArea(double xToRight,double heightOfGameScreen,double widthOfGameScreen, int sizeOctagon) {
  15. super();
  16. paths = new ArrayList<Path>();
  17. int amountOfAngles = 8;
  18. double middlePointX = widthOfGameScreen/2+xToRight;
  19. double middlePointY = heightOfGameScreen/2;
  20. octagon = new Polygon();
  21. for(int i = 0; i < amountOfAngles; i++){
  22. octagon.addPoint((int)(middlePointX+sizeOctagon*Math.cos(i*Math.PI/(amountOfAngles/2))),
  23. (int)(middlePointY+sizeOctagon*Math.sin(i*Math.PI/(amountOfAngles/2))));
  24. }
  25. hitArea = new Polygon();
  26. sizeOctagon += PlayState.sizeOfEnemy;
  27. for(int i = 0; i < amountOfAngles; i++){
  28. hitArea.addPoint((int)(middlePointX+sizeOctagon*Math.cos(i*Math.PI/(amountOfAngles/2))),
  29. (int)(middlePointY+sizeOctagon*Math.sin(i*Math.PI/(amountOfAngles/2))));
  30. }
  31. hitAreas = new Rectangle2D[amountOfAngles];
  32. int newIndex;
  33. Point2D beginPoint,endPoint;
  34. for(int index = 0; index < hitAreas.length; index++){
  35. //in het polygon staat de cooridinaten van de top niet als eerste in de array, maar op index 6.n
  36. newIndex = (index+6+8)%8;
  37. hitAreas[index] = new Rectangle2D.Double();
  38. beginPoint = new Point2D.Double(octagon.xpoints[newIndex], octagon.ypoints[newIndex]);
  39. endPoint = new Point2D.Double(hitArea.xpoints[newIndex], hitArea.ypoints[newIndex]);
  40. hitAreas[index].setFrameFromDiagonal(beginPoint,endPoint);
  41. }
  42. Rectangle2D cr = null;//cr --> current rectangle
  43. double size = (hitAreas[1].getWidth() - 10)/2;//dit is de grootte van een zijde als die van de oorspronklijke 0 was.
  44. cr = hitAreas[0];
  45. hitAreas[0].setFrame(cr.getX()-size/2, cr.getY()+1, size, cr.getHeight());
  46. cr = hitAreas[2];
  47. hitAreas[2].setFrame(cr.getX()-1, cr.getY()-size/2, cr.getWidth(), size);
  48. cr = hitAreas[4];
  49. hitAreas[4].setFrame(cr.getX()-size/2, cr.getY()-1, size, cr.getHeight());
  50. cr = hitAreas[6];
  51. hitAreas[6].setFrame(cr.getX()+1, cr.getY()-size/2, cr.getWidth(), size);
  52. widthOfGameScreen += xToRight;
  53. paths.add(new Path(middlePointX,0 ,hitArea.xpoints[6],hitArea.ypoints[6]));//top
  54. paths.add(new Path(widthOfGameScreen,0 ,hitArea.xpoints[7],hitArea.ypoints[7]));//right -top
  55. paths.add(new Path(widthOfGameScreen,middlePointY ,hitArea.xpoints[0],hitArea.ypoints[0]));//right
  56. paths.add(new Path(widthOfGameScreen,heightOfGameScreen,hitArea.xpoints[1],hitArea.ypoints[1]));//right -down
  57. paths.add(new Path(middlePointX,heightOfGameScreen ,hitArea.xpoints[2],hitArea.ypoints[2]));//down
  58. paths.add(new Path(xToRight,heightOfGameScreen ,hitArea.xpoints[3],hitArea.ypoints[3]));//left -down
  59. paths.add(new Path(xToRight,middlePointY ,hitArea.xpoints[4],hitArea.ypoints[4]));//left
  60. paths.add(new Path(xToRight,0 ,hitArea.xpoints[5],hitArea.ypoints[5]));//left -top
  61. }
  62. public void draw(Graphics2D g2){
  63. Line2D current;
  64. int index;
  65. for(int i = 0; i < paths.size(); i++){
  66. current = paths.get(i);
  67. index = (i+14)%8;
  68. g2.drawLine((int)current.getX1(), (int)current.getY1(), octagon.xpoints[index],octagon.ypoints[index]);
  69. }
  70. g2.draw(octagon);
  71. g2.draw(hitArea);
  72. // for(Rectangle2D hit : hitAreas){
  73. // g2.fill(hit);
  74. // }
  75. }
  76. public Line2D getLine(int index){
  77. if(index < 0){
  78. index = 0;
  79. }
  80. if(index >= 8 ){
  81. index = 7;
  82. }
  83. return paths.get(index);
  84. }
  85. }