82 lines
2.7 KiB
TypeScript
Raw Normal View History

2025-10-29 13:27:41 +04:00
// Learn TypeScript:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
const {ccclass, property} = cc._decorator;
@ccclass
export default class GameData extends cc.Component {
private static _instance: GameData = null;
public selectedLevel: number = 1;
2025-11-06 10:59:34 +04:00
public score: number = 0;
2025-10-29 13:27:41 +04:00
2025-11-06 16:02:53 +04:00
public soundEnabled: boolean = true;
public musicEnabled: boolean = true;
2025-11-07 15:52:33 +04:00
public selectedShip: number = 1;
2025-10-29 13:27:41 +04:00
public static get instance(): GameData {
if (!this._instance) {
this._instance = new GameData();
2025-11-07 14:28:59 +04:00
this._instance.soundEnabled = cc.sys.localStorage.getItem('soundEnabled') !== 'false';
this._instance.musicEnabled = cc.sys.localStorage.getItem('musicEnabled') !== 'false';
2025-11-07 15:52:33 +04:00
this._instance.selectedShip = Number(cc.sys.localStorage.getItem('selectedShip')) || 1;
2025-10-29 13:27:41 +04:00
}
return this._instance;
}
2025-11-07 15:52:33 +04:00
public setSelectedShip(ship: number) {
this.selectedShip = ship;
cc.sys.localStorage.setItem('selectedShip', ship.toString());
}
2025-11-06 16:02:53 +04:00
public setSoundEnabled(enabled: boolean) {
this.soundEnabled = enabled;
2025-11-07 14:28:59 +04:00
cc.sys.localStorage.setItem('soundEnabled', enabled.toString());
2025-11-06 16:02:53 +04:00
}
public setMusicEnabled(enabled: boolean) {
this.musicEnabled = enabled;
2025-11-07 14:28:59 +04:00
cc.sys.localStorage.setItem('musicEnabled', enabled.toString());
2025-11-06 16:02:53 +04:00
}
2025-11-06 10:59:34 +04:00
public setScore(score: number) {
this.score = score;
}
public getScore(): number {
return this.score;
}
2025-10-30 15:03:57 +04:00
public get enemyHealth(): number {
return this.selectedLevel;
2025-10-29 13:27:41 +04:00
}
2025-10-30 15:03:57 +04:00
private levelConfig = {
2025-11-06 10:59:34 +04:00
1: { shootingSpeed: 6, spawnInterval: 6, playerSpeed: 2.5 },
2: { shootingSpeed: 5, spawnInterval: 8, playerSpeed: 3 },
3: { shootingSpeed: 4, spawnInterval: 10, playerSpeed: 3.5 },
4: { shootingSpeed: 3, spawnInterval: 12, playerSpeed: 4 },
2025-10-30 15:03:57 +04:00
};
public get playerShootingInterval(): number {
return this.levelConfig[this.selectedLevel].shootingSpeed;
2025-10-29 13:27:41 +04:00
}
2025-10-30 15:03:57 +04:00
public get enemySpawnInterval(): number {
return this.levelConfig[this.selectedLevel].spawnInterval;
2025-10-29 13:27:41 +04:00
}
2025-10-30 15:03:57 +04:00
public get enemyMoveTime(): number {
return this.levelConfig[this.selectedLevel].playerSpeed;
2025-10-29 13:27:41 +04:00
}
}