-
Notifications
You must be signed in to change notification settings - Fork 2
/
asteroid.ts
32 lines (25 loc) · 1020 Bytes
/
asteroid.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Asteroid extends GameObject implements Observer{
private _speedX : number = Math.random() < 0.5 ? Math.random() - 1 * 1.5 : Math.random() * 1.5;
private _speedY : number = Math.random() < 0.5 ? Math.random() - 1 * 1.5 : Math.random() * 1.5;
private _subject : Subject;
private _rotationSpeed : number = Math.random()*2;
private _asteroidList : Array<Asteroid>;
constructor(s:Subject) {
super(Math.floor((Math.random() * window.innerWidth) + window.innerWidth / 2), Math.floor((Math.random() * window.innerHeight) + 1), 0, 'asteroid'+Math.floor(Math.random() * 4 + 1));
this._subject = s;
}
public update() : void {
this.x += this._speedX;
this.y += this._speedY;
this.rotation += this._rotationSpeed;
super.outsideWindow();
}
public notify() {
this.explode();
}
public explode() {
this._subject.unsubscribe(this);
new AsteroidExplosion(this.x, this.y);
this.remove();
}
}