75 lines
2.0 KiB
C#
75 lines
2.0 KiB
C#
using Godot;
|
|
|
|
public partial class TestCharacter : CharacterBody3D
|
|
{
|
|
[Export]
|
|
private float _speed = 5.0f;
|
|
[Export]
|
|
private PackedScene _fireProjectile;
|
|
[Export]
|
|
private PackedScene _altFireProjectile;
|
|
|
|
public bool CanShoot { get; private set; }
|
|
|
|
public override void _Ready()
|
|
{
|
|
CanShoot = true;
|
|
}
|
|
|
|
public override void _PhysicsProcess(double delta)
|
|
{
|
|
if (Input.IsActionJustPressed("p1_fire") && CanShoot)
|
|
Fire();
|
|
if (Input.IsActionJustPressed("p1_altfire") && CanShoot)
|
|
AltFire();
|
|
|
|
Velocity = CalculateCharacterMovement(delta);
|
|
MoveAndSlide();
|
|
}
|
|
|
|
private Vector3 CalculateCharacterMovement(double delta)
|
|
{
|
|
var velocity = Velocity;
|
|
|
|
var inputDir = Input.GetVector("p1_left", "p1_right", "p1_up", "p1_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 * 2;
|
|
GetNode<Node3D>("Pivot").LookAt(Position + direction, Vector3.Forward + Vector3.Up);
|
|
}
|
|
else
|
|
{
|
|
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
|
|
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed * 2);
|
|
}
|
|
return velocity;
|
|
}
|
|
|
|
private async void Fire()
|
|
{
|
|
var projectile = _fireProjectile.Instantiate<Projectile>();
|
|
projectile.Position = Position + new Vector3(0f, 1f, -3f);
|
|
GetParent().AddChild(projectile);
|
|
CanShoot = false;
|
|
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
|
|
CanShoot = true;
|
|
}
|
|
|
|
private async void AltFire()
|
|
{
|
|
var projectile = _altFireProjectile.Instantiate<Projectile>();
|
|
projectile.Position = Position + new Vector3(0f, 1f, -3f);
|
|
GetParent().AddChild(projectile);
|
|
CanShoot = false;
|
|
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
|
|
CanShoot = true;
|
|
}
|
|
|
|
private void OnHit(Node3D node)
|
|
{
|
|
QueueFree();
|
|
}
|
|
}
|