Slash-The-Hordes/assets/Scripts/AppRoot/AppRoot.ts

113 lines
3.4 KiB
TypeScript
Raw Normal View History

2023-01-02 09:56:41 +00:00
import { Camera, Component, director, instantiate, JsonAsset, Prefab, _decorator } from "cc";
2022-12-16 11:01:25 +00:00
import { GameSettings } from "../Game/Data/GameSettings";
2022-12-22 12:26:43 +00:00
import { GameAssets } from "../Game/Data/Assets/GameAssets";
2022-12-16 11:01:25 +00:00
import { TranslationData } from "../Game/Data/TranslationData";
2022-12-21 13:08:49 +00:00
import { UserData } from "../Game/Data/UserData";
2022-12-20 09:00:47 +00:00
import { AudioPlayer } from "../Services/AudioPlayer/AudioPlayer";
2022-12-07 09:47:46 +00:00
import { SaveSystem } from "./SaveSystem";
2023-01-02 09:56:41 +00:00
import { ModalWindowManager } from "../Services/ModalWindowSystem/ModalWindowManager";
import { OpenCloseAnimator } from "../Utils/OpenCloseAnimator";
2023-01-12 09:12:59 +00:00
import { Y8 } from "../../Plugins/Y8/Scripts/Y8";
import { Analytics } from "./Analytics";
2022-12-07 09:47:46 +00:00
const { ccclass, property } = _decorator;
@ccclass("AppRoot")
export class AppRoot extends Component {
2022-12-20 09:00:47 +00:00
@property(AudioPlayer) private audio: AudioPlayer;
2022-12-16 11:01:25 +00:00
@property(JsonAsset) private settingsAsset: JsonAsset;
@property(JsonAsset) private engTranslationAsset: JsonAsset;
2022-12-22 12:26:43 +00:00
@property(Prefab) private gameAssetsPrefab: Prefab;
2023-01-02 09:56:41 +00:00
@property(Camera) private mainCamera: Camera;
@property(ModalWindowManager) private modalWindowManager: ModalWindowManager;
@property(OpenCloseAnimator) private screenFader: OpenCloseAnimator;
2023-01-12 09:12:59 +00:00
@property(Y8) private y8: Y8;
2022-12-07 09:47:46 +00:00
private static instance: AppRoot;
private saveSystem: SaveSystem;
2022-12-21 13:08:49 +00:00
private liveUserData: UserData;
2022-12-22 12:26:43 +00:00
private gameAssets: GameAssets;
2023-01-12 09:12:59 +00:00
private analytics: Analytics;
2022-12-21 13:08:49 +00:00
2022-12-07 09:47:46 +00:00
public static get Instance(): AppRoot {
return this.instance;
}
2022-12-20 09:00:47 +00:00
public get AudioPlayer(): AudioPlayer {
return this.audio;
}
2022-12-22 12:26:43 +00:00
public get GameAssets(): GameAssets {
return this.gameAssets;
}
2022-12-21 13:08:49 +00:00
public get LiveUserData(): UserData {
return this.liveUserData;
2022-12-07 09:47:46 +00:00
}
2022-12-16 11:01:25 +00:00
public get Settings(): GameSettings {
return <GameSettings>this.settingsAsset.json;
}
public get TranslationData(): TranslationData {
return <TranslationData>this.engTranslationAsset.json;
}
2023-01-02 09:56:41 +00:00
public get ModalWindowManager(): ModalWindowManager {
return this.modalWindowManager;
}
public get MainCamera(): Camera {
return this.mainCamera;
}
public get ScreenFader(): OpenCloseAnimator {
return this.screenFader;
}
2023-01-12 09:12:59 +00:00
public get Y8(): Y8 {
return this.y8;
}
public get Analytics(): Analytics {
return this.analytics;
}
2022-12-21 13:08:49 +00:00
public saveUserData(): void {
this.saveSystem.save(this.liveUserData);
}
2022-12-07 09:47:46 +00:00
public start(): void {
if (AppRoot.Instance == null) {
AppRoot.instance = this;
director.addPersistRootNode(this.node);
this.init();
} else {
2023-01-02 09:56:41 +00:00
this.node.destroy();
2022-12-07 09:47:46 +00:00
}
}
2023-01-12 09:12:59 +00:00
public update(deltaTime: number): void {
if (this.analytics) this.analytics.update(deltaTime);
}
private async init(): Promise<void> {
2022-12-07 09:47:46 +00:00
this.saveSystem = new SaveSystem();
2022-12-21 13:08:49 +00:00
this.liveUserData = this.saveSystem.load();
2022-12-22 12:26:43 +00:00
const gameAssetsNode = instantiate(this.gameAssetsPrefab);
gameAssetsNode.setParent(this.node);
this.gameAssets = gameAssetsNode.getComponent(GameAssets);
2022-12-22 13:05:31 +00:00
this.gameAssets.init();
2022-12-22 12:26:43 +00:00
2022-12-21 13:08:49 +00:00
this.audio.init(this.LiveUserData.soundVolume, this.LiveUserData.musicVolume);
2023-01-02 09:56:41 +00:00
this.screenFader.init();
this.screenFader.node.active = false;
2023-01-12 09:12:59 +00:00
await this.y8.init();
this.analytics = new Analytics(this.y8);
2022-12-07 09:47:46 +00:00
}
}