73 lines
2.2 KiB
TypeScript
Raw Normal View History

2022-12-22 13:26:43 +01:00
import { Component, director, instantiate, JsonAsset, Prefab, _decorator } from "cc";
2022-12-16 12:01:25 +01:00
import { GameSettings } from "../Game/Data/GameSettings";
2022-12-22 13:26:43 +01:00
import { GameAssets } from "../Game/Data/Assets/GameAssets";
2022-12-16 12:01:25 +01:00
import { TranslationData } from "../Game/Data/TranslationData";
2022-12-21 14:08:49 +01:00
import { UserData } from "../Game/Data/UserData";
2022-12-20 10:00:47 +01:00
import { AudioPlayer } from "../Services/AudioPlayer/AudioPlayer";
2022-12-07 10:47:46 +01:00
import { SaveSystem } from "./SaveSystem";
const { ccclass, property } = _decorator;
@ccclass("AppRoot")
export class AppRoot extends Component {
2022-12-20 10:00:47 +01:00
@property(AudioPlayer) private audio: AudioPlayer;
2022-12-16 12:01:25 +01:00
@property(JsonAsset) private settingsAsset: JsonAsset;
@property(JsonAsset) private engTranslationAsset: JsonAsset;
2022-12-22 13:26:43 +01:00
@property(Prefab) private gameAssetsPrefab: Prefab;
2022-12-07 10:47:46 +01:00
private static instance: AppRoot;
private saveSystem: SaveSystem;
2022-12-21 14:08:49 +01:00
private liveUserData: UserData;
2022-12-22 13:26:43 +01:00
private gameAssets: GameAssets;
2022-12-21 14:08:49 +01:00
2022-12-07 10:47:46 +01:00
public static get Instance(): AppRoot {
return this.instance;
}
2022-12-20 10:00:47 +01:00
public get AudioPlayer(): AudioPlayer {
return this.audio;
}
2022-12-22 13:26:43 +01:00
public get GameAssets(): GameAssets {
return this.gameAssets;
}
2022-12-21 14:08:49 +01:00
public get LiveUserData(): UserData {
return this.liveUserData;
2022-12-07 10:47:46 +01:00
}
2022-12-16 12:01:25 +01:00
public get Settings(): GameSettings {
return <GameSettings>this.settingsAsset.json;
}
public get TranslationData(): TranslationData {
return <TranslationData>this.engTranslationAsset.json;
}
2022-12-21 14:08:49 +01:00
public saveUserData(): void {
this.saveSystem.save(this.liveUserData);
}
2022-12-07 10:47:46 +01:00
public start(): void {
if (AppRoot.Instance == null) {
AppRoot.instance = this;
director.addPersistRootNode(this.node);
this.init();
} else {
this.destroy();
}
}
private init(): void {
this.saveSystem = new SaveSystem();
2022-12-21 14:08:49 +01:00
this.liveUserData = this.saveSystem.load();
2022-12-22 13:26:43 +01:00
const gameAssetsNode = instantiate(this.gameAssetsPrefab);
gameAssetsNode.setParent(this.node);
this.gameAssets = gameAssetsNode.getComponent(GameAssets);
2022-12-22 14:05:31 +01:00
this.gameAssets.init();
2022-12-22 13:26:43 +01:00
2022-12-21 14:08:49 +01:00
this.audio.init(this.LiveUserData.soundVolume, this.LiveUserData.musicVolume);
2022-12-07 10:47:46 +01:00
}
}