Skip to content

Commit 2540b73

Browse files
committed
bgm regions
1 parent d8e3d85 commit 2540b73

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

base/Map.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ export class Map {
7272
rectangle: Phaser.Rectangle;
7373
background_key: string;
7474
}[];
75+
private bgm_regions: {
76+
rectangle: Phaser.Rectangle;
77+
bgm_keys: string[];
78+
sound_objects: Phaser.Sound[];
79+
}[];
7580
private _sand_collision_layer: number;
7681
private processed_polygons: {
7782
[collision_layer: number]: Array<{
@@ -166,6 +171,7 @@ export class Map {
166171
this.expected_party_level = expected_party_level;
167172
this._encounter_cumulator = 0;
168173
this.encounter_zones = [];
174+
this.bgm_regions = [];
169175
this._background_key = background_key;
170176
this.polygons_processed = false;
171177
this.bounding_boxes = [];
@@ -408,6 +414,7 @@ export class Map {
408414
this.sort_sprites();
409415
this.update_map_rotation();
410416
this.zone_check();
417+
this.bgm_region_check();
411418
}
412419

413420
/**
@@ -1423,6 +1430,29 @@ export class Map {
14231430
this.bounding_boxes.push(bounding_box);
14241431
});
14251432
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;
14261456
} else {
14271457
//the default behavior is to treat the layer name as a collision index.
14281458
++collision_layers_counter;
@@ -1526,6 +1556,27 @@ export class Map {
15261556
}
15271557
}
15281558

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+
15291580
/**
15301581
* Checks whether it's time to start a random battle.
15311582
*/
@@ -2190,6 +2241,9 @@ export class Map {
21902241
}
21912242
this._generic_sprites = {};
21922243

2244+
this.bgm_regions.forEach(region => region.sound_objects.forEach(obj => obj?.destroy()));
2245+
this.bgm_regions = [];
2246+
21932247
TileEvent.reset();
21942248
GameEvent.reset();
21952249

0 commit comments

Comments
 (0)