random pipe position working

This commit is contained in:
dhain 2022-12-06 09:45:43 +01:00
parent 4a40175884
commit 8e19fb7cfb
5 changed files with 59 additions and 17 deletions

View file

@ -1,29 +1,56 @@
class Obstacle extends Entity {
private pipeTop: Entity;
private pipeBottom: Entity;
private distanceBetweenPipes: number = 50;
private padding: number = 50;
private speed: number = 10;
private distanceBetweenPipes: number;
private padding: number = 300;
private speed: number = 8;
private static startX: number;
/**
* Constructs the Obstacle using the top and bottom Pipe
* (fill is not used here)
* @param pipeTop
* @param pipeBottom
*/
constructor(pipeTop: Entity, pipeBottom: Entity) {
super(pipeTop.position, pipeTop.width, height, 0);
super(pipeTop.position, pipeTop.width, pipeBottom.height, 0);
this.pipeTop = pipeTop;
this.pipeBottom = pipeBottom;
this.distanceBetweenPipes = height / 4;
Obstacle.startX = width;
}
private resetPosition(){
let randomY = Math.random() * (height - this.padding) + this.padding;
this.pipeTop.height = randomY - this.distanceBetweenPipes / 2;
this.pipeTop.position.x = Obstacle.startX;
this.pipeBottom.height = randomY + this.distanceBetweenPipes / 2;
public resetPosition() {
this.pipeBottom.position.y = this.distanceBetweenPipes + this.randomRange(0, height - this.padding - 1.2 * this.distanceBetweenPipes);
this.pipeBottom.position.x = Obstacle.startX;
this.pipeTop.position.y = this.pipeBottom.position.y - this.distanceBetweenPipes - this.pipeTop.height;
this.pipeTop.position.x = Obstacle.startX;
}
public update(){
private randomRange(min: number, max: number) {
return Math.random() * (max - min) + min;
}
public update() {
this.pipeTop.position.x -= this.speed;
this.pipeBottom.position.x -= this.speed;
}
public draw() {
fill(10, 200, 100); //TODO do not make static
rect(
this.pipeTop.position.x,
this.pipeTop.position.y,
this.pipeTop.width,
this.pipeTop.height
);
rect(
this.pipeBottom.position.x,
this.pipeBottom.position.y,
this.pipeBottom.width,
this.pipeBottom.height
)
}
}