150 lines
3.9 KiB
C#
150 lines
3.9 KiB
C#
using Godot;
|
|
|
|
public partial class Capricorn2 : Character2
|
|
{
|
|
private AnimatedSprite3D _sprite;
|
|
|
|
public override void _Ready()
|
|
{
|
|
_sprite = GetNode<AnimatedSprite3D>("Pivot/Sprite");
|
|
CanShoot = true;
|
|
_gameManager = GetTree().Root.GetNode<GameManager>("Main/GameManager");
|
|
}
|
|
|
|
[Export]
|
|
private PackedScene _fireProjectile;
|
|
[Export]
|
|
private PackedScene _altFireProjectile;
|
|
|
|
[Export]
|
|
private float _speed = 3.0f;
|
|
|
|
public bool CanShoot { get; private set; }
|
|
|
|
public bool IsShooting = false;
|
|
|
|
private GameManager _gameManager;
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
Velocity = CalculateCharacterMovement(delta);
|
|
MoveAndSlide();
|
|
}
|
|
|
|
public override void _UnhandledInput(InputEvent @event)
|
|
{
|
|
if (Input.IsActionJustPressed("exit"))
|
|
GetTree().Quit();
|
|
|
|
if (Input.IsActionJustPressed($"p2_fire") && CanShoot)
|
|
Fire();
|
|
if (Input.IsActionJustPressed($"p2_altfire") && CanShoot)
|
|
AltFire();
|
|
}
|
|
|
|
private Vector3 CalculateCharacterMovement(double delta)
|
|
{
|
|
var velocity = Velocity;
|
|
|
|
var inputDir = Input.GetVector($"p2_left", $"p2_right", $"p2_up", $"p2_down");
|
|
var direction = (Transform.Basis * new Vector3(inputDir.X, 0, inputDir.Y)).Normalized();
|
|
if (direction != Vector3.Zero)
|
|
{
|
|
velocity.X = direction.X * _speed;
|
|
velocity.Z = direction.Z * _speed;
|
|
}
|
|
else
|
|
{
|
|
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
|
|
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
|
|
}
|
|
|
|
if (IsShooting)
|
|
AttackSprite(direction);
|
|
else
|
|
WalkSprite(direction);
|
|
|
|
if (!IsShooting && direction.IsEqualApprox(Vector3.Zero))
|
|
{
|
|
_sprite.Play("WalkForward");
|
|
_sprite.Stop();
|
|
}
|
|
|
|
return velocity;
|
|
}
|
|
|
|
private void WalkSprite(Vector3 direction)
|
|
{
|
|
var roundedDirection = direction.Round();
|
|
|
|
if (roundedDirection == Vector3.Right)
|
|
{
|
|
_sprite.Play("WalkSide");
|
|
_sprite.FlipH = false;
|
|
}
|
|
if (roundedDirection == Vector3.Left)
|
|
{
|
|
_sprite.Play("WalkSide");
|
|
_sprite.FlipH = true;
|
|
}
|
|
if (roundedDirection == Vector3.Forward)
|
|
_sprite.Play("WalkForward");
|
|
if (roundedDirection == Vector3.Back)
|
|
_sprite.Play("WalkBack");
|
|
}
|
|
|
|
private async void Fire()
|
|
{
|
|
IsShooting = true;
|
|
var projectile = _fireProjectile.Instantiate<Projectile>();
|
|
projectile.Position = Position + new Vector3(0f, 1f, 0f);
|
|
projectile.ParentCharacter = this;
|
|
GetParent().AddChild(projectile);
|
|
CanShoot = false;
|
|
await ToSignal(GetTree().CreateTimer(2.0f), "timeout");
|
|
CanShoot = true;
|
|
IsShooting = false;
|
|
}
|
|
|
|
private async void AltFire()
|
|
{
|
|
IsShooting = true;
|
|
var projectile = _altFireProjectile.Instantiate<Projectile>();
|
|
projectile.Position = Position + new Vector3(0f, 1f, 0f);
|
|
projectile.ParentCharacter = this;
|
|
GetParent().AddChild(projectile);
|
|
CanShoot = false;
|
|
await ToSignal(GetTree().CreateTimer(2.0f), "timeout");
|
|
CanShoot = true;
|
|
IsShooting = false;
|
|
}
|
|
|
|
private void AttackSprite(Vector3 direction)
|
|
{
|
|
var roundedDirection = direction.Round();
|
|
|
|
if (roundedDirection == Vector3.Right)
|
|
{
|
|
_sprite.Play("AttackSide");
|
|
_sprite.FlipH = false;
|
|
}
|
|
if (roundedDirection == Vector3.Left)
|
|
{
|
|
_sprite.Play("AttackSide");
|
|
_sprite.FlipH = true;
|
|
}
|
|
if (roundedDirection == Vector3.Forward)
|
|
_sprite.Play("AttackForward");
|
|
if (roundedDirection == Vector3.Back)
|
|
_sprite.Play("AttackBack");
|
|
|
|
if (direction.IsEqualApprox(Vector3.Zero))
|
|
_sprite.Play("AttackForward");
|
|
}
|
|
|
|
public void OnHit(Node3D node)
|
|
{
|
|
_gameManager.CallDeferred(GameManager.MethodName.RemoveCharacter, this);
|
|
}
|
|
}
|