Guest User

Untitled

a guest
Dec 15th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.45 KB | None | 0 0
  1. @Override
  2.     public void run() {
  3.         int deg, dist;
  4.     int deg2, dist2;
  5.         int xtmp, ytmp;
  6.     int i;
  7.     int v1_x, v1_y;
  8.     double dist_test;
  9.  
  10.         TortueAmelioree PorteBalle = new TortueAmelioree();
  11.         TortueAmelioree APorteBalle = new TortueAmelioree();
  12.         TortueAmelioree tort_proche = new TortueAmelioree();
  13.     TortueAmelioree tort_proche2 = new TortueAmelioree();
  14.         double dist_min = 100000;
  15.     double dist_min2 = 100000;
  16.         double dist_tmp;
  17.     double dist_tmp2;
  18.     ArrayList<TortueAmelioree> equipeballe = new ArrayList<TortueAmelioree>();
  19.     ArrayList<TortueAmelioree> equipenonballe = new ArrayList<TortueAmelioree>();
  20.     ArrayList<TortueAmelioree> equipetmp = new ArrayList<TortueAmelioree>();
  21.     equipeballe = this.equipe1;
  22.     equipenonballe = this.equipe2;
  23.         while(isRuning) {
  24.             for (Iterator it = equipeballe.iterator(); it.hasNext();){
  25.                 TortueAmelioree tortame = (TortueAmelioree) it.next();
  26.                 xtmp = tortame.getPositionX();
  27.                 ytmp = tortame.getPositionY();
  28.                 do{
  29.                     tortame.setPositionX(xtmp);
  30.                     tortame.setPositionY(ytmp);
  31.                     deg = (int) (Math.random() * 90);
  32.                     dist = (int) (Math.random() * 5);
  33.                     if (Math.random() > 0.95) {
  34.                         if (Math.random() > 0.5) {
  35.                             tortame.gauche(deg);
  36.                         } else {
  37.                             tortame.droite(deg);
  38.                         }
  39.                     }
  40.                     tortame.avancer(dist);
  41.                     if (this.balle.GetJoueur() == tortame){
  42.                         PorteBalle = tortame;
  43.                     }
  44.                 }while(tortame.getPositionX() < 5 || tortame.getPositionY() < 5 || tortame.getPositionX() > feuille.getWidth()-5 || tortame.getPositionY() > feuille.getHeight()-5);
  45.             }
  46.         i = 0;
  47.         for (Iterator it = equipenonballe.iterator(); it.hasNext();){
  48.                 TortueAmelioree tortame = (TortueAmelioree) it.next();
  49.                 xtmp = tortame.getPositionX();
  50.                 ytmp = tortame.getPositionY();
  51.                 do{
  52.                     tortame.setPositionX(xtmp);
  53.                     tortame.setPositionY(ytmp);
  54.             if ((equipeballe.get(i).getPositionY() - tortame.getPositionY()) == 0)
  55.             {tortame.setPositionY(ytmp + 1);}
  56.  
  57.             v1_x = equipeballe.get(i).getPositionX() - tortame.getPositionX();
  58.             v1_y = equipeballe.get(i).getPositionY() - tortame.getPositionY();
  59.             //deg2 = (int) Math.acos(scal / (v1_norm * v1_norm));
  60.  
  61.             dist_test = Math.sqrt((v1_x*v1_x)+(v1_y*v1_y));
  62.  
  63.             deg2 = (int) (Math.acos(Math.cos(Math.abs(v1_x)/dist_test))* 180/Math.PI);
  64.  
  65.             if(v1_x < 0) {
  66.             //Coté gauche du point
  67.             if(v1_y < 0) {
  68.                 //En haut a gauche
  69.                 tortame.setDir(180+deg2);
  70.             }
  71.             else {
  72.                 //En bas a gauche
  73.                 tortame.setDir(90+deg2);
  74.             }
  75.             }
  76.             else {
  77.             //Coté droite du point
  78.             if(v1_y < 0) {
  79.                 //En haut a droite
  80.                 tortame.setDir(270+deg2);
  81.             }
  82.             else {
  83.                 //En bas a droite
  84.                 tortame.setDir(deg2);
  85.             }
  86.             }
  87.  
  88.  
  89.             /*
  90.             if (equipeballe.get(i).getPositionX() == tortame.getPositionX())
  91.             {
  92.                 deg2 = 0;
  93.             }
  94.             else {
  95.             deg2 = (int) Math.toDegrees(
  96.                             Math.atan2(
  97.                                    (equipeballe.get(i).getPositionY() - tortame.getPositionY()),
  98.                                    (equipeballe.get(i).getPositionX() - tortame.getPositionX())
  99.                                    )
  100.                             );
  101.             }
  102.             */
  103.                    /* deg2 = (int) Math.tan((equipeballe.get(i).getPositionY() - tortame.getPositionY()) / (equipeballe.get(i).getPositionX() - tortame.getPositionX()));
  104.  
  105.             System.out.printf(i+" : "+deg2);
  106.  
  107.             dist2 = (int) (Math.random() * 5);
  108.                     if (Math.random() > 0.05) {
  109.                         if (Math.random() > 0.5) {
  110.                             tortame.setDir(180 + deg2 - (int)(Math.random() * 5));
  111.                         } else {
  112.                             tortame.setDir(180 + deg2 + (int)(Math.random() * 5));
  113.                         }
  114.                     }
  115.            */
  116.  
  117.                     // deg2 = (int) (Math.random() * 90);
  118.                     dist2 = (int) (Math.random() * 5);
  119.                     if (Math.random() > 0.95) {
  120.             tortame.gauche(deg2);
  121.             }
  122.  
  123.                     tortame.avancer(dist2);
  124.                     if (this.balle.GetJoueur() == tortame){
  125.                         PorteBalle = tortame;
  126.                     }
  127.                 }while(tortame.getPositionX() < 5 || tortame.getPositionY() < 5 || tortame.getPositionX() > feuille.getWidth()-5 || tortame.getPositionY() > feuille.getHeight()-5);
  128.         i++;
  129.         }
  130.  
  131.             for (Iterator it = PorteBalle.get_liste().iterator(); it.hasNext();){
  132.                 TortueAmelioree tortame = (TortueAmelioree) it.next();
  133.                 dist_tmp = PorteBalle.calcEuclDistance(tortame);
  134.                 if (dist_tmp < dist_min) {
  135.                     dist_min = dist_tmp;
  136.                     tort_proche = tortame;
  137.                 }
  138.             }
  139.  
  140.         for (Iterator it = equipenonballe.iterator(); it.hasNext();){
  141.                 TortueAmelioree tortame = (TortueAmelioree) it.next();
  142.                 dist_tmp2 = PorteBalle.calcEuclDistance(tortame);
  143.                 if (dist_tmp2 < dist_min2) {
  144.                     dist_min2 = dist_tmp2;
  145.                     tort_proche2 = tortame;
  146.                 }
  147.             }
  148.  
  149.         if (dist_min2 < 80){
  150.                 APorteBalle = PorteBalle;
  151.                 this.balle.SetJoueur(tort_proche2);
  152.                 this.balle.setColor(tort_proche2.getColor());
  153.                 PorteBalle = tort_proche2;
  154.         equipetmp = equipenonballe;
  155.         equipenonballe = equipeballe;
  156.         equipeballe = equipetmp;
  157.  
  158.             }
  159.  
  160.             else{
  161.         if (dist_min < 100 && APorteBalle != tort_proche){
  162.             APorteBalle = PorteBalle;
  163.             this.balle.SetJoueur(tort_proche);
  164.             this.balle.setColor(tort_proche.getColor());
  165.             PorteBalle = tort_proche;
  166.         }
  167.         }
  168.  
  169.             this.balle.setPositionX(PorteBalle.getPositionX() + 20);
  170.             this.balle.setPositionY(PorteBalle.getPositionY() + 5);
  171.             dist_min = 100000;
  172.             this.feuille.repaint();
  173.             try {
  174.                 Thread.sleep(1000/30);
  175.             } catch (InterruptedException ex) {
  176.                 Logger.getLogger(JeuEquipe.class.getName()).log(Level.SEVERE, null, ex);
  177.             }
  178.         }
  179.     }
  180. }
Add Comment
Please, Sign In to add comment