重构了云函数

This commit is contained in:
2025-10-19 20:10:19 +08:00
parent cfb6819bc7
commit 1c40c10210
1942 changed files with 363888 additions and 435 deletions

View File

@@ -3,10 +3,22 @@ import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ec
import { Initialize } from "../initialize/Initialize";
import { GameMap } from "../map/GameMap";
import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/Oops";
import { WxCloudApi, UserGameData } from "../wx_clound_client_api/WxCloudApi";
import { WxCloudApi } from "../wx_clound_client_api/WxCloudApi";
import { Test } from "./Test";
import { GameEvent } from "./config/GameEvent";
/**
* 用远程数据覆盖本地数据(统一方法)
* @param remoteData 远程数据(云端或本地调试)
*/
interface GameDate{
gold:number,
heros:any,
fight_hero:number
}
interface CloudData {
openid: string;
data: GameDate;
}
/** 游戏模块 */
@ecs.register('SingletonModule')
export class SingletonModuleComp extends ecs.Comp {
@@ -14,6 +26,7 @@ export class SingletonModuleComp extends ecs.Comp {
initialize: Initialize = null!;
/** 游戏地图 */
map: GameMap = null!;
openid:string=''
mission:any={
status:0, //0:未开始 1:进行中 2:胜利 3:失败
play:false,
@@ -32,10 +45,7 @@ export class SingletonModuleComp extends ecs.Comp {
task:0,
}
fight_hero: number = 5001; // 单个出战英雄
heros:any = {
5001:{uuid:5001,lv:1},
5005:{uuid:5005,lv:1},
};
heros:any= [5001,5002]
monsters:any = [];
vmdata: any = {
game_over:false,
@@ -76,148 +86,105 @@ export class SingletonModuleComp extends ecs.Comp {
//存储到远程服务器 后续再添加
}
//调试用
syncDataFromLocal(){
if(this.isWxClient()) return
const loginResult = new Test().load_data_from_local()
this.overrideLocalDataWithRemote(loginResult, "本地调试");
updateCloudData(){
let gemeDate=this.getGameDate()
WxCloudApi.save(gemeDate).then((result) => {
if(result.result.code === 200) {
return true
} else {
console.warn(`[SMC]: 游戏数据增加失败: ${result.result.msg}`);
return false
}
}).catch((error) => {
console.error(`[SMC]: 增加游戏数据异常:`, error);
return false
});
return true
}
/**
* 用远程数据覆盖本地数据(统一方法)
* @param remoteData 远程数据(云端或本地调试)
* @param dataSource 数据源描述
*/
async overrideLocalDataWithRemote(remoteData: UserGameData, dataSource: string) {
getCloudData(){
WxCloudApi.get().then(async (result) => {
if(result.result.code === 200) {
let data=result.result.data
this.overrideLocalDataWithRemote(data)
return true
} else {
console.warn(`[SMC]: 游戏数据增加失败: ${result.result.msg}`);
return false
}
}).catch((error) => {
console.error(`[SMC]: 获取游戏数据异常:`, error);
});
}
public async overrideLocalDataWithRemote(CloudData) {
try {
// 直接覆盖基础游戏数据
if (remoteData.data) {
// 保留原有的data字段数据
Object.assign(this.data, remoteData.data);
// 同步gold到vmdata
if (remoteData.data.gold !== undefined) {
this.vmdata.gold = remoteData.data.gold;
}
if (CloudData.openid) {
this.openid=CloudData.openid
}
// 直接覆盖出战英雄配置
if (remoteData.fight_heros) {
this.fight_hero = remoteData.fight_heros[0] || this.fight_hero;
}
// 直接覆盖英雄数据
if (remoteData.heros) {
this.heros = { ...remoteData.heros };
if (CloudData.data) {
if(CloudData.data.gold) this.vmdata.gold=CloudData.data.gold
if(CloudData.data.heros) this.heros=CloudData.data.heros
if(CloudData.data.fight_hero) this.fight_hero=CloudData.data.fight_hero
}
} catch (error) {
console.error(`[SMC]: ${dataSource}数据覆盖失败:`, error);
console.error(`[SMC]: 数据覆盖失败:`, error);
}
}
addHero(hero_uuid:number,autoSave:boolean=true){
if(this.isWxClient()){
// 适配原有接口,保持与云函数的兼容性
const result = WxCloudApi.addHero(hero_uuid);
result.then((res) => {
if(res.result.code === 200) {
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
return true
}
return false
}).catch((error) => {
console.error(`[SMC]: 添加英雄异常:`, error);
this.error()
return false
});
getGameDate(){
return {gold:this.vmdata.gold,heros:this.heros,fight_hero:this.fight_hero}
}
addHero(hero_uuid:number){
if(this.heros.indexOf(hero_uuid)==-1){
this.heros.push(hero_uuid)
}
this.heros[hero_uuid]={ uuid:hero_uuid, lv:1, }
if(this.isWxClient()){
let res = this.updateCloudData()
if (res){
return true
}else{
// 同步不成功删除uuid
this.heros.splice(this.heros.indexOf(hero_uuid), 1);
oops.gui.toast("数据同步失败,已回滚操作");
return false
}
}
return true
}
// 设置单个出战英雄
setFightHero(heroId: number, autoSave: boolean = true) {
updateFihgtHero(heroId: number) {
this.fight_hero = heroId;
if (this.isWxClient()) {
// 适配原有接口,保持与云函数的兼容性
WxCloudApi.updateFightHeros({ 0: heroId }).then((result) => {
if (result.result.code !== 200) {
console.warn(`[SMC]: 出战英雄配置更新失败: ${result.result.msg}`);
}
}).catch((error) => {
console.error(`[SMC]: 更新出战英雄配置异常:`, error);
this.error()
});
if(this.isWxClient()){
let res = this.updateCloudData()
if (res){
return true
}else{
return false
}
}
return true
}
// 获取出战英雄
getFightHero(): number {
return this.fight_hero;
}
getHasHeroUUID(){
let heros=this.heros
let heros_uuid=[]
for(let key in heros){
heros_uuid.push(heros[key].uuid)
updateGold(gold:number){
this.vmdata.gold += gold;
if(this.isWxClient()){
let res = this.updateCloudData()
if (res){
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
}else{
this.vmdata.gold -= gold
return false
}
}
return heros_uuid
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
}
error(){
oops.gui.toast("数据处理异常,请重试或重新登录")
}
addGold(gold:number,autoSave:boolean=true){
if(this.isWxClient()){
WxCloudApi.addGameDataField("gold",gold).then((result) => {
if(result.result.code === 200) {
this.vmdata.gold += gold;
this.data.gold = this.vmdata.gold; // 同步到data字段
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
} else {
console.warn(`[SMC]: 游戏数据增加失败: ${result.result.msg}`);
this.error()
return false
}
}).catch((error) => {
console.error(`[SMC]: 增加游戏数据异常:`, error);
this.error()
return false
});
}
this.vmdata.gold += gold;
this.data.gold = this.vmdata.gold; // 同步到data字段
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
}
spendGold(gold:number,autoSave:boolean=true){
if(this.isWxClient()){
WxCloudApi.spendGameDataField("gold",gold).then((result) => {
if(result.result.code === 200) {
this.vmdata.gold -= gold;
this.data.gold = this.vmdata.gold; // 同步到data字段
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
} else {
console.warn(`[SMC]: 游戏数据消耗失败: ${result.result.msg}`);
return false
}
}).catch((error) => {
console.error(`[SMC]: 消耗游戏数据异常:`, error);
this.error()
return false
});
}
this.vmdata.gold -= gold;
this.data.gold = this.vmdata.gold; // 同步到data字段
oops.message.dispatchEvent(GameEvent.GOLD_UPDATE)
return true
}
}

View File

@@ -135,12 +135,26 @@ export class Initialize extends ecs.Entity {
// 2. 登录并获取云端数据
const loginResult = await WxCloudApi.login();
const response = loginResult.result;
if (response.code === 200) {
if (loginResult.result.code === 200) {
console.log("[Initialize]: 云端登录成功");
const cloudData = response.data;
// 3. 用云端数据覆盖本地数据
const cloudData = loginResult.result.data;
try {
// 直接覆盖基础游戏数据
if (cloudData.openid) {
smc.openid=cloudData.openid
}
// 直接覆盖出战英雄配置
if (cloudData.game_data) {
let gameDate=cloudData.game_data
if( gameDate.gold ) smc.vmdata.gold=gameDate.gold
if( gameDate.heros ) smc.heros=gameDate.heros
if( gameDate.fight_hero ) smc.fight_hero=gameDate.fight_hero
}
} catch (error) {
console.error(`[SMC]: 数据覆盖失败:`, error);
}
} else {
console.warn("[Initialize]: 云端登录失败:", response.msg);
@@ -160,8 +174,7 @@ export class Initialize extends ecs.Entity {
private async loadFromLocalDebug() {
try {
// 使用本地调试API模拟云端接口
const loginResult = new Test().load_data_from_local()
// 用本地调试数据覆盖客户端数据
} catch (error) {

View File

@@ -9,8 +9,6 @@ const { ccclass, property } = _decorator;
export class topComp extends Component {
protected onLoad(): void {
oops.message.on(GameEvent.GOLD_UPDATE,this.onGoldUpdate,this);
oops.message.on(GameEvent.DIAMOND_UPDATE,this.onDiamondUpdate,this);
oops.message.on(GameEvent.MEAT_UPDATE,this.onMeatUpdate,this);
this.update_all()
}
start() {
@@ -23,26 +21,12 @@ export class topComp extends Component {
.to(0.1,{scale:v3(1,1,1)})
.start()
}
onDiamondUpdate(event:string,data:any){
}
onMeatUpdate(event:string,data:any){
}
update_gold(gold:number){
this.node.getChildByName("bar").getChildByName("gold").getChildByName("num").getComponent(Label).string=NumberFormatter.formatNumber(gold);
}
update_diamond(diamond:number){
}
update_meat(meat:number){
}
update_all(){
this.update_gold(smc.data.gold)
this.update_diamond(smc.data.diamond)
this.update_meat(smc.data.meat)
this.update_gold(smc.vmdata.gold)
}
update(deltaTime: number) {

View File

@@ -2,7 +2,7 @@ import { oops } from "../../../../extensions/oops-plugin-framework/assets/core/O
import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ecs/ECS";
import { BoxSet, FacSet } from "../common/config/BoxSet";
import { HType } from "../common/config/heroSet";
import { BuffAttr, SkillSet } from "../common/config/SkillSet";
import {Attrs, SkillSet } from "../common/config/SkillSet";
import { smc } from "../common/SingletonModuleComp";
import { HeroViewComp } from "../hero/HeroViewComp";
import { SkillCom } from "./SkillCom";
@@ -82,20 +82,7 @@ export class Skill extends ecs.Entity {
group: caster.box_group,
fac: caster.fac,
// 技能数值
ap: caster.Attrs[BuffAttr.AP],
caster_crit: caster.Attrs[BuffAttr.CRITICAL],
caster_crit_d: caster.Attrs[BuffAttr.CRITICAL_DMG],
puncture: caster.Attrs[BuffAttr.PUNCTURE],
puncture_damage: caster.Attrs[BuffAttr.PUNCTURE_DMG],
burn_count: caster.Attrs[BuffAttr.BURN_COUNT],
burn_value: caster.Attrs[BuffAttr.BURN_VALUE],
stun_time: caster.Attrs[BuffAttr.STUN_TIME],
stun_ratio: caster.Attrs[BuffAttr.STUN_RATIO],
frost_time: caster.Attrs[BuffAttr.FROST_TIME],
frost_ratio: caster.Attrs[BuffAttr.FROST_RATIO],
debuff_up: caster.Attrs[BuffAttr.DEBUFF_UP],
debuff_value: caster.Attrs[BuffAttr.DEBUFF_VALUE],
debuff_count: caster.Attrs[BuffAttr.DEBUFF_COUNT],
});
this.add(SComp);

View File

@@ -3,7 +3,7 @@ import { ecs } from "../../../../extensions/oops-plugin-framework/assets/libs/ec
import { CCComp } from "../../../../extensions/oops-plugin-framework/assets/module/common/CCComp";
import { smc } from "../common/SingletonModuleComp";
import { GameEvent } from "../common/config/GameEvent";
import { Attrs, AType, DTType, EType, SkillSet, SType, TGroup } from "../common/config/SkillSet";
import { Attrs, DTType, EType, SkillSet, SType, TGroup } from "../common/config/SkillSet";
import { BoxSet, FacSet } from "../common/config/BoxSet";
import { HeroViewComp } from "../hero/HeroViewComp";
import { BezierMove } from "../BezierMove/BezierMove";
@@ -85,50 +85,50 @@ export class SkillCom extends CCComp {
}
let bm=this.node.getComponent(BezierMove)
// //console.log(this.group +"技能 collider ",collider);
switch(this.skillConfig.AType){
case AType.parabolic:
this.node.angle +=10
// bm.speed=700
if(this.group==BoxSet.MONSTER) {bm.controlPointSide=-1 }
bm.rotationSmoothness=0.6
bm.moveTo(this.targetPos)
break;
case AType.linear:
let s_x=this.startPos.x
let s_y=this.startPos.y
let t_x=this.targetPos.x
let t_y=this.targetPos.y
// 设定目标x
this.targetPos.x = 400;
if(this.group == BoxSet.MONSTER) {
bm.controlPointSide = -1;
this.targetPos.x = -400;
}
// 计算斜率
const k = (t_y - s_y) / (t_x - s_x);
// 按直线公式计算新的y
this.targetPos.y = k * (this.targetPos.x - s_x) + s_y;
bm.controlPointOffset=0
bm.rotationSmoothness=0.6
bm.moveTo(this.targetPos);
break;
case AType.StartEnd:
// 2段位移先升高然后移到目的地
this.node.setPosition(this.startPos.x > 360?300:this.startPos.x,0,0)
this.do_anim()
break;
case AType.fixedEnd:
this.node.setPosition(this.targetPos.x > 360?300:this.targetPos.x,0,0)
this.do_anim()
break;
case AType.fixedStart: //
if(this.s_uuid==6001){
console.log("skillcom startPos",this.startPos)
}
this.node.setPosition(this.startPos.x > 360?300:this.startPos.x,0,0)
this.do_anim()
break;
}
// switch(this.skillConfig.AType){
// case AType.parabolic:
// this.node.angle +=10
// // bm.speed=700
// if(this.group==BoxSet.MONSTER) {bm.controlPointSide=-1 }
// bm.rotationSmoothness=0.6
// bm.moveTo(this.targetPos)
// break;
// case AType.linear:
// let s_x=this.startPos.x
// let s_y=this.startPos.y
// let t_x=this.targetPos.x
// let t_y=this.targetPos.y
// // 设定目标x
// this.targetPos.x = 400;
// if(this.group == BoxSet.MONSTER) {
// bm.controlPointSide = -1;
// this.targetPos.x = -400;
// }
// // 计算斜率
// const k = (t_y - s_y) / (t_x - s_x);
// // 按直线公式计算新的y
// this.targetPos.y = k * (this.targetPos.x - s_x) + s_y;
// bm.controlPointOffset=0
// bm.rotationSmoothness=0.6
// bm.moveTo(this.targetPos);
// break;
// case AType.StartEnd:
// // 2段位移先升高然后移到目的地
// this.node.setPosition(this.startPos.x > 360?300:this.startPos.x,0,0)
// this.do_anim()
// break;
// case AType.fixedEnd:
// this.node.setPosition(this.targetPos.x > 360?300:this.targetPos.x,0,0)
// this.do_anim()
// break;
// case AType.fixedStart: //
// if(this.s_uuid==6001){
// console.log("skillcom startPos",this.startPos)
// }
// this.node.setPosition(this.startPos.x > 360?300:this.startPos.x,0,0)
// this.do_anim()
// break;
// }
}
@@ -170,12 +170,12 @@ export class SkillCom extends CCComp {
if(this.hit_count > 0 &&!is_range ){
ap=ap*(50+this.puncture_damage)/100
}
target.do_atked(ap,this.caster_crit,this.caster_crit_d,
this.burn_count,this.burn_value,
this.stun_time,this.stun_ratio,
this.frost_time,this.frost_ratio,
this.skillConfig.AtkedType
) // ap 及暴击 属性已经在skill.ts 处理
// target.do_atked(ap,this.caster_crit,this.caster_crit_d,
// this.burn_count,this.burn_value,
// this.stun_time,this.stun_ratio,
// this.frost_time,this.frost_ratio,
// this.skillConfig.AtkedType
// ) // ap 及暴击 属性已经在skill.ts 处理
// console.log("[SkillCom]:single_damage t:tp:rtp",this.node.position,this.targetPos,target.node.position)
if(this.skillConfig.debuff>0){
let debuff=this.skillConfig

View File

@@ -1,3 +1,5 @@
import { get } from "http";
export type CloudReturnType<T = any> = {
code: number,// 200成功
msg?:string,
@@ -81,4 +83,12 @@ export class WxCloudApi{
}
});
}
public static async get(): Promise<CloudCallFunctionResult<CloudReturnType<{}>>> {
return await wx?.cloud.callFunction({
name: 'cocos_cloud',
data: {
cmd: 'get',
}
});
}
}