using System.Collections.Generic; using UnityEngine; namespace Auralis { // Room-local scenery with bounded parallax. Fixed tread modules retain the // authored geometry; nearer scenery is confined beneath those treads. [DefaultExecutionOrder(10100)] public sealed class RoomDepthLayers:MonoBehaviour { public sealed class Plane { public SpriteRenderer Renderer;public Vector3 Anchor;public float Follow,Limit,Alpha;public bool Front,HorizontalOnly; } public readonly List Planes=new(); public readonly List Surfaces=new(); public readonly List PaintedSurfaces=new(); public Vector2 Origin{get;private set;} public bool Interior{get;private set;} public string Theme{get;private set;} public SpriteRenderer Source{get;private set;} public RoomArchitectureCutout Cutout{get;private set;} public DungeonDepthBackdrop FarBackdrop{get;private set;} Transform owner;Bounds region;bool built;readonly List owned=new(); public static RoomDepthLayers Attach(Transform owner,Vector2 origin,bool interior,Bounds region,SpriteRenderer source=null) { if(!WorldDepthComposition.Enabled)return null; var go=new GameObject("Raumtiefe · getrennte Kulissen und Spielflächen");go.transform.SetParent(owner,false); var stage=go.AddComponent();stage.owner=owner;stage.Origin=origin;stage.Interior=interior;stage.region=region;stage.Source=source;return stage; } void Start()=>Build(); public void Build() { if(built)return;built=true; string level=GameSession.I.Save.Data.currentLevel; bool coast=level==LevelCatalog.WindFerry||level==LevelCatalog.Coast||level==LevelCatalog.BellWatch; bool root=level==LevelCatalog.SleepingIslands||level==LevelCatalog.MistWorkshop||level==LevelCatalog.RootCathedral||level==LevelCatalog.RiverWithoutBanks; bool pale=level==LevelCatalog.OpenSky||level==LevelCatalog.SecondsCity||level==LevelCatalog.LastConnection||Source&&Source.sprite.texture.name=="CrownHeartEpilogue"; bool archive=level==LevelCatalog.StarArchive||level==LevelCatalog.ArchiveMemory||level==LevelCatalog.GlassGarden||level==LevelCatalog.Observatory; bool late=archive||root||pale||level==LevelCatalog.CrownHeart; bool enclosed=Interior||level==LevelCatalog.ArchiveMemory||level==LevelCatalog.Observatory; Theme=Interior?"vault":coast?"coast":root?"roots":pale||level==LevelCatalog.CrownHeart?"crown":archive?"archive":"garden"; var early=owner.GetComponent(); // Wet bays share source registration with live water. Only dry rear // architecture moves, together with its attached lamps, behind fixed piers. if(early&&early.Family!="cistern-embers"&&early.Family!="supply-veins") foreach(var bay in early.Backgrounds) Planes.Add(new Plane{Renderer=bay,Anchor=bay.transform.position,Follow=.08f,Limit=.28f,Alpha=bay.color.a,HorizontalOnly=true}); var banks=new List(); foreach(var box in owner.GetComponentsInChildren(true)) { if(box.isTrigger||box.gameObject.layer!=6||box.attachedRigidbody||box.GetComponentInParent()||box.GetComponentInParent())continue; // Late flight rooms also have broad, flat ceiling guards. They // are bounds, not treads: never paint or anchor scenery to them. if(!Interior&&late&&!box.GetComponent())continue; var b=box.bounds; // Inactive rooms have empty Collider.bounds: derive the axis-aligned authored bank. Vector3 center=box.transform.TransformPoint(box.offset);Vector3 size=Vector3.Scale(box.size,box.transform.lossyScale); b=new Bounds(center,new Vector3(Mathf.Abs(size.x),Mathf.Abs(size.y),.1f)); if(b.size.x<2||b.size.y>(Interior?4:1)||b.size.y<=0||Mathf.Abs(Mathf.DeltaAngle(box.transform.eulerAngles.z,0))>.01f||!region.Intersects(b))continue; banks.Add(box); } banks.Sort((a,b)=>a.transform.position.x.CompareTo(b.transform.position.x)); if(Source)Cutout=RoomArchitectureCutout.Attach(Source,banks); if(Cutout) { string sky=DistantArt(level,Source?Source.sprite.texture.name:""); if(!Resources.Load("GameArt/"+sky))sky=root?"LateVeinDepthV1":"WaterFarSky"; var far=ArtLibrary.Sprite("01 · unabhängige Raumferne",ArtLibrary.Get(sky),Origin,-90,transform,false); if(enclosed)far.color=new Color(.62f,.69f,.78f); FarBackdrop=DungeonDepthBackdrop.Attach(far,new Vector2(768,512),region.center,.018f); } bool openDepth=Cutout||coast||level==LevelCatalog.StarArchive; bool buildSurfaces=!Interior&&(coast&&level==LevelCatalog.WindFerry||!coast); // Painted rooms own their materials. Separate their existing near // faces instead of covering stone stairs with a generic timber kit. // Dungeon banks already use purpose-built family kits. foreach(var bank in banks) if(buildSurfaces&&!bank.GetComponent()&&bank.enabled&&(Source||bank.gameObject.activeInHierarchy)) { if(Source) { var front=PaintedBankFronts.Attach(bank,Source); if(front)PaintedSurfaces.Add(front); } else Surfaces.Add(WorldSurfaceModules.Attach(bank,bank.GetComponent()?.Material==GroundMaterial.Wood,pale,root)); } int used=0; bool authoredInterior=Interior&&owner.GetComponentInChildren(true); if(!Interior&&late&&Cutout) LateCampaignDepthFragments.Compose(this,banks,region,Theme,enclosed); foreach(var bank in banks) { Vector3 center=bank.transform.TransformPoint(bank.offset);Vector3 size=Vector3.Scale(bank.size,bank.transform.lossyScale); float width=Mathf.Abs(size.x),top=center.y+Mathf.Abs(size.y)*.5f; if(width<7||used>=3)continue; bool left=used%2==0;float x=center.x+(left?-.34f:.34f)*width; // Never frame a ladder, door or a landing with an opaque near object. bool corridor=false; foreach(var ladder in owner.GetComponentsInChildren(true))if(Mathf.Abs(ladder.X-x)<1.5f&&top>=ladder.Bottom&&top<=ladder.Top+.2f)corridor=true; if(corridor){x=center.x+(left?.21f:-.21f)*width;} // Later rooms compose their chapter fragments once, rather than // repeating the garden island/relief underneath every wide bank. if(!authoredInterior&&enclosed&&(Interior||!late)) { // This relief is behind the play architecture, never a second // transparent platform painted across the existing room face. Piece("02 · nahes Gewölberelief",DungeonArt.Prop(2),new Vector2(x,top-.12f),1.7f,-12,new Color(.30f,.36f,.39f,1),.10f,.35f,false,left); } else if(!authoredInterior&&openDepth&&(Interior||!late)) { Piece("02 · ferne Insel hinter dem Steg",ArtLibrary.Get("WaterGardenIsles",coast?1:2),new Vector2(x+(left?-4:4),top-5.8f),7.4f,-40,root?new Color(.65f,.54f,.83f,.78f):new Color(.71f,.81f,.86f,.78f),.56f,2.8f,false,!left); Piece("03 · nähere Felsschicht",ArtLibrary.Get("DepthAtlas",1),new Vector2(x,top-4.3f),4.3f,-20,root?new Color(.58f,.42f,.71f,1):new Color(.68f,.76f,.76f,1),.24f,1.4f,false,left); } // Space small attached foreground details along actual banks, so a // wide room does not lose its front plane when the camera pans. // Authored paintings already supply their foreground faces (or // an explicit partition such as NightGardenLayers). Do not hang // unrelated dungeon textiles across these original materials. int fronts=Source||authoredInterior?0:Mathf.Clamp(Mathf.CeilToInt(width/14),1,4); for(int n=0;n("GameArt/DungeonHearthPilotForegroundV1"); var cloth=Sprite.Create(texture,new Rect(61,1024-925,331,840),new Vector2(.5f,1),100,0,SpriteMeshType.FullRect);owned.Add(cloth); float fx=center.x-width*.5f+width*(n+.5f)/fronts; bool blocksLadder=false; foreach(var ladder in owner.GetComponentsInChildren(true))if(Mathf.Abs(ladder.X-fx)<1&&top>=ladder.Bottom&&top<=ladder.Top+.3f)blocksLadder=true; if(!blocksLadder)Piece("05 · am vorderen Sims befestigtes Gewebe",cloth,new Vector2(fx,top-.28f),.42f,25,new Color(.76f,.79f,.79f,1),0,.1f,true,left); } used++; } } static string DistantArt(string level,string source) { if(source=="CrownHeartCourt"||source=="CrownHeartArena")return "CrownNightDistantV1"; if(source=="CrownHeartVista")return "CrownVistaDistantV1"; if(source=="CrownHeartEpilogue")return "CrownEpilogueDistantV1"; if(source=="RootCathedralHub")return "NightRootsDistantSkyV1"; if(source=="RootCathedralFlight")return "RootFlightDistantV1"; if(level==LevelCatalog.ArchiveMemory)return "ArchiveMemoryDistantV1"; if(level==LevelCatalog.SecondsCity)return "SecondsCityDistantV1"; if(level==LevelCatalog.MistWorkshop)return "MistWorkshopDistantV1"; if(level==LevelCatalog.RootCathedral)return "RootCathedralDistantV1"; if(level==LevelCatalog.CrownHeart)return "CrownHeartDistantV1"; if(level==LevelCatalog.SleepingIslands)return "NightRootsDistantSkyV1"; if(level==LevelCatalog.GlassGarden)return "GlassGardenDistantV1"; if(level==LevelCatalog.Observatory)return "ObservatoryDistantV1"; if(level==LevelCatalog.RiverWithoutBanks)return "RiverDistantV1"; if(level==LevelCatalog.LastConnection)return "LastConnectionDistantV1"; return "WaterFarSky"; } void Piece(string label,Sprite sprite,Vector2 at,float width,int order,Color tint,float follow,float limit,bool front,bool flip) { var sr=ArtLibrary.Sprite(label,sprite,at,order,transform,false);ArtLibrary.Width(sr,width);sr.color=tint;sr.flipX=flip; Planes.Add(new Plane{Renderer=sr,Anchor=at,Follow=follow,Limit=limit,Alpha=tint.a,Front=front}); } void LateUpdate() { var camera=Camera.main;if(!camera)return; Vector2 delta=(Vector2)camera.transform.position-Origin; foreach(var p in Planes) { float x=p.Limit*(float)System.Math.Tanh(delta.x*p.Follow/p.Limit),y=p.HorizontalOnly?0:p.Limit*(float)System.Math.Tanh(delta.y*p.Follow/p.Limit); p.Renderer.transform.position=p.Anchor+new Vector3(x,y,0); if(p.Front&&GameSession.I.Player) { var body=GameSession.I.Player.transform.position+Vector3.up*.7f; var b=p.Renderer.bounds;b.Expand(new Vector3(2.2f,1.3f,0)); var color=p.Renderer.color;color.a=p.Alpha*(b.Contains(new Vector3(body.x,body.y,b.center.z))?.12f:1);p.Renderer.color=color; } } } void OnDestroy(){foreach(var sprite in owned)if(sprite)Destroy(sprite);} } }