forked from ArnovanDoesburg/Asteroid-Assault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gameObject.ts
79 lines (60 loc) · 2.34 KB
/
gameObject.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class GameObject {
private _x : number = 0;
private _y : number = 0;
private _rotation : number = 0;
private _width : number = 0;
private _height : number = 0;
private _div : HTMLElement;
public get x() : number {return this._x};
public set x(value : number) {this._x = value};
public get y() : number {return this._y};
public set y(value : number) {this._y = value};
public get rotation() : number {return this._rotation};
public set rotation(value : number) {this._rotation = value};
public get width() : number {return this._width};
public set width(value : number) {this._width = value};
public get height() : number {return this._height};
public set height(value : number) {this._height = value};
public get div() : HTMLElement {return this._div};
public set div(value : HTMLElement) {this._div = value};
constructor(x:number, y:number, rotation:number, tag:string) {
this._x = x;
this._y = y;
this._rotation = rotation;
this._div = document.createElement(tag);
document.body.appendChild(this._div);
this._width = this._div.clientWidth;
this._height = this._div.clientHeight;
GameManager.getInstance().addGameObject(this);
this.draw();
}
public update() : void {
}
public draw() : void {
this._div.style.transform = "translate("+this.x+"px, "+this.y+"px) rotate("+this.rotation+"deg)";
}
public hasCollision(obj:GameObject) : boolean {
return (this.x < obj.x + obj.width &&
this.x + this.width > obj.x &&
this.y < obj.y + obj.height &&
this.y + this.height > obj.y);
}
public outsideWindow() {
if (this.x + this.div.clientWidth < 0) {
this.x = window.innerWidth;
}
if (this.x > window.innerWidth) {
this.x = 0 - this.div.clientWidth;
}
if (this.y + this.div.clientHeight < 0) {
this.y = window.innerHeight;
}
if (this.y > window.innerHeight) {
this.y = 0 - this.div.clientHeight;
}
}
public remove() {
GameManager.getInstance().removeGameObject(this);
this.div.remove();
}
}