collision workey
This commit is contained in:
parent
a8681c0eb4
commit
a464da424a
5 changed files with 33 additions and 10 deletions
|
|
@ -11,11 +11,12 @@ let raspberry: Raspberry;
|
||||||
function setup() {
|
function setup() {
|
||||||
backgroundImage = loadImage(backgroundImagePath);
|
backgroundImage = loadImage(backgroundImagePath);
|
||||||
|
|
||||||
createCanvas(1000, 1000);
|
createCanvas(2000, 1000);
|
||||||
obstacleOffset = width / 3;
|
obstacleOffset = width / 3;
|
||||||
|
|
||||||
raspberry = new Raspberry();
|
raspberry = new Raspberry();
|
||||||
raspberry.image = raspberryImagePath;
|
raspberry.image = raspberryImagePath;
|
||||||
|
raspberry.showHitbox = true;
|
||||||
|
|
||||||
obstacles.push(new Obstacle(
|
obstacles.push(new Obstacle(
|
||||||
new Position(width, 0),
|
new Position(width, 0),
|
||||||
|
|
@ -45,16 +46,24 @@ function draw() {
|
||||||
raspberry.update();
|
raspberry.update();
|
||||||
|
|
||||||
obstacles.forEach((obstacle) => {
|
obstacles.forEach((obstacle) => {
|
||||||
|
|
||||||
obstacle.draw();
|
obstacle.draw();
|
||||||
obstacle.update();
|
obstacle.update();
|
||||||
|
|
||||||
checkObstacleReset(obstacle);
|
checkObstacleReset(obstacle);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (obstacles[0].collides(raspberry)) {
|
||||||
|
obstacles[0].draw();
|
||||||
|
console.log("SAMC")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkObstacleReset(obstacle: Obstacle){
|
function checkObstacleReset(obstacle: Obstacle){
|
||||||
if(obstacle.position.x < -obstacleWidth) {
|
if(obstacle.position.x < -obstacleWidth) {
|
||||||
obstacle.resetPosition(true);
|
obstacle.resetPosition(true);
|
||||||
|
obstacles.shift();
|
||||||
|
obstacles.push(obstacle);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ abstract class Entity {
|
||||||
private _width: number;
|
private _width: number;
|
||||||
private _height: number;
|
private _height: number;
|
||||||
private fill: number;
|
private fill: number;
|
||||||
|
private _showHitbox: boolean;
|
||||||
|
|
||||||
//region Getter & Setter
|
//region Getter & Setter
|
||||||
get position(): Position {
|
get position(): Position {
|
||||||
|
|
@ -28,6 +29,15 @@ abstract class Entity {
|
||||||
set height(value: number) {
|
set height(value: number) {
|
||||||
this._height = value;
|
this._height = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get showHitbox(): boolean {
|
||||||
|
return this._showHitbox;
|
||||||
|
}
|
||||||
|
|
||||||
|
set showHitbox(value: boolean) {
|
||||||
|
this._showHitbox = value;
|
||||||
|
}
|
||||||
|
|
||||||
//endregion
|
//endregion
|
||||||
|
|
||||||
protected constructor(position: Position, width: number, height: number, fill: number) {
|
protected constructor(position: Position, width: number, height: number, fill: number) {
|
||||||
|
|
@ -35,6 +45,7 @@ abstract class Entity {
|
||||||
this.width = width;
|
this.width = width;
|
||||||
this.height = height;
|
this.height = height;
|
||||||
this.fill = fill;
|
this.fill = fill;
|
||||||
|
this._showHitbox = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract update(): void;
|
public abstract update(): void;
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ class Obstacle extends Entity implements Collidable{
|
||||||
this.pipeTop.image = pipeImagePath;
|
this.pipeTop.image = pipeImagePath;
|
||||||
this.pipeBottom.image = pipeImagePath;
|
this.pipeBottom.image = pipeImagePath;
|
||||||
|
|
||||||
this.distanceBetweenPipes = height / 4;
|
this.distanceBetweenPipes = height / 2.5;
|
||||||
Obstacle.startX = width;
|
Obstacle.startX = width;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -51,7 +51,7 @@ class Obstacle extends Entity implements Collidable{
|
||||||
this.pipeBottom.draw();
|
this.pipeBottom.draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
collides(o: Entity): boolean {
|
public collides(o: Entity): boolean {
|
||||||
return this.pipeTop.collides(o) || this.pipeBottom.collides(o);
|
return this.pipeTop.collides(o) || this.pipeBottom.collides(o);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -31,9 +31,9 @@ class Pipe extends Entity implements Collidable {
|
||||||
}
|
}
|
||||||
|
|
||||||
collides(o: Entity): boolean {
|
collides(o: Entity): boolean {
|
||||||
return this.position.x < o.position.x + o.width && //inside left border
|
return this.position.x < (o.position.x + o.width) && //inside left border
|
||||||
this.position.x + this.width > o.position.x && //but not outside right border
|
(this.position.x + this.width) > o.position.x && //but not outside right border
|
||||||
this.position.y < o.position.y + o.height && //inside top border
|
this.position.y < (o.position.y + o.height) && //inside top border
|
||||||
this.position.y + this.height > o.position.y; //but not outside bottom border
|
(this.position.y + this.height) > o.position.y; //but not outside bottom border
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -59,7 +59,10 @@ class Raspberry extends Entity {
|
||||||
public draw(): void {
|
public draw(): void {
|
||||||
image(this.image, this.position.x, this.position.y, this.width, this.height);
|
image(this.image, this.position.x, this.position.y, this.width, this.height);
|
||||||
noFill();
|
noFill();
|
||||||
|
strokeWeight(50);
|
||||||
|
if (!this.showHitbox) {
|
||||||
noStroke();
|
noStroke();
|
||||||
|
}
|
||||||
rect(
|
rect(
|
||||||
this.position.x,
|
this.position.x,
|
||||||
this.position.y,
|
this.position.y,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue