"""Original pet reactions and drifting steam; append-only native sprite groups."""
import math
from PIL import Image

def interaction_assets(Art,dog,cat):
 assets=[];groups={}
 def group(name,ims):
  groups[name]=len(assets)
  assets.extend((name+str(i),im) for i,im in enumerate(ims))
 def ease(t):
  t=max(0,min(1,t));return t*t*(3-2*t)
 def dogpet(n):
  t=n/10;p=ease(t/1.2)*ease((5.9-t)/1.5)
  if p<.001:
   im=Image.new('RGBA',(102,80));im.alpha_composite(dog(0),(0,21));return im
  a=Art(102,80)
  def box(s,u):return tuple(x+(y-x)*p for x,y in zip(s,u))
  a.ellipse(box((20,33,91,69),(32,29,81,73)),'#8c6045')
  a.ellipse(box((29,35,86,66),(36,31,77,70)),'#ad8057')
  a.arc(box((51,46,95,70),(52,49,97,74)),330,200,'#c29b6c',5)
  for x in (39,57):a.ellipse(box((25,62,45,70),(x,44,x+10,75)),'#c4a279')
  shake=math.sin(max(0,t-1.2)*math.tau*2.7)*.28*p if 1.2<t<4.1 else 0
  flap=math.sin(max(0,t-1.2)*math.tau*2.7-1)*8*p if 1.2<t<4.1 else 0
  hx=27+20*p;hy=53-33*p
  def pt(x,y):return (hx+x*math.cos(shake)-y*math.sin(shake),hy+x*math.sin(shake)+y*math.cos(shake))
  # Ears hinge at their roots and lag behind the head shake.
  for side in (-1,1):
   a.poly([pt(side*11,-8),pt(side*(16+abs(flap)*.7),5-flap*side),pt(side*(12+abs(flap)),18-flap*side),pt(side*6,6)],'#694b3b')
  a.ellipse((hx-13,hy-12,hx+13,hy+12),'#ba9063')
  for x in (-5,6):
   xx,yy=pt(x,1)
   a.line([(xx-2,yy),(xx,yy+1.5),(xx+2,yy)],'#493b2b',1.1)
  xx,yy=pt(0,9);a.ellipse((xx-8,yy-3,xx+9,yy+7),'#d2b38a')
  xx,yy=pt(1,7);a.ellipse((xx-3,yy-2,xx+3,yy+2),'#3e3530')
  if 1.2<t<4.6:
   xx,yy=pt(3,15);a.ellipse((xx-2,yy-1,xx+2,yy+4),'#c5817e')
  return a.image()
 def catpet(n):
  t=n/10;p=ease(t/.8)*ease((3.9-t)/1)
  if p<.001:return cat(0)
  a=Art(85,88)
  def box(s,u):return tuple(x+(y-x)*p for x,y in zip(s,u))
  a.ellipse(box((11,58,72,83),(25,37,67,83)),'#ae7150')
  a.ellipse(box((16,60,65,81),(30,39,64,81)),'#c59364')
  a.arc(box((13,58,70,83),(20,60,74,84)),0,270,'#865a45',5)
  for j in range(3):a.arc(box((33+j*8,60,41+j*8,73),(30,45+j*8,62,53+j*8)),160,300,'#9d684b',1.5)
  hx=12+20*p;hy=60-38*p
  a.ellipse((hx,hy,hx+25,hy+23),'#c69468')
  a.poly([(hx+1,hy+8),(hx-5*p,hy-5),(hx+11,hy+3)],'#b6805b')
  a.poly([(hx+14,hy+3),(hx+25+5*p,hy-4),(hx+24,hy+10)],'#b6805b')
  for ex in (7,18):
   a.line([(hx+ex-3,hy+9),(hx+ex+2,hy+11)],'#493c32',1.3)
   a.ellipse((hx+ex-.6,hy+10,hx+ex+.5,hy+12),'#d6c777')
  a.poly([(hx+11,hy+14),(hx+14,hy+14),(hx+12.5,hy+16)],'#895d55')
  for side in (-1,1):a.line([(hx+12+side*3,hy+17),(hx+12+side*13,hy+15)],'#e0c19b',.6)
  # Two deliberate swipes toward the viewer; paw grows as it reaches out.
  reach=max(0,math.sin((t-.85)*math.pi*2)) if .85<t<2.85 else 0
  px=35+27*reach*p;py=76-22*p-15*reach*p;radius=4+4*reach*p
  a.line([(39,63),(px,py)],'#c59364',8)
  a.ellipse((px-radius,py-radius,px+radius,py+radius),'#d1a87b')
  if reach>.3:
   for dx in (-3,0,3):a.line([(px+dx,py-3),(px+dx+3,py-8)],'#f0e1b7',.8)
  return a.image()
 def steam(n):
  t=n/23;a=Art(40,44)
  for j,(x,y,r) in enumerate(((20,29,7),(14,22,8),(24,16,8),(19,10,6))):
   r*=.55+.8*t;x+=math.sin(t*4+j)*2
   c=tuple(round(v*(1-t*.35)) for v in (224,227,219))
   a.ellipse((x-r,y-r,x+r,y+r),c)
  im=a.image()
  # Ordered transparency uses the native binary mask, without a dark halo.
  pix=im.load();threshold=max(0,(t-.65)/.35)
  for y in range(im.height):
   for x in range(im.width):
    if ((x*3+y*5)%16)/16<threshold:pix[x,y]=(0,0,0,0)
  return im
 group('dogpet',[dogpet(n) for n in range(60)])
 group('catpet',[catpet(n) for n in range(40)])
 group('steam',[steam(n) for n in range(24)])
 return assets,groups
