@@ -10,9 +10,9 @@ public partial class PathFindingMovement : Node
1010 [ Signal ] public delegate void ReachedTargetEventHandler ( ) ;
1111
1212 [ Export ] private int _minTargetDistance = 40 ;
13- [ Export ] private int _targetDistanceVariation = 50 ; // Not currently used, consider removing or implementing variation
13+ [ Export ] private int _targetDistanceVariation = 50 ;
1414 [ Export ] private int _speed = 250 ;
15- [ Export ] int _minimumSpeed = 50 ;
15+ [ Export ] int _minimumSpeed = 150 ;
1616 private int _origMinimumSpeed ;
1717
1818 [ Export ] bool _debug = false ;
@@ -22,8 +22,8 @@ public partial class PathFindingMovement : Node
2222 [ Export ] Sprite2D _sprite = null ! ;
2323
2424 public Vector2 TargetPosition { get ; set ; }
25- private object ? _lastCollider = null ; // Speichert den letzten Kollisionspartner
26- private bool _recentlyBumped = false ; // Verhindert Dauersound
25+ private object _lastCollider = null ;
26+ private bool _recentlyBumped = false ;
2727 private bool _reachedTarget ;
2828 private int _currentTargetDistance ;
2929 private AudioStreamPlayer ? _bumpSound = null ! ;
@@ -44,9 +44,10 @@ public override void _Ready()
4444 {
4545 _origMinimumSpeed = _minimumSpeed ;
4646 _currentTargetDistance = _minTargetDistance ;
47- this . CallDeferred ( "ActorSetup" ) ; // Still good to defer setup
47+ this . CallDeferred ( "ActorSetup" ) ;
4848 _bumpSound = GetTree ( ) . Root . GetNode < AudioStreamPlayer > ( "Node2D/AudioManager/bump_sound" ) ;
4949 _buttonControl = GetTree ( ) . Root . GetNode < ButtonControl > ( "Node2D/UI" ) ;
50+
5051 }
5152
5253 public async void ActorSetup ( )
@@ -60,79 +61,82 @@ public void GoTo(Vector2 loc)
6061 TargetPosition = loc ;
6162 }
6263
63- public override void _PhysicsProcess ( double delta )
64- {
65- _speed = 250 ;
66- _agent . SetTargetPosition ( TargetPosition ) ;
67-
68- float distanceToTarget = _character . GlobalPosition . DistanceTo ( _agent . TargetPosition ) ;
69-
70- if ( distanceToTarget > _currentTargetDistance )
64+ public override void _PhysicsProcess ( double delta )
7165 {
72- _reachedTarget = false ;
66+ _speed = 250 ;
7367
74- Vector2 currentLocation = _character . GlobalPosition ;
75- Vector2 nextLocation = _agent . GetNextPathPosition ( ) ;
68+ _agent . SetTargetPosition ( TargetPosition ) ;
7669
77- // Motivation und Speed-Berechnung
78- Motivation motivation = GetParent ( ) . GetNode < Motivation > ( "Motivation" ) ;
79- double motivationFactor = ( double ) motivation . Amount / 10 ;
80- int modifiedSpeed = ( int ) ( _minimumSpeed + ( _speed - _minimumSpeed ) * motivationFactor ) ;
81-
82- // Bewegung berechnen
83- Vector2 newVel = ( nextLocation - currentLocation ) . Normalized ( ) * modifiedSpeed ;
84-
85- // Richtung für die Animation setzen
86- CurrentDirection = nextLocation . X < currentLocation . X ? WalkingState . Left : WalkingState . Right ;
87-
88- if ( newVel . X != 0 && distanceToTarget > 50 )
70+ if ( _debug )
8971 {
90- _sprite . FlipH = newVel . X > 0 ;
72+ float distance = _character . GlobalPosition . DistanceTo ( _agent . TargetPosition ) ;
73+ //GD.Print($"Distance: {distance}, Target Position: {_agent.TargetPosition}");
9174 }
9275
93- // **Langsamer werden bei Zielnähe (weiches Bremsen)**
94- if ( distanceToTarget < 50 )
76+ float distanceToTarget = _character . GlobalPosition . DistanceTo ( _agent . TargetPosition ) ;
77+
78+ if ( distanceToTarget > _currentTargetDistance )
9579 {
96- float slowdownFactor = Mathf . Clamp ( distanceToTarget / 50 , 0.1f , 1f ) ;
97- newVel *= slowdownFactor ;
98- }
80+ _reachedTarget = false ;
81+ Vector2 currentLocation = _character . GlobalPosition , nextLocation = _agent . GetNextPathPosition ( ) ;
82+
83+ /* Motivation motivation = GetParent().GetNode<Motivation>("Motivation");
84+ double motivationFactor = (double)motivation.Amount / 10;*/
85+ int modifiedSpeed = ( int ) ( _minimumSpeed + ( _speed - _minimumSpeed ) * 2 ) ;
86+ Ally ally = GetParent ( ) . GetParent ( ) . GetChild < Ally > ( 0 ) ;
87+ Chat chat = ally . FindChild ( "Speech" ) . GetParent ( ) as Chat ?? throw new InvalidOperationException ( ) ;
88+ _minimumSpeed = ( chat ! . GeminiService . IsBusy ( ) || ally ! . GetResponseQueue ( ) . Count > 0 || ! ally ! . IsTextBoxReady ) ? 0 : _origMinimumSpeed ; // dont move while responding or if more than one response is being processed.
89+ Vector2 newVel = ( nextLocation - currentLocation ) . Normalized ( ) * modifiedSpeed ;
90+ if ( nextLocation . X < currentLocation . X )
91+ {
92+ CurrentDirection = WalkingState . Left ;
93+ }
94+ else
95+ {
96+ CurrentDirection = WalkingState . Right ;
97+ }
9998
100- _character . Velocity = newVel ;
99+ if ( newVel . X != 0 && distanceToTarget > 50 )
100+ {
101+ _sprite . FlipH = newVel . X > 0 ;
102+ }
101103
102- // Kollisionsabfrage bleibt bestehen
103- KinematicCollision2D collision = _character . MoveAndCollide ( newVel * ( float ) delta ) ;
104- if ( collision != null && collision . GetCollider ( ) != _lastCollider )
105- {
106- if ( ( _character . Name == "Ally" && _buttonControl . CurrentCamera == 1 ) ||
107- ( _character . Name == "Ally2" && _buttonControl . CurrentCamera == 2 ) )
104+ _character . Velocity = newVel ;
105+ KinematicCollision2D collision = _character . MoveAndCollide ( newVel * ( float ) delta ) ;
106+ if ( collision != null )
108107 {
109- _lastCollider = collision . GetCollider ( ) ;
110- _bumpSound ! . Play ( ) ;
111- _recentlyBumped = true ;
108+ if ( collision . GetCollider ( ) != _lastCollider )
109+ {
110+ if ( _character . Name == "Ally" && _buttonControl . CurrentCamera == 1 || _character . Name == "Ally2" && _buttonControl . CurrentCamera == 2 )
111+ {
112+ _lastCollider = collision . GetCollider ( ) ;
113+ _bumpSound ! . Play ( ) ;
114+ _recentlyBumped = true ;
115+ }
116+ }
117+ }
118+ else
119+ {
120+ _lastCollider = null ;
121+ _recentlyBumped = false ;
112122 }
113- }
114- else
115- {
116- _lastCollider = null ;
117- _recentlyBumped = false ;
118- }
119- }
120- else if ( ! _reachedTarget )
121- {
122- // **Weiches Stoppen statt Teleport**
123-
124123
125- if ( distanceToTarget < 45f ) // Bei < 3 Pixel Restdistanz stoppen
124+ _character . Velocity = newVel ;
125+ }
126+ else if ( ! _reachedTarget ) // only emit and set _reachedTarget once, when the condition is first met
126127 {
127- _character . Velocity = Vector2 . Zero ;
128-
129- // Idle-Animation aktivieren
130- CurrentDirection = ( CurrentDirection == WalkingState . Left ) ? WalkingState . IdleLeft : WalkingState . IdleRight ;
128+ if ( CurrentDirection == PathFindingMovement . WalkingState . Left )
129+ {
130+ CurrentDirection = WalkingState . IdleLeft ;
131+ }
132+ else
133+ {
134+ CurrentDirection = WalkingState . IdleRight ;
135+ }
131136
132- _currentTargetDistance = _minTargetDistance ;
137+ _currentTargetDistance = _minTargetDistance ; // reset for the next target
133138 EmitSignal ( SignalName . ReachedTarget ) ;
134139 _reachedTarget = true ;
135140 }
136141 }
137- }
138- }
142+ }
0 commit comments