Animation tree fixes
This commit is contained in:
@@ -25,8 +25,12 @@ public partial class EnemyModelView2D : Node3D, IEnemyModelView
|
||||
|
||||
[Node] public AnimationTree AnimationTree { get; set; } = default!;
|
||||
|
||||
[ExportGroup("Animation Debug")]
|
||||
[Export(PropertyHint.Enum)]
|
||||
[ExportGroup("Enemy Model Properties")]
|
||||
[Export(PropertyHint.Range, "0.0, 1.0")]
|
||||
private float _upperThreshold { get; set; } = 0.5f;
|
||||
[Export(PropertyHint.Range, "-1.0, 0.0")]
|
||||
private float _lowerThreshold { get; set; } = -0.5f;
|
||||
|
||||
EnemyDirection _enemyDirection { get; set; } = EnemyDirection.Forward;
|
||||
|
||||
private AnimationNodeStateMachinePlayback _stateMachine;
|
||||
@@ -36,7 +40,7 @@ public partial class EnemyModelView2D : Node3D, IEnemyModelView
|
||||
_stateMachine = (AnimationNodeStateMachinePlayback)AnimationTree.Get("parameters/playback");
|
||||
}
|
||||
|
||||
public void SetCurrentDirection(Basis enemyBasis, Vector3 cameraDirection) => _enemyDirection = GetEnemyDirection(enemyBasis, cameraDirection, 0.55f, 0.45f);
|
||||
public void SetCurrentDirection(Basis enemyBasis, Vector3 cameraDirection) => _enemyDirection = GetEnemyDirection(enemyBasis, cameraDirection, _upperThreshold, _lowerThreshold);
|
||||
|
||||
public void PlayPrimaryAttackAnimation() => _stateMachine.Travel(_primaryAttackName);
|
||||
|
||||
@@ -52,17 +56,17 @@ public partial class EnemyModelView2D : Node3D, IEnemyModelView
|
||||
|
||||
public void PlayHitAnimation()
|
||||
{
|
||||
LoadShader("res://src/vfx/shaders/DamageHit.gdshader");
|
||||
var tweener = GetTree().CreateTween();
|
||||
tweener.TweenMethod(Callable.From((float x) => SetShaderValue(x)), 0.0f, 1.0f, 1.0f);
|
||||
LoadShader("res://src/vfx/shaders/DamageHit.gdshader");
|
||||
var tweener = GetTree().CreateTween();
|
||||
tweener.TweenMethod(Callable.From((float x) => SetShaderValue(x)), 0.0f, 1.0f, 1.0f);
|
||||
}
|
||||
|
||||
public void PlayDeathAnimation()
|
||||
{
|
||||
LoadShader("res://src/vfx/shaders/PixelMelt.gdshader");
|
||||
var tweener = GetTree().CreateTween();
|
||||
tweener.TweenMethod(Callable.From((float x) => SetShaderValue(x)), 0.0f, 1.0f, 0.8f);
|
||||
tweener.TweenCallback(Callable.From(QueueFree));
|
||||
LoadShader("res://src/vfx/shaders/PixelMelt.gdshader");
|
||||
var tweener = GetTree().CreateTween();
|
||||
tweener.TweenMethod(Callable.From((float x) => SetShaderValue(x)), 0.0f, 1.0f, 0.8f);
|
||||
tweener.TweenCallback(Callable.From(QueueFree));
|
||||
}
|
||||
|
||||
private EnemyDirection GetEnemyDirection(
|
||||
@@ -71,14 +75,14 @@ public partial class EnemyModelView2D : Node3D, IEnemyModelView
|
||||
float rotateUpperThreshold,
|
||||
float rotateLowerThreshold)
|
||||
{
|
||||
var enemyForwardDirection = enemyBasis.Z;
|
||||
var enemyLeftDirection = enemyBasis.X;
|
||||
var enemyForwardDirection = enemyBasis.Z;
|
||||
var enemyLeftDirection = enemyBasis.X;
|
||||
|
||||
var leftDotProduct = enemyLeftDirection.Dot(cameraDirection);
|
||||
var forwardDotProduct = enemyForwardDirection.Dot(cameraDirection);
|
||||
var leftDotProduct = enemyLeftDirection.Dot(cameraDirection);
|
||||
var forwardDotProduct = enemyForwardDirection.Dot(cameraDirection);
|
||||
|
||||
// Check if forward facing. If the dot product is -1, the enemy is facing the camera.
|
||||
if (forwardDotProduct < -rotateUpperThreshold)
|
||||
if (forwardDotProduct < _lowerThreshold)
|
||||
{
|
||||
SetForward();
|
||||
return EnemyDirection.Forward;
|
||||
@@ -93,17 +97,17 @@ public partial class EnemyModelView2D : Node3D, IEnemyModelView
|
||||
else
|
||||
{
|
||||
// If the dot product of the perpendicular direction is positive (up to 1), the enemy is facing to the left (since it's mirrored).
|
||||
if (leftDotProduct > 0)
|
||||
if (leftDotProduct < _lowerThreshold)
|
||||
{
|
||||
SetRight();
|
||||
return EnemyDirection.Right;
|
||||
return EnemyDirection.Left;
|
||||
}
|
||||
|
||||
// Check if side facing. If the dot product is close to zero in the positive or negative direction, its close to the threshold for turning.
|
||||
if (Mathf.Abs(forwardDotProduct) < rotateLowerThreshold)
|
||||
if (leftDotProduct > rotateUpperThreshold)
|
||||
{
|
||||
SetLeft();
|
||||
return EnemyDirection.Left;
|
||||
return EnemyDirection.Right;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,14 +116,14 @@ public partial class EnemyModelView2D : Node3D, IEnemyModelView
|
||||
|
||||
private void LoadShader(string shaderPath)
|
||||
{
|
||||
var shader = GD.Load<Shader>(shaderPath);
|
||||
var sprites = FindChildren("*", "AnimatedSprite2D", true).Cast<AnimatedSprite2D>();
|
||||
foreach (var sprite in sprites)
|
||||
{
|
||||
sprite.Material = new ShaderMaterial();
|
||||
var shaderMaterial = (ShaderMaterial)sprite.Material;
|
||||
shaderMaterial.Shader = shader;
|
||||
}
|
||||
var shader = GD.Load<Shader>(shaderPath);
|
||||
var sprites = FindChildren("*", "AnimatedSprite2D", true).Cast<AnimatedSprite2D>();
|
||||
foreach (var sprite in sprites)
|
||||
{
|
||||
sprite.Material = new ShaderMaterial();
|
||||
var shaderMaterial = (ShaderMaterial)sprite.Material;
|
||||
shaderMaterial.Shader = shader;
|
||||
}
|
||||
}
|
||||
|
||||
private void SetShaderValue(float shaderValue)
|
||||
|
||||
Reference in New Issue
Block a user