You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
2.3 KiB
72 lines
2.3 KiB
using System;
|
|
using Godot;
|
|
|
|
namespace Babushka.scripts.CSharp.Common.Animation;
|
|
|
|
public partial class Duck : Node2D
|
|
{
|
|
[Export] private CharacterBody2D _characterBody;
|
|
[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 _minDistanceToVesna = 1000f;
|
|
|
|
private bool _vesnaInReach = false;
|
|
private bool _penEntered = false;
|
|
private Vector2 _lastDirection = Vector2.Up;
|
|
private int _numberOfFramesPerDirection = 1000;
|
|
private int _currentFramesThisDirection = 0;
|
|
|
|
|
|
public void PenEntered()
|
|
{
|
|
_nakNakAudio.PlayOneShot();
|
|
_penEntered = true;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
if (_penEntered)
|
|
{
|
|
_characterBody.Velocity = Vector2.Zero;
|
|
_characterBody.MoveAndSlide();
|
|
GD.Print("Pen entered.");
|
|
return;
|
|
}
|
|
|
|
float currentDistance = _vesna.GlobalPosition.DistanceTo(_characterBody.GlobalPosition);
|
|
|
|
if (currentDistance < _minDistanceToVesna)
|
|
{
|
|
_characterBody.Velocity = new Vector2( _characterBody.GlobalPosition.X - _vesna.GlobalPosition.X,
|
|
_characterBody.GlobalPosition.Y - _vesna.GlobalPosition.Y).Normalized();
|
|
_characterBody.Velocity *= _runningSpeed;
|
|
_characterBody.MoveAndSlide();
|
|
// todo: Add noise!
|
|
}
|
|
else
|
|
{
|
|
if (_currentFramesThisDirection >= _numberOfFramesPerDirection)
|
|
{
|
|
_currentFramesThisDirection = 0;
|
|
Random random = new Random();
|
|
_lastDirection = new Vector2(random.NextSingle(), random.NextSingle()).Normalized();
|
|
}
|
|
|
|
_currentFramesThisDirection++;
|
|
_characterBody.Velocity = _lastDirection * _slowSpeed;
|
|
_characterBody.MoveAndSlide();
|
|
}
|
|
|
|
/*
|
|
if (_characterBody.Velocity.X < 0 && _characterBody.Scale.X > 0)
|
|
{
|
|
_characterBody.Scale = new Vector2(_characterBody.Scale.X * (-1), _characterBody.Scale.Y);
|
|
}
|
|
*/
|
|
|
|
base._Process(delta);
|
|
}
|
|
} |