Robert Oh- Project 10- Landscape

version2

//Robert Oh
//Section C
//rhoh@andrew.cmu.edu
//Project-10-Landscape

var fish = [];
var terrainSpeed = 0.0005;
var terrainDetail = 0.0005;

function setup() {
    createCanvas(480, 480);
    frameRate(30);
    noStroke();
    fish.push(makeFish(width));
}
 
function draw() {
    background(130, 255, 246);

    //creating the river
    push();
    beginShape(); 
    fill(96, 149, 255);
    vertex(0, height);
    for (var x = 0; x < width; x++) {
        var t = (x * terrainDetail) + (millis() * terrainSpeed);
        var y = map(noise(t), 0, 1, height / 6, height / 4);
        vertex(x, y); 
    }
    vertex(width, height);
    endShape();
    pop();
    
    //updating the fish locations 
    updateFish();

    //adding new fish 
    if (fish.length > 0){
        lastFish = fish[fish.length - 1]
        if (lastFish.x + lastFish.length < width - 50){
            addFish();
        }
    }

    // removing fish out of view
    removeFish();
}

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

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

function removeFish() {
    var keep = [];
    for (var i = 0; i < fish.length; i++){
        if (fish[i].x + fish[i].length * 3/2 > 0) {
            keep.push(fish[i]);
        }
    }
    fish = keep;
}

// drawing each fish
function fishDisplay() {
    //tail
    fill(this.r, 0, 0);
    triangle(this.x + this.length * (4 / 5), this.y, this.x + this.length * (3 / 2), this.y + 20, 
        this.x + this.length * (3 / 2), this.y - 20);

    //body
    fill(this.r, this.g, this.b);
    ellipse(this.x + this.length / 2, this.y, this.length, this.h);

    //eye
    fill(0);
    ellipse(this.x + this.length / 5, this.y - this.h / 8, 7, 7);

}

//adding a new fish on the right side of screen
function addFish() {
        fish.push(makeFish(width));
}

//fish object
function makeFish(startX) {
    var fsh = {x: startX,
                y : random(width / 3, width - 30),
                h : random(50, 70),
                length: random(50, 80),
                speed: random(-4, -2),
                r : random(255),
                g : random(255),
                b : random(255),
                move: fishMove,
                display: fishDisplay}
    return fsh;
}

I had a lot of fun making this project! While looking around for inspiration, I was looking at our old assignments we had completed. Then I remembered how much fun I had making the fish design for our first lab exam, and I decided to make this project off of that! (You’ll notice that I used similar code to create the fish). I wanted to give off a river design, where the fish in my landscape are swimming with the current down the river. Overall, I had a lot of fun designing the landscape, and I enjoyed being able to create my own object type 🙂 .

Leave a Reply