|
@@ -1,6 +1,13 @@
|
|
|
-import { assetManager, instantiate, Prefab, Node, UITransform, Vec3, Vec2, view, game } from "cc";
|
|
|
+import { assetManager, instantiate, Prefab, Node, UITransform, Vec3, Vec2, view, game, PhysicsSystem, geometry } from "cc";
|
|
|
import { resLoader } from "db://assets/core_tgx/base/ResLoader";
|
|
|
import { StormSunderGlobalInstance } from "../StormSunderGlobalInstance";
|
|
|
+import { TornadoComponent } from "../Component/TornadoComponent";
|
|
|
+
|
|
|
+const propRes = [
|
|
|
+ "Prefabs/Props/altman",
|
|
|
+ "Prefabs/Props/aula",
|
|
|
+ "Prefabs/Props/clown",
|
|
|
+]
|
|
|
|
|
|
/** 道具生成管理器*/
|
|
|
export class PropMgr {
|
|
@@ -21,12 +28,16 @@ export class PropMgr {
|
|
|
//数量上限
|
|
|
public propMaxNum: number = 100;
|
|
|
//生成周期
|
|
|
- public propCreateCycle: number = 10;
|
|
|
+ public propCreateCycle: number = 1;
|
|
|
//生成数量
|
|
|
- public propCreateNum: number = 1;
|
|
|
+ public propCreateNum: number = 10;
|
|
|
|
|
|
public tornadoNode: Node = null;//玩家节点
|
|
|
|
|
|
+ public spawnRadius: number = 20; // 生成范围半径
|
|
|
+ public raycastDistance: number = 1;//射线检测距离
|
|
|
+ currentPropsCount: number = 0; //当前道具数量
|
|
|
+
|
|
|
public genaratorInitialData() {
|
|
|
//DOTO 取配置 先定义假数据
|
|
|
|
|
@@ -38,7 +49,80 @@ export class PropMgr {
|
|
|
|
|
|
public update(deltaTime?: number) {
|
|
|
//DOTO 生成道具
|
|
|
- console.log('deltaTime:', deltaTime)
|
|
|
+ // console.log('deltaTime:', deltaTime);
|
|
|
+ }
|
|
|
+
|
|
|
+ public async genatorProp() {
|
|
|
+ if (this.currentPropsCount >= this.propMaxNum) return;
|
|
|
+
|
|
|
+ const propsUI = StormSunderGlobalInstance.instance.props!;
|
|
|
+ this.tornadoNode = this.getTornadoNode();
|
|
|
+
|
|
|
+ for (let index = 0; index < this.propInitNum; index++) {
|
|
|
+ let spawnPos = this.getValidSpawnPosition();
|
|
|
+ console.log(`随机点:${spawnPos.x},${spawnPos.y}}`)
|
|
|
+ // 进行四向物理检测
|
|
|
+ if (!this.isPositionBlocked(spawnPos)) {
|
|
|
+ const propPrefab = await resLoader.loadAsync(resLoader.gameBundleName, propRes[Math.floor(Math.random() * propRes.length)]);
|
|
|
+ let newMonster = instantiate(propPrefab) as any;
|
|
|
+ newMonster.setParent(propsUI);
|
|
|
+ newMonster.setWorldPosition(spawnPos);
|
|
|
+ this.currentPropsCount++;
|
|
|
+ console.log("怪物生成成功", spawnPos);
|
|
|
+ }
|
|
|
+
|
|
|
+ console.warn("未找到合适的怪物生成点");
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 获取一个合法的生成位置 */
|
|
|
+ getValidSpawnPosition(): Vec3 | null {
|
|
|
+ const maxAttempts = 10;
|
|
|
+ for (let i = 0; i < maxAttempts; i++) {
|
|
|
+ let spawnPos = this.getRandomSpawnPosition();
|
|
|
+ if (!this.isPositionBlocked(spawnPos)) {
|
|
|
+ return spawnPos; // 当前位置没有障碍物,返回该位置
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null; // 失败,返回 null
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 随机获取玩家附近的生成点 */
|
|
|
+ getRandomSpawnPosition(): Vec3 {
|
|
|
+ let playerPos = this.tornadoNode.worldPosition;
|
|
|
+ let angle = Math.random() * Math.PI * 2; // 随机角度
|
|
|
+
|
|
|
+ let distance = Math.random() * this.spawnRadius + this.spawnRadius;
|
|
|
+ let x = playerPos.x + Math.cos(angle) * distance;
|
|
|
+ let z = playerPos.z + Math.sin(angle) * distance;
|
|
|
+ let y = 0;
|
|
|
+ return new Vec3(x, y, z);
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 检测当前位置是否有障碍物 */
|
|
|
+ isPositionBlocked(position: Vec3): boolean {
|
|
|
+ let ray = new geometry.Ray(position.x, position.y, position.z, 0, 0, 0);
|
|
|
+
|
|
|
+ if (PhysicsSystem.instance.raycastClosest(ray, this.raycastDistance)) {
|
|
|
+ let hit = PhysicsSystem.instance.raycastClosestResult;
|
|
|
+ if (hit && hit.collider) {
|
|
|
+ console.log("检测到障碍物", hit.collider.node.name, "位置", position);
|
|
|
+ return true; // 当前位置有障碍物
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false; // 没有检测到障碍物
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取玩家龙卷风节点
|
|
|
+ getTornadoNode(): Node {
|
|
|
+ const playersUI = StormSunderGlobalInstance.instance.players!;
|
|
|
+ if (!this.tornadoNode) {
|
|
|
+ if (playersUI && playersUI.children.length > 0) {
|
|
|
+ this.tornadoNode = playersUI.children.filter(child => child.getComponent(TornadoComponent))[0]
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return this.tornadoNode;
|
|
|
}
|
|
|
}
|
|
|
|