Advertisement
Guest User

moar code

a guest
Jun 20th, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. main:
  2.  
  3. var world;
  4.  
  5. function setup() {
  6.   createCanvas(1280,720);
  7.   world = new World(10,5,width,height);
  8. }
  9.  
  10. function draw() {
  11.   background(0);
  12.   world.step();
  13.   world.drawWorld();
  14. }
  15.  
  16. function generateDNA(){
  17.   temp = [];
  18.   temp.push(random(0.0,50.0));
  19.   temp.push(random(1,10));
  20.   temp.push(random(0,255));
  21.   temp.push(random(0,255));
  22.   temp.push(random(0,255));
  23.   for(var i=0;i<random(10);i++){
  24.     temp.push(random(1,4));
  25.     if(temp[i]==1){
  26.       temp.push(0,9);
  27.     } else if(temp[i]==2){
  28.       temp.push(0,9);
  29.     } else if(temp[i]==3){
  30.       temp.push(0,256);
  31.       temp.push(0,256);
  32.       temp.push(0,256);
  33.     } else if(temp[i]==4){
  34.       temp.push(0,5);
  35.     }
  36.   }
  37.   return temp;
  38. }
  39.  
  40. function doubleArray(cols,rows,d){
  41.   var temp = [];
  42.   for(var i=0;i<cols;i++){
  43.     temp.push([]);
  44.     for(var j=0;j<rows;j++){
  45.       temp[i].push(d);
  46.     }
  47.   }
  48. }
  49.  
  50. World:
  51.  
  52. function World(minPop,size,width,height){
  53.   this.minPop = minPop;
  54.   this.pop = 0;
  55.   this.size = size;
  56.   this.world = doubleArray(width/size,height/size);
  57.   this.creatures = [];
  58.   for(var i=0;i<minPop;i++){
  59.     this.creatures.push(new Creature(random(width/size),random(height/size)));
  60.   }
  61.  
  62.   this.step = function(){
  63.     for(var i=pop;i<minPop;i++){
  64.       this.creatures.push(new Creature(random(width/size),random(height/size)));
  65.     }
  66.     for(var i=0;i<this.creatures.length;i++){
  67.       this.creatures[i].update();
  68.     }
  69.   }
  70.  
  71.   this.drawWorld = function(){
  72.     for(var x=0;x<world.length;x++){
  73.       for(var y=0;y<world[0].length;y++){
  74.         if(tiles[x][y].type==1){
  75.           fill(0,255,0);
  76.           rect(x*this.size,y*this.size,this.size,this.size);
  77.         }
  78.       }
  79.     }
  80.     for(var i=0;i<this.creatures.length;i++){
  81.       fill(this.creatures.dna[3],this.creatures.dna[4],this.creatures.dna[5]);
  82.       rect(this.creatures.x*this.size,this.creatures.y*this.size,this.size,this.size);
  83.     }
  84.   }
  85. }
  86.  
  87. Tile (irrelevant):
  88.  
  89. function Tile(type){
  90.   if(type=0){
  91.     this.tile = 0;
  92.   } else if(type=1){
  93.     this.tile = 1;
  94.   }
  95. }
  96.  
  97. Creature:
  98.  
  99. function Creature(x,y,dna){
  100.   this.x=x;
  101.   this.y=y;
  102.  
  103.   if(dna!=null){
  104.     this.dna = dna;
  105.   } else {
  106.     this.dna = generateDNA();
  107.   }
  108.  
  109.   this.update() = function(){
  110.     this.i=0;
  111.     while(i<this.dna.length){
  112.       switch(this.dna[i]){
  113.         case 1:
  114.           this.move(this.dna[i+1]);
  115.           i+=2;
  116.         default:
  117.           break;
  118.       }
  119.     }
  120.   }
  121.  
  122.   this.move = function(cell){
  123.     switch(cell){
  124.       case 1:
  125.         this.y--;
  126.         break;
  127.       case 2:
  128.         this.y--;
  129.         this.x++;
  130.         break;
  131.       case 3:
  132.         this.x++;
  133.         break;
  134.       case 4:
  135.         this.y++;
  136.         this.x++;
  137.         break;
  138.       case 5:
  139.         this.y++;
  140.         break;
  141.       case 6:
  142.         this.y++;
  143.         this.x--;
  144.         break;
  145.       case 7:
  146.         this.x--;
  147.         break;
  148.       case 8:
  149.         this.y--;
  150.         this.x--;
  151.         break;
  152.       case 9:
  153.         this.move(random(0,8));
  154.         break;
  155.     }
  156.   }
  157. }
  158.  
  159. index.html:
  160.  
  161. <!DOCTYPE html>
  162. <html>
  163.   <head>
  164.     <meta charset="utf-8">
  165.     <meta name="viewport" width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0>
  166.     <style> body {padding: 0; margin: 0;} </style>
  167.     <script src="../p5.min.js"></script>
  168.     <script src="../addons/p5.dom.min.js"></script>
  169.     <script src="../addons/p5.sound.min.js"></script>
  170.     <script src="sketch.js"></script>
  171.     <script src="World.js"></script>
  172.     <script src="Creature.js"></script>
  173.     <script src="Tile.js"></script>
  174.   </head>
  175.   <body>
  176.   </body>
  177. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement