Advertisement
Guest User

Untitled

a guest
Jan 27th, 2015
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. package zicabots;
  2.  
  3. import robocode.HitByBulletEvent;
  4. import robocode.HitRobotEvent;
  5. import robocode.HitWallEvent;
  6. import robocode.ScannedRobotEvent;
  7. import robocode.TeamRobot;
  8. import robocode.TurnCompleteCondition;
  9. import robocode.MessageEvent;
  10. import static robocode.util.Utils.normalRelativeAngleDegrees;
  11.  
  12. public class droideira extends TeamRobot{
  13.  
  14. boolean movingForward;
  15. boolean noLeader = false; //Inicialmente existe um líder
  16.  
  17. static int MIN_ROBOT_DISTANCE = 100;
  18. static int MAX_ROBOT_DISTANCE = 150;
  19.  
  20. public void run(){
  21.  
  22. while (true){
  23.  
  24. if(!noLeader){ //Se ainda tem líder
  25.  
  26. }else{ //Se não, atue como um doido
  27. setAhead(40000);
  28. movingForward = true;
  29.  
  30. setTurnRight(90);
  31.  
  32. waitFor(new TurnCompleteCondition(this));
  33. setTurnLeft(180);
  34.  
  35. waitFor(new TurnCompleteCondition(this));
  36. setTurnRight(180);
  37. waitFor(new TurnCompleteCondition(this));
  38. }
  39.  
  40.  
  41.  
  42. }
  43.  
  44. }
  45.  
  46. public void onMessageReceived(MessageEvent e) {
  47.  
  48. // Fire at a point
  49. if (e.getMessage() instanceof Point) {
  50. Point p = (Point) e.getMessage();
  51. // Calculate x and y to target
  52. double dx = p.getX() - this.getX();
  53. double dy = p.getY() - this.getY();
  54. // Calculate angle to target
  55. double theta = Math.toDegrees(Math.atan2(dx, dy));
  56. double distance = Math.sqrt(
  57. (this.getX() - p.getX()) * (this.getX() - p.getX()) +
  58. (this.getY() - p.getY()) * (this.getY() - p.getY())
  59. );
  60. System.out.println(distance);
  61.  
  62.  
  63.  
  64.  
  65. /*
  66. if(distance>MAX_ROBOT_DISTANCE){
  67.  
  68. movingAside = false;
  69. aproximar(theta - getHeading());
  70.  
  71. }else if(distance<=MAX_ROBOT_DISTANCE && distance>MIN_ROBOT_DISTANCE){
  72.  
  73. movingAside = true;
  74. posicionar(theta - getHeading());
  75.  
  76. }else{
  77.  
  78. movingAside = false;
  79. recuar(theta - getHeading(), distance);
  80.  
  81. }*/
  82.  
  83. // Turn gun to target
  84. setTurnGunRight(normalRelativeAngleDegrees(theta - getGunHeading()));
  85.  
  86. // Fire hard!
  87. fire(3);
  88. }
  89.  
  90.  
  91. }
  92.  
  93.  
  94. public void onHitRobot (HitRobotEvent e){
  95. if (!noLeader){
  96.  
  97. }else{
  98.  
  99. }
  100.  
  101.  
  102. if(e.isMyFault()){
  103. reverseDirection();
  104. }
  105.  
  106. }
  107.  
  108.  
  109. public void onHitByBullet(HitByBulletEvent e){
  110. if (!noLeader){
  111. //Só pra debugar
  112. System.out.println("Obedecer líder");
  113. }else{
  114.  
  115. }
  116.  
  117. }
  118.  
  119. public void onHitWall (HitWallEvent e){
  120.  
  121. if (!noLeader){
  122. //Só pra debugar
  123. System.out.println("Obedecer líder");
  124. }else{
  125. reverseDirection();
  126. }
  127. }
  128.  
  129. public void reverseDirection(){
  130. if (movingForward){
  131. setBack (40000);
  132. movingForward = false;
  133. }else{
  134. setAhead(40000);
  135. movingForward = true;
  136. }
  137. }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement