bullet.java
上传用户:jhzhutan
上传日期:2021-03-28
资源大小:374k
文件大小:4k
源码类别:

射击游戏

开发平台:

Java

  1. import java.awt.*;
  2. //Download:http://www.codefans.net
  3. public class bullet implements Actor{
  4. public final Rectangle map = new Rectangle(18, 18, 486, 486);
  5. private Rectangle border;
  6. private int direction;
  7. private int Speed;
  8. private int bulletpower;
  9. public int xPos, yPos;
  10. public Actor owner;
  11. public ServerModel gameModel;
  12. public boolean hitTarget;
  13. public bullet(int a, int b, int c, int d, int e, Actor owner, ServerModel gameModel){
  14. this.owner = owner;
  15. this.gameModel = gameModel;
  16. xPos = a; yPos = b;
  17. direction = c;
  18. if(direction == 0 || direction == 1)
  19. border = new Rectangle(a - 2, b - 5, 5, 13);
  20. else
  21. border = new Rectangle(a - 5, b - 2, 13, 5);
  22. Speed = d;
  23. bulletpower = e;
  24. }
  25. public void draw(Graphics g) {
  26. g.setColor(Color.lightGray);
  27. if(direction == 0 || direction == 1)
  28. g.fillRect(border.x + 1, border.y +1, 3, 9);
  29. if(direction == 2 || direction == 3)
  30. g.fillRect(border.x +1, border.y + 1, 9, 3);
  31. }
  32. public void move(){
  33. if(gameModel.gamePaused){
  34. writeToOutputLine();
  35. return;
  36. }
  37. //check if this bullet crash with the map border
  38. if(!border.intersects(map)){
  39. gameModel.removeActor(this);
  40. notifiyOwner();
  41. makeBomb();
  42. writeToOutputLine();
  43. return;
  44. }
  45. //check if this bullet hit other objects
  46. for(int i = 0; i < gameModel.actors.length; i++){
  47. if(gameModel.actors[i] != null){
  48. if(gameModel.actors[i] != this && gameModel.actors[i] != owner){
  49. if(border.intersects(gameModel.actors[i].getBorder())){
  50. if(gameModel.actors[i].getType().equals("steelWall")){
  51. Steelwall temp = (Steelwall)gameModel.actors[i];
  52. if(!temp.walldestoried){
  53. temp.damageWall(border,  bulletpower, direction);
  54. if(temp.bulletdestoried)
  55. hitTarget = true;
  56. }
  57. }else if(gameModel.actors[i].getType().equals("wall")){
  58. wall temp = (wall)gameModel.actors[i];
  59. if(!temp.walldestoried){
  60. temp.damageWall(border,  bulletpower, direction);
  61. if(temp.bulletdestoried)
  62. hitTarget = true;
  63. }
  64. }else if(gameModel.actors[i].getType().equals("bullet")){
  65. bullet temp = (bullet)gameModel.actors[i];
  66. if(temp.owner.getType().equals("Player")){
  67. hitTarget = true;
  68. gameModel.removeActor(gameModel.actors[i]);
  69. temp.notifiyOwner();
  70. }
  71. }else if(gameModel.actors[i].getType().equals("Player")){
  72. if(owner.getType().equals("enemy")){
  73. player temp = (player)gameModel.actors[i];
  74.     temp.hurt();
  75. }else{
  76. }
  77. hitTarget = true;
  78. }else if(gameModel.actors[i].getType().equals("enemy") && owner.getType().equals("Player")){
  79. enemy temp = (enemy)gameModel.actors[i];
  80. player tempe = (player)owner;
  81. if(temp.health == 0)
  82. tempe.scores+=temp.type*100;
  83. temp.hurt();
  84. hitTarget = true;
  85. }else if(gameModel.actors[i].getType().equals("base")){
  86. base temp = (base)gameModel.actors[i];
  87. temp.doom();
  88. hitTarget = true;
  89. gameModel.gameOver = true;
  90. }
  91. }
  92. }
  93. }
  94. }
  95. //if the bullet hit other objects, then remove this bullet objects from the game system
  96. if(hitTarget){
  97. gameModel.removeActor(this);
  98. notifiyOwner();
  99. makeBomb();
  100. writeToOutputLine();
  101. return;
  102. }
  103. if(direction == 0){
  104. border.y -= Speed;
  105. yPos -= Speed;
  106. }
  107. if(direction == 1){
  108. border.y += Speed;
  109. yPos += Speed;
  110. }
  111. if(direction == 2){
  112. border.x -= Speed;
  113. xPos -= Speed;
  114. }
  115. if(direction == 3){
  116. border.x += Speed;
  117. xPos += Speed;
  118. }
  119. writeToOutputLine();
  120. }
  121. public void writeToOutputLine(){
  122. gameModel.outputLine+="t"+ xPos + "," + yPos + "," + direction + ";";
  123. }
  124. public Rectangle getBorder(){
  125. return border;
  126. }
  127. public String getType(){
  128. return "bullet";
  129. }
  130. public void notifiyOwner(){
  131. if(owner != null){
  132. if(owner.getType().equals("Player")){
  133. player temp = (player)owner;
  134. temp.numberOfBullet++;
  135. }else if(owner.getType().equals("enemy")){
  136. enemy temp = (enemy)owner;
  137. temp.numberOfBullet++;
  138. }
  139. }
  140. }
  141. public void makeBomb(){
  142. gameModel.addActor(new bomb(xPos, yPos, "small", gameModel));
  143. }
  144. //unused method
  145. public Rectangle[] getDetailedBorder(){return null;}
  146. public boolean walldestoried(){return false;}
  147. }