using UnityEngine; namespace Auralis { [RequireComponent(typeof(Rigidbody2D),typeof(BoxCollider2D))] public sealed partial class PlayerMotor2D : MonoBehaviour { public Vector2 Velocity {get;private set;} public bool Grounded {get;private set;} public MovingLift LiftSupport{get;private set;} public GroundMaterial LastLandingMaterial{get;private set;} public int Facing {get;private set;}=1; // Whole-heart callers remain compatible; damage and healing never round a half heart. int healthHalfUnits; public int Health {get=>(healthHalfUnits+1)/2;private set=>healthHalfUnits=Mathf.Max(0,value)*2;} public float HealthHearts=>healthHalfUnits*.5f; public int HealthHalfUnits=>healthHalfUnits; public int Heals=>GameSession.I.Save.Data.healCharges; public bool Gliding {get;private set;} public float Invincible {get;private set;} public void ProtectForTransition(float seconds){Invincible=Mathf.Max(Invincible,Mathf.Clamp(seconds,0,2));} public bool Dashing=>dashTime>0||Sliding; public Vector2 SafePoint {get;private set;} public string PoseName=>visual&&visual.sprite?visual.sprite.name:""; Rigidbody2D body;BoxCollider2D box;GameConfig cfg;PlayerInputRouter input;SpriteRenderer visual,shadow; float coyote,buffer,dashTime,dashCooldown,animTime,healTime,shotPose,landingTime,glideTime;bool airDash,freeFallUsed;float visualScale=1; readonly SpriteRenderer[] echoes=new SpriteRenderer[8];readonly float[] echoLife=new float[8];int echoCursor;float echoTimer; RaycastHit2D[] hits=new RaycastHit2D[16];ContactFilter2D filter; WeaveBridge movingThreadSupport; WalkableProfile walkingProfileSupport; public void Init(GameConfig c,PlayerInputRouter i) { cfg=c;input=i;body=GetComponent();body.bodyType=RigidbodyType2D.Kinematic;body.interpolation=RigidbodyInterpolation2D.Interpolate; box=GetComponent();box.size=new Vector2(.62f,1.42f);box.offset=new Vector2(0,.73f); filter=new ContactFilter2D();filter.SetLayerMask(1<<6);filter.useTriggers=false; previousPose=ArtLibrary.Sprite("Lio · pose transition",ArtLibrary.Actor("LioAtlas",0),transform.position,15,transform);previousPose.enabled=false; visual=ArtLibrary.Sprite("Lio — animated",ArtLibrary.Actor("LioAtlas",0),transform.position,16,transform);visual.transform.localPosition=Vector3.zero; shadow=ArtLibrary.Sprite("Lio contact shadow",ArtLibrary.Soft,transform.position,8,transform,false);shadow.color=new Color(.06f,.16f,.17f,.4f);Grounded=true; Health=GameSession.I.Save.Data.healthCapacity;SafePoint=transform.position;InitRenderInterpolation(); for(int n=0;n.04f?input.Move.normalized:new Vector2(Facing,0);dashTime=cfg.dashDuration;}else if(Grounded&&input.Move.y<-.55f){slideTime=.42f;slideFacing=Facing;SetLow(true);}else dashTime=cfg.dashDuration;dashCooldown=cfg.dashCooldown;airDash=true;Invincible=Mathf.Max(Invincible,.1f);PlayerMovementAudio.Dash(CurrentGroundMaterial,transform.position,Grounded&&!Flying,Sliding);WorldEffects.I.Burst(transform.position+Vector3.up*.6f,ArtLibrary.Gold,8,1);} if(input.HealPressed&&Heals>0&&HealthHearts();} if(carriedByBell){Grounded=true;landedOn=bellLift.GetComponent();} if(Grounded&&!carriedByThread&&!carriedByBell&&support.collider&&support.collider.GetComponent() is MovingLift carrier)body.position+=carrier.Delta; if(Grounded&&!carriedByThread&&!carriedByBell&&support.collider&&support.collider.GetComponent() is WindFerry ferry)body.position+=ferry.Delta; if(Grounded&&!carriedByThread&&!carriedByBell&&support.collider&&support.collider.GetComponent() is BuoyantPlatform pontoon)body.position+=pontoon.Delta; if(Grounded&&!carriedByThread&&!carriedByBell&&support.collider&&support.collider.GetComponent() is SuspendedProp suspended)body.position+=suspended.Delta; if(Grounded&&!carriedByThread&&!carriedByBell&&support.collider&&support.collider.GetComponent() is SinkingLeaf leafCarrier)body.position+=leafCarrier.Delta; if(Grounded&&!Climbing&&!Flying){coyote=cfg.coyoteTime;airDash=false;if(Velocity.y<0)Velocity=new Vector2(Velocity.x,0); if(!Dashing&&Cast(Vector2.down,.15f,out var hit)&&hit.collider.GetComponent()==null&&hit.collider.GetComponent()==null&&hit.collider.GetComponent()==null&&hit.collider.GetComponent()==null&&hit.collider.GetComponent()==null&&hit.collider.GetComponent()==null)SafePoint=body.position;} if(healTime>0){healTime-=dt;if(healTime<=0&&Heals>0){GameSession.I.Save.Data.healCharges--;GameSession.I.Save.Write();HealHearts(3);WorldEffects.I.Burst(transform.position+Vector3.up,ArtLibrary.Teal,24);AudioDirector.I.PotionConsumed(transform.position);}} if(!Climbing&&!Dashing&&Mathf.Abs(input.Move.x)>.1f)Facing=input.Move.x>0?1:-1; if(Grounded&&landedOn)CurrentGroundMaterial=GroundSurface.Resolve(landedOn,body.position); float previousStride=stridePhase; previousFixedStride=stridePhase; var v=Velocity;bool dashMovement=Dashing; bool traversalMovement=ResolveTraversal(dt,ref v); if(traversalMovement){} else if(dashTime>0){dashTime-=dt;v=new Vector2(dashFacing*cfg.dashSpeed,0);} else { float target=input.Move.x*(Crouching?1.6f:input.WalkHeld?2.0f:cfg.runSpeed)*NightPollen.SlowFactor(this);if(hurtTime>.22f)v.x=Mathf.MoveTowards(v.x,0,dt*7);else v.x=Mathf.MoveTowards(v.x,target,(Mathf.Abs(target)>.1f?cfg.acceleration:cfg.braking)*(Grounded?1:.85f)*dt); if(buffer>0&&coyote>0&&!Crouching&&hurtTime<=.22f){v.y=cfg.jumpSpeed;buffer=0;coyote=0;Grounded=false;visualScale=1.07f;PlayerMovementAudio.Jump(CurrentGroundMaterial,transform.position,false);} float g=v.y>0?cfg.riseGravity:cfg.fallGravity;if(v.y>0&&!input.JumpHeld)g*=2.4f; v.y=Mathf.Max(v.y-g*dt,-24); Gliding=GameSession.I.Save.Data.glide&&!Grounded&&v.y<0&&input.JumpHeld; if(Gliding)v.y=Mathf.Max(v.y,-cfg.glideSpeed); } float windLift=OrvoWindGift.I?OrvoWindGift.I.Lift(this):0;if(windLift>0){v.y=Mathf.MoveTowards(Velocity.y,windLift,dt*12);Gliding=true;} if(hurtTime<=.22f&&ArchiveWindField.Apply(this,input.JumpHeld,dt,ref v))Gliding=true; float landingSpeed=Mathf.Max(0,-v.y); var delta=v*dt;if(CoastWind.I)delta.x+=CoastWind.I.Drift(this)*dt;if(ShipyardSailRepair.I)delta.x+=ShipyardSailRepair.I.Drift(this)*dt;if(OrvoRoof.I)delta.x+=OrvoRoof.I.Boss.Drift(this)*dt;if(BelfryWindWindow.I)delta.x+=BelfryWindWindow.I.Drift(this)*dt;Vector2 pos=body.position; if(Mathf.Abs(delta.x)>.0001f&&Cast(Vector2.right*Mathf.Sign(delta.x),Mathf.Abs(delta.x)+.02f,out var hx)) { float direction=Mathf.Sign(delta.x);var planter=hx.collider.GetComponent(); var contactedLoad=hx.collider.GetComponent();if(contactedLoad&&hx.distance<=.04f)contactedLoad.PushAt(hx.point,v.x); float pushed=planter?planter.Push(delta.x,this):0; delta.x=direction*Mathf.Min(Mathf.Abs(delta.x),Mathf.Max(0,hx.distance-.02f)+Mathf.Abs(pushed));v.x=pushed==0?0:delta.x/dt; } body.position=pos+new Vector2(delta.x,0); // Only moving sloped threads use this surface projection. Existing flat // platforms retain their tested collision and jump behaviour. var threadSupport=landedOn?landedOn.GetComponent():null; if(Grounded&&v.y<=0&&!Climbing&&!Flying&&threadSupport&&threadSupport.IsMoving&&threadSupport.Active&&threadSupport.OverSurface(body.position.x)) body.position=new Vector2(body.position.x,threadSupport.StandingY(body.position.x,box.size.x*.5f)+.01f); var shapedSupport=landedOn?landedOn.GetComponent():null; if(Grounded&&v.y<=0&&!Climbing&&!Flying&&shapedSupport&&shapedSupport.Contains(body.position.x)) body.position=new Vector2(body.position.x,shapedSupport.StandingY(body.position.x,box.size.x*.5f)+.01f); if(Mathf.Abs(delta.y)>.0001f&&Cast(Vector2.up*Mathf.Sign(delta.y),Mathf.Abs(delta.y)+.02f,out var hy)){delta.y=Mathf.Sign(delta.y)*Mathf.Max(0,hy.distance-.02f);if(v.y<0){Grounded=true;landedOn=hy.collider;}v.y=0;} body.position+=new Vector2(0,delta.y);Velocity=v;DashStrike(pos,body.position,dashMovement); movingThreadSupport=Grounded&&landedOn?landedOn.GetComponent():null; walkingProfileSupport=Grounded&&landedOn?landedOn.GetComponent():null; LiftSupport=Grounded&&landedOn?landedOn.GetComponent():null; if(movingThreadSupport&&!movingThreadSupport.IsMoving)movingThreadSupport=null; if(Flying&&Grounded&&input.Move.y<-.1f)Flying=false; float previousClimb=climbDistance; travelDistance+=Mathf.Abs(delta.x);climbDistance+=Climbing?delta.y:0; // Integrate a shared gait phase instead of re-scaling lifetime distance on a clip change. AdvanceGaitPosture(dt,delta.x); float stepDistance=LioLocomotionRig.TravelCycleDistance(Mathf.Abs(v.x),cfg.runSpeed,(previousFixedCrouchPose+fixedCrouchPose)*.5f); if(Grounded&&!Climbing&&!dashMovement&&stepDistance>.00001f)stridePhase=Mathf.Repeat(stridePhase+Mathf.Abs(delta.x)*8/stepDistance,8); if(Grounded){Gliding=false;if(!wasGround){LastLandingMaterial=GroundSurface.Resolve(landedOn,body.position);WorldEffects.I.Burst(body.position,LastLandingMaterial==GroundMaterial.Water?ArtLibrary.Teal:ArtLibrary.Gold,6,1);AudioDirector.I.Landing(LastLandingMaterial,body.position,landingSpeed);if(landingSpeed>13)GameSession.I.Camera.Kick(Mathf.Lerp(.035f,.075f,Mathf.InverseLerp(13,24,landingSpeed)));}} // Long drops may land on any lower terrace. SafePoint is only the return location. if(!Grounded&&body.position.y()) { var thread=collider.GetComponent(); var shape=collider.GetComponent(); float top=thread&&thread.IsMoving?thread.StandingY(body.position.x,box.size.x*.5f):shape?shape.StandingY(body.position.x,box.size.x*.5f):collider.bounds.max.y; if(Climbing||dir.y>-.5f||body.position.y0)continue; } if(hits[i].normal.sqrMagnitude<.1f)continue;if(Vector2.Dot(hits[i].normal,dir)>-.1f)continue;if(hits[i].distance0||Rescuing||EnteringDoor||!GameSession.I.Playing)return; LoseHearts(damage);Invincible=.9f;hurtTime=.48f;Gliding=false;glideTime=0;ladder=null;Flying=Flying&&FlightAvailable;slideTime=dashTime=0;Velocity=new Vector2(Mathf.Sign(transform.position.x-source.x)*4,4); WorldEffects.I.Burst(transform.position+Vector3.up,ArtLibrary.Gold,16,3);AudioDirector.I.Play("hurt",transform.position);GameSession.I.Camera.Kick(.16f);HapticsDirector.I?.Pulse(GameSession.I.InBossEncounter?.72f:.38f,.34f); TryAutoHeal();if(Health<=0)GameSession.I.Respawn(); } public void Fall() { if(Rescuing)return; bool feather=GameSession.I.Save.Has("heimfeder")&&!freeFallUsed; if(feather)freeFallUsed=true; if(!GameSession.I.Save.Data.fallRescue&&!feather&&GameSession.I.Save.SpendRescueFeathers()){BeginFeatherRescue();return;} if(!GameSession.I.Save.Data.fallRescue&&!feather)LoseHearts(1); TryAutoHeal(); if(Health<=0){GameSession.I.Respawn();return;} if(WorldBuilder.IsLastConnection&&!PocketRoom.Active&&!DungeonRun.Inside)LastConnectionWorld.I.RestoreCheckpointRoom();if(WorldBuilder.IsSecondsCity&&!PocketRoom.Active&&!DungeonRun.Inside)SecondsCityWorld.I.RestoreCheckpointRoom();if(WorldBuilder.IsCrownHeart&&!PocketRoom.Active&&!DungeonRun.Inside)CrownHeartWorld.I.RestoreCheckpointRoom();if(WorldBuilder.IsOpenSky&&!PocketRoom.Active&&!DungeonRun.Inside)OpenSkyWorld.I.RestoreCheckpointRoom();if(WorldBuilder.IsRiver&&!PocketRoom.Active&&!DungeonRun.Inside)RiverWithoutBanksWorld.I.RestoreCheckpointRoom(); if(WorldBuilder.IsObservatory&&!PocketRoom.Active&&!DungeonRun.Inside)ObservatoryWorld.I.RestoreCheckpointRoom(); Teleport(FallReturnPoint+Vector2.up*.1f);Invincible=1;AudioDirector.I.Play("rescue",transform.position);WorldEffects.I.Burst(transform.position,ArtLibrary.Teal,20); } public int TeleportSerial{get;private set;} public void Teleport(Vector2 p){TeleportSerial++;if(CoastWind.I)CoastWind.I.RestartWarning();entryRoom=null;entryAction=null;entryAge=0;movingThreadSupport=null;LiftSupport=null;Rescuing=false;healTime=0;body.position=p;transform.position=p;Velocity=Vector2.zero;dashTime=slideTime=hurtTime=0;ladder=null;Flying=false;SetLow(false);Grounded=false;Gliding=false;landingTime=glideTime=0;shotAge=99;SafePoint=p;ResetPresentation();} public void Restore(){Health=GameSession.I.Save.Data.healthCapacity;Invincible=.9f;freeFallUsed=false;} // A practice restart can recover its entry health, never refill an injured visitor. // Health deliberately gained with a consumed potion is not taken away or refunded. public void RestorePracticeHealth(int entryHealth)=>RestorePracticeHealth((float)Mathf.Max(1,entryHealth)); public void RestorePracticeHealth(float entryHealth) { int entryUnits=Mathf.Clamp(Mathf.RoundToInt(entryHealth*2),1,GameSession.I.Save.Data.healthCapacity*2); healthHalfUnits=Mathf.Max(healthHalfUnits,entryUnits);Invincible=.9f; } public void RestoreSourceHeart()=>HealHearts(1); void LoseHearts(int hearts) { int units=hearts*(GameSession.I.Save.Data.gentle?1:2); healthHalfUnits=Mathf.Max(0,healthHalfUnits-units); } void HealHearts(int hearts)=>healthHalfUnits=Mathf.Min(GameSession.I.Save.Data.healthCapacity*2,healthHalfUnits+hearts*2); public bool TryAutoHeal() { var g=GameSession.I;if(!g.Playing||SourceRecharge.Healing||!g.Save.Data.autoHeal||HealthHearts>1||g.Save.Data.healCharges<=0)return false; // One real bottle per critical event. Cancel pending manual healing to avoid a second charge. healTime=0;float before=HealthHearts;g.Save.Data.healCharges--;g.Save.Write();HealHearts(3); AudioDirector.I.PotionConsumed(transform.position);WorldEffects.I.Burst(transform.position+Vector3.up,ArtLibrary.Teal,16,1.3f);g.Hud.Toast("Heiltrank automatisch verwendet · +"+(HealthHearts-before).ToString("0.#")+" Lebensherzen",2.5f);return true; } void OnDestroy(){foreach(var e in echoes)if(e)Destroy(e.gameObject);} } }