Advertisement
Guest User

Untitled

a guest
Apr 30th, 2019
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.16 KB | None | 0 0
  1.  
  2. import java.awt.Color;
  3. import java.awt.Graphics;
  4. import java.util.ArrayList;
  5.  
  6. public class Cell {
  7.  
  8.     public static ArrayList<Cell> cells = new ArrayList<Cell>();
  9.     public static int cellCount;
  10.    
  11.     double startTime = System.currentTimeMillis();
  12.  
  13.     String name;
  14.  
  15.     int mass = 100;
  16.  
  17.     int speed = 4;
  18.        
  19.     boolean isPlayer = false;
  20.  
  21.     Cell target;
  22.     Particle pTarget;
  23.  
  24.     boolean isTarget = false;
  25.     String targetType = "p";
  26.  
  27.     double x, y;
  28.  
  29.     Color cellColor;
  30.  
  31.     double goalX, goalY;
  32.     boolean goalReached = true;
  33.  
  34.     public Cell(String name, int x, int y, boolean isPlayer) {
  35.         this.name = name;
  36.         this.x = x;
  37.         this.y = y;
  38.  
  39.         this.isPlayer = isPlayer;
  40.  
  41.         this.randomColor();
  42.  
  43.         if (System.currentTimeMillis() - startTime <= 1) {
  44.             cellCount++;
  45.         }
  46.     }
  47.  
  48.     public int Speed() {
  49.        
  50.         int moveSpeed;
  51.  
  52.         if(10 - mass * 0.001 < 0.1) {
  53.             moveSpeed = (int) 0.1;
  54.         } else {
  55.             moveSpeed = (int) (1 - mass * 0.001);
  56.         }
  57.                
  58.         return moveSpeed;
  59.     }
  60.    
  61.     public void randomColor() {
  62.  
  63.         int r = (int) (Math.random() * 256);
  64.         int g = (int) (Math.random() * 256);
  65.         int b = (int) (Math.random() * 256);
  66.         Color randomColor = new Color(r, g, b);
  67.  
  68.         cellColor = randomColor;
  69.     }
  70.  
  71.     public void addMass(double mass) {
  72.         this.mass += mass;
  73.     }
  74.  
  75.     public int returnNearestCell() {
  76.  
  77.         int x = 0;
  78.         int distance = 9999999;
  79.         int min = distance;
  80.         int max = 500;
  81.         for (Cell cell : cells) {
  82.             if (this != cell) {
  83.                 distance = (int) Math
  84.                         .sqrt((this.x - cell.x) * (this.x - cell.x) + (cell.y - this.y) * (cell.y - this.y));
  85.                 if (distance < min && this.mass > cell.mass + 10) {
  86.                     min = distance;
  87.                     x = cells.indexOf(cell);
  88.                 } else if (distance < min && this.mass < cell.mass + 10 && cell.cellCount == cells.size()) {
  89.                     x = -1;
  90.                 }
  91.             }
  92.         }
  93.         return x;
  94.     }
  95.  
  96.     public int returnNearestP() {
  97.  
  98.         int x = 0;
  99.         int distance = 99999999;
  100.         int min = distance;
  101.  
  102.         for (Particle p : Particle.particles) {
  103.             distance = (int) Math.sqrt((this.x - p.x) * (this.x - p.x) + (this.y - p.y) * (this.y - p.y));
  104.             if (distance < min && this.mass > p.mass) {
  105.                 min = distance;
  106.                 x = Particle.particles.indexOf(p);
  107.             }
  108.         }
  109.  
  110.         return x;
  111.     }
  112.  
  113.     public void Update() {
  114.         if (this.mass > 3500) {
  115.             this.mass = 3500;
  116.         }
  117.         for (Cell cell : cells) {
  118.             if (this.checkCollide(cell.x, cell.y, cell.mass) && this != cell && this.mass > cell.mass + 10) {
  119.                 if (1 / (this.mass / cell.mass) >= .4 && this.mass < 4000) {
  120.                     addMass(cell.mass);
  121.                 }
  122.                 respawn(cell);
  123.             }
  124.         }
  125.         if (!isPlayer) {
  126.             if (goalReached) {
  127.                 if (returnNearestCell() > -1) { // No Cell Found
  128.                     if (!isTarget) {
  129.                         target = cells.get(returnNearestCell());
  130.                         isTarget = true;
  131.                         targetType = "c";
  132.                     } else if (isTarget && targetType.equals("c")) {
  133.                         targetType = "n";
  134.                         isTarget = false;
  135.                     }
  136.                 } else if (returnNearestCell() == -1) { // Cell Found
  137.                     if (!isTarget) {
  138.                         pTarget = Particle.particles.get(returnNearestP());
  139.                         isTarget = true;
  140.                         targetType = "p";
  141.                     } else if (isTarget) {
  142.                         targetType = "n";
  143.                         isTarget = false;
  144.                     }
  145.                 }
  146.                 goalReached = false;
  147.             } else {
  148.                 double dx = 0;
  149.  
  150.                 double dy = 0;
  151.                 if (targetType.equals("c")) {
  152.                     if (returnNearestCell() > -1) {
  153.                         target = cells.get(returnNearestCell());
  154.                         dx = (target.x - this.x);
  155.                         dy = (target.y - this.y);
  156.                     } else {
  157.                         goalReached = true;
  158.                     }
  159.                 } else if (targetType.equals("p")) {
  160.                     pTarget = Particle.particles.get(returnNearestP());
  161.                     dx = (pTarget.x - this.x);
  162.                     dy = (pTarget.y - this.y);
  163.                 } else {
  164.                     goalReached = true;
  165.                 }
  166.                 double distance = Math.sqrt((dx) * (dx) + (dy) * (dy));
  167.                 if (distance > 1) {
  168.                     x += (dx) / distance * speed;
  169.                     y += (dy) / distance * speed;
  170.                 } else if (distance <= 1) {
  171.                     goalReached = true;
  172.                 }
  173.  
  174.             }
  175.         } else {
  176.             double dx = (goalX - this.x);
  177.             double dy = (goalY - this.y);
  178.             this.x += (dx) * 1 / 50;
  179.             this.y += (dy) * 1 / 50;
  180.             // addMass(10);
  181.         }
  182.     }
  183.  
  184.     public void getMouseX(int mx) {
  185.         goalX = mx;
  186.     }
  187.  
  188.     public void getMouseY(int my) {
  189.         goalY = my;
  190.     }
  191.  
  192.     public void respawn(Cell cell) {
  193.         cell.x = (int) Math.floor(Math.random() * 10001);
  194.         cell.y = (int) Math.floor(Math.random() * 10001);
  195.         cell.mass = 20;
  196.     }
  197.  
  198.     public boolean checkCollide(double x, double y, double mass) {
  199.         return x < this.x + this.mass && x + mass > this.x && y < this.y + this.mass && y + mass > this.y;
  200.     }
  201.  
  202.     public void shootMass() {
  203.         double startX = this.x + this.mass / 2;
  204.         double startY = this.y + this.mass / 2;
  205.         Particle a = new Particle((int) startX, (int) startY, 100, true);
  206.         Particle.particles.add(a);
  207.         a.speed = 5;
  208.         a.angle = Math.atan2(goalX - startX, goalY - startY);
  209.         a.dx = (a.speed) * Math.cos(a.angle);
  210.         a.dy = (a.speed) * Math.sin(a.angle);
  211.         a.isShot = true;
  212.     }
  213.  
  214.     public void Draw(Graphics bbg) {
  215.         bbg.setColor(cellColor);
  216.         bbg.drawOval((int) x, (int) y, (int) mass, (int) mass);
  217.         bbg.fillOval((int) x, (int) y, (int) mass, (int) mass);
  218.  
  219.         bbg.setColor(Color.WHITE);
  220.         bbg.drawString(name, ((int) x + (int) mass / 2 - name.length() * 3),
  221.                 ((int) y + (int) mass / 2 + name.length()));
  222.     }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement