123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- import { App } from "../Manager/App";
- import player from "../player";
- export enum LoopType {
- Loop = 0,
- Once = 1,
- PingPong = 2
- }
- export enum LineType {
- any,
- Vertical,
- Horizontal
- }
- export enum ShapeType {
- StraightLine,
- PathLine,
- CircleLine
- }
- const { ccclass, property } = cc._decorator;
- @ccclass
- export default class NewClass extends cc.Component {
- /**移动速度 */
- @property
- public speed: number = 1;
- /**移动pingpong返回速度 */
- @property
- public backSpeed: number = 1;
- private currentSpeed: number = 1;
- /**循环类型 */
- @property({ type: cc.Enum(LoopType) })
- loopType: LoopType = LoopType.Loop;
- /**移动形状类型 */
- @property({ type: cc.Enum(ShapeType) })
- shapeType: ShapeType = ShapeType.StraightLine;
- /**直线类型 */
- @property({ type: cc.Enum(LineType) })
- lineType: LineType = LineType.any;
- /**路径个数 */
- public pathNum: number = 1;
- /** 是否延时开始*/
- @property
- public isDelayStart: boolean = false;
- /** 延时时间*/
- @property
- public delayTime: number = 0;
- /**圆标志 */
- private _circleFlag: boolean = false;
- /**加标志 */
- private _addFlag: boolean = true;
- /** 目标位置组*/
- private posArr: any = [];
- /** 当前位置下标*/
- private currentPosIndex = 0;
- /** 初始位置*/
- private startPos: any = null;
- /**目标位置 */
- private targetPos: any = null;
- /**半径 */
- private radius: number = 0;
- /**角度 */
- @property
- private angle: number = 0;
- @property
- private isPlaySound: boolean = false;
- /** 坐标组 */
- @property([cc.Vec2])
- public positionArray: cc.Vec2[] = [];
- private startMove: boolean = false;
- @property(cc.ParticleSystem)
- public particle: cc.ParticleSystem = null;
- // onLoad () {}
- start() {
- this.startPos = this.positionArray[0];
- this.currentSpeed = this.speed;
- if (this.isDelayStart) this.scheduleOnce(() => { this.SetStutas(this.positionArray); }, this.delayTime);
- else this.SetStutas(this.positionArray);
- }
- /**设置状态 */
- private SetStutas(tempPosArr) {
- this._circleFlag = false;
- this.currentPosIndex = 1;
- if (this.shapeType == ShapeType.PathLine) {
- this.posArr = tempPosArr;
- // this.posArr.unshift(this.startPos);
- } else {
- this.targetPos = tempPosArr[1];
- if (this.shapeType == ShapeType.StraightLine) {
- if (this.lineType == LineType.Vertical) {
- this.targetPos.x = this.node.x;
- } else if (this.lineType == LineType.Horizontal) {
- this.targetPos.y = this.node.y;
- }
- this.posArr = [this.startPos, this.targetPos];
- // console.log('this.posArr', this.posArr);
- } else if (this.shapeType == ShapeType.CircleLine) {
- this._circleFlag = true;
- this.radius = this.startPos.sub(this.targetPos).mag();
- }
- }
- this.startMove = true;
- };
- update(dt) {
- if (!this.startMove) return;
- if (this._circleFlag) this.CircleMove();
- else this.PathMove();
- };
- /**圆形运动 */
- private CircleMove() {
- this.node.position = this.AroundMove(this.targetPos, this.radius, this.angle);
- // console.log(' this.node.position ', this.node.position, 'this._角度 ', this._角度);
- if (this.angle >= 360) {
- if (this.loopType == LoopType.Once) return;
- if (this.loopType == LoopType.Loop) {
- this.angle = 0;
- } else if (this.loopType == LoopType.PingPong) {
- this.currentSpeed = -Math.abs(this.currentSpeed);
- }
- } else if (this.angle <= 0) {
- if (this.loopType == LoopType.PingPong) this.currentSpeed = Math.abs(this.currentSpeed);
- }
- this.angle += this.currentSpeed;
- };
- /**路径移动 */
- private PathMove() {
- let _targetPos = this.posArr[this.currentPosIndex];
- let _distance = this.node.position.sub(_targetPos).mag();
- if (_distance < this.currentSpeed) {
- if (this.loopType == LoopType.Once) return;
- if (this.loopType == LoopType.Loop) {
- if (this.currentPosIndex < this.posArr.length - 1) {
- this.currentPosIndex++;
- } else {
- this.currentPosIndex = 1;
- this.node.position = this.posArr[0];
- return;
- }
- } else if (this.loopType == LoopType.PingPong) {
- this.node.position = _targetPos;
- if (this._addFlag) {
- if (this.currentPosIndex < this.posArr.length - 1) {
- this.currentPosIndex++;
- this.currentSpeed = this.speed;
- } else {
- this._addFlag = false;
- }
- } else {
- if (this.currentPosIndex > 0) {
- this.currentPosIndex--;
- this.currentSpeed = this.backSpeed;
- this.PlayAudio();
- } else {
- this._addFlag = true;
- }
- }
- }
- }
- var returnPos = this.TowardsMove(_targetPos, this.node.position, this.currentSpeed);//, dt
- this.node.position = returnPos;
- };
- private PlayAudio() {
- if (!this.isPlaySound || !App.SoundManager.allowPlayEffect) return;
- var pos1 = this.node.parent.convertToWorldSpaceAR(this.node.getPosition());
- var newVec2 = cc.find('Canvas').convertToNodeSpaceAR(pos1);
- if (this.IsCameraSaw(newVec2)) {
- this.node.getComponent(cc.AudioSource).play();
- if (this.particle) this.particle.resetSystem();
- }
- }
- private IsCameraSaw(targetPos: any): boolean {
- var heights = this.node.height;
- var posY = player.getInstance().node.y + cc.winSize.height + heights;
- var posY1 = player.getInstance().node.y - cc.winSize.height - heights + player.getInstance().cameraOffsetY;
- if (targetPos.y < posY && targetPos.y > posY1) {
- return true;
- } else {
- return false;
- }
- }
- private AroundMove(axis: any, radius: any, angle: any) {
- var x1 = axis.x + radius * Math.sin(angle * Math.PI / 180);
- var y1 = axis.y + radius * Math.cos(angle * Math.PI / 180);
- return new cc.Vec3(x1, y1, 0);
- }
- private TowardsMove(endPos: any, movePos: any, speed: any) { //旋转角度没求//, dt
- var moveDir = endPos.sub(movePos).normalize();
- var vx = moveDir.x * speed;
- var vy = moveDir.y * speed;
- var returnX = movePos.x + vx;//* dt
- var returnY = movePos.y + vy;//* dt
- return new cc.Vec3(returnX, returnY, 0);
- };
- }
|