import ScillaComponent from 'components/base/ScillaComponent';
import { getX, getY, setXY } from './transformUtils';

export default class Icon extends ScillaComponent {
	private GRAVITY = .7;
	private _vx = 0;
	private _vy = 0;
	onAwake() {
		super.onAwake();
		const children = this.entity.children;
		const index = Math.floor(children.length * Math.random());
		children.forEach((v, i) => v.enabled = index == i);
		const baseY = -100;
		const offsetX = 150;
		const offsetY = -100;
		const randomXoffset = Math.random() * offsetX * 2 - offsetX;
		const randomYoffset = Math.random() * offsetY;
		setXY(this.entity, randomXoffset, baseY + randomYoffset);
		//初始vy小于0
		this._vy = (-Math.random() * 3 - 10) * 2;
		this._vx = (Math.random() * 5 + 5) * (Math.random() > 0.5 ? 1 : -1) * 1;
	}

	onUpdate(t) {
		this._vy += this.GRAVITY;
		super.onUpdate(t);
		const x = getX(this) + this._vx;
		const y = getY(this) + this._vy;
		setXY(this, x, y)
	}
}
