42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
|
import { _decorator, CCFloat, Component, Node, randomRangeInt, tween, Vec3 } from 'cc';
|
||
|
const { ccclass, property } = _decorator;
|
||
|
|
||
|
@ccclass('ObstacleBehaviour')
|
||
|
export class ObstacleBehaviour extends Component {
|
||
|
@property([Node])
|
||
|
private targets: Node[] = [];
|
||
|
@property([Node])
|
||
|
private waypoints: Node[] = [];
|
||
|
@property(CCFloat)
|
||
|
private speed: number = 0;
|
||
|
@property({ type: Node, readonly: true })
|
||
|
private selectedObstacle: Node = null;
|
||
|
|
||
|
protected onEnable() {
|
||
|
if (this.targets == null || this.targets.length <= 0) return;
|
||
|
|
||
|
let randomInt = randomRangeInt(0, this.targets.length);
|
||
|
this.selectedObstacle = this.targets[randomInt];
|
||
|
this.waypoints.forEach(element => {
|
||
|
element.setPosition(element.position.x, this.selectedObstacle.position.y, element.position.z);
|
||
|
});
|
||
|
this.selectedObstacle.setPosition(this.waypoints[this.waypoints.length - 1].position);
|
||
|
|
||
|
this.followPath(this.selectedObstacle);
|
||
|
}
|
||
|
|
||
|
private followPath(target: Node) {
|
||
|
const tweenPath = tween(target);
|
||
|
|
||
|
for (let i = 0; i < this.waypoints.length; i++) {
|
||
|
|
||
|
let a = tween(target).to(this.speed, { position: this.waypoints[i].getPosition() })
|
||
|
tweenPath.then(a)
|
||
|
}
|
||
|
// tweenPath.union().repeat(10)
|
||
|
tweenPath.union().repeatForever()
|
||
|
tweenPath.start();
|
||
|
}
|
||
|
}
|
||
|
|