1
- import { IClimbingEnemy , IEnemy , IFlyingEnemy , IGame } from "../types/shadowhound" ;
1
+ import { IEnemy , IGame , IFlyingEnemy , IClimbingEnemy , IBoss } from "../types/shadowhound" ;
2
2
import { getImage } from "../utils/misc" ;
3
3
import { FireBall } from "./particles.class" ;
4
4
5
+ /**
6
+ * @class Enemy
7
+ * @implements {IEnemy}
8
+ * @property {IGame } game - The game object.
9
+ * @property {number } width - The width of the enemy.
10
+ * @property {number } height - The height of the enemy.
11
+ * @property {number } x - The x position of the enemy.
12
+ * @property {number } y - The y position of the enemy.
13
+ * @property {HTMLImageElement } image - The image element for the enemy.
14
+ * @property {number } speedX - The x speed of the enemy.
15
+ * @property {number } speedY - The y speed of the enemy.
16
+ * @property {number } maxFrame - The maximum frame of the enemy.
17
+ * @property {number } frameX - The x position of the frame of the enemy.
18
+ * @property {number } frameY - The y position of the frame of the enemy.
19
+ * @property {number } fps - The frames per second of the enemy.
20
+ * @property {number } frameInterval - The frame interval of the enemy in milliseconds.
21
+ * @property {number } frameTimer - The frame timer of the enemy.
22
+ * @property {boolean } markedForDeletion - The flag to check if the enemy is marked for deletion.
23
+ * @property {number } lives - The lives of the enemy.
24
+ */
5
25
class Enemy implements IEnemy {
26
+ /**
27
+ * @constructor
28
+ * @param {IGame } game - The game object.
29
+ */
6
30
constructor ( game : IGame ) {
7
31
this . game = game ;
8
32
this . width = 0 ;
@@ -38,36 +62,50 @@ class Enemy implements IEnemy {
38
62
markedForDeletion : boolean ;
39
63
lives : number ;
40
64
65
+ /**
66
+ * Updates the enemy.
67
+ * @param {number } deltaTime - The delta time.
68
+ */
41
69
update ( deltaTime : number ) : void {
42
70
this . x -= this . speedX + this . game . speed ;
43
71
this . y += this . speedY ;
72
+
73
+ this . frameTimer += deltaTime ;
74
+
44
75
if ( this . frameTimer > this . frameInterval ) {
45
76
this . frameTimer = 0 ;
77
+
46
78
if ( this . frameX < this . maxFrame ) this . frameX ++ ;
47
79
else this . frameX = 0 ;
48
- } else this . frameTimer += deltaTime ;
80
+ }
49
81
50
- // check if off screen
51
- if ( this . x + this . width < 0 ) this . markedForDeletion = true ;
82
+ if ( this . x + this . width < 0 ) {
83
+ this . markedForDeletion = true ;
84
+ }
52
85
}
53
86
87
+ /**
88
+ * Draws the enemy.
89
+ * @param {CanvasRenderingContext2D } ctx - The canvas context.
90
+ */
54
91
draw ( ctx : CanvasRenderingContext2D ) : void {
55
92
if ( this . game . debug ) ctx . strokeRect ( this . x , this . y , this . width , this . height ) ;
56
- ctx . drawImage (
57
- this . image ,
58
- this . frameX * this . width ,
59
- 0 ,
60
- this . width ,
61
- this . height ,
62
- this . x ,
63
- this . y ,
64
- this . width ,
65
- this . height
66
- ) ;
93
+ ctx . drawImage ( this . image , this . frameX * this . width , 0 , this . width , this . height , this . x , this . y , this . width , this . height ) ;
67
94
}
68
95
}
69
96
97
+ /**
98
+ * @class FlyingEnemy
99
+ * @extends {Enemy }
100
+ * @implements {IFlyingEnemy}
101
+ * @property {number } angle - The angle of the enemy.
102
+ * @property {number } va - The vertical angle of the enemy.
103
+ */
70
104
export class FlyingEnemy extends Enemy implements IFlyingEnemy {
105
+ /**
106
+ * @constructor
107
+ * @param {IGame } game - The game object.
108
+ */
71
109
constructor ( game : IGame ) {
72
110
super ( game ) ;
73
111
this . width = 60 ;
@@ -81,18 +119,31 @@ export class FlyingEnemy extends Enemy implements IFlyingEnemy {
81
119
this . angle = 0 ;
82
120
this . va = Math . random ( ) * 0.1 + 0.1 ;
83
121
}
84
-
85
122
angle : number ;
86
123
va : number ;
87
124
125
+ /**
126
+ * Updates the enemy.
127
+ * @param {number } deltaTime - The delta time.
128
+ */
88
129
update ( deltaTime : number ) : void {
89
130
super . update ( deltaTime ) ;
90
131
this . angle += this . va ;
91
132
this . y += Math . sin ( this . angle ) ;
92
133
}
93
134
}
94
135
136
+ /**
137
+ * @class GroundEnemy
138
+ * @extends {Enemy }
139
+ * @property {number } angle - The angle of the enemy.
140
+ * @property {number } va - The vertical angle of the enemy.
141
+ */
95
142
export class GroundEnemy extends Enemy {
143
+ /**
144
+ * @constructor
145
+ * @param {IGame } game - The game object.
146
+ */
96
147
constructor ( game : IGame ) {
97
148
super ( game ) ;
98
149
this . width = 60 ;
@@ -104,7 +155,18 @@ export class GroundEnemy extends Enemy {
104
155
}
105
156
}
106
157
158
+ /**
159
+ * @class ClimbingEnemy
160
+ * @extends {Enemy }
161
+ * @implements {IClimbingEnemy}
162
+ * @property {number } angle - The angle of the enemy.
163
+ * @property {number } va - The vertical angle of the enemy.
164
+ */
107
165
export class ClimbingEnemy extends Enemy implements IClimbingEnemy {
166
+ /**
167
+ * @constructor
168
+ * @param {IGame } game - The game object.
169
+ */
108
170
constructor ( game : IGame ) {
109
171
super ( game ) ;
110
172
this . width = 120 ;
@@ -116,12 +178,20 @@ export class ClimbingEnemy extends Enemy implements IClimbingEnemy {
116
178
this . maxFrame = 5 ;
117
179
}
118
180
181
+ /**
182
+ * Updates the enemy.
183
+ * @param {number } deltaTime - The delta time.
184
+ */
119
185
update ( deltaTime : number ) : void {
120
186
super . update ( deltaTime ) ;
121
187
if ( this . y > this . game . height - this . height - this . game . groundMargin ) this . speedY *= - 1 ;
122
188
if ( this . y < - this . height ) this . markedForDeletion = true ;
123
189
}
124
190
191
+ /**
192
+ * Draws the enemy.
193
+ * @param {CanvasRenderingContext2D } ctx - The canvas context.
194
+ */
125
195
draw ( ctx : CanvasRenderingContext2D ) : void {
126
196
super . draw ( ctx ) ;
127
197
ctx . beginPath ( ) ;
@@ -131,7 +201,19 @@ export class ClimbingEnemy extends Enemy implements IClimbingEnemy {
131
201
}
132
202
}
133
203
134
- export class Boss extends FlyingEnemy {
204
+ /**
205
+ * @class Boss
206
+ * @extends {FlyingEnemy }
207
+ * @property {number } attackTimer - The timer for the boss's attack.
208
+ * @property {number } attackInterval - The interval for the boss's attack in milliseconds.
209
+ * @property {number } lives - The lives of the boss.
210
+ * @property {boolean } hit - The flag to check if the boss is hit.
211
+ */
212
+ export class Boss extends FlyingEnemy implements IBoss {
213
+ /**
214
+ * @constructor
215
+ * @param {IGame } game - The game object.
216
+ */
135
217
constructor ( game : IGame ) {
136
218
super ( game ) ;
137
219
this . width = 238 ;
@@ -146,15 +228,21 @@ export class Boss extends FlyingEnemy {
146
228
147
229
attackTimer : number ;
148
230
attackInterval : number ;
149
- lives : number ;
150
231
hit : boolean ;
151
232
233
+ /**
234
+ * Updates the enemy.
235
+ * @param {number } deltaTime - The delta time.
236
+ */
152
237
update ( deltaTime : number ) : void {
153
238
super . update ( deltaTime ) ;
154
239
if ( this . x < this . game . width - this . width - 50 ) this . x = this . game . width - this . width - 50 ;
155
240
else this . speedX = 0 ;
156
241
}
157
242
243
+ /**
244
+ * Attacks the player.
245
+ */
158
246
attack ( ) {
159
247
const playerCenterX = this . game . player . x + this . game . player . width / 2 ;
160
248
const playerCenterY = this . game . player . y + this . game . player . height / 2 ;
0 commit comments