PlayArea.java 5.3 KB

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