Link CAN hit
This commit is contained in:
22
Scripts/Projectile.cs
Normal file
22
Scripts/Projectile.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Godot;
|
||||
|
||||
public partial class Projectile : Node3D
|
||||
{
|
||||
[Export]
|
||||
public double Cooldown { get; protected set; }
|
||||
|
||||
[Export]
|
||||
private float _projectileSpeed = 100f;
|
||||
|
||||
public override void _Ready()
|
||||
{
|
||||
Speed = _projectileSpeed;
|
||||
}
|
||||
|
||||
public float Speed { get; private set; }
|
||||
|
||||
public void OnTimeToLiveTimeout()
|
||||
{
|
||||
QueueFree();
|
||||
}
|
||||
}
|
||||
10
Scripts/TestBullet.cs
Normal file
10
Scripts/TestBullet.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using Godot;
|
||||
|
||||
public partial class TestBullet : Projectile
|
||||
{
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
Translate(new Vector3(0, 0, Speed * -(float)delta));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,32 +1,46 @@
|
||||
using Godot;
|
||||
using System;
|
||||
|
||||
public partial class TestCharacter : CharacterBody3D
|
||||
{
|
||||
[Export]
|
||||
private float _speed = 5.0f;
|
||||
[Export]
|
||||
private float _speed = 5.0f;
|
||||
[Export]
|
||||
private PackedScene _attackPattern;
|
||||
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
Vector3 velocity = Velocity;
|
||||
public override void _PhysicsProcess(double delta)
|
||||
{
|
||||
if (Input.IsActionJustPressed("p1_fire"))
|
||||
Fire();
|
||||
|
||||
// Get the input direction and handle the movement/deceleration.
|
||||
// As good practice, you should replace UI actions with custom gameplay actions.
|
||||
Vector2 inputDir = Input.GetVector("p1_left", "p1_right", "p1_up", "p1_down");
|
||||
Vector3 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);
|
||||
}
|
||||
Velocity = CalculateCharacterMovement();
|
||||
MoveAndSlide();
|
||||
}
|
||||
|
||||
Velocity = velocity;
|
||||
MoveAndSlide();
|
||||
}
|
||||
private Vector3 CalculateCharacterMovement()
|
||||
{
|
||||
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;
|
||||
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()
|
||||
{
|
||||
GD.Print("Shoot");
|
||||
var projectile = _attackPattern.Instantiate<Projectile>();
|
||||
projectile.Position = Position + new Vector3(0f, 0f, -0.2f);
|
||||
GetParent().AddChild(projectile);
|
||||
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user