Source: components/SpotLightHelperComponent.js

import { MathUtils } from '@uino/base-thing';
import { Utils } from '../common/Utils';
import { BaseLineSegmentsHelperComponent } from './BaseLineSegmentsHelperComponent';

/**
 * @class SpotLightHelperComponent
 * The spot light helper component.
 * @memberof THING
 * @extends THING.BaseLineSegmentsHelperComponent
 */
class SpotLightHelperComponent extends BaseLineSegmentsHelperComponent {

	/**
	 * The helper component of spot light object, show auxiliary lines of it.
	 */
	constructor() {
		super();
	}

	// #region Private

	_getTargetPosition() {
		let object = this.object;
		let bodyNode = object.bodyNode;

		return MathUtils.getPositionOnDirection(object.position, object.forward, -bodyNode.getDistance());
	}

	// #endregion

	// #region BaseComponent Interface

	onUpdate(deltaTime) {
		// Update scale by distance
		const bodyNode = this.object.bodyNode;
		const distance = bodyNode.getDistance();
		const coneLength = distance ? distance : 1000;
		const coneWidth = coneLength * Math.tan(bodyNode.getAngle());

		this.lineSegments.setScale([coneWidth, coneWidth, coneLength]);
		this.lineSegments.getStyle().setColorOp(() => {
			return Utils.parseColor(this.object.color);
		});
	}

	onAdd(object) {
		super.onAdd(object);

		const positions = [
			[0, 0, 0], [0, 0, -1],
			[0, 0, 0], [1, 0, -1],
			[0, 0, 0], [-1, 0, -1],
			[0, 0, 0], [0, 1, -1],
			[0, 0, 0], [0, -1, -1]
		];

		for (let i = 0, j = 1, l = 32; i < l; i++, j++) {
			const p1 = (i / l) * Math.PI * 2;
			const p2 = (j / l) * Math.PI * 2;

			positions.push([Math.cos(p1), Math.sin(p1), -1]);
			positions.push([Math.cos(p2), Math.sin(p2), -1]);
		}

		// Begin to build lines
		this.begin();

		for (let i = 0; i < positions.length; i += 2) {
			this.addLine(positions[i], positions[i + 1], [1, 1, 1, 1]);
		}

		// End to build lines
		this.end();
	}

	// #endregion

}

export { SpotLightHelperComponent }