Jelajahi Sumber

组件动态添加脚本

woso_javan 2 bulan lalu
induk
melakukan
f4f414768b

+ 107 - 51
assets/module_storm_sunder/Prefabs/Tornado.prefab

@@ -23,16 +23,15 @@
       },
       {
         "__id__": 11
-      }
-    ],
-    "_active": true,
-    "_components": [
+      },
       {
-        "__id__": 21
+        "__id__": 15
       }
     ],
+    "_active": true,
+    "_components": [],
     "_prefab": {
-      "__id__": 23
+      "__id__": 25
     },
     "_lpos": {
       "__type__": "cc.Vec3",
@@ -256,6 +255,95 @@
     "targetOverrides": null,
     "nestedPrefabInstanceRoots": null
   },
+  {
+    "__type__": "cc.Node",
+    "_name": "radiusTigger",
+    "_objFlags": 0,
+    "__editorExtras__": {},
+    "_parent": {
+      "__id__": 1
+    },
+    "_children": [],
+    "_active": true,
+    "_components": [
+      {
+        "__id__": 12
+      }
+    ],
+    "_prefab": {
+      "__id__": 14
+    },
+    "_lpos": {
+      "__type__": "cc.Vec3",
+      "x": 0,
+      "y": 0,
+      "z": 0
+    },
+    "_lrot": {
+      "__type__": "cc.Quat",
+      "x": 0,
+      "y": 0,
+      "z": 0,
+      "w": 1
+    },
+    "_lscale": {
+      "__type__": "cc.Vec3",
+      "x": 1,
+      "y": 1,
+      "z": 1
+    },
+    "_mobility": 0,
+    "_layer": 1073741824,
+    "_euler": {
+      "__type__": "cc.Vec3",
+      "x": 0,
+      "y": 0,
+      "z": 0
+    },
+    "_id": ""
+  },
+  {
+    "__type__": "cc.CylinderCollider",
+    "_name": "",
+    "_objFlags": 0,
+    "__editorExtras__": {},
+    "node": {
+      "__id__": 11
+    },
+    "_enabled": true,
+    "__prefab": {
+      "__id__": 13
+    },
+    "_material": null,
+    "_isTrigger": false,
+    "_center": {
+      "__type__": "cc.Vec3",
+      "x": 0,
+      "y": 0,
+      "z": 0
+    },
+    "_radius": 15,
+    "_height": 7.8,
+    "_direction": 1,
+    "_id": ""
+  },
+  {
+    "__type__": "cc.CompPrefabInfo",
+    "fileId": "6bOC1H1W9LPb+T0GiWk25W"
+  },
+  {
+    "__type__": "cc.PrefabInfo",
+    "root": {
+      "__id__": 1
+    },
+    "asset": {
+      "__id__": 0
+    },
+    "fileId": "03Qof0YENAW587XVEG6hia",
+    "instance": null,
+    "targetOverrides": null,
+    "nestedPrefabInstanceRoots": null
+  },
   {
     "__type__": "cc.Node",
     "_name": "points",
@@ -266,22 +354,22 @@
     },
     "_children": [
       {
-        "__id__": 12
+        "__id__": 16
       },
       {
-        "__id__": 14
+        "__id__": 18
       },
       {
-        "__id__": 16
+        "__id__": 20
       },
       {
-        "__id__": 18
+        "__id__": 22
       }
     ],
     "_active": true,
     "_components": [],
     "_prefab": {
-      "__id__": 20
+      "__id__": 24
     },
     "_lpos": {
       "__type__": "cc.Vec3",
@@ -318,13 +406,13 @@
     "_objFlags": 0,
     "__editorExtras__": {},
     "_parent": {
-      "__id__": 11
+      "__id__": 15
     },
     "_children": [],
     "_active": true,
     "_components": [],
     "_prefab": {
-      "__id__": 13
+      "__id__": 17
     },
     "_lpos": {
       "__type__": "cc.Vec3",
@@ -374,13 +462,13 @@
     "_objFlags": 0,
     "__editorExtras__": {},
     "_parent": {
-      "__id__": 11
+      "__id__": 15
     },
     "_children": [],
     "_active": true,
     "_components": [],
     "_prefab": {
-      "__id__": 15
+      "__id__": 19
     },
     "_lpos": {
       "__type__": "cc.Vec3",
@@ -430,13 +518,13 @@
     "_objFlags": 0,
     "__editorExtras__": {},
     "_parent": {
-      "__id__": 11
+      "__id__": 15
     },
     "_children": [],
     "_active": true,
     "_components": [],
     "_prefab": {
-      "__id__": 17
+      "__id__": 21
     },
     "_lpos": {
       "__type__": "cc.Vec3",
@@ -486,13 +574,13 @@
     "_objFlags": 0,
     "__editorExtras__": {},
     "_parent": {
-      "__id__": 11
+      "__id__": 15
     },
     "_children": [],
     "_active": true,
     "_components": [],
     "_prefab": {
-      "__id__": 19
+      "__id__": 23
     },
     "_lpos": {
       "__type__": "cc.Vec3",
@@ -549,38 +637,6 @@
     "targetOverrides": null,
     "nestedPrefabInstanceRoots": null
   },
-  {
-    "__type__": "6a63dO/kz1JKKcWaanOItmF",
-    "_name": "",
-    "_objFlags": 0,
-    "__editorExtras__": {},
-    "node": {
-      "__id__": 1
-    },
-    "_enabled": true,
-    "__prefab": {
-      "__id__": 22
-    },
-    "points": [
-      {
-        "__id__": 12
-      },
-      {
-        "__id__": 14
-      },
-      {
-        "__id__": 16
-      },
-      {
-        "__id__": 18
-      }
-    ],
-    "_id": ""
-  },
-  {
-    "__type__": "cc.CompPrefabInfo",
-    "fileId": "7aI3hGBslEiZ/KnKTUXIJi"
-  },
   {
     "__type__": "cc.PrefabInfo",
     "root": {

+ 25 - 0
assets/module_storm_sunder/Script/Component/TornadoAIComponent.ts

@@ -0,0 +1,25 @@
+import { BoxCollider, Button, Collider, Component, ConeCollider, CylinderCollider, ITriggerEvent, Label, Node, NodeEventType, RigidBody, SphereCollider, Vec3, _decorator, find, game } from 'cc';
+import { StormSunderAudioMgr } from '../Manager/StormSunderAudioMgr';
+import { EventDispatcher } from 'db://assets/core_tgx/easy_ui_framework/EventDispatcher';
+import { GameEvent } from '../Enum/GameEvent';
+import { UIJoyStick } from '../UIJoyStick';
+import { GameMgr, GameStatus } from '../Manager/GameMgr';
+import { PropComponent, PropStatus } from './PropComponent';
+import { PlayerInfo } from './PlayerInfoComponent';
+import { Effect2DUIMgr } from '../Manager/Effect2DUIMgr';
+import { PlayerStatus, TornadoComponent } from './TornadoComponent';
+const { ccclass, property } = _decorator;
+
+
+/** 龙卷风AI组件*/
+@ccclass('TornadoAIComponent')
+export class TornadoAIComponent extends TornadoComponent {
+
+    attack_ai: number = 1;
+    speed_ai: number = 1;
+    expPower_ai: number = 1;
+
+    protected start(): void {
+        super.start();
+    }
+}

+ 1 - 1
assets/module_storm_sunder/Script/Model/LevelModel.ts.meta → assets/module_storm_sunder/Script/Component/TornadoAIComponent.ts.meta

@@ -2,7 +2,7 @@
   "ver": "4.0.23",
   "importer": "typescript",
   "imported": true,
-  "uuid": "2655ee42-ab62-46d2-a2ce-ff3c99571cf2",
+  "uuid": "07287c94-2ee0-4d1c-9d43-36bd35f482c3",
   "files": [],
   "subMetas": {},
   "userData": {}

+ 7 - 6
assets/module_storm_sunder/Script/Component/TornadoComponent.ts

@@ -18,9 +18,8 @@ export enum PlayerStatus {
 /** 龙卷风组件*/
 @ccclass('TornadoComponent')
 export class TornadoComponent extends Component {
-    @property({ type: [Node] })
-    points: Node[] = [];
 
+    points: Node[] = [];
     tornado: Node = null!;
     rigidBody: RigidBody = null!;
     tigger: Collider = null!; //龙卷风触发器
@@ -50,6 +49,7 @@ export class TornadoComponent extends Component {
         this.initializeData();
         this.initilizeUI();
         this.registerEvent();
+        this.changeStatus(PlayerStatus.LIFE);
     }
 
     initilizeUI(): void {
@@ -59,6 +59,9 @@ export class TornadoComponent extends Component {
         this.rigidBody = this.tornado.getComponent(RigidBody)!;
         this.tigger = this.tornado.getComponent(Collider)!;
 
+        const points = this.node.getChildByName('points')!;
+        this.points = points.children.map((child) => child);
+
         this.tigger.on('onTriggerEnter', this.onTriggerEnter, this);
         this.tigger.on('onTriggerStay', this.onTriggerStay, this);
         this.tigger.on('onTriggerExit', this.onTriggerExit, this);
@@ -75,12 +78,10 @@ export class TornadoComponent extends Component {
         this.ai = false;
 
         this.playerInfo = {
-            nickName: '爸爸在此',
+            nickName: this.nickName,
             level: 1,
             exp: 0,
         }
-
-        this.changeStatus(PlayerStatus.LIFE);
     }
 
     protected registerEvent() {
@@ -178,7 +179,7 @@ export class TornadoComponent extends Component {
     }
 
     onMoveHandler(): void {
-        if (this.isColliding) return;
+        if (this.isColliding || this.ai) return;
         const playerDir = UIJoyStick.ins.dir;
         const playerX = playerDir.x * this.speed * game.deltaTime;
         const playerZ = playerDir.y * this.speed * game.deltaTime;

+ 3 - 1
assets/module_storm_sunder/Script/Manager/GameMgr.ts

@@ -5,6 +5,7 @@ import { StormSunderGlobalInstance } from "../StormSunderGlobalInstance";
 import { PropMgr } from "./PropMgr";
 import { TimerMgr } from "./TimerMgr";
 import { UI_BattleResult } from "db://assets/scripts/UIDef";
+import { PlayerMgr } from "./PlayerMgr";
 
 export class GameMgr {
     private static _instance: GameMgr;
@@ -53,8 +54,9 @@ export class GameMgr {
 
     public async startGame() {
         GameMgr.inst.setGameStatus(GameStatus.Playing);
-        TimerMgr.inst.startCountdown();
+        await PlayerMgr.inst.playerAddComponent();
         await PropMgr.inst.genatorProp();
+        TimerMgr.inst.startCountdown();
     }
 }
 

+ 10 - 1
assets/module_storm_sunder/Script/Manager/PlayerMgr.ts

@@ -37,11 +37,20 @@ export class PlayerMgr {
             } else {
                 const playersUI = StormSunderGlobalInstance.instance.players!;
                 if (playersUI && playersUI.children.length > 0)
-                    this.tornadoNode = playersUI.children.filter(child => child.getComponent(TornadoComponent))[0];
+                    this.tornadoNode = playersUI.children[0];
 
                 resolve(this.tornadoNode);
             }
         })
     }
+
+    async playerAddComponent(): Promise<void> {
+        return new Promise((resolve, reject) => {
+            this.getTornadoNode().then(node => {
+                node.addComponent(TornadoComponent)!;
+                resolve();
+            })
+        })
+    }
 }
 

+ 1 - 1
assets/module_storm_sunder/Script/Manager/TimerMgr.ts

@@ -18,7 +18,7 @@ export class TimerMgr {
         return this.Instance;
     }
 
-    public countDownTime: number = 10;
+    public countDownTime: number = 20;
     private timerId: number = 0;
     private propMgr: PropMgr;
 

+ 0 - 99
assets/module_storm_sunder/Script/Model/LevelModel.ts

@@ -1,99 +0,0 @@
-import { JsonUtil } from "db://assets/core_tgx/base/utils/JsonUtil";
-import { Tablelevels_config } from "../../../module_basic/table/Tablelevels_config";
-import { GlobalConfig } from "../../../start/Config/GlobalConfig";
-import { sys } from "cc";
-import { WaterColors } from "../StormSunderGlobalInstance";
-
-
-export enum TYPE_GAME_STATE {
-    GAME_STATE_INIT = 0, //准备阶段
-    GAME_STATE_START = 1, //开始
-    GAME_STATE_END = 2, //结束(倒计时结束)
-    GAME_STATE_RESULT = 3, //结算
-    GAME_STATE_PAUSE = 4, //暂停
-}
-
-/**关卡数据模型
- */
-export class LevelModel {
-    public levelConfig: Tablelevels_config;
-
-    public levelColors: WaterColors[] = [];
-    /** 当前关卡等级*/
-    public level: number = 1;
-    /** 创建问号水的水量*/
-    public createQuestionCount: number = 0;
-    /** 当前创建的原浆杯*/
-    public currentOriginCup: number = 0;
-    /** 剩余杯子数量*/
-    public remainCupCount: number = 0;
-    /** 保存可随机的关卡*/
-    public randomLevelList: number[] = [];
-    /** 输赢*/
-    public isWin: boolean = false;
-    /** 是否结束*/
-    public isEnd: boolean = false;
-
-    /** 当前游戏状态*/
-    public curGameState: TYPE_GAME_STATE = TYPE_GAME_STATE.GAME_STATE_INIT;
-
-    constructor() {
-        this.levelConfig = new Tablelevels_config();
-
-        this.getRandomLevelList();
-
-        const isDebug = GlobalConfig.isDebug;
-        if (isDebug) {
-            this.level = GlobalConfig.initilizeLevel;
-        } else {
-            const level = sys.localStorage.getItem('level');
-            if (!level) {
-                this.level = 1;
-            } else {
-                if (level > GlobalConfig.levelTotal) {
-                    const randomLevel = this.randomLevelList[Math.floor(Math.random() * this.randomLevelList.length - 1)];
-                    this.level = randomLevel;
-                } else {
-                    this.level = parseInt(level);
-                }
-            }
-        };
-        this.levelConfig.init(this.level);
-        this.initLevelColors();
-    }
-
-    public initLevelColors() {
-        const count = this.levelConfig.color; //获取关卡颜色数
-        this.levelColors = [];
-        const allColors = Object.values(WaterColors).filter(v => !isNaN(Number(v)) && v != WaterColors.Black) as WaterColors[];
-        // Fisher-Yates 洗牌算法
-        for (let i = allColors.length - 2; i > 0; i--) {
-            const j = Math.floor(Math.random() * (i + 1));
-            [allColors[i], allColors[j]] = [allColors[j], allColors[i]];
-        }
-        this.levelColors.push(...allColors.slice(0, count))
-    }
-
-    /** 可随机的关卡合集*/
-    getRandomLevelList() {
-        const table = JsonUtil.get(Tablelevels_config.TableName);
-        if (!table) {
-            console.warn('Get level table is fail!');
-        }
-        this.randomLevelList = Object.values(table).filter(item => item['random'] == 1)
-            .map(item => item['level']);
-
-        // console.log('随机关卡列表:', this.randomLevelList);
-    }
-
-    /** 清除关卡数据*/
-    clearLevel() {
-        this.levelColors = [];
-        this.createQuestionCount = 0;
-        this.remainCupCount = 0;
-        this.currentOriginCup = 0;
-        this.isWin = false;
-        this.isEnd = false;
-    }
-
-}

+ 44 - 0
assets/module_storm_sunder/Script/Model/UserModel.ts

@@ -0,0 +1,44 @@
+/**玩家数据模型
+ * @param attack 攻击力
+ * @param money 金币
+ * @param expPower 经验加成
+*/
+export class UserModel {
+    private _attack: number = 1; //基础攻击
+    private _speed: number = 1; //基础速度
+    private _expPower: number = 1; //经验加成
+    private _money: number = 10000;
+    private _nickName: string = '落山鸡';
+
+    constructor() {
+    }
+
+    public get money(): number {
+        return this._money;
+    }
+    public set money(value: number) {
+        this._money = value;
+    }
+
+    public get nickName(): string {
+        return this._nickName;
+    }
+    public set nickName(value: string) {
+        this._nickName = value;
+    }
+
+    public get attack(): number {
+        return this._attack;
+    }
+    public set attack(value: number) {
+        this._attack = value;
+    }
+
+    public get speed(): number {
+        return this._speed;
+    }
+
+    public set speed(value: number) {
+        this._speed = value;
+    }
+}

+ 9 - 0
assets/module_storm_sunder/Script/Model/UserModel.ts.meta

@@ -0,0 +1,9 @@
+{
+  "ver": "4.0.23",
+  "importer": "typescript",
+  "imported": true,
+  "uuid": "234a3a01-5a77-44bf-9365-77ec24d399fe",
+  "files": [],
+  "subMetas": {},
+  "userData": {}
+}