Pārlūkot izejas kodu

添加安卓消息支持

mojunshou 8 mēneši atpakaļ
vecāks
revīzija
adf628cc9d

+ 207 - 0
assets/script/game/common/manager/CocosHandler.ts

@@ -0,0 +1,207 @@
+import { native } from 'cc';
+import { _decorator, Component, Node } from 'cc';
+import { DeviceUtil } from 'db://oops-framework/core/utils/DeviceUtil';
+import { GameComponent } from 'db://oops-framework/module/common/GameComponent';
+const { ccclass, property } = _decorator;
+
+@ccclass('CocosHandler')
+export class CocosHandler extends GameComponent {
+
+    // 单例模式
+    private static instance: CocosHandler;
+    public static getInstance(): CocosHandler {
+        if (!CocosHandler.instance) {
+            CocosHandler.instance = new CocosHandler();
+        }
+        return CocosHandler.instance;
+    }
+
+
+    // 处理 Android 发送的消息
+    private onAndroidMessage(jsonStr: string) {
+        const data = JSON.parse(jsonStr);
+        console.log('Cocos 收到 Android 消息:', data);
+
+        // 处理逻辑
+        // 例如:根据 data 的内容执行不同的操作,不是每个回调都要处理
+    }
+
+    // 发送消息到 Android
+    public async sendMessageToAndroid(json: Object) {
+        if (DeviceUtil.isAndroid && DeviceUtil.isNative) {
+            let jsonStr = JSON.stringify(json);
+            console.log("js传给安卓的数据打印>>>>", jsonStr);
+            let result = await native.reflection.callStaticMethod("com/cocos/game/AndroidHandler", "onCocosMessage", "(Ljava/lang/String;)Ljava/lang/String;", jsonStr);
+            //等待上边安卓返回数据,再执行下边代码
+            if (result) {
+                let android_result = JSON.parse(result);
+                console.log("安卓返回数据打印>>>>>", android_result);
+                return android_result;
+            }
+        }
+    }
+
+
+    //显示广告
+    showBannerAd() {
+
+
+    }
+
+
+    async login(username: string, password: string) {
+        //区分普通登录,微信登录,游客登录
+        let obj = {
+            method: "login",
+            callback: "StartViewComp.androidCallJs",            //安卓完成业务的回调JS函数
+            data: {
+                username: username,
+                password: password,
+                type: 1   //登录类型
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+
+
+    //用户注册
+    async user_register(username: string, password: string) {
+        let obj = {
+            method: "user_register",
+            callback: "",
+            data: {
+                username: username,
+                password: password
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+    //微信登录
+    async wechat_login() {
+        let obj = {
+            method: "wechat_login",
+            callback: "",
+            data: {
+
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+
+
+    //用户提现
+    async user_withdrawal(user_id: string) {
+        let obj = {
+            method: "user_withdrawal",
+            callback: "",
+            data: {
+
+
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+    //获取提现记录
+    async get_withdrawal_record(user_id: string) {
+        let obj = {
+            method: "get_withdrawal_record",
+            callback: "",
+            data: {
+
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+    //获取用户信息
+    async get_user_info(user_id: string) {
+        let obj = {
+            method: "get_user_info",
+            callback: "",
+            data: {
+                user_id: user_id
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+    //现金提现
+    async cash_withdrawal(user_id: string, amount: string) {
+        let obj = {
+            method: "cash_withdrawal",
+            callback: "",
+            data: {
+                user_id: user_id,
+                amount: amount
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+
+    //红包提现
+    async red_packet_withdrawal(user_id: string, amount: string) {
+        let obj = {
+            method: "red_packet_withdrawal",
+            callback: "",
+            data: {
+                user_id: user_id,
+                amount: amount
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+
+    //复制文字
+    async copy_text(text: string) {
+        let obj = {
+            method: "copy_text",
+            callback: "",
+            data: {
+                text: text
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+
+    //获取远程CDN地址,这用和安卓交互
+
+    //获取游戏数据信息--返回后才能进入游戏渲染数据,也可以登录后推送过来
+    async get_game_data() {
+        let obj = {
+            method: "get_game_data",
+            callback: "",
+            data: {
+
+            }
+        }
+        let result = await this.sendMessageToAndroid(obj);
+        return result;
+    }
+
+
+    //安卓直接调用广告回调,显示失败还是成功,成功提供什么奖励,失败又怎么做
+    async ad_callback(ad_type: string, ad_status: string, ad_reward: string) {
+
+
+    }
+
+}
+window['CocosHandler'] = CocosHandler;
+
+

+ 9 - 0
assets/script/game/common/manager/CocosHandler.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.24",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "81afa1e2-5a1f-4eaf-92bd-d66f2dcf5b17",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}

+ 77 - 34
assets/script/game/eliminate/view/EliminateViewComp.ts

@@ -2,7 +2,7 @@
  * @Author: mojunshou 1637302775@qq.com
  * @Date: 2025-03-20 15:01:09
  * @LastEditors: mojunshou 1637302775@qq.com
- * @LastEditTime: 2025-03-28 17:23:57
+ * @LastEditTime: 2025-03-31 14:11:58
  * @Description: 消除游戏主场景
  */
 import { _decorator, Color, EventTouch, instantiate, JsonAsset, Label, Node, Prefab, randomRangeInt, Sprite, Toggle, tween, UITransform, Vec2, Vec3, Widget } from "cc";
@@ -145,6 +145,7 @@ export class EliminateViewComp extends CCComp {
     private money: number = 0;      //左边金钱
     private cash: number = 0;       //右边红包钱数
 
+
     private totalNum: number = 0;   //累计消除次数,断了就是又从0开始
 
     //网格列表
@@ -196,6 +197,20 @@ export class EliminateViewComp extends CCComp {
     private shouldResetEliminateCount: boolean = true;
     private consecutiveEliminations: boolean = false;
 
+    // 添加配置属性 - 消除基础分
+    @property({ displayName: "消除一行/列基础分" })
+    private eliminateBaseScore: number = 10;
+
+    //当前局数
+    private curGameNum: number = 0;
+    //第一通关开始新人模式
+    //第二局开始马上播放新的弹窗
+    //往后根据每局需要弹窗就弹窗
+    //通关一局算一块金砖
+    //通关比例算金砖比例
+
+
+
     /** 视图层逻辑代码分离演示 */
     async start() {
         // const entity = this.ent as ecs.Entity;         // ecs.Entity 可转为当前模块的具体实体对象
@@ -465,15 +480,8 @@ export class EliminateViewComp extends CCComp {
     addBrick(index: number) {
         //随机名字
         const brickKey = Object.keys(this.brickConfig['bricks'])[randomRangeInt(0, Object.keys(this.brickConfig['bricks']).length)]
-        // // console.log("brickKey>>>>>", brickKey)
-        // //随机颜色-一个颜色出来
-        // const gridColorKey = Object.keys(this.brickConfig['GridType'])[randomRangeInt(0, Object.keys(this.brickConfig['GridType']).length)]
-        // // console.log("gridColorKey>>>>>", gridColorKey)
-        // //随机配置-通过名字拿到那个配置
         const brickConfig = this.brickConfig['bricks'][brickKey];
-        // console.log("brickConfig>>>>>", brickConfig)
-        const randomIndex = RandomManager.instance.getRandomInt(1, 5, 2);
-        console.log("随机颜色>>>>>", randomIndex)
+        const randomIndex = oops.random.getRandomInt(1, 5);
         // 生成方块
         const brickData: BrickData = {
             index,
@@ -494,10 +502,6 @@ export class EliminateViewComp extends CCComp {
             this.brickNode.addChild(brickNode)
         }
         brickData.brickNode = brickNode;
-        // 方块间隔
-
-        //这里修改一下,分别放到3个Node中,然后
-
         let offset = 220
         if (this.brickNum % 2 === 1) {
             const middleNum = Math.floor(this.brickNum / 2) + 1
@@ -1017,14 +1021,12 @@ export class EliminateViewComp extends CCComp {
                 // 计算所有被消除格子的平均Y轴位置
                 let totalY = 0;
                 let validGrids = 0;
-
                 for (const grid of gridEliminateList) {
                     if (grid.gridNode) {
                         totalY += grid.gridNode.getWorldPosition().y;
                         validGrids++;
                     }
                 }
-
                 // 计算平均Y位置
                 const avgY = validGrids > 0 ? totalY / validGrids : 0;
 
@@ -1038,7 +1040,6 @@ export class EliminateViewComp extends CCComp {
             gridEliminateList.forEach((gridData) => {
                 if (gridData.gridNode?.children[0]) {
                     const startPos = gridData.gridNode.getWorldPosition();
-
                     tween(gridData.gridNode.children[0])
                         .to(0.2, { scale: new Vec3(0.5, 0.5) })
                         .call(() => {
@@ -1066,25 +1067,59 @@ export class EliminateViewComp extends CCComp {
             })
 
             this.scheduleOnce(() => {
-                let score = 0
-                for (let i = 1; i <= eliminateRowNum; i++) {
-                    score += this.cols * i
-                }
-                for (let i = 1; i <= eliminateColumnNum; i++) {
-                    score += this.rows * i
-                }
-                this.score += score
+                // 新的分数计算逻辑
+                let baseScore = 0;
+                // 计算基础分(每行/列 * 基础分值)
+                baseScore = (eliminateRowNum + eliminateColumnNum) * this.eliminateBaseScore;
+
+                // 格子数量分
+                const gridScore = gridEliminateList.length;
+
+                // 总分 = 基础分 + 格子数量分
+                const totalScore = baseScore + gridScore;
+
+                // 更新总分
+                this.score += totalScore;
                 this.lab_score.string = this.score.toString();
 
-                const lastGrid = gridEliminateList[gridEliminateList.length - 1];
-                if (lastGrid?.gridNode && this.coinPrefab && this.amountLb?.node) {
-                    const lastPos = lastGrid.gridNode.getWorldPosition();
-                    this.createCoinFlyAnimation(
-                        this.coinPrefab,
-                        lastPos,
-                        this.amountLb.node.getWorldPosition(),
-                        score
-                    );
+                // 显示分数增加动画
+                if (gridEliminateList.length > 0) {
+                    const lastGrid = gridEliminateList[gridEliminateList.length - 1];
+                    if (lastGrid?.gridNode) {
+                        const lastPos = lastGrid.gridNode.getWorldPosition();
+
+                        // 创建分数提示
+                        const scoreNode = new Node();
+                        this.node.addChild(scoreNode);
+                        scoreNode.setWorldPosition(lastPos);
+
+                        const scoreLabel = scoreNode.addComponent(Label);
+                        scoreLabel.string = `+${totalScore}`;
+                        scoreLabel.fontSize = 40;
+                        scoreLabel.color = Color.YELLOW;
+
+                        // 分数飞行动画
+                        tween(scoreNode)
+                            .to(0.5, {
+                                position: new Vec3(scoreNode.position.x, scoreNode.position.y + 100, 0),
+                                scale: new Vec3(1.5, 1.5, 1.5)
+                            })
+                            .to(0.3, { opacity: 0 })
+                            .call(() => {
+                                scoreNode.destroy();
+                            })
+                            .start();
+
+                        // 金币飞行动画
+                        if (this.coinPrefab && this.amountLb?.node) {
+                            this.createCoinFlyAnimation(
+                                this.coinPrefab,
+                                lastPos,
+                                this.amountLb.node.getWorldPosition(),
+                                totalScore
+                            );
+                        }
+                    }
                 }
 
                 // 告诉调用者有消除发生
@@ -1621,8 +1656,13 @@ export class EliminateViewComp extends CCComp {
     }
 
     private onToggleContainerAutoClick(toggle: Toggle) {
+
         switch (toggle.node.name) {
             case "On":
+                if (this.autoClick) {
+                    oops.gui.toast("已经是自动模式啦")
+                    return;
+                }
                 this.autoClick = true;
                 break;
             case "Off":
@@ -1639,6 +1679,10 @@ export class EliminateViewComp extends CCComp {
             this.editingFlag
         ) return
         // 切换自动模式状态
+        //如果状态一样,不做任何操作
+        if (this.isAutoMode === this.autoClick) {
+            return;
+        }
         this.isAutoMode = this.autoClick;
         if (this.isAutoMode) {
             this._isAutoFunc = () => {
@@ -1651,7 +1695,6 @@ export class EliminateViewComp extends CCComp {
                 this._isAutoFunc = null
             }
         }
-
     }
 
 

+ 20 - 4
assets/script/game/initialize/view/LoadingViewComp.ts

@@ -2,7 +2,7 @@
  * @Author: mojunshou 1637302775@qq.com
  * @Date: 2025-03-19 16:23:51
  * @LastEditors: mojunshou 1637302775@qq.com
- * @LastEditTime: 2025-03-28 12:03:26
+ * @LastEditTime: 2025-03-31 11:56:22
  * @Description: loading界面
  */
 import { _decorator, sys } from "cc";
@@ -16,8 +16,6 @@ import { UIID } from "../../common/config/GameUIConfig";
 import { AndroidMessageCenter } from '../../common/manager/AndroidMessageCenter';
 import { smc } from "../../common/SingletonModuleComp";
 import { EliminateViewComp } from "../../eliminate/view/EliminateViewComp";
-import { GameView } from "../../eliminate/view/GameView";
-
 const { ccclass, property } = _decorator;
 
 /** 游戏资源加载 */
@@ -49,6 +47,15 @@ export class LoadingViewComp extends CCVMParentComp {
     enter() {
         this.addEvent();
         //查看缓存有没有同意过
+        if (DeviceUtil.isNative && DeviceUtil.isAndroid) {
+
+
+
+
+        } else {
+            this.loadRes();
+        }
+
         // let data = sys.localStorage.getItem("agree");
         // if (!data) {
         //     //打开温馨提示
@@ -61,7 +68,7 @@ export class LoadingViewComp extends CCVMParentComp {
         //     //显示微信登录按钮
         //     // this.showWxLogin();
         // } else {
-        //     //登录了就加载
+        //     //登录了就--请求服务器,微信登录,成功再加载资源
         //     this.loadRes();
         // }
         // if (!DeviceUtil.isNative) {
@@ -77,6 +84,7 @@ export class LoadingViewComp extends CCVMParentComp {
     private addEvent() {
         this.on(AndroidEvent.AgreePrivacy, this.onAgreePrivacy, this);
 
+
     }
 
     private onAgreePrivacy() {
@@ -138,5 +146,13 @@ export class LoadingViewComp extends CCVMParentComp {
 
     }
 
+    /**
+     * @description: 显示微信登录
+     * @return {*}
+     */
+    showWxLogin() {
+
+    }
+
     reset(): void { }
 }