Code viewer for World: jump/duck/hide (clone by ...

// Cloned by CA400_tst1 on 6 May 2019 from World "jump/duck/hide " by Alex Murphy 
// Please leave this clone trail here.
 
let fullwidth  = ABWorld.fullwidth();
let fullheight = ABWorld.fullheight();

// object
var object_x = fullwidth;
let car_x_size = fullwidth/9.5;
let speed = 5;

// character
let character_x_size = fullwidth/19;
let character_y_size = fullheight/4.5;
let character_x_pos = (fullwidth/2)-(fullwidth/19);
let character_y_pos = 3*fullheight/5;

let groundLvl = 4*fullheight/5;
var runner = 'thieves';
var tmp;

function preload() {
    car = loadImage('/uploads/alexmurphy1996/car.png');
    character = loadImage('/uploads/alexmurphy1996/finn.png');
    bird = loadImage('/uploads/alexmurphy1996/bird.png');
    thief = loadImage('/uploads/alexmurphy1996/thief.png');
}

function setup() {
    createCanvas(fullwidth, fullheight);
}


function draw()             
{   
    terrain();
    enemy();
    
// car 
    if(runner == 'cars') {
        jump();
    }
    
    // thief
    if (runner == 'thieves') {
        hide();
    }
    
    // bird
    if(runner == 'birds') {
        duck();
    }
}

function enemy() {
     // thief
    if (runner == 'thieves') {
        image(thief, object_x, 4.5*fullheight/5, fullwidth/12, fullheight/7);
    }
    else{
        if (character_x_pos < ((fullwidth/2)-(fullwidth/19))){
            character_x_pos += speed;
        }
    }
    
    // car 
    if(runner == 'cars') {
        image(car, object_x, 3.5*fullheight/5, fullwidth/9.5, fullheight/9);
    }
    
    // bird
    if(runner == 'birds') {
        image(bird, object_x, 3*fullheight/5, fullwidth/19, fullheight/12);
    }
    
    
    
    object_x-=speed;
    
    if (object_x <= 0) {
        tmp = getRandomInt(4);
        if(tmp==1){
            runner = 'cars';
        }
        else if (tmp==2){
            runner = 'thieves';
        }
        else {
            runner = 'birds';
        }
        reset();
    }

    if (Math.abs(object_x-character_x_pos)<=6 && (Math.abs(character_y_pos-(3*fullheight/5))<=20) && character_x_pos-((fullwidth/5)-fullwidth/10) >= 6){
        console.log('dead');
        dead();
    }
}

function terrain() {
    // sky
    background('lightblue');
    var sun = color('#ffdf81');
    noStroke();
    for (i = 800; i > 0; i -= 50) {
        fill(sun);
        sun = lerpColor(sun, color('#f9ae4f'), 0.25);
        ellipse(fullwidth/2, fullheight/2, i);
    }
    
    // land
    fill('#429f46');
    rect(0, 4*fullheight/5, fullwidth, fullheight/5);
    fill('green');
        ellipse(fullwidth/5, 3.8*fullheight/5, fullwidth/12);
        fill('green');
        ellipse((fullwidth/5)-fullwidth/10, 3.8*fullheight/5, fullwidth/12);
        fill('green');
        ellipse((fullwidth/5)-fullwidth/20, 3.8*fullheight/5, fullwidth/12);
    
    // character
    image(character, character_x_pos, character_y_pos, character_x_size, character_y_size);
}

function jump(){
    if (((object_x - character_x_pos) < (10 + car_x_size)) && (0 < (object_x - character_x_pos))) {
        character_y_pos-=3*speed/4;
    }
    if (((character_x_pos - object_x) < (10 + car_x_size)) && (character_y_pos < 3*fullheight/5) && ((character_x_pos - object_x) > 0)){
        character_y_pos+=3*speed/4;
    }
}

function duck(){
    if (((object_x - character_x_pos) < (car_x_size)) && (0 < (object_x - character_x_pos))) {
        character_y_pos+=3*speed/4;
    }
    if (((character_x_pos - object_x) < (car_x_size)) && (character_y_pos > 3*fullheight/5) && ((character_x_pos - object_x) > 0)){
        character_y_pos-=3*speed/4;
    }
}

function hide() {
    fill('green');
    ellipse(fullwidth/5, 3.8*fullheight/5, fullwidth/12);
    fill('green');
    ellipse((fullwidth/5)-fullwidth/10, 3.8*fullheight/5, fullwidth/12);
    fill('green');
    ellipse((fullwidth/5)-fullwidth/20, 3.8*fullheight/5, fullwidth/12);
    if (character_x_pos>((fullwidth/5)-fullwidth/10)){
        character_x_pos-=speed;
    }
}

function dead(){
    noLoop();
}

function reset(){
    object_x = fullwidth;
    speed++;
}

function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}