// Learn cc.Class:
//  - https://docs.cocos.com/creator/manual/en/scripting/class.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import {
    Between,
    RandomInt
} from '../script/Z_04_syys_utils';

cc.Class({
    extends: cc.Component,

    properties: {
        targetPosList: [],
        currentTargetIdx: 0,
        speed: 3,
        offsetX: 0,
        offsetY: 0,
    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},

    start() {

    },

    update(dt) {
        const targetPos = this.targetPosList[this.currentTargetIdx];
        if (!targetPos) {
            return;
        }
        this.node.x += this.speed;
        this.node.y += this.speed * Math.tan(this.node.angle * Math.PI / 180);

        const sideX = targetPos.x + this.offsetX - this.node.x;
        const sideY = targetPos.y + this.offsetY - this.node.y;
        if (targetPos.x - this.node.x <= this.speed) {
            this.currentTargetIdx++;
        }
        let newAngle = Math.atan(sideY / sideX) * 180 / Math.PI;
        if (newAngle < -45) {
            newAngle += 90;
        }
        this.node.angle += Between(-1, newAngle - this.node.angle, 1);
    },
});
