PlayArea.java 5.8 KB

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