using System; using System.Collections.Generic; using UnityEngine; namespace Auralis { // Authored room composition: existing supports, finite enemies, lifts and chest receipts. // This never replaces story gates or disables earned flight. [DefaultExecutionOrder(-80)] public sealed class LateConnectedPassage:MonoBehaviour { public sealed class Stop { public BoxCollider2D Bank; public Vector2 Stand; public Func Ready; public string Hint; } public readonly List Stops=new(); public readonly List ReturnBanks=new(); public readonly List Groups=new(); public readonly List Splinters=new(); public readonly List MistCreatures=new(); public GardenTreasure Reward{get;private set;} public MovingLift Carrier{get;private set;} public int Reached{get;private set;} public bool Earned=>GameSession.I&&GameSession.I.Save.Data.mechanisms.Contains("connected-route-earned-"+Id); public string Id{get;private set;} public bool Peaceful{get;private set;} public float RideDistance{get;private set;} public Func Context; public Vector2 Start,Finish; readonly List sprites=new(); readonly Dictionary arrivals=new(); readonly Dictionary arrived=new(); Sprite middle,leftCap,rightCap;GroundMaterial material; int serial=-1;float stepGrace;Vector2 previousCarrier;bool carrierWasSupported,carrierStarted; Func allCleared=()=>true; public static LateConnectedPassage Create(Transform parent,string id,Func context,bool peaceful,bool crown) { var o=new GameObject("Zusammenhängende Spielstrecke · "+id);o.transform.SetParent(parent,false); var p=o.AddComponent();p.Id=id;p.Context=context;p.Peaceful=peaceful;p.material=crown?GroundMaterial.Stone:GroundMaterial.Wood; if(RoomKey(id)!=null)return p; var texture=Resources.Load(crown?"GameArt/LateCrownArchitectureV1":"GameArt/LateRootArchitectureV1"); if(!texture)throw new InvalidOperationException("Missing native route architecture for "+id); if(crown){p.middle=p.Cut(texture,100,338,98,223);p.leftCap=p.Cut(texture,24,100,98,223);p.rightCap=p.Cut(texture,1437,1513,98,223);} else{p.middle=p.Cut(texture,320,512,316,434);p.leftCap=p.Cut(texture,14,86,316,434);p.rightCap=p.Cut(texture,1450,1522,316,434);} return p; } static string RoomKey(string id)=>id switch { LateConnectedRoutes.NightId=>"night-roots", LateConnectedRoutes.MistId=>"mist-tension", LateConnectedRoutes.CarryId=>"root-carry", LateConnectedRoutes.ClockId=>"seconds-clock", LateConnectedRoutes.WeaverId=>"seconds-weaver", "hearth-last-protection-banks"=>"last-hearth-hall", RootCathedralTurnPassage.RouteId=>"root-turn", _=>null }; Sprite Cut(Texture2D t,float a,float b,float top,float bottom) {var s=Sprite.Create(t,new Rect(a,t.height-bottom,b-a,bottom-top),new Vector2(.5f,1),100,0,SpriteMeshType.FullRect);sprites.Add(s);return s;} public BoxCollider2D Bank(string title,Vector2 at,float width,bool fragile=false,bool moving=false) { var o=new GameObject(title);o.transform.SetParent(transform,false);o.transform.position=at;o.layer=6; var c=o.AddComponent();c.size=new Vector2(width,.18f);c.offset=Vector2.down*.09f; o.AddComponent();o.AddComponent().Material=material;o.AddComponent(); Dress(c,title,moving); if(fragile) { for(int i=0;i<2;i++) { var crack=ArtLibrary.Sprite("Angekündigter Riss",ArtLibrary.Solid,at+new Vector2((i-.5f)*.42f,-.22f),14,o.transform,false); crack.color=new Color(.12f,.08f,.17f,.9f);crack.transform.localScale=new Vector3(.03f,.26f,1);crack.transform.localRotation=Quaternion.Euler(0,0,i==0?25:-30); } CrumblingPlatform.Attach(c,1.35f,3.8f); } return c; } public void Dress(BoxCollider2D c,string title,bool moving=false) { var roomKey=RoomKey(Id); if(roomKey!=null) { // Reused source galleries already have their complete face. if(c.transform.parent!=transform){if(!c.GetComponent())c.gameObject.AddComponent();return;} RoomIntegratedPlatformArt.Dress(c,roomKey,moving);return; } if(!c.GetComponent())c.gameObject.AddComponent(); const float scale=100*WorldSurfaceModules.TexelScale; float width=c.size.x,lw=leftCap.bounds.size.x*scale,rw=rightCap.bounds.size.x*scale; var at=(Vector2)c.transform.position+Vector2.up*(c.offset.y+c.size.y*.5f); var face=ArtLibrary.Sprite(title+" · native Front",middle,at,13,c.transform);face.drawMode=SpriteDrawMode.Tiled; face.size=new Vector2(Mathf.Max(.01f,(width-lw-rw)/scale),middle.bounds.size.y);face.transform.localScale=Vector3.one*scale; var left=ArtLibrary.Sprite(title+" · linker Abschluss",leftCap,at+Vector2.left*(width-lw)*.5f,13,c.transform); var right=ArtLibrary.Sprite(title+" · rechter Abschluss",rightCap,at+Vector2.right*(width-rw)*.5f,13,c.transform); left.transform.localScale=right.transform.localScale=Vector3.one*scale; if(material==GroundMaterial.Wood)face.color=left.color=right.color=new Color(.90f,.78f,1); } public BoxCollider2D Step(string title,Vector2 at,float width,string hint,Func ready=null) {var c=Bank(title,at,width);Use(c,at,hint,ready);return c;} public void Use(BoxCollider2D c,Vector2 stand,string hint,Func ready=null) {Stops.Add(new Stop{Bank=c,Stand=stand,Hint=hint,Ready=ready});arrivals[c]=0;arrived[c]=false;} public BoxCollider2D Ride(string title,Vector2 from,Vector2 to,float width,string hint) { var bank=Bank(title,(from+to)*.5f,width,false,true);Carrier=bank.gameObject.AddComponent(); Carrier.Amplitude=(to.y-from.y)*.5f;Carrier.HorizontalTravel=to.x-from.x;Carrier.CycleSeconds=10;Carrier.ConfigureDrive(); Use(bank,from,hint,()=>RideDistance>=.85f);return bank; } public BoxCollider2D Return(string title,Vector2 at,float width,bool fragile=false) {var bank=Bank(title,at,width,fragile);ReturnBanks.Add(bank);return bank;} static bool Supported(PlayerMotor2D p,BoxCollider2D c) { if(!p||!c||!c.enabled||!p.Grounded||p.Climbing||p.Flying||p.Rescuing||p.EnteringDoor)return false; var at=p.GetComponent().position;var b=c.bounds; return at.x>=b.min.x+.16f&&at.x<=b.max.x-.16f&&Mathf.Abs(at.y-b.max.y)<.09f; } public bool CanFight(BoxCollider2D bank) { if(Peaceful||Context==null||!Context()||!arrived.TryGetValue(bank,out bool ready)||!ready)return false; var p=GameSession.I.Player.GetComponent().position;var b=bank.bounds; return p.x>=b.min.x-.2f&&p.x<=b.max.x+.2f&&p.y>=b.max.y-.25f&&p.y<=b.max.y+3; } void RequireClear(Func cleared){var earlier=allCleared;allCleared=()=>earlier()&&cleared();} public LittleFoeGroup Buds(BoxCollider2D bank,string suffix,Vector2 left,Vector2 right,Func previous=null) { var group=LittleFoeGroup.Create(transform,Id+"-"+suffix,LittleFoeKind.DuskBud,left,right,2,true,0, ()=>CanFight(bank)&&(previous==null||previous()));Groups.Add(group);RequireClear(()=>Peaceful||group.Cleared);return group; } public Func Crowns(BoxCollider2D bank,string suffix,Vector2 left,Vector2 right,Func previous=null) { var bounds=Rect.MinMaxRect(left.x,left.y+.1f,right.x,left.y+3.1f); Kronensplitter first=null,second=null; first=Kronensplitter.Create(transform,Id+"-"+suffix+"-0",Vector2.Lerp(left,right,.7f)+Vector2.up*1.3f,bounds, ()=>CanFight(bank)&&(previous==null||previous())); second=Kronensplitter.Create(transform,Id+"-"+suffix+"-1",Vector2.Lerp(left,right,.85f)+Vector2.up*1.65f,bounds, ()=>CanFight(bank)&&!first.Alive&&(previous==null||previous()),.6f); Splinters.Add(first);Splinters.Add(second);Func cleared=()=>Peaceful||!first.Alive&&!second.Alive;RequireClear(cleared);return cleared; } public MistWorkshopCreature Mist(BoxCollider2D bank,string id,MistWorkshopSpecies species,Vector2 feet,Vector2 barrier,MistThreatBudget budget,Func previous=null) { var foe=MistWorkshopCreature.Create(id,species,transform,feet,barrier,budget,()=>CanFight(bank)&&(previous==null||previous())); if(Peaceful)foe.gameObject.SetActive(false); MistCreatures.Add(foe);RequireClear(()=>Peaceful||!foe.Alive);return foe; } public void Chest(Vector2 at,string title,int amount=35) { string key="chest-loot-"+Id+"-gift|";var data=GameSession.I.Save.Data; if(!data.mechanisms.Exists(k=>k.StartsWith(key,StringComparison.Ordinal))) {string loot=key+"H|"+amount;data.mechanisms.Add(loot);try{GameSession.I.Save.Write();}catch{data.mechanisms.Remove(loot);throw;}} Reward=GardenTreasure.Create(Id+"-gift",at,transform);Reward.FoundTitle=title; Reward.LockedHint="Dem verbundenen Weg über die festen Landungen folgen";Reward.Available=()=>Context()&&Earned; Reward.gameObject.SetActive(Earned||Reward.Opened); QuestBeacon.Create(transform,at+Vector2.up*1.15f,.25f,ArtLibrary.Teal,()=>Context()&&Earned&&!Reward.Opened); } public string Guidance=>Earned?Reward&&!Reward.Opened?"Freiwilliger Wegfund · danach zum bestehenden Bogen":"Bestehender Bogen und breiter Rückweg bleiben offen": Stops.Count==0?"Dem bestehenden Weg folgen":Stops[Mathf.Min(Reached,Stops.Count-1)].Hint; void Update() { var g=GameSession.I;if(!g||!g.Player||!g.Playing||Context==null||!Context())return; var p=g.Player; if(serial!=p.TeleportSerial) {serial=p.TeleportSerial;Reached=0;RideDistance=stepGrace=0;carrierWasSupported=carrierStarted=false;foreach(var c in new List(arrivals.Keys)){arrivals[c]=0;arrived[c]=false;}} foreach(var stop in Stops) { var c=stop.Bank;var b=c.bounds;var feet=p.GetComponent().position; bool near=feet.x>=b.min.x-.2f&&feet.x<=b.max.x+.2f&&feet.y>=b.max.y-.25f&&feet.y<=b.max.y+3; if(!near){arrived[c]=false;arrivals[c]=0;} if(!arrived[c]){arrivals[c]=Supported(p,c)?arrivals[c]+Time.deltaTime:0;if(arrivals[c]>0)arrived[c]=true;} } if(Carrier) { bool support=Supported(p,Carrier.GetComponent()); carrierStarted|=support;Carrier.RequestPower(carrierStarted); if(support&&carrierWasSupported)RideDistance+=Vector2.Distance(Carrier.transform.position,previousCarrier); carrierWasSupported=support;previousCarrier=Carrier.transform.position; } if(Reached=.22f){Reached++;stepGrace=0;} } if(!Earned&&Reached==Stops.Count&&allCleared()) { string receipt="connected-route-earned-"+Id;g.Save.Data.mechanisms.Add(receipt); try{g.Save.Write();}catch{g.Save.Data.mechanisms.Remove(receipt);throw;} if(Reward)Reward.gameObject.SetActive(true);AudioDirector.I?.Play("discovery",Finish,.35f); } } void OnDisable(){stepGrace=0;foreach(var c in new List(arrivals.Keys)){arrivals[c]=0;arrived[c]=false;}} void OnDestroy(){foreach(var s in sprites)if(s)Destroy(s);} } }