Lan Wei – Project 10 – Generative Landscape

my-sketch.js

//Lan Wei
//Section D
//lanw@andrew.cmu.edu
//Project 10 - Generative Landscape

//Cave people

var groundDetail = [0.004, 0.0001, 0.005, 0.002];
var groundSpeed = 0.0005;
var minY = [-10, 70, 110, 300]; //min values of terrain domains
var maxY = [150, 120, 300, 400]; //max values of terrain domains
var people = [];

function setup() {
    createCanvas(450, 450);
    frameRate(10);

    // initial people
    for (var i = 0; i < 7; i++){
        var rx = random(width);
        var ry = random(290, 313);
        people[i] = makePeople(rx, ry);
    }
}

var moonX = 450;

function draw() {
    //mountains & ground
    background(8, 46, 84);
    var colMountain = color(0);
    var colGround = color(176, 23, 31);
    var colHole = color(255, 222, 173);
    var col = [colMountain, colGround, colHole, colGround];
    noStroke();

    //moon
    moonX -= 1;
    fill(190);
    ellipse(moonX, 30, 70, 70);
    fill(255);
    ellipse(moonX, 30, 60, 60);

    for (var i = 0; i < 4; i ++){
        var yRange = [];
        fill(col[i]);
        beginShape();
        for (var x = 0; x < width; x++) {
            var t = (x * groundDetail[i]) + (millis() * groundSpeed);
            var y = map(noise(t), 0, 1, minY[i], maxY[i]);
            if (i === 0){ // reverse the direction of the mountains
                y = ((maxY[i] - minY[i]) - 1.5 * y);
            }
            vertex(x, y);
            yRange.push(y);
        }
        vertex(width, yRange[width - 1]);
        vertex(width, height);
        vertex(0, height);
        vertex(0, yRange[0]);
        endShape();

        if (i === 2){  ////the legs should be coverd by the bottom ground
            updateAndDisplayPeople();
            removePeopleThatHaveSlippedOutOfView();
            addNewPeopleWithSomeRandomProbability();
        }
    }
}

function updateAndDisplayPeople(){
    for (var i = 0; i < people.length; i++){
        people[i].move();
        people[i].display();
    }
}

function removePeopleThatHaveSlippedOutOfView(){
    var peopleToKeep = [];
    for (var i = 0; i < people.length; i++){
        if (people[i].x + people[i].bellyWidth/2 > 0) {
            peopleToKeep.push(people[i]);
        }
    }
    people = peopleToKeep; // remember the surviving buildings
}

function addNewPeopleWithSomeRandomProbability() {
    var newPeopleLikelihood = 0.2;
    if (random(0,1) < newPeopleLikelihood) {
        people.push(makePeople(width, random(290, 303)));
    }
}

function makePeople(bellyX, bellyY){
    var ppl = {x: bellyX,/////////////////////////belly
               y: bellyY,
               speed: -10,
               bellyWidth: random(23, 47),
               bellyHeight: random(50, 59),
               bellyCol: random(70, 255),
               headD: 10,/////////////////////////head
               headCol: random(20, 90),
               legY: bellyY,//////////////////////legs
               move: pplMove,
               display: pplDisplay}
    return ppl;
}

function pplMove(){
    this.x += this.speed;
}

function pplDisplay(){
    //head
    noStroke();
    fill(this.headCol);
    ellipse(this.x, this.y - this.bellyHeight/2 - 10, this.headD, this.headD);
    //legs
    noFill();
    stroke(0);
    strokeWeight(2);
    line(this.x - this.bellyWidth/4, this.legY, this.x - this.bellyWidth/4, height);
    line(this.x + this.bellyWidth/4, this.legY, this.x + this.bellyWidth/4, height);
    //belly
    noStroke();
    fill(this.bellyCol);
    ellipse(this.x, this.y, this.bellyWidth, this.bellyHeight);
}

This is my first time to practice using objects and it was tough but very fun.  I started with doing sketches and decided to make cave people at the end. Making mountains ground and the cave took me a while since I need to create several different noises with different qualities and I would like to do it with  loops. For the people part, I studied the building example and did modifications based on that, which is also challenging. To be honest, at the beginning I planned to achieve something more complicated but the process is more challenging than I’ve imagined so I simplified it a bit. But anyway, I feel that the  project helps me a lot to understand objects and the result makes me very happy.

Sketch

Leave a Reply