<commit_before>import AddEventListener, DispatchEvent from require "LunoPunk.utils.EventListener"
import TweenEvent from require "LunoPunk.tweens.TweenEvent"

TweenType = {
	Persist: "Persist"
	Looping: "Looping"
	OneShot: "OneShot"
}

class Tween
	-- Constructor. Specify basic information about the Tween.
	-- @param	duration		Duration of the tween (in seconds or frames).
	-- @param	type			Tween type, one of Tween.PERSIST (default), Tween.LOOPING, or Tween.ONESHOT.
	-- @param	complete		Optional callback for when the Tween completes.
	-- @param	ease			Optional easer function to apply to the Tweened value.
	new: (duration, type = TweenType.Persist, complete = nil, ease = nil) =>
		assert duration != nil, "Cannot create a Tween without a duration"
		@active = false
		@__scale = 0.0
		@__parent = nil
		@__finish = false

		@__target = duration
		@type = if type == nil then "Persist" else type
		@ease = ease
		@t = 0
		@__time = 0

		unless complete == nil
			AddEventListener TweenEvent.FINISH, complete

	-- Updates the Tween, called by World.
	update: =>
		import LP from require "LunoPunk.LP"
		-- Only update the Tween if active
		return if not @active
		@__time += if LP.fixed then 1 else LP.elapsed
		@t = @__time/@__target
		if @ease != nil and @t > 0 and @t < 1
			@t = @ease @t

		if @__time >= @__target
			@t = 0
			@__finish = true

		DispatchEvent TweenEvent TweenEvent.UPDATE
		return

	-- Starts the Tween, or restarts it if it's currently running.
	start: =>
		@__time = 0
		if @__target == 0
			@active = false
			DispatchEvent TweenEvent TweenEvent.FINISH
		else
			@active = true
			DispatchEvent TweenEvent TweenEvent.START
		return

	-- @private Called when the Tween completes.
	finish: =>
		switch @type
			when "Persist"
				@__time = @__target
				@active = false
			when "Looping"
				@__time %= @__target
				@t = @__time/@__target
				if @ease != nil and @t > 0 and @t < 1
					@t = @ease @t
				@start!
			when "OneShot"
				@__time = @__target
				@active = false
				@__parent\removeTween @ unless @__parent == nil

		@__finish = false
		DispatchEvent TweenEvent TweenEvent.FINISH

	-- Immediately stops the Tween and removes it from its Tweener without calling the complete callback.
	cancel: =>
		@active = false
		@__parent\removeTween @ unless @__parent == nil
		return

	percent: (value) =>
		return @__time/@__target if value == nil
		@__time = @__target * value
		@__time

	scale: => @__scale

{ :TweenType, :Tween }<commit_msg>Minor syntactic changes to Tween<commit_after>import AddEventListener, DispatchEvent from require "LunoPunk.utils.EventListener"
import TweenEvent from require "LunoPunk.tweens.TweenEvent"

TweenType = {
	Persist: "Persist"
	Looping: "Looping"
	OneShot: "OneShot"
}

class Tween
	-- Constructor. Specify basic information about the Tween.
	-- @param	duration		Duration of the tween (in seconds or frames).
	-- @param	type			Tween type, one of Tween.PERSIST (default), Tween.LOOPING, or Tween.ONESHOT.
	-- @param	complete		Optional callback for when the Tween completes.
	-- @param	ease			Optional easer function to apply to the Tweened value.
	new: (duration, type = TweenType.Persist, complete = nil, ease = nil) =>
		assert duration != nil, "Cannot create a Tween without a duration"
		@active = false
		@__scale = 0.0
		@__parent = nil
		@__finish = false

		@__target = duration
		@type = if type == nil then "Persist" else type
		@ease = ease
		@t = 0
		@__time = 0

		unless complete == nil
			AddEventListener TweenEvent.FINISH, complete

	-- Updates the Tween, called by World.
	update: =>
		import LP from require "LunoPunk.LP"
		-- Only update the Tween if active
		return unless @active
		@__time += if LP.fixed then 1 else LP.elapsed
		@t = @__time/@__target
		if @ease != nil and @t > 0 and @t < 1
			@t = @ease @t

		if @__time >= @__target
			@t = 0
			@__finish = true

		DispatchEvent TweenEvent TweenEvent.UPDATE
		return

	-- Starts the Tween, or restarts it if it's currently running.
	start: =>
		@__time = 0
		if @__target == 0
			@active = false
			DispatchEvent TweenEvent TweenEvent.FINISH
		else
			@active = true
			DispatchEvent TweenEvent TweenEvent.START
		return

	-- @private Called when the Tween completes.
	finish: =>
		switch @type
			when "Persist"
				@__time = @__target
				@active = false
			when "Looping"
				@__time %= @__target
				@t = @__time/@__target
				if @ease != nil and @t > 0 and @t < 1
					@t = @ease @t
				@start!
			when "OneShot"
				@__time = @__target
				@active = false
				@__parent\removeTween @ unless @__parent == nil

		@__finish = false
		DispatchEvent TweenEvent TweenEvent.FINISH
		return

	-- Immediately stops the Tween and removes it from its Tweener without calling the complete callback.
	cancel: =>
		@active = false
		@__parent\removeTween @ unless @__parent == nil
		return

	percent: (value) =>
		return @__time/@__target if value == nil
		@__time = @__target * value
		@__time

	scale: => @__scale

{ :TweenType, :Tween }
{'fraction_non_alphanumeric': 0.10160642570281124, 'fraction_numerical': 0.004819277108433735, 'mean_word_length': 3.2391489361702126, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 23, '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': '5857615', 'n_tokens_mistral': 1927, 'n_tokens_neox': 1681, 'n_words': 576}