1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- import { _decorator, Component, instantiate, Node, Prefab, resources, sys, UITransform } from 'cc';
- import { PropBase } from './Prop/Base/PropBase';
- import { ObjectPoolManager } from './Prop/ObjectPoolManager';
- import { EventManager } from './EventManager';
- const { ccclass, property } = _decorator;
- @ccclass('AutoBuild')
- export class AutoBuild extends Component {
- ISbuild = true
- //多少秒 生成一个
- Time = 0.25
- // 底板和地刺的生成概率
- Probability = [0.8, 0.2]
- protected onLoad(): void {
- ObjectPoolManager.getInstance().initializePools().then((ok) => {
- if (ok) {
- console.log('初始化成功');
- let floorType = [FloorType.diban, FloorType.dicin]
- this.schedule(() => {
- this.ISbuild && this.BuildOnce(floorType[this.selectByProbability()]);
- }, this.Time)
- }
- });
- }
- Pause(b: boolean) {
- this.ISbuild = b
- }
- BuildOnce(Type: FloorType) {
- let Node = ObjectPoolManager.getInstance().getNodeFromPool(Type, this.node)
- if (!Node) {
- console.error('没有找到节点')
- return
- }
- // // 加一分
- // EventManager.instance.et.emit(EventManager.EventType.Add_Score);
- }
- ClerarNode() {
- }
- getRandomNumber(min: number, max: number): number {
- return Math.floor(Math.random() * (max - min + 1)) + min;
- }
- public selectByProbability(): number {
- if (this.Probability.length === 0) {
- console.error('Probability array is empty');
- return -1;
- }
- const totalProbability = this.Probability.reduce((sum, probability) => sum + probability, 0);
- if (totalProbability !== 1) {
- console.warn('Probability array does not sum to 1, normalizing this.Probability');
- this.Probability = this.Probability.map(p => p / totalProbability);
- }
- const randomValue = Math.random();
- let cumulativeProbability = 0;
- for (let i = 0; i < this.Probability.length; i++) {
- cumulativeProbability += this.Probability[i];
- if (randomValue < cumulativeProbability) {
- return i;
- }
- }
- // 这个返回应该永远不会触发,因为随机值一定会小于等于 1
- return this.Probability.length - 1;
- }
- }
- export enum FloorType {
- diban = 'diban',//预制体的名称
- dicin = 'dici',
- }
|