@@ -5,7 +5,7 @@ import { Vector2 } from '../structs/Vector2';
55import { DrawSettings , defaultDrawSettings } from './DrawSettings' ;
66
77/**
8- *
8+ * Class represents Game Renderer
99 */
1010export class Renderer {
1111 private readonly handler : Game ;
@@ -64,24 +64,29 @@ export class Renderer {
6464 return new Vector2 ( this . canvasWidth , this . canvasHeight ) ;
6565 }
6666
67+ /**
68+ * Scales number from client coordinate to grid coordinate
69+ * @param x The client coordinate
70+ * @returns The scaled number
71+ */
6772 scale ( x : number ) : number {
6873 return x * this . gridSize ;
6974 }
7075
71- radians ( degrees : number ) : number {
76+ private radians ( degrees : number ) : number {
7277 degrees %= 360 ;
7378 return degrees * Math . PI / 180 ;
7479 }
7580
76- degrees ( radians : number ) : number {
81+ private degrees ( radians : number ) : number {
7782 return radians / Math . PI * 180 ;
7883 }
7984
80- combineDrawSettings ( drawSettings : DrawSettings | undefined ) : DrawSettings {
85+ private combineDrawSettings ( drawSettings : DrawSettings | undefined ) : DrawSettings {
8186 return { ...defaultDrawSettings , ...drawSettings } ;
8287 }
8388
84- setContextSettings ( drawSettings : DrawSettings ) {
89+ private setContextSettings ( drawSettings : DrawSettings ) {
8590 if ( drawSettings . color !== undefined )
8691 this . ctx . fillStyle = drawSettings . color ;
8792 if ( drawSettings . borderColor !== undefined )
@@ -100,6 +105,14 @@ export class Renderer {
100105 this . ctx . shadowBlur = this . scale ( drawSettings . shadow . blur ) ;
101106 }
102107
108+ /**
109+ * Draws a rect.
110+ * @param x The X-coordinate
111+ * @param y The Y-coordinate
112+ * @param width The width
113+ * @param height The height
114+ * @param drawSettings The draw settings
115+ */
103116 drawRectangle ( x : number , y : number , width : number , height : number , drawSettings ?: DrawSettings ) {
104117 drawSettings = this . combineDrawSettings ( drawSettings ) ;
105118 const dx = this . scale ( x ) ;
@@ -121,6 +134,13 @@ export class Renderer {
121134 this . ctx . restore ( ) ;
122135 }
123136
137+ /**
138+ * Draws a circle.
139+ * @param x The X-coordinate
140+ * @param y The Y-coordinate
141+ * @param diameter The diameter
142+ * @param drawSettings The draw settings
143+ */
124144 drawCircle ( x : number , y : number , diameter : number , drawSettings ?: DrawSettings ) {
125145 drawSettings = this . combineDrawSettings ( drawSettings ) ;
126146 const dx = this . scale ( x ) ;
@@ -137,24 +157,15 @@ export class Renderer {
137157 this . ctx . stroke ( ) ;
138158 }
139159
140- // drawTriangle(x: number, y: number, width: number, height: number, drawSettings: DrawSettings = undefined){
141- // drawSettings = this.combineDrawSettings(drawSettings);
142- // const dx = this.scale(x);
143- // const dy = this.scale(y);
144- // const dw = this.scale(width);
145- // const dh = this.scale(height);
146- // this.ctx.save();
147- // this.ctx.translate(dx + dw / 2, dy + dh / 2);
148- // this.ctx.rotate(this.radians(drawSettings.angle));
149- // this.ctx.translate(- dx - dw / 2, - dy - dh / 2);
150- // this.setContextSettings(drawSettings);
151- // //
152-
153- // //
154- // this.ctx.restore();
155- // }
156-
157- drawLine ( x1 : number , y1 : number , x2 : number , y2 : number , drawSettings : DrawSettings ) {
160+ /**
161+ * Draws line from position to another position.
162+ * @param x1 From X-coordinate
163+ * @param y1 From Y-coordinate
164+ * @param x2 To X-coordinate
165+ * @param y2 To Y-coordinate
166+ * @param drawSettings The draw settings
167+ */
168+ drawLine ( x1 : number , y1 : number , x2 : number , y2 : number , drawSettings ?: DrawSettings ) {
158169 drawSettings = this . combineDrawSettings ( drawSettings ) ;
159170 this . setContextSettings ( drawSettings ) ;
160171 this . ctx . beginPath ( ) ;
@@ -164,7 +175,15 @@ export class Renderer {
164175 this . ctx . stroke ( ) ;
165176 }
166177
167- drawArrow ( x1 : number , y1 : number , x2 : number , y2 : number , drawSettings : DrawSettings ) {
178+ /**
179+ * Draws arrow from position to another position.
180+ * @param x1 From X-coordinate
181+ * @param y1 From Y-coordinate
182+ * @param x2 To X-coordinate
183+ * @param y2 To Y-coordinate
184+ * @param drawSettings The draw settings
185+ */
186+ drawArrow ( x1 : number , y1 : number , x2 : number , y2 : number , drawSettings ?: DrawSettings ) {
168187 drawSettings = this . combineDrawSettings ( drawSettings ) ;
169188 // Scaling
170189 const size = this . gridSize / 16 ;
@@ -195,21 +214,41 @@ export class Renderer {
195214 this . ctx . restore ( ) ;
196215 }
197216
217+ /**
218+ * Clears canvas
219+ */
198220 clearFrame ( ) {
199221 this . ctx . clearRect ( 0 , 0 , this . canvasWidth , this . canvasHeight ) ;
200222 }
201223
224+ /**
225+ * Fills canvas with color property from settings
226+ * @param drawSettings The settings
227+ */
202228 fillFrame ( drawSettings : DrawSettings ) {
203229 drawSettings = this . combineDrawSettings ( drawSettings ) ;
204230 this . setContextSettings ( drawSettings ) ;
205231 this . ctx . fillRect ( 0 , 0 , this . canvasWidth , this . canvasHeight ) ;
206232 }
207233
234+ /**
235+ * Fills canvas with image.
236+ * @param image The image
237+ */
208238 fillImageFrame ( image : HTMLCanvasElement ) {
209239 this . ctx . drawImage ( image , 0 , 0 , this . canvasWidth , this . canvasHeight ) ;
210240 }
211241
212- drawImage ( image : HTMLImageElement , x : number , y : number , width : number , height : number , drawSettings : DrawSettings ) {
242+ /**
243+ * Draws image.
244+ * @param image The image
245+ * @param x The X-coordinate
246+ * @param y The Y-coordinate
247+ * @param width The width
248+ * @param height The height
249+ * @param drawSettings The draw settings
250+ */
251+ drawImage ( image : HTMLImageElement , x : number , y : number , width : number , height : number , drawSettings ?: DrawSettings ) {
213252 drawSettings = this . combineDrawSettings ( drawSettings ) ;
214253 const dx = this . scale ( x ) ;
215254 const dy = this . scale ( y ) ;
@@ -224,10 +263,19 @@ export class Renderer {
224263 this . ctx . restore ( ) ;
225264 }
226265
227- drawSprite ( sprite : Sprite ) {
228- if ( sprite . texture === undefined ) {
266+ /**
267+ * Draws sprite texture
268+ * @param sprite The sprite
269+ * @returns is success?
270+ */
271+ drawSprite ( sprite : Sprite ) : boolean {
272+ if ( sprite . texture === undefined || sprite . texture === null ) {
229273 console . warn ( "The Sprite " , sprite , " has not assigned texture!" ) ;
230- return ;
274+ return false ;
275+ }
276+ if ( ! ( sprite . texture instanceof HTMLImageElement ) ) {
277+ console . warn ( `Texture ${ sprite . name } [${ sprite . id } ] cannot be ${ typeof sprite . texture } !` ) ;
278+ return false ;
231279 }
232280 this . drawImage (
233281 sprite . texture ,
@@ -237,8 +285,13 @@ export class Renderer {
237285 sprite . transform . scale . y ,
238286 { angle : sprite . transform . rotation }
239287 ) ;
288+ return true ;
240289 }
241290
291+ /**
292+ * Draws hitbox to sprite
293+ * @param sprite The sprite
294+ */
242295 drawSpriteHitBox ( sprite : Sprite ) {
243296 const pos = sprite . transform . position ;
244297 const center = sprite . transform . positionCenter ;
0 commit comments