import {BS} from 'bullet-storm/BS';
import {GameObject} from 'bullet-storm/GameObject';
import {PhysicsObject, CircleCollider} from 'bullet-storm/Physics';

export class EnemyBehaviour
{
	static default(enemy)
	{
		vec3.add(enemy.pos, enemy.pos, vec3.fromValues(0, 0, 0.1));
	}

	static zigZag(enemy)
	{
		vec3.add(enemy.pos, enemy.pos, vec3.fromValues(0, 0, 0.1));

		if(enemy._movdir_ == undefined)
		{
			enemy._movdir_ = 1;
		}

		vec3.add(enemy.pos, enemy.pos, vec3.fromValues(0.1 * enemy._movdir_, 0, 0));

		if(Math.abs(enemy.pos[0]) > 2)
		{
			enemy._movdir_ = -enemy._movdir_;
		}
	}

	static sinWave(enemy)
	{
		vec3.add(enemy.pos, enemy.pos, vec3.fromValues(0, 0, 0.1));

		if(enemy._startx_ == undefined)
		{
			enemy._startx_ = enemy.pos[0];
		}

		enemy.pos[0] = enemy._startx_ + Math.sin(enemy.pos[2] / 2.0) * 4.0;
	}
}

export class Enemy extends GameObject
{
	constructor(pos, behaviour)
	{
		super(pos, BS.SHAPE_TRI_0, BS.SHADER_OBJECT_0, [0.0, 1.0, 0.0, 1.0]);

		this.behaviour = behaviour;
		this.rotation = vec3.fromValues(0, 3.14, 0);
		this.enabled = true;
		this.physicsObject = new PhysicsObject(new CircleCollider(this.pos, 1.0), this);		
	}

	init()
	{
		this.enabled = true;
		this.visible = true;
	}

	update()
	{
		this.behaviour(this);

		if(this.pos[2] > 0)
		{
			this.destroyed = true;
		}
	}

	onCollisionEnter(other)
	{
		this.destroyed = true;
	}
}

export class EnemyManager 
{
	constructor()
	{
		this.enemyPool = new Array();	
	}

	update()
	{

	}

	createEnemy(pos, behaviour)
	{
		return new Enemy(pos, behaviour);
	}
}
