82 lines
2.1 KiB
C#
82 lines
2.1 KiB
C#
using Godot;
|
|
using System;
|
|
|
|
public partial class Player2 : 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)
|
|
{
|
|
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;
|
|
GetNode<Node3D>("Pivot").LookAt(Position + direction, Vector3.Up);
|
|
}
|
|
else
|
|
{
|
|
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
|
|
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
|
|
}
|
|
return velocity;
|
|
}
|
|
|
|
private async void Fire()
|
|
{
|
|
var projectile = _fireProjectile.Instantiate<Projectile>();
|
|
projectile.Position = Position + new Vector3(0f, 1f, -1f);
|
|
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, -1f);
|
|
GetParent().AddChild(projectile);
|
|
CanShoot = false;
|
|
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
|
|
CanShoot = true;
|
|
}
|
|
|
|
private void OnHit(Node3D node)
|
|
{
|
|
QueueFree();
|
|
}
|
|
}
|