|
@@ -1,23 +1,43 @@
|
|
|
-import { _decorator, Component, instantiate, Node, Prefab, resources, UITransform } from 'cc';
|
|
|
|
|
|
|
+import { _decorator, Component, instantiate, Node, Prefab, resources, sys, UITransform } from 'cc';
|
|
|
import { PropBase } from './Prop/Base/PropBase';
|
|
import { PropBase } from './Prop/Base/PropBase';
|
|
|
import { ObjectPoolManager } from './Prop/ObjectPoolManager';
|
|
import { ObjectPoolManager } from './Prop/ObjectPoolManager';
|
|
|
|
|
+import { EventManager } from './EventManager';
|
|
|
const { ccclass, property } = _decorator;
|
|
const { ccclass, property } = _decorator;
|
|
|
|
|
|
|
|
@ccclass('AutoBuild')
|
|
@ccclass('AutoBuild')
|
|
|
export class AutoBuild extends Component {
|
|
export class AutoBuild extends Component {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ ISbuild = true
|
|
|
|
|
+
|
|
|
|
|
+ //多少秒 生成一个
|
|
|
|
|
+ Time = 0.25
|
|
|
|
|
+
|
|
|
|
|
+ // 底板和地刺的生成概率
|
|
|
|
|
+ Probability = [0.8, 0.2]
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
protected onLoad(): void {
|
|
protected onLoad(): void {
|
|
|
|
|
+
|
|
|
ObjectPoolManager.getInstance().initializePools().then((ok) => {
|
|
ObjectPoolManager.getInstance().initializePools().then((ok) => {
|
|
|
if (ok) {
|
|
if (ok) {
|
|
|
console.log('初始化成功');
|
|
console.log('初始化成功');
|
|
|
|
|
+
|
|
|
|
|
+ let floorType = [FloorType.diban, FloorType.dicin]
|
|
|
this.schedule(() => {
|
|
this.schedule(() => {
|
|
|
- this.BuildOnce(FloorType.diban)
|
|
|
|
|
- }, 0.1)
|
|
|
|
|
|
|
+ this.ISbuild && this.BuildOnce(floorType[this.selectByProbability()]);
|
|
|
|
|
+ }, this.Time)
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ Pause(b: boolean) {
|
|
|
|
|
+ this.ISbuild = b
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
BuildOnce(Type: FloorType) {
|
|
BuildOnce(Type: FloorType) {
|
|
|
let Node = ObjectPoolManager.getInstance().getNodeFromPool(Type, this.node)
|
|
let Node = ObjectPoolManager.getInstance().getNodeFromPool(Type, this.node)
|
|
|
if (!Node) {
|
|
if (!Node) {
|
|
@@ -25,31 +45,48 @@ export class AutoBuild extends Component {
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- // this.InitNode(Node)
|
|
|
|
|
|
|
+ // 加一分
|
|
|
|
|
+ EventManager.instance.et.emit(EventManager.EventType.Add_Score);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
ClerarNode() {
|
|
ClerarNode() {
|
|
|
|
|
|
|
|
}
|
|
}
|
|
|
- InitNode(node: Node) {
|
|
|
|
|
- this.node.addChild(node);
|
|
|
|
|
- let size = this.node.getComponent(UITransform).contentSize
|
|
|
|
|
-
|
|
|
|
|
- let width = (size.width - (node.getComponent(UITransform).contentSize.width * 1.33)) / 2;
|
|
|
|
|
- let height = -size.height / 2;
|
|
|
|
|
- height -= 200;
|
|
|
|
|
- const x = this.getRandomNumber(-width, width);
|
|
|
|
|
- const y = height
|
|
|
|
|
-
|
|
|
|
|
- node.setPosition(x, y, 0);
|
|
|
|
|
- node.getComponent(PropBase).UpSpeed = 20
|
|
|
|
|
- }
|
|
|
|
|
|
|
|
|
|
getRandomNumber(min: number, max: number): number {
|
|
getRandomNumber(min: number, max: number): number {
|
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
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 {
|
|
export enum FloorType {
|