139 lines
3.7 KiB
C#
139 lines
3.7 KiB
C#
using Godot;
|
|
using System.Linq;
|
|
|
|
public partial class CapricornControls : Character
|
|
{
|
|
private AnimatedSprite3D _sprite;
|
|
|
|
public override void _Ready()
|
|
{
|
|
base._Ready();
|
|
Position = OwnerPlayer.SpawnPoint.Position;
|
|
_sprite = GetNode<AnimatedSprite3D>("Pivot/Sprite");
|
|
}
|
|
|
|
public bool IsShooting = false;
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
Velocity = CalculateCharacterMovement(delta);
|
|
MoveAndSlide();
|
|
}
|
|
|
|
public override void _Input(InputEvent @event)
|
|
{
|
|
if (Input.IsActionJustPressed("exit"))
|
|
GetTree().Quit();
|
|
|
|
if (Input.IsActionJustPressed(OwnerPlayer.PlayerInput.Fire()) && CanShoot)
|
|
Fire();
|
|
if (Input.IsActionJustPressed(OwnerPlayer.PlayerInput.AltFire()) && CanShoot)
|
|
AltFire();
|
|
}
|
|
|
|
private Vector3 CalculateCharacterMovement(double delta)
|
|
{
|
|
var velocity = Velocity;
|
|
|
|
var inputDir = Input.GetVector(OwnerPlayer.PlayerInput.Left(), OwnerPlayer.PlayerInput.Right(), OwnerPlayer.PlayerInput.Up(), OwnerPlayer.PlayerInput.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;
|
|
CanShoot = false;
|
|
var projectile = _fireProjectile.Instantiate<Node3D>();
|
|
await ToSignal(GetTree().CreateTimer(1.0f), "timeout");
|
|
projectile.Position = Position;
|
|
if (GetParent() != null)
|
|
GetParent().AddChild(projectile);
|
|
CanShoot = true;
|
|
IsShooting = false;
|
|
}
|
|
|
|
private async void AltFire()
|
|
{
|
|
IsShooting = true;
|
|
CanShoot = false;
|
|
var projectile = _altFireProjectile.Instantiate<Node3D>();
|
|
await ToSignal(GetTree().CreateTimer(1.0f), "timeout");
|
|
projectile.Position = Position;
|
|
if (GetParent() != null)
|
|
GetParent().AddChild(projectile);
|
|
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 new void OnHit(Node3D node)
|
|
{
|
|
_gameManager.CallDeferred(GameManager.MethodName.RemoveCharacter, OwnerPlayer);
|
|
}
|
|
}
|