Added dynamic spawning and despawning of objects

This commit is contained in:
Aslan 2026-01-27 07:08:03 -05:00
parent adbb208436
commit e0cf8d9755
23 changed files with 794 additions and 350 deletions

View file

@ -2,15 +2,19 @@ using Godot;
public partial class Player : CharacterBody3D
{
[Export] Control GameMenu;
[Export] public float Speed = 5f;
[Export] public float JumpForce = 5f;
[Export] public float MouseSensitivity = 0.2f;
[Export] public double Speed = 5;
[Export] public double SprintMultiplier = 2;
[Export] public double JumpForce = 5;
[Export] public double MouseSensitivity = 0.2;
public Control GameMenu { get; set; }
public Character PlayerData { get; set; }
private Vector3 gravityVelocity = Vector3.Zero;
private Vector3 movementVelocity = Vector3.Zero;
private double cameraPitch = 0f;
private double cameraPitch = 0;
private Camera3D camera;
private GravityReceiver gravityReceiver;
@ -18,6 +22,14 @@ public partial class Player : CharacterBody3D
{
camera = GetNode<Camera3D>("Camera");
gravityReceiver = GetNode<GravityReceiver>("GravityReceiver");
PlayerData.UpdateNodePosition();
}
public override void _Process(double delta)
{
PlayerData.SetCoordinatesFromLocal(GlobalPosition);
PlayerData.Simulate(delta);
}
public override void _PhysicsProcess(double delta)
@ -69,7 +81,7 @@ public partial class Player : CharacterBody3D
if (IsInGravity())
{
cameraPitch = Mathf.Clamp(cameraPitch + pitchDelta, -90f, 90f);
cameraPitch = Mathf.Clamp(cameraPitch + pitchDelta, -90, 90);
}
else
{
@ -82,7 +94,7 @@ public partial class Player : CharacterBody3D
public bool IsInGravity()
{
return gravityReceiver.InGravityZone && gravityReceiver.GetGravityStrength() > 0f;
return gravityReceiver.InGravityZone && gravityReceiver.GetGravityStrength() > 0;
}
public bool IsOnGravityFloor()
@ -94,7 +106,7 @@ public partial class Player : CharacterBody3D
double alignment = collisionNormal.Dot(gravityReceiver.GetGravityDirection());
if (alignment > 0.7f)
if (alignment > 0.7)
{
return true;
}
@ -118,28 +130,28 @@ public partial class Player : CharacterBody3D
Vector3 targetUp = gravityReceiver.GetGravityDirection();
Vector3 axis = currentUp.Cross(targetUp);
if (axis.Length() < 0.00001f)
if (axis.Length() < 0.00001)
{
return;
}
double angle = currentUp.AngleTo(targetUp);
GlobalRotate(axis.Normalized(), angle * delta * 10f);
GlobalRotate(axis.Normalized(), angle * delta * 10);
}
private void ProcessCamera(double delta)
{
if (cameraPitch > 0.01f)
if (cameraPitch > 0.01)
{
cameraPitch -= 90f * delta;
cameraPitch -= 90 * delta;
}
else if (cameraPitch < -0.001f)
else if (cameraPitch < -0.001)
{
cameraPitch += 90f * delta;
cameraPitch += 90 * delta;
}
if (Mathf.Abs(cameraPitch) < 1f)
if (Mathf.Abs(cameraPitch) < 1)
{
cameraPitch = 0f;
cameraPitch = 0;
}
camera.RotationDegrees = new Vector3(cameraPitch, 0, 0);
}
@ -149,19 +161,21 @@ public partial class Player : CharacterBody3D
float inputX = Input.GetAxis("move_left", "move_right");
float inputZ = Input.GetAxis("move_forward", "move_back");
bool sprint = Input.IsActionPressed("sprint");
Vector3 direction = GlobalTransform.Basis.X * inputX + GlobalTransform.Basis.Z * inputZ;
if (direction != Vector3.Zero)
{
newMovementVelocity.X = direction.X * Speed;
newMovementVelocity.Y = direction.Y * Speed;
newMovementVelocity.Z = direction.Z * Speed;
newMovementVelocity.X = direction.X * Speed * (sprint ? SprintMultiplier : 1);
newMovementVelocity.Y = direction.Y * Speed * (sprint ? SprintMultiplier : 1);
newMovementVelocity.Z = direction.Z * Speed * (sprint ? SprintMultiplier : 1);
}
else
{
newMovementVelocity.X = 0f;
newMovementVelocity.Y = 0f;
newMovementVelocity.Z = 0f;
newMovementVelocity.X = 0;
newMovementVelocity.Y = 0;
newMovementVelocity.Z = 0;
}
if (Input.IsActionJustPressed("jump") && IsOnGravityFloor())
@ -176,19 +190,21 @@ public partial class Player : CharacterBody3D
float inputY = -Input.GetAxis("move_up", "move_down");
float inputZ = Input.GetAxis("move_forward", "move_back");
bool sprint = Input.IsActionPressed("sprint");
Vector3 direction = Transform.Basis.X * inputX + Transform.Basis.Y * inputY + Transform.Basis.Z * inputZ;
if (direction != Vector3.Zero)
{
newMovementVelocity.X = direction.X * Speed;
newMovementVelocity.Y = direction.Y * Speed;
newMovementVelocity.Z = direction.Z * Speed;
newMovementVelocity.X = direction.X * Speed * (sprint ? SprintMultiplier : 1);
newMovementVelocity.Y = direction.Y * Speed * (sprint ? SprintMultiplier : 1);
newMovementVelocity.Z = direction.Z * Speed * (sprint ? SprintMultiplier : 1);
}
else
{
newMovementVelocity.X = 0f;
newMovementVelocity.Y = 0f;
newMovementVelocity.Z = 0f;
newMovementVelocity.X = 0;
newMovementVelocity.Y = 0;
newMovementVelocity.Z = 0;
}
double inputRotateZ = Input.GetAxis("rotate_left", "rotate_right");