Audio player

This commit is contained in:
Martin 2022-12-20 10:00:47 +01:00
parent 8512aa898d
commit 2105682fd9
14 changed files with 713 additions and 270 deletions

12
assets/Media/Audio.meta Normal file
View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "ce9925d5-4409-4530-84ab-7e0cec7a7747",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "c6c86a60-ac9b-449a-ba87-c7066c0a9f98",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -17,15 +17,19 @@
"_objFlags": 0, "_objFlags": 0,
"__editorExtras__": {}, "__editorExtras__": {},
"_parent": null, "_parent": null,
"_children": [], "_children": [
"_active": true,
"_components": [
{ {
"__id__": 2 "__id__": 2
} }
], ],
"_active": true,
"_components": [
{
"__id__": 14
}
],
"_prefab": { "_prefab": {
"__id__": 4 "__id__": 16
}, },
"_lpos": { "_lpos": {
"__type__": "cc.Vec3", "__type__": "cc.Vec3",
@ -55,6 +59,243 @@
}, },
"_id": "" "_id": ""
}, },
{
"__type__": "cc.Node",
"_name": "AudioPlayer",
"_objFlags": 0,
"_parent": {
"__id__": 1
},
"_children": [
{
"__id__": 3
},
{
"__id__": 7
}
],
"_active": true,
"_components": [
{
"__id__": 11
}
],
"_prefab": {
"__id__": 13
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 1073741824,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.Node",
"_name": "SoundSource",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 4
}
],
"_prefab": {
"__id__": 6
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 1073741824,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.AudioSource",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 3
},
"_enabled": true,
"__prefab": {
"__id__": 5
},
"_clip": null,
"_loop": false,
"_playOnAwake": false,
"_volume": 1,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "77UXuJEIdANaHvlgcFuOrx"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "1aDYMPapRFMLNmIBbTyeB7"
},
{
"__type__": "cc.Node",
"_name": "MusicSource",
"_objFlags": 0,
"_parent": {
"__id__": 2
},
"_children": [],
"_active": true,
"_components": [
{
"__id__": 8
}
],
"_prefab": {
"__id__": 10
},
"_lpos": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_lrot": {
"__type__": "cc.Quat",
"x": 0,
"y": 0,
"z": 0,
"w": 1
},
"_lscale": {
"__type__": "cc.Vec3",
"x": 1,
"y": 1,
"z": 1
},
"_layer": 1073741824,
"_euler": {
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 0
},
"_id": ""
},
{
"__type__": "cc.AudioSource",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 7
},
"_enabled": true,
"__prefab": {
"__id__": 9
},
"_clip": null,
"_loop": true,
"_playOnAwake": false,
"_volume": 1,
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "833YOi7/5NN4grS+PV/a6t"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "cafhYAziVHcLDM7ubrqoeN"
},
{
"__type__": "958e24+jRZMdr5ue2HNRuRM",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 2
},
"_enabled": true,
"__prefab": {
"__id__": 12
},
"soundSource": {
"__id__": 4
},
"musicSource": {
"__id__": 8
},
"_id": ""
},
{
"__type__": "cc.CompPrefabInfo",
"fileId": "16TD0T+m1B6qqSPgiaqN/U"
},
{
"__type__": "cc.PrefabInfo",
"root": {
"__id__": 1
},
"asset": {
"__id__": 0
},
"fileId": "5eL70HILtD6oNB7UetCf7j"
},
{ {
"__type__": "4943dSvt9FBvLizJ1HhFOzg", "__type__": "4943dSvt9FBvLizJ1HhFOzg",
"_name": "", "_name": "",
@ -64,7 +305,10 @@
}, },
"_enabled": true, "_enabled": true,
"__prefab": { "__prefab": {
"__id__": 3 "__id__": 15
},
"audio": {
"__id__": 11
}, },
"settingsAsset": { "settingsAsset": {
"__uuid__": "4a156ee8-535e-4443-8bf5-8b979a08d232", "__uuid__": "4a156ee8-535e-4443-8bf5-8b979a08d232",

File diff suppressed because it is too large Load Diff

View File

@ -1,13 +1,13 @@
import { _decorator, Component, Node, director, AudioSource, JsonAsset } from "cc"; import { Component, director, JsonAsset, _decorator } from "cc";
import { GameSettings } from "../Game/Data/GameSettings"; import { GameSettings } from "../Game/Data/GameSettings";
import { TranslationData } from "../Game/Data/TranslationData"; import { TranslationData } from "../Game/Data/TranslationData";
import { AudioPlayer } from "../Services/AudioPlayer/AudioPlayer";
import { SaveSystem } from "./SaveSystem"; import { SaveSystem } from "./SaveSystem";
const { ccclass, property } = _decorator; const { ccclass, property } = _decorator;
@ccclass("AppRoot") @ccclass("AppRoot")
export class AppRoot extends Component { export class AppRoot extends Component {
@property(AudioSource) private soundSource: AudioSource; @property(AudioPlayer) private audio: AudioPlayer;
@property(AudioSource) private musicSource: AudioSource;
@property(JsonAsset) private settingsAsset: JsonAsset; @property(JsonAsset) private settingsAsset: JsonAsset;
@property(JsonAsset) private engTranslationAsset: JsonAsset; @property(JsonAsset) private engTranslationAsset: JsonAsset;
@ -18,6 +18,10 @@ export class AppRoot extends Component {
return this.instance; return this.instance;
} }
public get AudioPlayer(): AudioPlayer {
return this.audio;
}
public get SaveSystem(): SaveSystem { public get SaveSystem(): SaveSystem {
return this.saveSystem; return this.saveSystem;
} }
@ -42,5 +46,6 @@ export class AppRoot extends Component {
private init(): void { private init(): void {
this.saveSystem = new SaveSystem(); this.saveSystem = new SaveSystem();
this.audio.init(1, 1);
} }
} }

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "263ab535-5757-4b2b-adc1-04823e77e433",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,32 @@
import { _decorator, Component, Node, AudioClip } from "cc";
import { AppRoot } from "../../AppRoot/AppRoot";
import { AudioPlayer } from "../../Services/AudioPlayer/AudioPlayer";
import { Enemy } from "../Unit/Enemy/Enemy";
import { EnemyManager } from "../Unit/Enemy/EnemyManager";
const { ccclass, property } = _decorator;
@ccclass("GameAudioAdapter")
export class GameAudioAdapter extends Component {
@property(AudioClip) private enemyHit: AudioClip;
private audioPlayer: AudioPlayer;
public init(enemyManager: EnemyManager): void {
this.audioPlayer = AppRoot.Instance.AudioPlayer;
enemyManager.EnemyAddedEvent.on(this.addEnemyListeners, this);
enemyManager.EnemyRemovedEvent.on(this.removeEnemyListeners, this);
}
private addEnemyListeners(enemy: Enemy): void {
enemy.Health.HealthPointsChangeEvent.on(this.playEnemyHitSound, this);
}
private removeEnemyListeners(enemy: Enemy): void {
enemy.Health.HealthPointsChangeEvent.off(this.playEnemyHitSound);
}
private playEnemyHitSound(): void {
this.audioPlayer.playSound(this.enemyHit);
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "eb84241d-4cc1-4e50-911d-c0c880ec024d",
"files": [],
"subMetas": {},
"userData": {}
}

View File

@ -2,6 +2,7 @@ import { Camera, Component, JsonAsset, KeyCode, Vec2, _decorator } from "cc";
import { runInThisContext } from "vm"; import { runInThisContext } from "vm";
import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager"; import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager";
import { delay } from "../Services/Utils/AsyncUtils"; import { delay } from "../Services/Utils/AsyncUtils";
import { GameAudioAdapter } from "./Audio/GameAudioAdapter";
import { Background } from "./Background/Background"; import { Background } from "./Background/Background";
import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem"; import { PlayerCollisionSystem } from "./Collision/PlayerCollisionSystem";
import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem"; import { PlayerProjectileCollisionSystem } from "./Collision/PlayerProjectileCollisionSystem";
@ -39,6 +40,7 @@ export class Game extends Component {
@property(GameUI) private gameUI: GameUI; @property(GameUI) private gameUI: GameUI;
@property(Background) private background: Background; @property(Background) private background: Background;
@property(ModalWindowManager) private modalWindowManager: ModalWindowManager; @property(ModalWindowManager) private modalWindowManager: ModalWindowManager;
@property(GameAudioAdapter) private gameAudioAdapter: GameAudioAdapter;
private playerCollisionSystem: PlayerCollisionSystem; private playerCollisionSystem: PlayerCollisionSystem;
private haloProjectileLauncher: HaloProjectileLauncher; private haloProjectileLauncher: HaloProjectileLauncher;
@ -127,6 +129,7 @@ export class Game extends Component {
this.player.Level.addXp(testValues.startXP); this.player.Level.addXp(testValues.startXP);
} }
this.gameAudioAdapter.init(this.enemyManager);
this.gamePauser.resume(); this.gamePauser.resume();
while (this.player.Health.IsAlive) await delay(100); while (this.player.Health.IsAlive) await delay(100);

View File

@ -1,5 +1,5 @@
import { Component, Node, random, randomRange, Vec3, _decorator } from "cc"; import { Component, Node, random, randomRange, Vec3, _decorator } from "cc";
import { ISignal } from "../../../Services/EventSystem/ISignal";
import { EnemyManagerSettings } from "../../Data/GameSettings"; import { EnemyManagerSettings } from "../../Data/GameSettings";
import { GoldSpawner } from "../../Gold/GoldSpawner"; import { GoldSpawner } from "../../Gold/GoldSpawner";
import { XPSpawner } from "../../XP/XPSpawner"; import { XPSpawner } from "../../XP/XPSpawner";
@ -30,7 +30,7 @@ export class EnemyManager extends Component {
public init(targetNode: Node, settings: EnemyManagerSettings): void { public init(targetNode: Node, settings: EnemyManagerSettings): void {
this.enemySpawner.init(targetNode, settings.enemies); this.enemySpawner.init(targetNode, settings.enemies);
this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this); this.enemySpawner.EnemyAddedEvent.on(this.onEnemyAdded, this);
this.enemySpawner.enemyRemovedEvent.on(this.onRemoveEnemy, this); this.enemySpawner.EnemyRemovedEvent.on(this.onEnemyRemoved, this);
for (const individualSpawnerSettings of settings.individualEnemySpawners) { for (const individualSpawnerSettings of settings.individualEnemySpawners) {
const individualSpawner = new IndividualEnemySpawner(this.enemySpawner, individualSpawnerSettings); const individualSpawner = new IndividualEnemySpawner(this.enemySpawner, individualSpawnerSettings);
@ -65,10 +65,25 @@ export class EnemyManager extends Component {
} }
} }
private onEnemyDied(enemy: Enemy): void { public get EnemyAddedEvent(): ISignal<Enemy> {
enemy.DeathEvent.off(this.onEnemyDied); return this.enemySpawner.EnemyAddedEvent;
enemy.LifetimeEndedEvent.off(this.onEnemyLifetimeEnded); }
public get EnemyRemovedEvent(): ISignal<Enemy> {
return this.enemySpawner.EnemyRemovedEvent;
}
private onEnemyAdded(enemy: Enemy): void {
enemy.DeathEvent.on(this.onEnemyDied, this);
this.getEnemyMover(enemy).addEnemy(enemy);
}
private onEnemyRemoved(enemy: Enemy): void {
enemy.DeathEvent.off(this.onEnemyDied);
this.getEnemyMover(enemy).removeEnemy(enemy);
}
private onEnemyDied(enemy: Enemy): void {
for (let index = 0; index < enemy.XPReward; index++) { for (let index = 0; index < enemy.XPReward; index++) {
const position: Vec3 = enemy.node.worldPosition; const position: Vec3 = enemy.node.worldPosition;
position.x += randomRange(-10, 10); position.x += randomRange(-10, 10);
@ -92,22 +107,6 @@ export class EnemyManager extends Component {
} }
} }
private onEnemyLifetimeEnded(enemy: Enemy): void {
enemy.DeathEvent.off(this.onEnemyDied);
enemy.LifetimeEndedEvent.off(this.onEnemyLifetimeEnded);
}
private onEnemyAdded(enemy: Enemy): void {
enemy.DeathEvent.on(this.onEnemyDied, this);
enemy.LifetimeEndedEvent.on(this.onEnemyLifetimeEnded, this);
this.getEnemyMover(enemy).addEnemy(enemy);
}
private onRemoveEnemy(enemy: Enemy): void {
this.getEnemyMover(enemy).removeEnemy(enemy);
}
private getEnemyMover(enemy: Enemy): EnemyMover { private getEnemyMover(enemy: Enemy): EnemyMover {
if (this.movementTypeToMover.has(enemy.MovementType)) { if (this.movementTypeToMover.has(enemy.MovementType)) {
return this.movementTypeToMover.get(enemy.MovementType); return this.movementTypeToMover.get(enemy.MovementType);

View File

@ -12,8 +12,8 @@ const { ccclass, property } = _decorator;
export class EnemySpawner extends Component { export class EnemySpawner extends Component {
@property(Prefab) private enemies: Prefab[] = []; @property(Prefab) private enemies: Prefab[] = [];
public enemyAddedEvent: Signal<Enemy> = new Signal<Enemy>(); private enemyAddedEvent: Signal<Enemy> = new Signal<Enemy>();
public enemyRemovedEvent: Signal<Enemy> = new Signal<Enemy>(); private enemyRemovedEvent: Signal<Enemy> = new Signal<Enemy>();
private enemyGraphicsTypeToPool = new Map<EnemyGraphicsType, ObjectPool<Enemy>>(); private enemyGraphicsTypeToPool = new Map<EnemyGraphicsType, ObjectPool<Enemy>>();
private targetNode: Node; private targetNode: Node;

View File

@ -0,0 +1,12 @@
{
"ver": "1.1.0",
"importer": "directory",
"imported": true,
"uuid": "d887156b-67db-40b0-ab80-680c7400ba2a",
"files": [],
"subMetas": {},
"userData": {
"compressionType": {},
"isRemoteBundle": {}
}
}

View File

@ -0,0 +1,30 @@
import { AudioClip, AudioSource, Component, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("AudioPlayer")
export class AudioPlayer extends Component {
@property(AudioSource) private soundSource: AudioSource;
@property(AudioSource) private musicSource: AudioSource;
public init(soundVolume: number, musicVolume: number): void {
this.setSoundVolume(soundVolume);
this.setMusicVolume(musicVolume);
}
public setSoundVolume(volume: number): void {
this.soundSource.volume = volume;
}
public setMusicVolume(volume: number): void {
this.musicSource.volume = volume;
}
public playSound(clip: AudioClip): void {
this.soundSource.playOneShot(clip);
}
public playMusic(clip: AudioClip): void {
this.musicSource.clip = clip;
this.musicSource.play();
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "4.0.23",
"importer": "typescript",
"imported": true,
"uuid": "958e2e3e-8d16-4c76-be6e-7b61cd46e44c",
"files": [],
"subMetas": {},
"userData": {}
}