extends Node2D
## Layered runtime prototype. Feet stay at (0, 0) in all four facings.
const Catalog = preload("res://scripts/catalog.gd")
var catalog = Catalog.new()
var appearance: Dictionary = {}
var facing := 0 # south, west, east, north
var walking := false
var phase := 0.0

func _init() -> void:
	appearance = catalog.cosmetics.default.duplicate(true)

func _process(delta: float) -> void:
	if walking:
		phase += delta * 11.0
	else:
		phase = 0.0
	queue_redraw()

func color_slot(slot: String) -> Color:
	return Color("#" + str(catalog.cosmetics.palettes[slot][int(appearance[slot + "_color"])]))

func shape(points: Array, color: Color, border: Color = Color("111925")) -> void:
	var polygon := PackedVector2Array(points)
	draw_colored_polygon(polygon, color)
	polygon.append(polygon[0])
	draw_polyline(polygon, border, 2.2, true)

func oval(center: Vector2, radii: Vector2, color: Color, outline: bool = true) -> void:
	var points: Array = []
	for i in 24:
		var angle := TAU * float(i) / 24.0
		points.append(center + Vector2(cos(angle) * radii.x, sin(angle) * radii.y))
	shape(points, color) if outline else draw_colored_polygon(PackedVector2Array(points), color)

func _draw() -> void:
	var shell := color_slot("shell")
	var top := color_slot("shirt")
	var pants := color_slot("pants")
	var accent := color_slot("accent")
	var hair := color_slot("hair")
	var step := sin(phase) * 3.2 if walking else 0.0
	var bob := absf(sin(phase)) * 1.2 if walking else 0.0
	var side := facing == 1 or facing == 2
	var back := facing == 3
	var flip := -1.0 if facing == 1 else 1.0
	oval(Vector2(0, 0), Vector2(17, 6), Color(0.01, 0.025, 0.04, 0.45), false)
	# Feet and leg layers; wider long trousers genuinely change the silhouette.
	for leg in [-1, 1]:
		var x := float(leg) * (5.0 if side else 8.0)
		var dy := step * float(leg)
		oval(Vector2(x, -5 + dy), Vector2(7.5, 5), shell.darkened(0.08))
		var bottom := int(appearance.bottom)
		var width := 9.0 if bottom == 2 else 6.0
		var length := 16.0 if bottom > 0 else 7.0
		shape([Vector2(x-width, -27), Vector2(x+width, -27), Vector2(x+width+1, -27+length+dy), Vector2(x-width-1, -27+length+dy)], pants)
		if bottom > 0:
			draw_line(Vector2(x-width+2, -24), Vector2(x-width+2, -13+dy), pants.lightened(0.16), 1.6)
	# Far arms are behind the torso. Gloves preserve the white mascot identity.
	for arm in [-1, 1]:
		var ax := float(arm) * (12.0 if side else 18.0)
		var ay := -27.0 + bob + step * float(-arm) * 0.6
		oval(Vector2(ax, ay), Vector2(6, 10), shell)
		draw_line(Vector2(ax + float(arm)*5, ay-4), Vector2(ax + float(arm)*5, ay+4), accent if arm > 0 else Color("ee2896"), 1.6)
	shape([Vector2(-14,-44+bob),Vector2(14,-44+bob),Vector2(18,-26+bob),Vector2(12,-22+bob),Vector2(-12,-22+bob),Vector2(-18,-26+bob)], top)
	# Shirt seams, hoodie pocket and cuffs are independent of the shell palette.
	draw_line(Vector2(-12,-27+bob),Vector2(12,-27+bob),top.lightened(0.15),2)
	if int(appearance.top) == 1:
		oval(Vector2(0,-44+bob),Vector2(16,7),top.lightened(0.1))
		draw_line(Vector2(-6,-39+bob),Vector2(-5,-31+bob),accent,1.5)
		draw_line(Vector2(6,-39+bob),Vector2(5,-31+bob),accent,1.5)
		draw_line(Vector2(-7,-28+bob),Vector2(7,-28+bob),top.lightened(0.3),1.5)
	elif not back:
		shape([Vector2(-3,-36+bob),Vector2(0,-33+bob),Vector2(3,-36+bob),Vector2(5,-34+bob),Vector2(2,-31+bob),Vector2(5,-28+bob),Vector2(3,-26+bob),Vector2(0,-29+bob),Vector2(-3,-26+bob),Vector2(-5,-28+bob),Vector2(-2,-31+bob),Vector2(-5,-34+bob)],accent,accent)
	# Large spherical head, visible crown and low eyes: overhead three-quarter view.
	var head := Vector2(0,-59+bob)
	oval(head, Vector2(23 if not side else 21,22),shell)
	oval(head+Vector2(-2,-7),Vector2(18,13),shell.lightened(0.12),false)
	oval(head+Vector2(8,-12),Vector2(5,3),Color(1,1,1,0.65),false)
	draw_arc(head,23,PI*0.6,PI*1.4,14,Color("ed2698"),2,true)
	draw_arc(head,23,-PI*0.4,PI*0.4,14,accent,2,true)
	if not back:
		var eye_positions: Array = [Vector2(-9,7),Vector2(9,7)] if not side else [Vector2(10*flip,7)]
		for eye in eye_positions:
			oval(head+eye,Vector2(5.5,7),Color("0c1422"))
			oval(head+eye+Vector2(0,1),Vector2(3,4),accent,false)
	# Hair is drawn before hats. Back-facing hair has no face fringe.
	if int(appearance.hair) == 1:
		shape([head+Vector2(-21,-7),head+Vector2(-16,-19),head+Vector2(-4,-24),head+Vector2(13,-20),head+Vector2(22,-8),head+Vector2(8,-12),head+Vector2(-3,-8),head+Vector2(-10,-13)],hair)
	elif int(appearance.hair) == 2:
		shape([head+Vector2(-24,-6),head+Vector2(-18,-20),head+Vector2(-7,-17),head+Vector2(-4,-31),head+Vector2(5,-20),head+Vector2(18,-26),head+Vector2(17,-13),head+Vector2(26,-5),head+Vector2(9,-11),head+Vector2(0,-6),head+Vector2(-8,-12)],hair)
	if int(appearance.hat) == 1:
		shape([head+Vector2(-23,-7),head+Vector2(-18,-23),head+Vector2(0,-28),head+Vector2(19,-21),head+Vector2(23,-7)],top)
		var brim := Vector2(12*flip,-6) if side else Vector2(0, -4 if back else -1)
		oval(head+brim,Vector2(24 if not side else 17,5),top.lightened(0.13))
		draw_line(head+Vector2(-9,-14),head+Vector2(8,-14),accent,3)
	elif int(appearance.hat) == 2:
		shape([head+Vector2(-23,-5),head+Vector2(-22,-19),head+Vector2(-10,-28),head+Vector2(8,-29),head+Vector2(22,-19),head+Vector2(24,-5)],top)
		shape([head+Vector2(-24,-10),head+Vector2(24,-10),head+Vector2(24,-1),head+Vector2(-24,-1)],top.lightened(0.15))
		for x in range(-18,22,8):
			draw_line(head+Vector2(x,-21),head+Vector2(x,-12),top.lightened(0.3),1)
		draw_rect(Rect2(head+Vector2(12,-9),Vector2(6,7)),accent)
