import { _decorator, Component, Node, Vec3, math, CCInteger, Pool, instantiate, Collider } from 'cc'; import { BuildSpawnCtrl } from './BuildSpawnCtrl'; import { SpawnItemData, SpawnItem } from './SpawnConfig'; import { GameCtl } from '../GameCtl'; import { Constants } from '../data/Constants'; const { ccclass, property } = _decorator; @ccclass('FishSpawnCtrl') export class FishSpawnCtrl extends Component { @property({ type: CCInteger, displayOrder: 2 }) public roadStartZ = 0;//道路 开始的位置 @property({ type: CCInteger, displayOrder: 5 }) public roadLength = 0;//道路长度 @property({ type: CCInteger, displayOrder: 8 }) public recoverOffset = 0;//超过相机多少米回收 @property({ type: [SpawnItemData], displayOrder: 12 //SpawnItemData就是 prefab预制体 和 int 预制的长度 }) public spawnItemDataArray: SpawnItemData[] = []; startCreateZ: number = 0; runtimeSpawnItemList: SpawnItem[] = []; poolMap: Map<string, Pool<SpawnItem>> = new Map<string, Pool<SpawnItem>>(); @property gap = 10;//每个金币之间的 间隔。 x_pos = 0; start() { for (let item of this.spawnItemDataArray) { let temp = new Pool<SpawnItem>(() => { return this.CreateSpawnItem(item) }, 1) this.poolMap.set(item.prefab._uuid, temp); } this.startCreateZ = this.roadStartZ; } update(dt: number) { this.CreateInEnd_z();//创建物体到末尾 this.recoverLessZ();//超过范围的物体回收 } protected endZ(): number { //这个应该是随着 奔跑而变化的 一个数 我们的终点一直在变。 我们小车当前的位置加上道路的长度 return GameCtl._instance.getCurrentZ() + this.roadLength; } CreateInEnd_z() { var p_endZ = this.endZ(); while (this.startCreateZ < p_endZ) { var x = math.randomRangeInt(0, 3); if (x == 0) { x = -GameCtl._instance._interval; } else if (x == 1) { x = 0 } else { x = GameCtl._instance._interval; } this.x_pos = x; //创建3个 为一组 for (let index = 0; index < 3; index++) { let spawnItem = this.DoSpawnItem(this.startCreateZ); if (spawnItem) { this.startCreateZ += spawnItem.spawnItemData.node_length; } } this.startCreateZ += this.gap; } } //创建物体 z是设置的位置 protected DoSpawnItem(z: number): SpawnItem | undefined { //随机创建预制体 let rndIdk = math.randomRangeInt(0, this.spawnItemDataArray.length); var spawnItemData = this.spawnItemDataArray[rndIdk]; let item = this.poolMap.get(spawnItemData.prefab._uuid)?.alloc(); if (item != null) { this.node.scene.addChild(item.node);//取出 this.onSpawn(item.node, spawnItemData, z); } return item; } protected CreateSpawnItem(spawnItemDate: SpawnItemData) { var newGo = instantiate(spawnItemDate.prefab) as Node; this.node.scene.addChild(newGo); /* let collider = newGo.getComponent(Collider) as Collider; console.log(collider); collider.setGroup(Constants.GROUP.OTHER); collider.setMask(-1); */ let spawnItem = new SpawnItem(); spawnItem.node = newGo; spawnItem.spawnItemData = spawnItemDate; //给回收用的 this.runtimeSpawnItemList.push(spawnItem); return spawnItem; } //跑道有三条,障碍物在另外一侧 protected onSpawn(newGo: Node, spawnItemData: SpawnItemData, z: number) { newGo.setPosition(new Vec3(this.x_pos, 0, z));//y轴是离地间隔。 newGo.active = true; } recoverLessZ() { //回收超出镜头的物体 for (const spawnItem of this.runtimeSpawnItemList) { // spawnItemData(prefab预制体 和 node_length 预制的长度) + Node if (spawnItem.node.active) { let length = spawnItem.spawnItemData.node_length;// if (spawnItem.node.position.z + length + this.recoverOffset < GameCtl._instance.getCurrentZ()) { this.poolMap.get(spawnItem.spawnItemData.prefab._uuid)?.free(spawnItem); spawnItem.node.active = false; } } } } }
生成树的代码ObstacleSpawnCtrl
import { _decorator, Component, Node, CCInteger,Animation, Pool, math, instantiate, Vec3, BoxCollider, Collider } from 'cc'; import { GameCtl } from '../GameCtl'; import { SpawnItemData, SpawnItem } from './SpawnConfig'; import { Constants } from '../data/Constants'; const { ccclass, property } = _decorator; @ccclass('ObstacleSpawnCtrl') export class ObstacleSpawnCtrl extends Component { @property({ type: CCInteger, displayOrder: 2 }) public roadStartZ = 0;//道路 开始的位置 @property({ type: CCInteger, displayOrder: 5 }) public roadLength = 0;//道路长度 @property({ type: CCInteger, displayOrder: 8 }) public recoverOffset = 0;//超过相机多少米回收 @property({ type: [SpawnItemData], displayOrder: 12 //SpawnItemData就是 prefab预制体 和 int 预制的长度 }) public spawnItemDataArray: SpawnItemData[] = []; startCreateZ: number = 0; runtimeSpawnItemList: SpawnItem[] = []; poolMap: Map<string, Pool<SpawnItem>> = new Map<string, Pool<SpawnItem>>(); @property gap = 10;//每个障碍之间的 间隔。 x_pos = 0; start() { for (let spawnItemData of this.spawnItemDataArray) { let temp = new Pool<SpawnItem>(() => { return this.CreateSpawnItem(spawnItemData) }, 1) this.poolMap.set(spawnItemData.prefab._uuid, temp); } console.log(this.poolMap.size);//这里是1个。 this.startCreateZ = this.roadStartZ; } update(dt: number) { this.CreateInEnd_z();//创建物体到末尾 this.recoverLessZ();//超过范围的物体回收 } protected endZ(): number { //这个应该是随着 奔跑而变化的 一个数 我们的终点一直在变。 我们小车当前的位置加上道路的长度 return GameCtl._instance.getCurrentZ() + this.roadLength; } CreateInEnd_z() { var p_endZ = this.endZ();//路的终点 while (this.startCreateZ < p_endZ) { var x = math.randomRangeInt(0, 3); if (x == 0) { x = -GameCtl._instance._interval; } else if (x == 1) { x = 0 } else { x = GameCtl._instance._interval; } this.x_pos = x; let spawnItem = this.DoSpawnItem(this.startCreateZ); if (spawnItem) { this.startCreateZ += spawnItem.spawnItemData.node_length; } this.startCreateZ += this.gap; } } //创建物体 z是设置的位置 protected DoSpawnItem(z: number): SpawnItem | undefined { //随机创建预制体 let rndIdk = math.randomRangeInt(0, this.spawnItemDataArray.length); var spawnItemData = this.spawnItemDataArray[rndIdk]; let item = this.poolMap.get(spawnItemData.prefab._uuid)?.alloc();//alloc 是线程池取出来 if (item != null) { this.node.scene.addChild(item.node);//取出 this.onSpawn(item.node, spawnItemData, z); } return item; } protected CreateSpawnItem(spawnItemDate: SpawnItemData) { var newGo = instantiate(spawnItemDate.prefab) as Node; this.node.scene.addChild(newGo); let spawnItem = new SpawnItem(); spawnItem.node = newGo; spawnItem.spawnItemData = spawnItemDate; //给回收用的 this.runtimeSpawnItemList.push(spawnItem); return spawnItem; } //跑道有三条,障碍物在另外一侧 protected onSpawn(newGo: Node, spawnItemData: SpawnItemData, z: number) { newGo.setPosition(new Vec3(this.x_pos, 0, z));//y轴是离地间隔。 newGo.active = true; newGo.getComponent(Animation).play("tree_up"); } recoverLessZ() { //回收超出镜头的物体 for (const spawnItem of this.runtimeSpawnItemList) { // spawnItemData(prefab预制体 和 node_length 预制的长度) + Node if (spawnItem.node.active) { let length = spawnItem.spawnItemData.node_length;// if (spawnItem.node.position.z + length + this.recoverOffset < GameCtl._instance.getCurrentZ()) { this.poolMap.get(spawnItem.spawnItemData.prefab._uuid)?.free(spawnItem); spawnItem.node.active = false; } } } } }
站长微信:xiaomao0055
站长QQ:14496453