import Component from "../../Component"
import Vector2 from "../../math/Vector2"

const point = new Vector2()
const direction = new Vector2()

class TileBody extends Component
{
	constructor() {
		super()
		this.x = 0
		this.y = 0
		this.speed = 60
		this.speedX = 0
		this.speedY = 0
		this.targetX = 0
		this.targetY = 0
		this._target = false
		this._path = null
		this.isTargetValid = null
		this.onTargetDone = null
	}

	onEnable() {
		this.targetX = this.parent.x
		this.targetY = this.parent.y
		this.parent.parent.getTileFromWorld(this.targetX, this.targetY, point)
		this.x = point.x
		this.y = point.y
	}

	update(tDelta) {
		this.speedX = 0
		this.speedY = 0

		if(this._target) {
			let speed = this.speed * tDelta

			direction.set(this.targetX - this.parent.x, this.targetY - this.parent.y)
			const distance = direction.length()
			if(distance <= speed) {
				speed = distance
				if(this._path && this._path.length > 0) {
					const node = this._path.pop()
					this.target(node.x, node.y)
				}
				else {
					this._target = false
				}
			}
			direction.normalize()
			this.speedX = direction.x * speed
			this.speedY = direction.y * speed
			this.parent.move(this.speedX, this.speedY)

			if(!this._target) {
				this.speedX = 0
				this.speedY = 0
				this._target = false
				if(this.onTargetDone) {
					this.onTargetDone()
				}			
			}
		}
	}

	stop() {
		this._target = false
		this._path = null
	}

	target(x, y) {
		if(this.isTargetValid) {
			if(!this.isTargetValid(x, y)) {
				this.stop()
				return
			}
		}
		this.parent.parent.getWorldFromTile(x, y, point)
		this.x = x
		this.y = y
		this.targetX = point.x
		this.targetY = point.y
		this._target = true
	}

	path(path) {
		this._path = path
		if(!this._target && path && path.length > 0) {
			const node = path.pop()
			this.target(node.x, node.y)
		}
	}
}

export default TileBody
import Component from "../../Component"
import Vector2 from "../../math/Vector2"

const point = new Vector2()
const direction = new Vector2()

class TileBody extends Component
{
	constructor() {
		super()
		this.x = 0
		this.y = 0
		this.speed = 60
		this.speedX = 0
		this.speedY = 0
		this.targetX = 0
		this.targetY = 0
		this.targeting = false
		this.onTargetDone = null
	}

	onEnable() {
		this.parent.parent.getWorldFromTile(this.x, this.y, point)
		this.parent.position.set(point.x, point.y)	
	}

	update(tDelta) {
		this.speedX = 0
		this.speedY = 0

		if(this.targeting) {
			let speed = this.speed * tDelta

			direction.set(this.targetX - this.parent.x, this.targetY - this.parent.y)
			const distance = direction.length()
			if(distance <= speed) {
				speed = distance
				this.targeting = false
			}
			direction.normalize()
			this.speedX = direction.x * speed
			this.speedY = direction.y * speed
			this.parent.move(this.speedX, this.speedY)

			if(!this.targeting) {
				this.speedX = 0
				this.speedY = 0
				this.targeting = false			
				if(this.onTargetDone) {
					this.onTargetDone()
				}			
			}
		}
	}

	target(x, y) {
		this.parent.parent.getWorldFromTile(x, y, point)
		this.x = x
		this.y = y
		this.targetX = point.x
		this.targetY = point.y
		this.targeting = true
	}

	tile(x, y) {
		this.x = x
		this.y = y
		if(this.parent.parent) {
			this.parent.parent.getWorldFromTile(data.x, data.y, point)
			this.parent.position.set(point.x, point.y)	
		}
	}
}

export default TileBody
Update TileBody component functionality