@@ -21,13 +21,13 @@ import {
2121import '@babylonjs/loaders/glTF'
2222//@ts -ignore
2323import * as CANNON from 'cannon' ;
24- import type { Size3D } from './types' ;
24+ import type { GameArgs , Size3D } from './types' ;
2525import { notRepeatedRandomFreeSpacePositionGenerator } from './utils' ;
2626
2727
2828export class Game {
2929 private static readonly SinglePlatformSize : Size3D = Object . freeze ( { height : 0.6 , depth : 6 , width : 8 } ) ;
30- private static readonly SingleWallSize : Size3D = Object . freeze ( { height : Game . SinglePlatformSize . width / 3 , depth : Game . SinglePlatformSize . width / 6 , width : Game . SinglePlatformSize . width / 3 } ) ;
30+ private static readonly SingleWallSize : Size3D = Object . freeze ( { height : Game . SinglePlatformSize . width / 3 , depth : Game . SinglePlatformSize . width / 8 , width : Game . SinglePlatformSize . width / 3 } ) ;
3131 private static readonly ZeroVector = new Vector3 ( 0 , 0 , 0 ) ;
3232 private static readonly SpeedOfMovingStraight = 6 ;
3333 private static readonly SpeedOfMovingAside = 2 ;
@@ -47,12 +47,18 @@ export class Game {
4747 private readonly platforms : Mesh [ ] ;
4848 private readonly walls : Mesh [ ] ;
4949 private readonly ball : Mesh ;
50- private coinModel : Mesh = { } as Mesh ;
50+ private readonly coins : Mesh [ ] = [ ] ;
5151 private readonly shadowGenerator : ShadowGenerator ;
52+ private coinScore = 0 ;
53+ private readonly canvasRef : HTMLCanvasElement ;
54+ private readonly scoreTextRef : HTMLElement ;
5255
5356
54- public constructor ( private readonly canvas : HTMLCanvasElement ) {
55- this . engine = new Engine ( this . canvas ) ;
57+ public constructor ( args : GameArgs ) {
58+ this . scoreTextRef = args . scoreTextRef ;
59+ this . canvasRef = args . canvasRef ;
60+
61+ this . engine = new Engine ( this . canvasRef ) ;
5662 this . scene = this . configureScene ( ) ;
5763 this . light = this . configureLight ( ) ;
5864 this . camera = this . configureCamera ( ) ;
@@ -72,18 +78,18 @@ export class Game {
7278
7379 this . initControls ( ) ;
7480
75- this . loadCoinModel ( ) ;
76- void this . coinModel ;
77-
78- this . scene . registerBeforeRender ( this . checkSphereBoxCollision . bind ( this ) ) ;
81+ this . scene . registerBeforeRender ( ( ) => {
82+ this . checkSphereBoxCollision ( ) ;
83+ this . checkCoinEarned ( ) ;
84+ } ) ;
7985
8086 this . engine . runRenderLoop ( ( ) => {
8187 this . updateCameraAndLight ( ) ;
8288 this . scene . render ( ) ;
8389 } ) ;
8490 }
8591
86- private loadCoinModel ( ) {
92+ private createCoin ( position : Vector3 ) {
8793 // I see, that deprecated, but Babylon is so strange, that the fastest way to load this
8894 // is just to use deprecated SceneLoader sync import
8995 SceneLoader . ImportMesh (
@@ -95,10 +101,10 @@ export class Game {
95101 console . log ( m )
96102 const meshArray = m as unknown as Mesh [ ] ;
97103 const coin = meshArray [ 0 ] ;
98- coin . scaling = new Vector3 ( 0.07 , 0.07 , 0.07 ) ;
99- coin . position = new Vector3 ( 2 , 1 , 0 ) ;
104+ coin . scaling = new Vector3 ( 0.05 , 0.05 , 0.05 ) ;
105+ coin . position = position ;
100106
101- this . coinModel = coin ;
107+ this . coins . push ( coin ) ;
102108
103109 this . shadowGenerator . addShadowCaster ( coin ) ;
104110 coin . receiveShadows = true ;
@@ -163,15 +169,17 @@ export class Game {
163169
164170 private updateCameraAndLight ( ) : void {
165171 this . camera . position . z = this . ball . getAbsolutePosition ( ) . z - 12 ;
166- this . camera . position . y = this . ball . getAbsolutePosition ( ) . y + 5 ;
172+ this . camera . position . y = this . ball . getAbsolutePosition ( ) . y + 6 ;
173+ //this.camera.setTarget(this.ball.getAbsolutePosition());
174+
167175 this . light . position . z = this . ball . getAbsolutePosition ( ) . z + 10 ;
168176 this . light . position . y = this . ball . getAbsolutePosition ( ) . y + 10 ;
169177 this . light . position . z = this . ball . getAbsolutePosition ( ) . z + 10 ;
170178 }
171179
172180 private shrinkCanvas ( ) {
173- this . canvas . width = this . canvas . clientWidth ;
174- this . canvas . height = this . canvas . clientHeight ;
181+ this . canvasRef . width = this . canvasRef . clientWidth ;
182+ this . canvasRef . height = this . canvasRef . clientHeight ;
175183 this . engine . resize ( ) ;
176184 }
177185
@@ -200,7 +208,7 @@ export class Game {
200208 }
201209
202210 private configureCamera ( ) : Camera {
203- const camera = new FreeCamera ( 'camera' , new Vector3 ( - 1 , 5 , - 10 ) , this . scene ) ;
211+ const camera = new FreeCamera ( 'camera' , new Vector3 ( - 2 , 5 , - 10 ) , this . scene ) ;
204212 camera . setTarget ( Game . ZeroVector ) ;
205213 ///camera.attachControl(this.canvas)
206214
@@ -290,6 +298,14 @@ export class Game {
290298 const walls : Mesh [ ] = [ ]
291299 for ( let counter = 0 ; counter < 3 ; ++ counter ) {
292300 if ( counter === skipPartIndex ) {
301+ // here probably must be coin with probability of ~70%
302+ if ( Math . random ( ) < 0.6 ) {
303+ this . createCoin ( new Vector3 (
304+ counter * oneThirdOfWidth - oneThirdOfWidth ,
305+ 1 ,
306+ 3 + z
307+ ) )
308+ }
293309 continue ;
294310 }
295311
@@ -309,7 +325,7 @@ export class Game {
309325
310326 for ( let counter = 0 ; counter < 20 ; ++ counter ) {
311327 const skippingPart = notRepeatedFreeSpaceGenerator ( ) ;
312- const row = this . createWallRow ( counter * ( Game . SingleWallSize . depth * 7 ) + offset , skippingPart ) ;
328+ const row = this . createWallRow ( counter * ( Game . SingleWallSize . depth * 10 ) + offset , skippingPart ) ;
313329 walls . push ( ...row ) ;
314330 }
315331
@@ -345,4 +361,20 @@ export class Game {
345361 }
346362 }
347363 }
364+ private updateScoreText ( ) {
365+ this . scoreTextRef . innerText = String ( this . coinScore ) ;
366+ }
367+
368+ private checkCoinEarned ( ) : void {
369+ for ( let i = 0 ; i < this . coins . length ; ++ i ) {
370+ if ( this . ball . intersectsMesh ( this . coins [ i ] , true ) ) {
371+ ++ this . coinScore ;
372+ this . updateScoreText ( ) ;
373+ this . scene . removeMesh ( this . coins [ i ] ) ;
374+ this . coins [ i ] . dispose ( ) ;
375+ this . coins . splice ( i , 1 ) ;
376+ break ;
377+ }
378+ }
379+ }
348380}
0 commit comments