In progress work for Chariot

This commit is contained in:
2025-04-01 02:20:27 -07:00
parent b8e23ac9c4
commit 762444fe2c
6693 changed files with 107666 additions and 15459 deletions

View File

@@ -1,21 +1,30 @@
using Chickensoft.AutoInject;
using Chickensoft.Introspection;
using Godot;
using System;
namespace Zennysoft.Game.Ma;
[Meta(typeof(IAutoNode))]
public partial class EnemyModelView2D : Node3D, IEnemyModelView
{
private const string PRIMARY_ATTACK = "primary_attack";
private const string SECONDARY_ATTACK = "secondary_attack";
private const string PRIMARY_SKILL = "primary_skill";
private const string IDLE_FORWARD = "idle_front";
private const string IDLE_LEFT = "idle_left";
private const string IDLE_BACK = "idle_back";
private const string IDLE_FORWARD_WALK = "idle_front_walk";
private const string IDLE_LEFT_WALK = "idle_left_walk";
private const string IDLE_BACK_WALK = "idle_back_walk";
private const string PARAMETERS_PLAYBACK = "parameters/playback";
protected const string PRIMARY_ATTACK = "primary_attack";
protected const string PRIMARY_ATTACK_LEFT = "primary_attack_left";
protected const string PRIMARY_ATTACK_RIGHT = "primary_attack_right";
protected const string PRIMARY_ATTACK_BACK = "primary_attack_back";
protected const string SECONDARY_ATTACK = "secondary_attack";
protected const string SECONDARY_ATTACK_LEFT = "secondary_attack_left";
protected const string SECONDARY_ATTACK_RIGHT = "secondary_attack_right";
protected const string SECONDARY_ATTACK_BACK = "secondary_attack_back";
protected const string PRIMARY_SKILL = "primary_skill";
protected const string IDLE_FORWARD = "idle_front";
protected const string IDLE_LEFT = "idle_left";
protected const string IDLE_RIGHT = "idle_right";
protected const string IDLE_BACK = "idle_back";
protected const string IDLE_FORWARD_WALK = "idle_front_walk";
protected const string IDLE_LEFT_WALK = "idle_left_walk";
protected const string IDLE_RIGHT_WALK = "idle_right_walk";
protected const string IDLE_BACK_WALK = "idle_back_walk";
protected const string PARAMETERS_PLAYBACK = "parameters/playback";
public override void _Notification(int what) => this.Notify(what);
@@ -29,107 +38,150 @@ public partial class EnemyModelView2D : Node3D, IEnemyModelView
[Node] public AnimationTree AnimationTree { get; set; } = default!;
public void Setup()
{
AnimationTree.AnimationFinished += AnimationTree_AnimationFinished;
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Start(IDLE_FORWARD);
}
protected DirectionType _currentDirection = DirectionType.FORWARD;
public void SetCurrentDirection(Basis enemyBasis, Basis cameraBasis) => _currentDirection = GetEnemyDirection(enemyBasis, -cameraBasis.Z, 0.55f, 0.45f);
public void PlayPrimaryAttackAnimation()
{
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(PRIMARY_ATTACK);
switch (_currentDirection)
{
case DirectionType.FORWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(PRIMARY_ATTACK);
break;
case DirectionType.RIGHT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(PRIMARY_ATTACK_RIGHT);
break;
case DirectionType.LEFT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(PRIMARY_ATTACK_LEFT);
break;
case DirectionType.BACKWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(PRIMARY_ATTACK_BACK);
break;
}
}
public void PlaySecondaryAttackAnimation()
{
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(SECONDARY_ATTACK);
switch (_currentDirection)
{
case DirectionType.FORWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(SECONDARY_ATTACK);
break;
case DirectionType.RIGHT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(SECONDARY_ATTACK_RIGHT);
break;
case DirectionType.LEFT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(SECONDARY_ATTACK_LEFT);
break;
case DirectionType.BACKWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(SECONDARY_ATTACK_BACK);
break;
}
}
public void PlayPrimarySkillAnimation()
{
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(PRIMARY_SKILL);
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(PRIMARY_SKILL);
}
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));
}
public void RotateModel(Basis enemyBasis, Vector3 cameraDirection, bool isWalking) => RotateModel(enemyBasis, cameraDirection, 0.55f, 0.45f, isWalking);
public virtual void RotateModel(
Basis enemyBasis,
Vector3 cameraDirection,
float rotateUpperThreshold,
float rotateLowerThreshold,
bool isWalking)
public virtual void PlayIdleAnimation()
{
var enemyForwardDirection = enemyBasis.Z;
var enemyLeftDirection = enemyBasis.X;
switch (_currentDirection)
{
case DirectionType.FORWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_FORWARD);
break;
case DirectionType.RIGHT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_RIGHT);
break;
case DirectionType.LEFT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_LEFT);
break;
case DirectionType.BACKWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_BACK);
break;
}
}
var leftDotProduct = enemyLeftDirection.Dot(cameraDirection);
var forwardDotProduct = enemyForwardDirection.Dot(cameraDirection);
public virtual void PlayWalkAnimation()
{
switch (_currentDirection)
{
case DirectionType.FORWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_FORWARD_WALK);
break;
case DirectionType.RIGHT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_RIGHT_WALK);
break;
case DirectionType.LEFT:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_LEFT_WALK);
break;
case DirectionType.BACKWARD:
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_BACK_WALK);
break;
}
}
// Check if forward facing. If the dot product is -1, the enemy is facing the camera.
if (forwardDotProduct < -rotateUpperThreshold)
{
if (isWalking)
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_FORWARD_WALK);
else
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_FORWARD);
}
// Check if backward facing. If the dot product is 1, the enemy is facing the same direction as the camera.
else if (forwardDotProduct > rotateUpperThreshold)
{
if (isWalking)
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_BACK_WALK);
else
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_BACK);
}
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).
AnimatedSprite.FlipH = leftDotProduct > 0;
// 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 (isWalking)
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_LEFT_WALK);
else
AnimationTree.Get(PARAMETERS_PLAYBACK).As<AnimationNodeStateMachinePlayback>().Travel(IDLE_LEFT);
}
}
private DirectionType GetEnemyDirection(
Basis enemyBasis,
Vector3 cameraDirection,
float rotateUpperThreshold,
float rotateLowerThreshold)
{
var enemyForwardDirection = enemyBasis.Z;
var enemyLeftDirection = enemyBasis.X;
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)
return DirectionType.FORWARD;
// Check if backward facing. If the dot product is 1, the enemy is facing the same direction as the camera.
else if (forwardDotProduct > rotateUpperThreshold)
return DirectionType.BACKWARD;
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)
return DirectionType.RIGHT;
// 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)
return DirectionType.LEFT;
}
return _currentDirection;
}
private void LoadShader(string shaderPath)
{
var shader = GD.Load<Shader>(shaderPath);
AnimatedSprite.Material = new ShaderMaterial();
var shaderMaterial = (ShaderMaterial)AnimatedSprite.Material;
shaderMaterial.Shader = shader;
}
private void AnimationTree_AnimationFinished(StringName animName)
{
if (animName == PRIMARY_ATTACK || animName == SECONDARY_ATTACK || animName == PRIMARY_SKILL)
{
AnimationTree.Get("parameters/playback").As<AnimationNodeStateMachinePlayback>().Travel(IDLE_FORWARD);
}
var shader = GD.Load<Shader>(shaderPath);
AnimatedSprite.Material = new ShaderMaterial();
var shaderMaterial = (ShaderMaterial)AnimatedSprite.Material;
shaderMaterial.Shader = shader;
}
private void SetShaderValue(float shaderValue)
{
var shaderMaterial = (ShaderMaterial)AnimatedSprite.Material;
shaderMaterial.SetShaderParameter("progress", shaderValue);
var shaderMaterial = (ShaderMaterial)AnimatedSprite.Material;
shaderMaterial.SetShaderParameter("progress", shaderValue);
}
}