<commit_before>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<commit_msg>Refactor TileBody to tweening mechanism<commit_after>import Component from "../../Component"
import Time from "../../Time"
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.targetX = 0
		this.targetY = 0		
		this.speed = 250
		this._path = null
		this.direction = new Vector2(0, 0)
		this.onTargetDone = null
		this.onPathDone = null
		this.tStart = 0
		this.duration = 0
	}

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

	update(tDelta) {
		if(this.tStart > 0) {
			let elapsed = (Time.current - this.tStart) / this.duration
			if(elapsed >= 1) {
				this.tStart = 0
				this.direction.set(0, 0)					
				this.parent.position.set(this.targetX, this.targetY)
				if(this.onTargetDone) {
					this.onTargetDone()
				}
				if(this.path && this.path.length > 0) {
					const node = this.path.pop()
					if(node) {
						this.moveTo(node.x, node.y)
						return
					}
				}
				if(this.onPathDone) {
					this.onPathDone()
				}
			}
			else {
				const x = this.startX + (this.targetX - this.startX) * elapsed
				const y = this.startY + (this.targetY - this.startY) * elapsed
				this.parent.position.set(x, y)
			}
		}
		else {
			if(this.path && this.path.length > 0) {
				const node = this.path.pop()
				if(node) {
					this.moveTo(node.x, node.y)
					return
				}
			}			
		}
	}

	moveTo(x, y) {
		if(this.x === x && this.y === y) { 
			return 
		}
		this.parent.parent.getWorldFromTile(x, y, point)
		this.x = x
		this.y = y
		this.startX = this.parent.x
		this.startY = this.parent.y
		this.targetX = point.x
		this.targetY = point.y

		this.direction.set(this.targetX - this.parent.x, this.targetY - this.parent.y)
		this.direction.normalize()
	
		this.tStart = Time.current
		this.duration = this.speed
	}	

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

export default TileBody
{'fraction_non_alphanumeric': 0.14274084124830394, 'fraction_numerical': 0.010583446404341926, 'mean_word_length': 2.5821185617103986, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '107223', 'n_tokens_mistral': 1665, 'n_tokens_neox': 1398, 'n_words': 278}