-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.ts
187 lines (143 loc) · 5.49 KB
/
game.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/// <reference path="typings/index.d.ts" />
class Game {
private _scene : THREE.Scene;
private _camera : THREE.PerspectiveCamera;
private renderer : THREE.WebGLRenderer;
private _player : Player;
private _boundingBoxGame : THREE.Box3;
private _util : Utility;
private _enemies = new Array<Enemy>();
private _lasers = new Array<Laser>();
private startScreen : StartScreen;
private enemyRenderer : number;
private gameRenderer : number;
private gameRendererLoop : number;
public get scene() : THREE.Scene {
return this._scene;
}
public get camera() : THREE.PerspectiveCamera {
return this._camera;
}
public get boundingBoxGame() : THREE.Box3 {
return this._boundingBoxGame;
}
public get enemies() : Array<Enemy>{
return this._enemies;
}
public set enemies(array : Array<Enemy>){
this._enemies = array;
}
public get lasers() : Array<Laser>{
return this._lasers;
}
public set lasers(array : Array<Laser>){
this._lasers = array;
}
public get util() : Utility {
return this._util;
}
public get player() : Player {
return this._player;
}
constructor(){
this._util = new Utility();
this.setupEnvironment();
this.startScreen = new StartScreen(this);
}
private setupEnvironment() : void {
let screen = document.createElement("canvas");
screen.id = "canvas";
document.body.appendChild(screen);
this._scene = new THREE.Scene();
this._camera = new THREE.PerspectiveCamera(35, window.innerWidth / window.innerHeight, 0.1, 3000);
this._camera.position.z = 15;
this.renderer = new THREE.WebGLRenderer({canvas: screen, antialias: true});
this.renderer.setPixelRatio(window.devicePixelRatio);
this.renderer.setSize(window.innerWidth, window.innerHeight);
let ambientLight = new THREE.AmbientLight(0xffffff, 0.4);
this._scene.add(ambientLight);
let pointLight = new THREE.PointLight(0xffffff, 0.5);
pointLight.position.set(0, 0, this._camera.position.z + 15);
this._scene.add(pointLight);
let geometry = new THREE.PlaneGeometry(window.innerWidth * 4, 10000, 100, 100);
let material = new THREE.MeshLambertMaterial( { color: 0x00ff00 } );
material.wireframe = true;
let floor = new THREE.Mesh(geometry, material);
floor.rotation.x = -90 * Math.PI / 180;
floor.position.y = - (window.innerHeight / 2);
this._scene.add(floor);
this._boundingBoxGame = new THREE.Box3( new THREE.Vector3(-6, -4, -80) , new THREE.Vector3(6,4,8));
}
public startGame(){
this.startScreen = undefined;
this._player = new Player(this, new Health(this), new Score(), ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"]);
this.enemyRenderer = window.setInterval(() => this.renderEnemies(), 4000);
this.gameRenderer = requestAnimationFrame(() => this.gameLoop());
}
public endGame() : void {
if(this._player){
let endScreen = new EndScreen(this._player.score);
this._player.remove();
this._player = undefined;
}
window.clearInterval(this.enemyRenderer);
for (let enemy of this.enemies){
enemy.remove();
}
this.renderer.clear();
window.clearInterval(this.gameRenderer);
window.clearInterval(this.gameRendererLoop);
}
public addLaser(positionX : number, positionY : number, positionZ : number) : void{
if(this._player){
let laser = new Laser(positionX, positionY, positionZ, this);
this._lasers.push(laser);
}
}
private renderEnemies() : void {
let amountOfEnemies = this._util.generateRandomNumber(1,5);
amountOfEnemies = Math.floor(amountOfEnemies);
for(var i = 0; i < amountOfEnemies; i++) {
let enemy = new Enemy(this);
this._enemies.push(enemy);
}
}
private gameLoop() : void {
if(this._player){
for(var enemyIndex = 0; enemyIndex < this._enemies.length; enemyIndex++ ){
let enemy = this._enemies[enemyIndex];
if(this._util.detectCollision(this._player, enemy)){
this._player.health.percentage -= enemy.damageValue;
let enemyIndex = this._enemies.indexOf(enemy);
enemy.remove();
enemy = undefined;
break;
}
for(var laserIndex = 0; laserIndex < this._lasers.length; laserIndex++){
let laser = this._lasers[laserIndex];
if(this._util.detectCollision(enemy, laser)){
this._player.score.points += enemy.pointValue;
enemy.remove();
laser.remove(laserIndex);
enemy = undefined;
laser = undefined;
break;
}
}
}
if(this._player.health.checkDeath()){
this.endGame();
return;
}
this._player.move();
}
for(var laser of this._lasers){
laser.move();
}
for(var enemy of this._enemies){
enemy.move();
}
this.renderer.render(this._scene, this._camera);
this.gameRendererLoop = requestAnimationFrame(() => this.gameLoop());
}
}