@@ -72,6 +72,11 @@ export class Map {
72
72
rectangle : Phaser . Rectangle ;
73
73
background_key : string ;
74
74
} [ ] ;
75
+ private bgm_regions : {
76
+ rectangle : Phaser . Rectangle ;
77
+ bgm_keys : string [ ] ;
78
+ sound_objects : Phaser . Sound [ ] ;
79
+ } [ ] ;
75
80
private _sand_collision_layer : number ;
76
81
private processed_polygons : {
77
82
[ collision_layer : number ] : Array < {
@@ -166,6 +171,7 @@ export class Map {
166
171
this . expected_party_level = expected_party_level ;
167
172
this . _encounter_cumulator = 0 ;
168
173
this . encounter_zones = [ ] ;
174
+ this . bgm_regions = [ ] ;
169
175
this . _background_key = background_key ;
170
176
this . polygons_processed = false ;
171
177
this . bounding_boxes = [ ] ;
@@ -408,6 +414,7 @@ export class Map {
408
414
this . sort_sprites ( ) ;
409
415
this . update_map_rotation ( ) ;
410
416
this . zone_check ( ) ;
417
+ this . bgm_region_check ( ) ;
411
418
}
412
419
413
420
/**
@@ -1423,6 +1430,29 @@ export class Map {
1423
1430
this . bounding_boxes . push ( bounding_box ) ;
1424
1431
} ) ;
1425
1432
return layer_name ;
1433
+ } else if ( objs . properties ?. bgm_regions ) {
1434
+ //check for bgm regions. When the hero is inside these regions, a list of bgms are executed.
1435
+ objs . objectsData . forEach ( this_obj => {
1436
+ const bgm_rectangle = new Phaser . Rectangle (
1437
+ this_obj . x | 0 ,
1438
+ this_obj . y | 0 ,
1439
+ this_obj . width | 0 ,
1440
+ this_obj . height | 0
1441
+ ) ;
1442
+ try {
1443
+ const bgms_list = JSON . parse ( this_obj . properties ?. bgms_list ) as string [ ] ;
1444
+ if ( bgms_list && bgms_list . length ) {
1445
+ this . bgm_regions . push ( {
1446
+ rectangle : bgm_rectangle ,
1447
+ bgm_keys : bgms_list ,
1448
+ sound_objects : bgms_list . map ( key => this . game . add . audio ( key , 1.0 , true ) ) ,
1449
+ } ) ;
1450
+ }
1451
+ } catch {
1452
+ this . data . logger . log_message ( `BGMs list is not a valid JSON in ${ layer_name } layer.` ) ;
1453
+ }
1454
+ } ) ;
1455
+ return layer_name ;
1426
1456
} else {
1427
1457
//the default behavior is to treat the layer name as a collision index.
1428
1458
++ collision_layers_counter ;
@@ -1526,6 +1556,27 @@ export class Map {
1526
1556
}
1527
1557
}
1528
1558
1559
+ /**
1560
+ * Update function to check whether BGMs should be played if the hero is inside the specified regions.
1561
+ */
1562
+ private bgm_region_check ( ) {
1563
+ this . bgm_regions . forEach ( region => {
1564
+ if ( region . rectangle . contains ( this . data . hero . x , this . data . hero . y ) ) {
1565
+ region . sound_objects . forEach ( sound_obj => {
1566
+ if ( ! sound_obj . isPlaying ) {
1567
+ sound_obj . fadeIn ( 750 , true ) ;
1568
+ }
1569
+ } ) ;
1570
+ } else {
1571
+ region . sound_objects . forEach ( sound_obj => {
1572
+ if ( sound_obj . isPlaying ) {
1573
+ sound_obj . fadeOut ( 750 ) ;
1574
+ }
1575
+ } ) ;
1576
+ }
1577
+ } ) ;
1578
+ }
1579
+
1529
1580
/**
1530
1581
* Checks whether it's time to start a random battle.
1531
1582
*/
@@ -2190,6 +2241,9 @@ export class Map {
2190
2241
}
2191
2242
this . _generic_sprites = { } ;
2192
2243
2244
+ this . bgm_regions . forEach ( region => region . sound_objects . forEach ( obj => obj ?. destroy ( ) ) ) ;
2245
+ this . bgm_regions = [ ] ;
2246
+
2193
2247
TileEvent . reset ( ) ;
2194
2248
GameEvent . reset ( ) ;
2195
2249
0 commit comments