108 lines
2.8 KiB
JavaScript
Executable File
108 lines
2.8 KiB
JavaScript
Executable File
class Dinosaur {
|
|
constructor(x, y, wd, ht) {
|
|
this.pos = new createVector(x, y);
|
|
this.x = x;
|
|
this.y = y;
|
|
this.wd = wd;
|
|
this.ht = ht;
|
|
this.speed = 10;
|
|
this.vel = 0;
|
|
this.grav = 1;
|
|
this.jmpfrc = 17;
|
|
this.counter = 0;
|
|
this.obs = [new Obstacle(this.x, this.y, 1920)];
|
|
this.lost = false;
|
|
};
|
|
|
|
update(c) {
|
|
this.speed += 0.001;
|
|
this.counter += this.speed;
|
|
if (this.counter >= 1000) {
|
|
this.obs.push(new Obstacle(this.x, this.y, 1700));
|
|
this.counter = 0;
|
|
}
|
|
if (this.pos.y == this.y) {
|
|
if (c == '1') {
|
|
this.wd = 50;
|
|
this.ht = 50;
|
|
this.vel = this.jmpfrc;
|
|
} else if(c == '0'){
|
|
this.wd = 75;
|
|
this.ht = 30;
|
|
this.vel = 0;
|
|
} else{
|
|
this.wd = 50;
|
|
this.ht = 50;
|
|
this.vel = 0;
|
|
}
|
|
} else if (this.pos.y > this.y) {
|
|
this.vel -= this.grav;
|
|
} else if (this.pos.y < this.y) {
|
|
this.pos.y = this.y;
|
|
}
|
|
this.pos.y += this.vel;
|
|
|
|
for (let i = 0; i < this.obs.length; i++) {
|
|
this.obs[i].update(this.speed);
|
|
this.obs[i].show();
|
|
}
|
|
if ((this.obs[0]) && (this.obs[0].pos.x <= -50)) {
|
|
this.obs.shift();
|
|
}
|
|
if (this.obs.length>0) {
|
|
for (let i = 0; i < this.obs.length; i++) {
|
|
if ((this.obs[i].pos.x - this.pos.x < this.wd)&&(this.obs[i].pos.x - this.pos.x > 0-this.obs[i].wd)) {
|
|
if(this.obs[i].pos.y + this.obs[i].ht - this.pos.y > 0){
|
|
this.lost = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
show() {
|
|
push();
|
|
//transform();
|
|
rectMode(CORNER)
|
|
fill(200);
|
|
stroke(200);
|
|
strokeWeight(0);
|
|
rect(this.pos.x, mapY(this.pos.y + this.ht), this.wd, this.ht);
|
|
strokeWeight(2);
|
|
line(0, mapY(this.y), width, mapY(this.y));
|
|
pop();
|
|
for (let i = 0; i < this.obs.length; i++) {
|
|
this.obs[i].show();
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
class Obstacle {
|
|
constructor(x, y, dist) {
|
|
this.pos = createVector(x + dist + random(0,500), y);
|
|
this.wd = 30;
|
|
this.ht = 60;
|
|
};
|
|
|
|
update(spd) {
|
|
this.pos.x -= spd;
|
|
}
|
|
|
|
show() {
|
|
push();
|
|
rectMode(CORNER)
|
|
fill(200);
|
|
stroke(200);
|
|
strokeWeight(0);
|
|
rect(this.pos.x, mapY(this.pos.y + this.ht), this.wd, this.ht);
|
|
strokeWeight(2);
|
|
line(0, mapY(this.pos.y), width, mapY(this.pos.y));
|
|
pop();
|
|
}
|
|
|
|
}
|
|
|
|
function mapY(y) {
|
|
return map(y, 0, height, height, 0);
|
|
} |