PlayArea.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package model.objects;
  2. import image.Images;
  3. import java.awt.AlphaComposite;
  4. import java.awt.BasicStroke;
  5. import java.awt.Color;
  6. import java.awt.Graphics2D;
  7. import java.awt.Polygon;
  8. import java.awt.RenderingHints;
  9. import java.awt.Stroke;
  10. import java.awt.Transparency;
  11. import java.awt.geom.GeneralPath;
  12. import java.awt.geom.Line2D;
  13. import java.awt.geom.Rectangle2D;
  14. import java.awt.image.VolatileImage;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import model.drawObjects.Enemy;
  18. import model.gameState.PlayState;
  19. public class PlayArea {
  20. public List<Path> paths;
  21. private Polygon innerHitAreaBorder,outsideHitAreaBorder;
  22. private GeneralPath hitArea;
  23. private Color hitAreaColor,pathColor = null;
  24. private VolatileImage background;
  25. private boolean hit = false;
  26. private int count = 0,maxCount = 100,pathID = -1;
  27. private Stroke stroke = new BasicStroke(5);
  28. private Rectangle2D backgroundPlay = new Rectangle2D.Double(256, 0, 1024, 1024);
  29. public PlayArea(double xToRight,double heightOfGameScreen,double widthOfGameScreen, int sizeOctagon) {
  30. super();
  31. paths = new ArrayList<Path>();
  32. setHitAreaColor(Color.WHITE);
  33. //make the polygons
  34. int amountOfAngles = 8;
  35. double middlePointX = widthOfGameScreen/2+xToRight;
  36. double middlePointY = heightOfGameScreen/2;
  37. //the inner octagon
  38. innerHitAreaBorder = new Polygon();
  39. for(int i = 0; i < amountOfAngles; i++){
  40. innerHitAreaBorder.addPoint((int)(middlePointX+sizeOctagon*Math.cos(i*Math.PI/(amountOfAngles/2))),
  41. (int)(middlePointY+sizeOctagon*Math.sin(i*Math.PI/(amountOfAngles/2))));
  42. }
  43. //the outside octagon
  44. outsideHitAreaBorder = new Polygon();
  45. sizeOctagon += PlayState.sizeOfEnemy;
  46. for(int i = 0; i < amountOfAngles; i++){
  47. outsideHitAreaBorder.addPoint((int)(middlePointX+sizeOctagon*Math.cos(i*Math.PI/(amountOfAngles/2))),
  48. (int)(middlePointY+sizeOctagon*Math.sin(i*Math.PI/(amountOfAngles/2))));
  49. }
  50. //hitArea the area where you must click the enemy
  51. hitArea = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
  52. hitArea.moveTo(innerHitAreaBorder.xpoints[0], innerHitAreaBorder.ypoints[0]);
  53. for(int i = 1; i < innerHitAreaBorder.npoints;i++){
  54. hitArea.lineTo(innerHitAreaBorder.xpoints[i], innerHitAreaBorder.ypoints[i]);
  55. }
  56. hitArea.moveTo(outsideHitAreaBorder.xpoints[0], outsideHitAreaBorder.ypoints[0]);
  57. for(int i = 1; i < innerHitAreaBorder.npoints;i++){
  58. hitArea.lineTo(outsideHitAreaBorder.xpoints[i], outsideHitAreaBorder.ypoints[i]);
  59. }
  60. //make the paths
  61. double angleX,angleY;
  62. angleX = (Math.cos(Math.toRadians(45)))*Enemy.distanceToOctagon;
  63. angleY = (Math.sin(Math.toRadians(45)))*Enemy.distanceToOctagon;
  64. paths.add(new Path(outsideHitAreaBorder.xpoints[6],outsideHitAreaBorder.ypoints[6]-Enemy.distanceToOctagon ,outsideHitAreaBorder.xpoints[6],outsideHitAreaBorder.ypoints[6]));//top
  65. paths.add(new Path(outsideHitAreaBorder.xpoints[7] + angleX,outsideHitAreaBorder.ypoints[7] - angleY ,outsideHitAreaBorder.xpoints[7],outsideHitAreaBorder.ypoints[7]));//right -top
  66. paths.add(new Path(outsideHitAreaBorder.xpoints[0]+Enemy.distanceToOctagon,outsideHitAreaBorder.ypoints[0] ,outsideHitAreaBorder.xpoints[0],outsideHitAreaBorder.ypoints[0]));//right
  67. paths.add(new Path(outsideHitAreaBorder.xpoints[1]+angleX,outsideHitAreaBorder.ypoints[1]+angleY ,outsideHitAreaBorder.xpoints[1],outsideHitAreaBorder.ypoints[1]));//right -down
  68. paths.add(new Path(outsideHitAreaBorder.xpoints[2],outsideHitAreaBorder.ypoints[2]+Enemy.distanceToOctagon ,outsideHitAreaBorder.xpoints[2],outsideHitAreaBorder.ypoints[2]));//down
  69. paths.add(new Path(outsideHitAreaBorder.xpoints[3] - angleX,outsideHitAreaBorder.ypoints[3] + angleY ,outsideHitAreaBorder.xpoints[3],outsideHitAreaBorder.ypoints[3]));//left -down
  70. paths.add(new Path(outsideHitAreaBorder.xpoints[4] - Enemy.distanceToOctagon,outsideHitAreaBorder.ypoints[4] ,outsideHitAreaBorder.xpoints[4],outsideHitAreaBorder.ypoints[4]));//left
  71. paths.add(new Path(outsideHitAreaBorder.xpoints[5] - angleX,outsideHitAreaBorder.ypoints[5] - angleY ,outsideHitAreaBorder.xpoints[5],outsideHitAreaBorder.ypoints[5]));//left -top
  72. //drawing into buffer
  73. background = Images.initVolatileImage(1280,1024, Transparency.BITMASK);
  74. Graphics2D g2 = background.createGraphics();
  75. RenderingHints rh = new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
  76. g2.setRenderingHints(rh);
  77. // g2.setColor(Color.WHITE);
  78. // g2.fillRect(256, 0, 1024, 1024);
  79. Line2D current;
  80. g2.setColor(Color.BLACK);
  81. g2.setStroke(stroke);
  82. for(int i = 0; i < paths.size(); i++){
  83. current = paths.get(i);
  84. g2.draw(current);
  85. }
  86. g2.draw(innerHitAreaBorder);
  87. g2.draw(outsideHitAreaBorder);
  88. g2.dispose();
  89. background.createGraphics();
  90. }
  91. public void draw(Graphics2D g2){
  92. g2.drawImage(background, 0, 0, 1280, 1024, null);
  93. if(hit){
  94. g2.setColor(hitAreaColor);
  95. g2.fill(hitArea);
  96. if(count == maxCount){
  97. hit = false;
  98. }
  99. }
  100. if(pathID >= 0 && pathColor != null){
  101. g2.setColor(new Color(pathColor.getRed(), pathColor.getGreen(), pathColor.getBlue(),50));
  102. g2.fill(backgroundPlay);
  103. g2.setStroke(stroke);
  104. g2.setColor(pathColor);
  105. g2.draw(paths.get(pathID));
  106. }
  107. }
  108. public Line2D getLine(int index){
  109. if(index < 0){
  110. index = 0;
  111. }
  112. if(index >= 8 ){
  113. index = 7;
  114. }
  115. return paths.get(index);
  116. }
  117. public void setHitAreaColor(Color color) {
  118. hitAreaColor = color;
  119. count = 0;
  120. }
  121. public void hit(){
  122. hit = true;
  123. }
  124. public void count(){
  125. if(hit){
  126. count += 3;
  127. if(count > maxCount){
  128. count = maxCount;
  129. }
  130. hitAreaColor = new Color(hitAreaColor.getRed(),hitAreaColor.getGreen(),hitAreaColor.getBlue(),255-(255*count)/maxCount);
  131. }
  132. }
  133. public void pathPainted(int pathID,Color pathColor){
  134. this.pathID = pathID;
  135. this.pathColor = pathColor;
  136. }
  137. }