import Unit from './unit';
import SpriteItem from './sprite-item';
import resources from './resources';
import { SpriteCharacter } from './characters';
import { copy, equal, normalize, subtractV, Vector } from './vector';

class Hero extends Unit {
	private _body: SpriteCharacter;
	private _direction: Vector = [1, 0];
	private _exp = 0;
	private _level = 1;

	constructor(x: number, y: number) {
		const body = new SpriteCharacter(
			new SpriteItem(resources.getImage('dungeon'), 128, 68, 16, 28, 4),
		);

		super({
			x,
			y,
			w: 16,
			h: 28,
			speed: 0.02,
			health: 6,
			power: 0,
			body,
		});

		this._body = body;
	}

	reset() {
		this._exp = 0;
		this._level = 1;
		super.reset();
	}

	update(dt: number) {
		this._body.update(dt);
	}

	move(pos: Vector) {
		if (!equal(this.position, pos)) {
			this._direction = normalize(subtractV(copy(pos), this.position));
		}
		super.move(pos);
	}

	addExp(amount: number = 1) {
		this._exp += amount;

		if (this._exp >= this.expForLevel) {
			this._exp -= this.expForLevel;
			this._level++;
			return true;
		}

		return false;
	}

	get expForLevel() {
		return this._level * 3;
	}

	get direction() {
		return this._direction;
	}

	get exp() {
		return this._exp;
	}

	get level() {
		return this._level;
	}
}

export default Hero;
