import { _decorator, Component, Node, director, game } from 'cc'; import { UITheme } from './ui/common/UITheme'; import { CharacterData, RaceType, ProfessionType, RealmLevel, SubRealm } from './data/GameData'; const { ccclass, property } = _decorator; /** * 游戏主管理器 * 管理游戏状态、界面切换、数据流 */ @ccclass('GameManager') export class GameManager extends Component { private static _instance: GameManager = null!; static get instance(): GameManager { return this._instance; } @property(Node) mainUI: Node = null!; @property(Node) loadingScreen: Node = null!; // 当前角色数据 private _currentCharacter: CharacterData = null!; get currentCharacter(): CharacterData { return this._currentCharacter; } // 当前界面索引 private _currentScreen: number = 0; get currentScreen(): number { return this._currentScreen; } // 界面名称 private _screenNames: string[] = ['吐纳', '门派', '武学', '江湖', '游历']; onLoad() { if (GameManager._instance && GameManager._instance !== this) { this.node.destroy(); return; } GameManager._instance = this; director.addPersistRootNode(this.node); this.initGame(); } /** * 初始化游戏 */ private initGame() { // 创建默认角色数据 this._currentCharacter = this.createDefaultCharacter(); // 加载本地存储 this.loadLocalData(); console.log('GameManager initialized'); } /** * 创建默认角色 */ private createDefaultCharacter(): CharacterData { return { id: 'player_001', name: '无名侠客', race: RaceType.HUMAN, profession: ProfessionType.SWORD_CULTIVATOR, level: 1, realm: RealmLevel.QI_REFINING, subRealm: SubRealm.EARLY, // 基础属性 strength: 10, constitution: 10, wisdom: 10, speed: 10, agility: 10, luck: 10, // 资源 copper: 1000, spiritStones: 0, internalForce: 0, energy: 100, maxEnergy: 100, purity: 100, // 状态 hp: 100, maxHp: 100, san: 100, alignment: 0, factionReputation: 0, // 时间 createdAt: Date.now(), lastLoginAt: Date.now(), totalPlayTime: 0, }; } /** * 切换界面 */ switchScreen(screenIndex: number) { if (screenIndex < 0 || screenIndex >= this._screenNames.length) return; if (screenIndex === this._currentScreen) return; const oldScreen = this._currentScreen; this._currentScreen = screenIndex; // 通知UI更新 this.onScreenChanged(oldScreen, screenIndex); } /** * 界面切换回调 */ protected onScreenChanged(oldIndex: number, newIndex: number) { console.log(`Screen changed: ${this._screenNames[oldIndex]} -> ${this._screenNames[newIndex]}`); // 子类或外部监听器处理具体切换逻辑 } /** * 获取当前界面名称 */ getCurrentScreenName(): string { return this._screenNames[this._currentScreen]; } /** * 更新角色属性 */ updateCharacterStats(updates: Partial) { Object.assign(this._currentCharacter, updates); this.onCharacterUpdated(); } /** * 角色数据更新回调 */ protected onCharacterUpdated() { // 保存本地数据 this.saveLocalData(); } /** * 添加铜钱 */ addCopper(amount: number) { this._currentCharacter.copper += amount; this.onCharacterUpdated(); } /** * 消耗能量 */ consumeEnergy(amount: number): boolean { if (this._currentCharacter.energy < amount) { return false; } this._currentCharacter.energy -= amount; this.onCharacterUpdated(); return true; } /** * 恢复能量 */ restoreEnergy(amount: number) { this._currentCharacter.energy = Math.min( this._currentCharacter.energy + amount, this._currentCharacter.maxEnergy ); this.onCharacterUpdated(); } /** * 保存本地数据 */ private saveLocalData() { try { const data = JSON.stringify(this._currentCharacter); localStorage.setItem('character_data', data); } catch (e) { console.error('Failed to save character data:', e); } } /** * 加载本地数据 */ private loadLocalData() { try { const data = localStorage.getItem('character_data'); if (data) { this._currentCharacter = JSON.parse(data); this._currentCharacter.lastLoginAt = Date.now(); } } catch (e) { console.error('Failed to load character data:', e); } } /** * 获取境界名称 */ getRealmName(realm: RealmLevel): string { const names: { [key: number]: string } = { [RealmLevel.QI_REFINING]: '炼气期', [RealmLevel.FOUNDATION]: '筑基期', [RealmLevel.GOLDEN_CORE]: '金丹期', [RealmLevel.NASCENT_SOUL]: '元婴期', [RealmLevel.SPIRIT_SEVERING]: '化神期', [RealmLevel.BODY_INTEGRATION]: '合体期', [RealmLevel.MAHAYANA]: '大乘期', [RealmLevel.TRIBULATION]: '渡劫期', [RealmLevel.ASCENSION]: '飞升', }; return names[realm] || '未知'; } /** * 获取种族名称 */ getRaceName(race: RaceType): string { const names: { [key: string]: string } = { [RaceType.HUMAN]: '人族', [RaceType.ELF]: '精灵', [RaceType.DWARF]: '矮人', [RaceType.ORC]: '兽人', [RaceType.GOBLIN]: '地精', [RaceType.DRAGONBORN]: '龙裔', [RaceType.UNDEAD]: '亡灵', [RaceType.DEMON]: '魔族', [RaceType.ANGEL]: '神族', [RaceType.TITAN]: '巨人', [RaceType.WEREWOLF]: '狼人', [RaceType.VAMPIRE]: '吸血鬼', [RaceType.NEPHILIM]: '深潜裔', [RaceType.CELESTIAL]: '天人', [RaceType.SPIRIT]: '精魂', [RaceType.FLAME]: '火裔', [RaceType.FROST]: '冰裔', [RaceType.THUNDER]: '雷裔', [RaceType.EARTH]: '土裔', }; return names[race] || '未知'; } onDestroy() { if (GameManager._instance === this) { GameManager._instance = null!; } } }