This commit is contained in:
2023-07-17 13:10:26 -07:00
parent 036651f2d3
commit 86b8ddc3f0
10 changed files with 131 additions and 128 deletions

View File

@@ -1,7 +1,9 @@
using Godot;
public partial class Blizzard : Projectile
{
public override void _PhysicsProcess(double delta)
{
Translate(new Godot.Vector3(0, 0, Speed * (float)delta));
}
public override void _PhysicsProcess(double delta)
{
Translate(new Vector3(0, 0, Speed * (float)delta));
}
}

View File

@@ -2,8 +2,8 @@ using Godot;
public partial class Fireball : Projectile
{
public override void _PhysicsProcess(double delta)
{
Translate(new Vector3(0, 0, Speed * (float)delta));
}
public override void _PhysicsProcess(double delta)
{
Translate(new Vector3(0, 0, Speed * (float)delta));
}
}

View File

@@ -4,93 +4,102 @@ using Godot.Collections;
public partial class Player : CharacterBody3D
{
[Export]
private float _speed = 5.0f;
[Export]
private float _jumpVelocity = 4.5f;
[Export]
private float _sensitivityHorizontal = 0.05f;
[Export]
private float _sensitivityVertical = 0.05f;
[Export]
private float _joystickDeadZone = 0.5f;
[Export]
private Array<PackedScene> _projectiles;
[Export]
private float _speed = 5.0f;
[Export]
private float _jumpVelocity = 4.5f;
[Export]
private float _sensitivityHorizontal = 0.05f;
[Export]
private float _sensitivityVertical = 0.05f;
[Export]
private float _joystickDeadZone = 0.5f;
[Export]
private Array<PackedScene> _projectiles;
private Node3D _cameraMount;
private Node3D _pivot;
private Node3D _camera;
private PackedScene _weaponType;
public bool CanShoot { get; private set; }
// Get the _gravity from the project settings to be synced with RigidBody nodes.
public float _gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
private Node3D _cameraMount;
private Node3D _pivot;
private Node3D _camera;
private PackedScene _weaponType;
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Captured;
_cameraMount = GetNode<Node3D>("/root/Main/Player/CameraMount");
_camera = GetNode<Node3D>("/root/Main/Player/CameraMount/Camera");
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
_weaponType = _projectiles.First();
}
// Get the _gravity from the project settings to be synced with RigidBody nodes.
public float _gravity = ProjectSettings.GetSetting("physics/3d/default_gravity").AsSingle();
public override void _Ready()
{
Input.MouseMode = Input.MouseModeEnum.Captured;
_cameraMount = GetNode<Node3D>("/root/Main/Player/CameraMount");
_camera = GetNode<Node3D>("/root/Main/Player/CameraMount/Camera");
_pivot = GetNode<Node3D>("/root/Main/Player/Pivot");
_weaponType = _projectiles.First();
CanShoot = true;
}
public override void _Process(double delta)
{
if (Input.IsActionJustPressed("quit"))
GetTree().Quit();
if (Input.IsActionJustPressed("shoot"))
Shoot();
if (Input.IsActionJustPressed("switch"))
SwitchWeaponType();
if (Input.IsActionJustPressed("quit"))
GetTree().Quit();
if (Input.IsActionJustPressed("shoot") && CanShoot)
Shoot();
if (Input.IsActionJustPressed("switch"))
SwitchWeaponType();
}
public override void _PhysicsProcess(double delta)
{
var gravityDelta = _gravity * (float)delta;
var velocity = CalculateMovement(Velocity, Transform, gravityDelta);
{
var gravityDelta = _gravity * (float)delta;
var velocity = CalculateMovement(Velocity, Transform, gravityDelta);
Velocity = velocity;
MoveAndSlide();
}
Velocity = velocity;
MoveAndSlide();
}
private void Shoot()
{
GD.Print("Shoot");
var projectile = _weaponType.Instantiate<Node3D>();
projectile.Position = Position;
projectile.Rotation = Rotation;
GetParent().AddChild(projectile);
}
private async void Shoot()
{
GD.Print("Shoot");
var projectile = _weaponType.Instantiate<Projectile>();
projectile.Position = Position;
projectile.Rotation = Rotation;
GetParent().AddChild(projectile);
CanShoot = false;
await ToSignal(GetTree().CreateTimer(projectile.Cooldown), "timeout");
CanShoot = true;
}
private void SwitchWeaponType()
{
var index = _projectiles.IndexOf(_weaponType);
_weaponType = _projectiles[(index + 1) % _projectiles.Count];
private async void SwitchWeaponType()
{
var index = _projectiles.IndexOf(_weaponType);
_weaponType = _projectiles[(index + 1) % _projectiles.Count];
CanShoot = false;
await ToSignal(GetTree().CreateTimer(.5), "timeout");
CanShoot = true;
}
}
private Vector3 CalculateMovement(Vector3 velocity, Transform3D transform, float gravityDelta)
{
if (!IsOnFloor())
velocity.Y -= gravityDelta;
private Vector3 CalculateMovement(Vector3 velocity, Transform3D transform, float gravityDelta)
{
if (!IsOnFloor())
velocity.Y -= gravityDelta;
if (Input.IsActionJustPressed("jump") && IsOnFloor())
velocity.Y = _jumpVelocity;
if (Input.IsActionJustPressed("jump") && IsOnFloor())
velocity.Y = _jumpVelocity;
Vector2 inputDir = Input.GetVector("right", "left", "back", "forward");
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.Normalized(), Vector3.Up);
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
}
Vector2 inputDir = Input.GetVector("right", "left", "back", "forward");
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.Normalized(), Vector3.Up);
}
else
{
velocity.X = Mathf.MoveToward(Velocity.X, 0, _speed);
velocity.Z = Mathf.MoveToward(Velocity.Z, 0, _speed);
}
return velocity;
}
return velocity;
}
}

View File

@@ -2,13 +2,21 @@
public partial class Projectile : Node3D
{
[Export]
private float _projectileSpeed = 0.1f;
[Export]
public double Cooldown { get; protected set; }
public override void _Ready()
{
Speed = _projectileSpeed;
}
[Export]
private float _projectileSpeed = 0.1f;
public float Speed { get; private set; }
public override void _Ready()
{
Speed = _projectileSpeed;
}
public float Speed { get; private set; }
public void OnTimeToLiveTimeout()
{
QueueFree();
}
}

View File

@@ -1,8 +0,0 @@
namespace Sandbox.Scripts
{
internal enum WeaponType
{
Fire = 0,
Blizzard = 1
}
}