# Hurry Curry! - a game about cooking
# Copyright (C) 2026 Hurry Curry! Contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.
#
@abstract
class_name ActiveInteractCounter
extends Tile

var interact_sound: AudioStreamPlayer3D = AudioStreamPlayer3D.new()
var interact_tool: Node3D
var acting_players: Array[Player] = []
var play_character_animation: Callable

func _init(ctx: TileFactory.TileCC, station_model_: PackedScene, interact_tool_path: NodePath, play_character_animation_: Callable, audio_stream_: AudioStream):
	super(ctx)
	base.add_child(station_model_.instantiate())
	interact_sound.stream = audio_stream_
	add_child(interact_sound)
	interact_tool = base.get_node(interact_tool_path)
	play_character_animation = play_character_animation_

func progress(position_: float, speed: float, warn: bool, acting_players_: Array[Player]):
	super(position_, speed, warn, acting_players)
	
	if speed != 0.:
		if not interact_sound.playing: interact_sound.play()
		acting_players = acting_players.filter(is_instance_valid) # Some players might have disconnected
		var players_who_stopped := G.unordered_array_difference(acting_players, acting_players_)
		for p: Player in players_who_stopped:
			if p != null: play_character_animation.call(p, false)
		for p: Player in acting_players_:
			interact_tool.visible = false
			play_character_animation.call(p, true)
	elif speed == 0:
		interact_sound.stop()
		interact_tool.visible = true
		for p: Player in acting_players:
			play_character_animation.call(p, false)
	acting_players = acting_players_

func finish():
	super()
	acting_players = acting_players.filter(is_instance_valid) # Some players might have disconnected
	for p: Player in acting_players:
		play_character_animation.call(p, false)
	interact_tool.visible = true
	interact_sound.stop()

static func interact_target(): # -> Vector3?
	return Vector3(0., 0.575, 0.)
