Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- main:
- var world;
- function setup() {
- createCanvas(1280,720);
- world = new World(10,5,width,height);
- }
- function draw() {
- background(0);
- world.step();
- world.drawWorld();
- }
- function generateDNA(){
- temp = [];
- temp.push(random(0.0,50.0));
- temp.push(random(1,10));
- temp.push(random(0,255));
- temp.push(random(0,255));
- temp.push(random(0,255));
- for(var i=0;i<random(10);i++){
- temp.push(random(1,4));
- if(temp[i]==1){
- temp.push(0,9);
- } else if(temp[i]==2){
- temp.push(0,9);
- } else if(temp[i]==3){
- temp.push(0,256);
- temp.push(0,256);
- temp.push(0,256);
- } else if(temp[i]==4){
- temp.push(0,5);
- }
- }
- return temp;
- }
- function doubleArray(cols,rows,d){
- var temp = [];
- for(var i=0;i<cols;i++){
- temp.push([]);
- for(var j=0;j<rows;j++){
- temp[i].push(d);
- }
- }
- }
- World:
- function World(minPop,size,width,height){
- this.minPop = minPop;
- this.pop = 0;
- this.size = size;
- this.world = doubleArray(width/size,height/size);
- this.creatures = [];
- for(var i=0;i<minPop;i++){
- this.creatures.push(new Creature(random(width/size),random(height/size)));
- }
- this.step = function(){
- for(var i=pop;i<minPop;i++){
- this.creatures.push(new Creature(random(width/size),random(height/size)));
- }
- for(var i=0;i<this.creatures.length;i++){
- this.creatures[i].update();
- }
- }
- this.drawWorld = function(){
- for(var x=0;x<world.length;x++){
- for(var y=0;y<world[0].length;y++){
- if(tiles[x][y].type==1){
- fill(0,255,0);
- rect(x*this.size,y*this.size,this.size,this.size);
- }
- }
- }
- for(var i=0;i<this.creatures.length;i++){
- fill(this.creatures.dna[3],this.creatures.dna[4],this.creatures.dna[5]);
- rect(this.creatures.x*this.size,this.creatures.y*this.size,this.size,this.size);
- }
- }
- }
- Tile (irrelevant):
- function Tile(type){
- if(type=0){
- this.tile = 0;
- } else if(type=1){
- this.tile = 1;
- }
- }
- Creature:
- function Creature(x,y,dna){
- this.x=x;
- this.y=y;
- if(dna!=null){
- this.dna = dna;
- } else {
- this.dna = generateDNA();
- }
- this.update() = function(){
- this.i=0;
- while(i<this.dna.length){
- switch(this.dna[i]){
- case 1:
- this.move(this.dna[i+1]);
- i+=2;
- default:
- break;
- }
- }
- }
- this.move = function(cell){
- switch(cell){
- case 1:
- this.y--;
- break;
- case 2:
- this.y--;
- this.x++;
- break;
- case 3:
- this.x++;
- break;
- case 4:
- this.y++;
- this.x++;
- break;
- case 5:
- this.y++;
- break;
- case 6:
- this.y++;
- this.x--;
- break;
- case 7:
- this.x--;
- break;
- case 8:
- this.y--;
- this.x--;
- break;
- case 9:
- this.move(random(0,8));
- break;
- }
- }
- }
- index.html:
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0>
- <style> body {padding: 0; margin: 0;} </style>
- <script src="../p5.min.js"></script>
- <script src="../addons/p5.dom.min.js"></script>
- <script src="../addons/p5.sound.min.js"></script>
- <script src="sketch.js"></script>
- <script src="World.js"></script>
- <script src="Creature.js"></script>
- <script src="Tile.js"></script>
- </head>
- <body>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement