Formatted files, added types and updated class diagram
This commit is contained in:
parent
4d22976621
commit
f272d3a44f
6 changed files with 52 additions and 32 deletions
|
|
@ -2,32 +2,47 @@
|
|||
classDiagram
|
||||
Entity <|-- Raspberry
|
||||
Entity <|-- Obstacle
|
||||
Entity: -Position position
|
||||
Entity: -int width
|
||||
Entity: -int height
|
||||
Entity: -update()
|
||||
Entity: -draw()
|
||||
Entity <|-- Pipe
|
||||
Entity: -number fill
|
||||
Entity: +Position position
|
||||
Entity: +number width
|
||||
Entity: +number height
|
||||
Entity: +abstract update()
|
||||
Entity: +draw()
|
||||
Entity: -detectCollision(Entity other)
|
||||
|
||||
class Raspberry{
|
||||
-input()
|
||||
-number lift
|
||||
-number gravity
|
||||
-static number maxVelocity
|
||||
+number velocity
|
||||
|
||||
-applyGravity()
|
||||
-forceBoundaries()
|
||||
-boost()
|
||||
+update()
|
||||
}
|
||||
|
||||
class Obstacle{
|
||||
-Entity pipeTop
|
||||
-Entity pipeBottom
|
||||
-int distanceBetweenPipes
|
||||
-int padding
|
||||
-resetPosition()
|
||||
-number distanceBetweenPipes
|
||||
-number padding
|
||||
-number speed
|
||||
-static number startX
|
||||
|
||||
-randomRange()
|
||||
+resetPosition()
|
||||
+update()
|
||||
+draw()
|
||||
}
|
||||
|
||||
class Pipe {
|
||||
+update()
|
||||
}
|
||||
|
||||
class Position{
|
||||
-int _x
|
||||
-int _y
|
||||
|
||||
+get x()
|
||||
+set x()
|
||||
+get y()
|
||||
+set y()
|
||||
+int x
|
||||
+int y
|
||||
}
|
||||
```
|
||||
|
|
@ -2,7 +2,7 @@ abstract class Entity {
|
|||
private _position: Position;
|
||||
private _width: number;
|
||||
private _height: number;
|
||||
private _fill: number;
|
||||
private fill: number;
|
||||
|
||||
//region Getter & Setter
|
||||
get position(): Position {
|
||||
|
|
@ -34,13 +34,13 @@ abstract class Entity {
|
|||
this.position = position;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this._fill = fill;
|
||||
this.fill = fill;
|
||||
}
|
||||
|
||||
public abstract update(): void;
|
||||
|
||||
public draw() {
|
||||
fill(this._fill);
|
||||
public draw(): void {
|
||||
fill(this.fill);
|
||||
rect(this.position.x, this.position.y, this.width, this.height);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ class Obstacle extends Entity {
|
|||
Obstacle.startX = width;
|
||||
}
|
||||
|
||||
public resetPosition() {
|
||||
public resetPosition(): void {
|
||||
this.pipeBottom.position.y = this.distanceBetweenPipes + this.randomRange(0, height - this.padding - 1.2 * this.distanceBetweenPipes);
|
||||
this.pipeBottom.position.x = Obstacle.startX;
|
||||
|
||||
|
|
@ -29,16 +29,16 @@ class Obstacle extends Entity {
|
|||
this.pipeTop.position.x = Obstacle.startX;
|
||||
}
|
||||
|
||||
private randomRange(min: number, max: number) {
|
||||
private randomRange(min: number, max: number): number {
|
||||
return Math.random() * (max - min) + min;
|
||||
}
|
||||
|
||||
public update() {
|
||||
public update(): void {
|
||||
this.pipeTop.position.x -= this.speed;
|
||||
this.pipeBottom.position.x -= this.speed;
|
||||
}
|
||||
|
||||
public draw() {
|
||||
public draw(): void {
|
||||
fill(10, 200, 100); //TODO do not make static
|
||||
rect(
|
||||
this.pipeTop.position.x,
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@ class Pipe extends Entity {
|
|||
super(position, width, height, 0);
|
||||
}
|
||||
|
||||
update(): void {
|
||||
public update(): void {
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ class Position {
|
|||
private _x: number;
|
||||
private _y: number;
|
||||
|
||||
//region Getter & Setter
|
||||
get x(): number {
|
||||
return this._x;
|
||||
}
|
||||
|
|
@ -17,6 +18,7 @@ class Position {
|
|||
set y(value: number) {
|
||||
this._y = value;
|
||||
}
|
||||
//endregion
|
||||
|
||||
constructor(x: number, y: number) {
|
||||
this._x = x;
|
||||
|
|
|
|||
|
|
@ -2,12 +2,13 @@ class Raspberry extends Entity{
|
|||
private lift: number = -10;
|
||||
private gravity: number = 1;
|
||||
private _velocity: number = 0;
|
||||
private static maxVelocity = 5;
|
||||
private static maxVelocity: number = 5;
|
||||
|
||||
constructor() {
|
||||
super(new Position(2 * width / 6, height / 2), 10, 10, 0);
|
||||
}
|
||||
|
||||
//region Getter & Setter
|
||||
get velocity(): number {
|
||||
return this._velocity;
|
||||
}
|
||||
|
|
@ -16,18 +17,20 @@ class Raspberry extends Entity{
|
|||
this._velocity = (this.velocity > Raspberry.maxVelocity) ? Raspberry.maxVelocity : value;
|
||||
}
|
||||
|
||||
update() {
|
||||
//endregion
|
||||
|
||||
public update(): void {
|
||||
this.applyGravity();
|
||||
this.forceBoundaries();
|
||||
}
|
||||
|
||||
private applyGravity() {
|
||||
private applyGravity(): void {
|
||||
if (this.position.y - this.height > 0) {
|
||||
this.velocity += this.gravity;
|
||||
}
|
||||
}
|
||||
|
||||
private forceBoundaries() {
|
||||
private forceBoundaries(): void {
|
||||
if (this.position.y > height) {
|
||||
this.position.y = height;
|
||||
this.velocity = 0;
|
||||
|
|
@ -39,7 +42,7 @@ class Raspberry extends Entity{
|
|||
}
|
||||
}
|
||||
|
||||
public boost() {
|
||||
public boost(): void {
|
||||
this.velocity += this.lift;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue