"use strict";
cc._RF.push(module, 'f971b+TGNxM96cklRi+wtVI', 'verlet_render');
// OP13/script/verlet_render.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var verlet_assembler_1 = require("./verlet_assembler");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var PagePoint = /** @class */ (function () {
    function PagePoint(x, y) {
        this.oldPos = this.newPos = cc.v2(x, y);
    }
    return PagePoint;
}());
var VerletRender = /** @class */ (function (_super) {
    __extends(VerletRender, _super);
    function VerletRender() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.textureList = [];
        _this.pointsCount = 30;
        _this.constraintTimes = 100;
        _this.damping = 0.1;
        _this.gravity = 0;
        _this._initedMaterial = false;
        _this._pointList = [];
        _this._angle = 0;
        return _this;
    }
    VerletRender.prototype.onEnable = function () {
        _super.prototype.onEnable.call(this);
        this.initPointList();
        this.draw();
    };
    VerletRender.prototype._resetAssembler = function () {
        var assembler = this._assembler = new verlet_assembler_1.default();
        assembler.init(this);
    };
    VerletRender.prototype._updateMaterial = function () {
        var material = this.getMaterial(0);
        if (material) {
            material.define('CC_USE_MODEL', 1);
            if (this.textureList.length === 2) {
                material.setProperty('texture0', this.textureList[0]);
                material.setProperty('texture1', this.textureList[1]);
            }
        }
    };
    VerletRender.prototype.updateMaterial = function () {
        if (this.textureList.length === 2) {
            this._updateMaterial();
            this._initedMaterial = true;
            return;
        }
    };
    VerletRender.prototype.updateAngle = function (angle) {
        this._angle = angle;
    };
    VerletRender.prototype.getPointList = function () {
        var pointList = [];
        for (var _i = 0, _a = this._pointList; _i < _a.length; _i++) {
            var point = _a[_i];
            pointList.push(new cc.Vec2(point.newPos.x, point.newPos.y));
        }
        return pointList;
    };
    // 初始化质点
    VerletRender.prototype.initPointList = function () {
        for (var i = 0; i < this.pointsCount; ++i) {
            var posX = i / (this.pointsCount - 1) * this.node.width;
            this._pointList.push(new PagePoint(posX, 0));
        }
    };
    VerletRender.prototype.update = function () {
        this.simulate();
        this.applyConstraint();
        this.draw();
    };
    // 使用verlet积分更新位置
    VerletRender.prototype.simulate = function () {
        var gravity = cc.v2(0, this.gravity);
        for (var i = this.pointsCount - 1; i >= 1; i--) {
            var point = this._pointList[i];
            // 速度等于当前位置与上一个位置的差乘上衰减系数
            var velocity = point.newPos.sub(point.oldPos).mul(this.damping);
            // 模拟一个水平放置的绳子，当y小于等于0时，将不再受重力影响
            if (point.newPos.y <= 0) {
                gravity.y = Math.max(0, gravity.y);
            }
            point.oldPos = point.newPos;
            point.newPos = point.newPos.add(velocity);
            point.newPos = point.newPos.add(gravity);
        }
    };
    VerletRender.prototype._updateEndPos = function (endPos) {
        var tailPoint = this._pointList[this.pointsCount - 1];
        tailPoint.newPos = new cc.Vec2(endPos.x, endPos.y);
    };
    VerletRender.prototype._getEndPos = function () {
        var endPos = new cc.Vec2(0, 0);
        var width = this.node.width;
        var rad = this._angle * Math.PI / 180;
        // 与贝塞尔曲线使用相同的运动轨迹
        var per = rad * 2 / Math.PI;
        if (this._angle <= 90) {
            var endPosX = width * (1 - Math.pow(per, 3));
            var endPosY = width * 1 / 4 * (1 - Math.pow(1 - per, 4));
            endPos = new cc.Vec2(endPosX, endPosY);
        }
        else {
            per = per - 1;
            var endPosX = -width * (1 - Math.pow(1 - per, 3));
            var endPosY = width * 1 / 4 * (1 - Math.pow(per, 4));
            endPos = new cc.Vec2(endPosX, endPosY);
        }
        return endPos;
    };
    // 约束纠正
    VerletRender.prototype.applyConstraint = function () {
        // 两个质点之间的固定距离
        var normalDistance = this.node.width / (this.pointsCount - 1);
        var endPos = this._getEndPos();
        for (var t = 0; t < this.constraintTimes; t++) {
            this._updateEndPos(endPos);
            //由最后一个质点开始依次纠正
            for (var i = this.pointsCount - 1; i >= 1; i--) {
                var firstPoint = this._pointList[i - 1];
                var secondPoint = this._pointList[i];
                var delatPos = secondPoint.newPos.sub(firstPoint.newPos);
                var distance = delatPos.mag();
                var fixDirection = null;
                if (distance < normalDistance) {
                    fixDirection = delatPos.normalize().negate();
                }
                else if (distance > normalDistance) {
                    fixDirection = delatPos.normalize();
                }
                else {
                    continue;
                }
                var fixLen = Math.abs(distance - normalDistance);
                if (i == 1) {
                    // 由于第一个质点是固定的，所以只对第二个质点做纠正
                    var fixVector = fixDirection.mul(fixLen);
                    secondPoint.newPos.subSelf(fixVector);
                }
                else {
                    // 将两个质点之间的距离纠正为固定长度
                    var fixHalfVector = fixDirection.mul(fixLen * 0.5);
                    firstPoint.newPos.addSelf(fixHalfVector);
                    secondPoint.newPos.subSelf(fixHalfVector);
                }
            }
        }
    };
    VerletRender.prototype.draw = function () {
        if (!this._initedMaterial) {
            this.updateMaterial();
        }
        this.setVertsDirty();
    };
    __decorate([
        property({ type: [cc.Texture2D], displayName: "纹理" })
    ], VerletRender.prototype, "textureList", void 0);
    __decorate([
        property({ displayName: "每条边上的顶点数量" })
    ], VerletRender.prototype, "pointsCount", void 0);
    __decorate([
        property({ displayName: "纠正次数" })
    ], VerletRender.prototype, "constraintTimes", void 0);
    __decorate([
        property({ displayName: "速度衰减系数" })
    ], VerletRender.prototype, "damping", void 0);
    __decorate([
        property({ displayName: "重力" })
    ], VerletRender.prototype, "gravity", void 0);
    VerletRender = __decorate([
        ccclass
    ], VerletRender);
    return VerletRender;
}(cc.RenderComponent));
exports.default = VerletRender;

cc._RF.pop();