WIP duck behaviour

This commit is contained in:
2025-07-09 20:52:43 +02:00
parent 8d40529349
commit 6b5a8ee126
4 changed files with 282 additions and 104 deletions
+32 -16
View File
@@ -1,4 +1,3 @@
using System;
using Godot;
namespace Babushka.scripts.CSharp.Common.Animation;
@@ -9,15 +8,18 @@ public partial class Duck : Node2D
[Export] private AudioPlayer _nakNakAudio;
[Export] private AudioPlayer _wingFlapAudio;
[Export] private Node2D _vesna;
[Export] private float _runningSpeed = 5f;
[Export] private float _slowSpeed = 1f;
[Export] private float _runningSpeed = 3f;
[Export] private float _slowSpeed = 0.5f;
[Export] private float _minDistanceToVesna = 1000f;
[Export] private Node2D _duckRight;
[Export] private Node2D _duckLeft;
private bool _vesnaInReach = false;
private bool _penEntered = false;
private Vector2 _lastDirection = Vector2.Up;
private int _numberOfFramesPerDirection = 1000;
private int _currentFramesThisDirection = 0;
private bool _duckLookingRight = true;
private Vector2 _movementVector = Vector2.Zero;
public void PenEntered()
@@ -26,6 +28,11 @@ public partial class Duck : Node2D
_penEntered = true;
}
public override void _Draw()
{
DrawLine(_characterBody.GlobalPosition, _movementVector, new Color(255, 0, 0), 2f, false);
}
public override void _Process(double delta)
{
if (_penEntered)
@@ -40,33 +47,42 @@ public partial class Duck : Node2D
if (currentDistance < _minDistanceToVesna)
{
_characterBody.Velocity = new Vector2( _characterBody.GlobalPosition.X - _vesna.GlobalPosition.X,
_movementVector = new Vector2( _characterBody.GlobalPosition.X - _vesna.GlobalPosition.X,
_characterBody.GlobalPosition.Y - _vesna.GlobalPosition.Y).Normalized();
_characterBody.Velocity *= _runningSpeed;
_movementVector *= _runningSpeed;
_characterBody.Velocity = _movementVector;
_characterBody.MoveAndSlide();
// todo: Add noise!
}
else
{
if (_currentFramesThisDirection >= _numberOfFramesPerDirection)
{
_currentFramesThisDirection = 0;
Random random = new Random();
_lastDirection = new Vector2(random.NextSingle(), random.NextSingle()).Normalized();
FastNoiseLite fastNoiseLite = new FastNoiseLite();
_movementVector = new Vector2(fastNoiseLite.GetNoise1D(_characterBody.GlobalPosition.X * Time.GetTicksMsec()), fastNoiseLite.GetNoise1D(_characterBody.GlobalPosition.Y * Time.GetTicksMsec())).Normalized();
}
_currentFramesThisDirection++;
_characterBody.Velocity = _lastDirection * _slowSpeed;
_characterBody.Velocity = _movementVector * _slowSpeed;
_characterBody.MoveAndSlide();
}
/*
if (_characterBody.Velocity.X < 0 && _characterBody.Scale.X > 0)
// if läuftNachLinks && schautNachRechts || läuftNachRechts && schautNachLinks
if ((_characterBody.Velocity.X < 0 && _duckLookingRight)
|| (_characterBody.Velocity.X > 0 && !_duckLookingRight))
{
_characterBody.Scale = new Vector2(_characterBody.Scale.X * (-1), _characterBody.Scale.Y);
if (_duckLookingRight)
{
_duckRight.Visible = false;
_duckLeft.Visible = true;
_duckLookingRight = false;
}
else
{
_duckRight.Visible = true;
_duckLeft.Visible = false;
_duckLookingRight = true;
}
}
*/
base._Process(delta);
}
}