初始版本可以去申请电子版权和软著了

This commit is contained in:
panfudan
2025-08-21 13:54:28 +08:00
parent 0a654d130a
commit 1b56cb7a8c
12 changed files with 1816 additions and 185 deletions

View File

@@ -9,6 +9,8 @@ import { gameDataSyncManager } from "./GameDataSyncManager";
import { GameSet } from "./config/BoxSet";
import { Test } from "./Test";
import { GameEvent } from "./config/GameEvent";
import { Items } from "./config/Items";
import { HeroInfo } from "./config/heroSet";
// import { Role } from "../role/Role";
@@ -94,6 +96,8 @@ export class SingletonModuleComp extends ecs.Comp {
return typeof wx !== 'undefined' && typeof (wx as any).getSystemInfoSync === 'function';
}
//调试用
syncDataFromLocal(){
if(this.isWxClient()) return
@@ -101,6 +105,18 @@ export class SingletonModuleComp extends ecs.Comp {
this.gameDataSyncManager.overrideLocalDataWithRemote(loginResult, "本地调试");
}
addHero(hero_uuid:number,autoSave:boolean=true){
if(this.isWxClient()){
if(this.gameDataSyncManager.addHero(hero_uuid)){
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
return true
}
return false
}
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
return true
}
setFightHero(position:number,heroId:number,autoSave:boolean=true){
this.fight_heros[position] = heroId;
if(this.isWxClient()){
@@ -121,9 +137,11 @@ export class SingletonModuleComp extends ecs.Comp {
}
return heros_uuid
}
levelUpHero(heroId:number,exp:number,gold:number){
levelUpHero(heroId:number){
if(this.isWxClient()){
let result=this.gameDataSyncManager.levelUpHero(heroId,exp,gold);
let result=this.gameDataSyncManager.levelUpHero(heroId);
if(result){
this.heros[heroId].lv++;
return true
@@ -136,98 +154,112 @@ export class SingletonModuleComp extends ecs.Comp {
}
}
// ==================== 统一的数据操作接口 ====================
/**
* 增加游戏数据属性(统一接口)
* @param property 属性名
* property list:
* ***gold:金币
* ***diamond:钻石
* ***meat:肉
* ***exp:经验
* ***score:分数
* ***mission:关卡
* @param value 增加的值
* @param autoSave 是否自动保存 (默认true)
* @returns 操作结果
*/
error(){
oops.gui.toast("数据处理异常,请重试或重新登录")
}
addExp(exp:number,autoSave:boolean=true){
this.data.exp+=exp
if(this.isWxClient()){
this.gameDataSyncManager.addGameProperty("exp",exp)
if(this.gameDataSyncManager.addGameProperty("exp",exp)){
this.data.exp+=exp
return true
}
return false
}
this.data.exp+=exp
return true
}
addGold(gold:number,autoSave:boolean=true){
if(this.isWxClient()){
if(this.gameDataSyncManager.addGameProperty("gold",gold)){
this.data.gold+=gold
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
}
this.error()
return false
}
this.data.gold+=gold
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
if(this.isWxClient()){
this.gameDataSyncManager.addGameProperty("gold",gold)
}
return true
}
addDiamond(diamond:number,autoSave:boolean=true){
if(this.isWxClient()){
if(this.gameDataSyncManager.addGameProperty("diamond",diamond)){
this.data.diamond+=diamond
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
return true
}
return false
}
this.data.diamond+=diamond
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
if(this.isWxClient()){
this.gameDataSyncManager.addGameProperty("diamond",diamond)
}
return true
}
addMission(mission:number,autoSave:boolean=true){
if(this.isWxClient()){
if(this.gameDataSyncManager.addGameProperty("mission",mission)){
this.data.mission+=mission
oops.message.dispatchEvent(GameEvent.MISSION_UPDATE)
return true
}
return false
}
this.data.mission+=mission
oops.message.dispatchEvent(GameEvent.MISSION_UPDATE)
if(this.isWxClient()){
this.gameDataSyncManager.addGameProperty("mission",mission)
}
return true
}
spendExp(exp:number,autoSave:boolean=true){
this.data.exp-=exp
if(this.isWxClient()){
this.gameDataSyncManager.spendGameProperty("exp",exp)
if(this.gameDataSyncManager.spendGameProperty("exp",exp)){
this.data.exp-=exp
return true
}
return false
}
this.data.exp-=exp
return true
}
spendGold(gold:number,autoSave:boolean=true){
if(this.isWxClient()){
if(this.gameDataSyncManager.spendGameProperty("gold",gold)){
this.data.gold-=gold
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
}
return false
}
this.data.gold-=gold
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
if(this.isWxClient()){
this.gameDataSyncManager.spendGameProperty("gold",gold)
}
return true
}
spendDiamond(diamond:number,autoSave:boolean=true){
if(this.isWxClient()){
if(this.gameDataSyncManager.spendGameProperty("diamond",diamond)){
this.data.diamond-=diamond
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
return true
}
return false
}
this.data.diamond-=diamond
oops.message.dispatchEvent(GameEvent.DIAMOND_UPDATE)
if(this.isWxClient()){
this.gameDataSyncManager.spendGameProperty("diamond",diamond)
}
return true
}
/**
* 消耗游戏数据属性(统一接口)
* - 支持单个字段spendGameProperty('gold', 10)
* - 支持多个字段spendGameProperty({ gold: 10, exp: 5 })
* 只有当所有字段都满足扣除条件时,才会一次性扣减
* @param property 属性名或属性映射
* @param value 消耗的值(当 property 为字符串时有效)
* @param autoSave 是否自动保存 (默认true)
* @returns 是否成功消耗
* 处理多个字段spendGameProperty({ gold: 10, exp: 5 })
*/
async spendGameProperty(property: string | Record<string, number>, value: any = undefined, autoSave: boolean = true): Promise<boolean> {
// 单字段扣除
if (typeof property === 'string') {
const currentValue = this.data[property] || 0;
if (currentValue < value) {
console.warn(`[SMC]: ${property} 不足,当前: ${currentValue}, 需要: ${value}`);
return false;
async spendGameProperty(property: Record<string, number>, autoSave: boolean = true): Promise<boolean> {
if(this.isWxClient()){
if(this.gameDataSyncManager.spendGameProperty(property)){
return true
}
const newValue = currentValue - value;
this.data[property] = newValue;
console.log(`[SMC]: 消耗游戏数据 ${property} = ${value}, 当前值: ${newValue}`);
return true;
return false
}
// 多字段扣除(原子性:全部满足才扣)
const deductions = property as Record<string, number>;
// 1) 校验是否全部满足
@@ -237,10 +269,10 @@ export class SingletonModuleComp extends ecs.Comp {
const current = this.data[key] || 0;
if (current < need) {
console.warn(`[SMC]: ${key} 不足,当前: ${current}, 需要: ${need}`);
oops.gui.toast(`${key} 不足,当前: ${current}, 需要: ${need}`)
return false;
}
}
// 2) 统一扣减
for (const key in deductions) {
if (!Object.prototype.hasOwnProperty.call(deductions, key)) continue;
@@ -250,18 +282,32 @@ export class SingletonModuleComp extends ecs.Comp {
this.data[key] = next;
console.log(`[SMC]: 消耗游戏数据 ${key} = ${need}, 当前值: ${next}`);
}
return true;
}
addItem(item_uuid:number,count:number,autoSave:boolean=true){
if(this.isWxClient()){
this.gameDataSyncManager.addItem(item_uuid,count);
}
else{
this.items[item_uuid] = (this.items[item_uuid] || 0) + count;
if(this.gameDataSyncManager.addItem(item_uuid,count)){
this.items[item_uuid] = (this.items[item_uuid] || 0) + count;
return true
}
return false
}
this.items[item_uuid] = (this.items[item_uuid] || 0) + count;
return true
}
spendItem(item_uuid:number,count:number,autoSave:boolean=true){
if(this.isWxClient()){
if(this.gameDataSyncManager.consumeItem(item_uuid,count)){
this.items[item_uuid] = (this.items[item_uuid] || 0) - count;
return true
}
return false
}
this.items[item_uuid] = (this.items[item_uuid] || 0) - count;
return true
}
}