/**
 * Created by rockyl on 2019-01-04.
 *
 * 缩放组件
 */

import {createTween, Tween, Ease, ease, ScillaEvent} from "scilla";
import ScillaComponent from "../base/ScillaComponent";

export default class Translation extends ScillaComponent {
	duration: number = 1000;
	autoPlay = false;
	easeName: Ease = Ease.linear;
	delay = 0;
	loop = -1;

	onLoopComplete = new ScillaEvent();
	onComplete = new ScillaEvent();

	private _tween: Tween;

	onAwake() {
		super.onAwake();

		if (this.autoPlay) {
			this.play();
		}
	}

	play() {
		const easeFunc = ease[this.easeName];

		this._tween = createTween(this, this.transform, false, {autoPlay: true, loop: this.loop, onLoopComplete: this._onLoopComplete, onComplete: this._onComplete})
			.set(this.getFromProp(), true)
			.wait(this.delay)
			.to(this.getToProp(), this.duration, easeFunc);
		//this._tween.play(false, this.delay);
	}

	stop() {
		this._tween.stop();
	}

	protected getFromProp(){
		return {};
	}

	protected getToProp(){
		return {};
	}

	private _onLoopComplete=()=>{
		this.onLoopComplete.invoke();
	}

	private _onComplete=()=>{
		this.onComplete.invoke();
	}
}
