Advertisement
alarmringing

Ecosystem

Jul 30th, 2013
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.55 KB | None | 0 0
  1. ArrayList<Entity> entities = new ArrayList<Entity>();
  2. ArrayList<Food> foods = new ArrayList<Food>();
  3.  
  4. color RED = color(255,0,0);
  5. color GREEN = color(0,255,0);
  6. color BLUE = color(0,0,255);
  7. int count = 0;
  8. int timer = 0;
  9. final int ROT_TIME = 40;
  10.  
  11. void setup(){
  12.  
  13.    size(400,400);
  14.    background(255);  
  15. }
  16.  
  17.  
  18.    
  19. void mousePressed(){
  20.  
  21.  
  22.   if(mouseButton == LEFT)
  23.   {
  24.     entities.add(new Entity());
  25.     int i = entities.size()-1;
  26.     Entity e = entities.get(i);
  27.  
  28.     System.out.println(entities.size() + "entities");
  29.    
  30.   }
  31.   else if(mouseButton == RIGHT)
  32.   {
  33.  
  34.    switch(count%3) {
  35.      
  36.     case 0: foods.add(new Food(color(255,0,0)));
  37.             break;
  38.     case 1: foods.add(new Food(color(0,255,0)));
  39.             break;
  40.     case 2: foods.add(new Food(color(0,0,255)));
  41.             break;
  42.    }
  43.  
  44.   int j = foods.size()-1;
  45.   Food f = foods.get(j);
  46.  
  47.  
  48.   System.out.println(foods.size() + "foods!");
  49.  
  50.   count++;
  51.   }
  52.  
  53. }
  54.  
  55. void draw() {
  56.  
  57.  
  58.   if(timer%ROT_TIME==0){
  59.  
  60.     for(int i=0;i<entities.size();i++){
  61.    
  62.      
  63.       entities.get(i).rot("red");
  64.       entities.get(i).rot("green");
  65.       entities.get(i).rot("blue");
  66.    
  67.    
  68.     }
  69.  
  70.   }
  71.  
  72.   background(255);
  73.   for(int i=0;i<entities.size();i++){
  74.    
  75.     if(entities.get(i).red == 0 || entities.get(i).green == 0 || entities.get(i).blue == 0) entities.remove(i);
  76.     else {
  77.       entities.get(i).decide();
  78.       entities.get(i).checkCollision();
  79.       entities.get(i).render();
  80.     }
  81.   }
  82.   for(int j=0;j<foods.size();j++){
  83.    
  84.    
  85.    
  86.     foods.get(j).render();
  87.    
  88.     if(foods.get(j).radius == 0) foods.remove(j);
  89.    
  90.     }
  91.  
  92.   timer++;
  93.  
  94.  
  95.  
  96. }
  97.  
  98. class Entity
  99. {
  100.   public int red = 255;
  101.   public int green = 255;
  102.   public int blue =255;
  103.   int maxindex1=0;
  104.   int maxindex2=0;
  105.   int radius = 10;
  106.   public int MORALITY = 0;
  107.   int VEL = 1;
  108.   float x = 0;
  109.   public PVector position;
  110.   boolean hostility = false;
  111.   Entity entityT;
  112.   Food foodT;
  113.  
  114.   public Entity(){
  115.     position = new PVector(mouseX,mouseY);
  116.    
  117.  
  118.   }
  119.  
  120.   void render(){
  121.     ellipse(position.x, position.y, 10, 10);
  122.     stroke(0);
  123.     fill(red,green,blue);
  124.   }
  125.  
  126.   void rot(String col) {
  127.    
  128.     if (col=="red" && this.red>=5) this.red -= 5;
  129.     else if (col=="green" && this.green>=5) this.green -= 5;
  130.     else if (col=="blue" && this.blue >=5) this.blue -= 5;
  131.  
  132.   }
  133.  
  134.  
  135.   void decide() {
  136.  
  137.   //  if(foods.size() > 0 && entities.size() > 0) {
  138.       double[] eatPrior = new double[foods.size()];
  139.       double[] killPrior = new double[entities.size()];
  140.         double max = 0;
  141.        maxindex1 = 0;
  142.        maxindex2 = 0;
  143.       int redPrior = (255-this.red)*2;
  144.       int greenPrior = (255-this.green)*2;
  145.       int bluePrior = (255-this.blue)*2;
  146.    
  147.       for(int i=0;i<foods.size();i++){
  148.      
  149.         if(foods.get(i).foodColor == color(255,0,0)) eatPrior[i] = redPrior - getDistance(foods.get(i).position)/VEL*5/ROT_TIME;
  150.         else if (foods.get(i).foodColor == color(0,255,0)) eatPrior[i] = greenPrior - getDistance(foods.get(i).position)/VEL*5/ROT_TIME;
  151.         else if (foods.get(i).foodColor == color(0,0,255)) eatPrior[i] = bluePrior - getDistance(foods.get(i).position)/VEL*5/ROT_TIME;
  152.      
  153.         if(max < eatPrior[i]){
  154.           max = eatPrior[i];
  155.           maxindex1 = i;
  156.         }
  157.       }
  158.       max = 0;
  159.       for(int j=0;j<entities.size();j++){
  160.    
  161.         killPrior[j]=0 - MORALITY;
  162.         maxindex2 = j;
  163.    
  164.       }
  165.    
  166.      if(eatPrior[maxindex1] >= killPrior[maxindex2]){
  167.         foodT = foods.get(maxindex1);
  168.         entityT = null;
  169.         goEat(foodT);
  170.       }
  171.       else {
  172.         entityT = entities.get(maxindex2);
  173.         foodT = null;
  174.         goKill(entityT);
  175.       }
  176.      
  177.      
  178.      
  179.    
  180.     }
  181.  
  182. //  }
  183.   float getDistance(PVector target){
  184.     return sqrt(pow((target.x - this.position.x),2) + pow(target.y-this.position.y,2));
  185.   }
  186.  
  187.   void goEat(Food food) {
  188.    if(food.position.x > this.position.x) this.position.x += VEL;
  189.    else if (food.position.x< this.position.x) this.position.x -= VEL;
  190.    if(food.position.y > this.position.y) this.position.y += VEL;
  191.    else if (food.position.y< this.position.y) this.position.y -= VEL;
  192.   }
  193.  
  194.   void goKill(Entity prey) {
  195.     if(prey == this) return;
  196.     this.hostility = true;
  197.     if(prey.position.x > this.position.x) this.position.x += VEL;
  198.     else if (prey.position.x< this.position.x) this.position.x -= VEL;
  199.     if(prey.position.y > this.position.y) this.position.y += VEL;
  200.     else if (prey.position.y< this.position.y) this.position.y -= VEL;
  201.   }
  202.  
  203.   void checkCollision() {
  204.  
  205.    
  206.     if(entityT == null && foodT == null) this.decide();
  207.     float xDist;
  208.     float yDist;
  209.     float distance;
  210.    
  211.    if(entities.size() < 1 || foods.size() < 1) return;
  212.    
  213.    if(foodT != null){
  214.     xDist = this.position.x-foodT.position.x;                                   // distance horiz
  215.     yDist = this.position.y-foodT.position.y;                                   // distance vert
  216.     distance = sqrt((xDist*xDist) + (yDist*yDist));  // diagonal distance
  217.  
  218.     // test for collision
  219.     if (this.radius/2 + foodT.radius/2 > distance) {
  220.       foodT.eaten();
  221.      
  222.       if(foodT.foodColor==color(255,0,0)) this.red+=100;
  223.       else if(foodT.foodColor == color(0,255,0)) this.green+=100;
  224.       else if(foodT.foodColor == color(0,0,255)) this.blue += 100;
  225.      
  226.     }
  227.     if(entityT!=null){
  228.       xDist = this.position.x-entityT.position.x;                                   // distance horiz
  229.       yDist = this.position.y-entityT.position.y;                                   // distance vert
  230.       distance = sqrt((xDist*xDist) + (yDist*yDist));  // diagonal distance
  231.  
  232.     // test for collision
  233.       if (this.radius/2 + entityT.radius/2 > distance)
  234.       {
  235.         if(entityT.hostility)
  236.         {
  237.           this.red /= 2;
  238.           this.green /= 2;
  239.           this.blue /= 2;  
  240.         }
  241.         else
  242.         {
  243.           this.red += entityT.red;
  244.           this.green += entityT.green;
  245.           this.blue += entityT.blue;
  246.           entities.remove(maxindex2);
  247.         }
  248.       }
  249.    
  250.    }
  251.    
  252.  }
  253.  
  254.  
  255.   }
  256. }
  257.  
  258. public class Food
  259. {
  260.    public PVector position;
  261.    public color foodColor;
  262.   public int radius = 40;
  263.  
  264.   public Food(color C)
  265.   {
  266.     position = new PVector(mouseX, mouseY);  
  267.     this.foodColor = C;
  268.   }
  269.  
  270.  
  271.   public void render()
  272.   {
  273.     ellipse(position.x, position.y, radius, radius);
  274.     noStroke();
  275.     fill(foodColor);
  276.   }
  277.  
  278.   public void eaten()
  279.   {
  280.     this.radius -= 5;
  281.   }
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement