Object parenting; Ship module prototype; Use 64 bit

This commit is contained in:
Aslan 2026-01-24 23:05:19 -05:00
parent 3f97e18615
commit 8335714acc
30 changed files with 436 additions and 62 deletions

12
scripts/GameManager.cs Normal file
View file

@ -0,0 +1,12 @@
using Godot;
public partial class GameManager : Node
{
public override void _Ready()
{
PackedScene shipPrefab = ResourceLoader.Load<PackedScene>("res://prefabs/vessel.tscn");
Node3D shipInstance = shipPrefab.Instantiate<Node3D>();
GetTree().CurrentScene.CallDeferred("add_child", shipInstance);
}
}

View file

@ -0,0 +1 @@
uid://bwgjvm21oi3d6

4
scripts/GameObject.cs Normal file
View file

@ -0,0 +1,4 @@
public abstract class GameObject
{
public abstract void Simulate(double delta);
}

View file

@ -0,0 +1 @@
uid://dknsws58ej0bp

View file

@ -0,0 +1,14 @@
using System.Collections.Generic;
public class Vessel : GameObject
{
public List<VesselModule> Modules = [];
public override void Simulate(double delta)
{
Modules.ForEach(module =>
{
module.Simulate(delta);
});
}
}

View file

@ -0,0 +1 @@
uid://b8lwtj6w6u5d1

View file

@ -2,21 +2,85 @@ using Godot;
public partial class GravityReceiver : Node3D
{
public int CheckInterval = 100;
public bool InGravityZone = false;
public float GravityStrength = 0f;
public Vector3 GravityDirection = Vector3.Down;
public GravityZone Zone { get; private set; }
public Node3D Parent { get; private set; }
public void EnterGravityZone(float gravityStrength, Vector3 gravityDirection)
private int checkCounter = 0;
private Node3D owner;
private Node3D root;
private GravityZone oldZone;
private Node3D oldParent;
public override void _Ready()
{
InGravityZone = true;
GravityStrength = gravityStrength;
GravityDirection = gravityDirection;
owner = GetParent<Node3D>();
root = GetTree().Root.GetNode<Node3D>("Game");
}
public void ExitGravityZone()
public override void _PhysicsProcess(double delta)
{
InGravityZone = false;
GravityStrength = 0f;
GravityDirection = Vector3.Down;
checkCounter++;
if (checkCounter >= CheckInterval)
{
checkCounter = 0;
if (InGravityZone && owner.GetParent() != Parent)
{
owner.Reparent(Parent, true);
}
if (!InGravityZone && owner.GetParent() != root)
{
owner.Reparent(root, true);
}
}
}
public void EnterGravityZone(GravityZone gravityZone, Node3D newParent)
{
if (Zone != null)
{
oldZone = Zone;
oldParent = Parent;
}
InGravityZone = true;
Zone = gravityZone;
Parent = newParent;
}
public void ExitGravityZone(GravityZone gravityZone)
{
if (gravityZone == Zone)
{
if (oldZone != null && oldZone.GetOverlappingBodies().Contains(owner))
{
GravityZone tempZone = Zone;
Node3D tempParent = Parent;
InGravityZone = true;
Zone = oldZone;
Parent = oldParent;
oldZone = tempZone;
oldParent = tempParent;
}
else
{
InGravityZone = false;
Zone = null;
Parent = null;
}
}
}
public double GetGravityStrength()
{
return Zone.Gravity;
}
public Vector3 GetGravityDirection()
{
return Zone.GlobalTransform.Basis.Y;
}
}

View file

@ -2,15 +2,22 @@ using Godot;
public partial class GravityZone : Area3D
{
public Node3D Parent;
public override void _Ready()
{
Parent = GetParent<Node3D>();
}
public void OnBodyEntered(Node3D body)
{
GravityReceiver receiver = body.GetNodeOrNull<GravityReceiver>("GravityReceiver");
receiver?.EnterGravityZone(Gravity, GlobalTransform.Basis.Y);
receiver?.EnterGravityZone(this, Parent.GetParent<Node3D>());
}
public void OnBodyExited(Node3D body)
{
GravityReceiver receiver = body.GetNodeOrNull<GravityReceiver>("GravityReceiver");
receiver?.ExitGravityZone();
receiver?.ExitGravityZone(this);
}
}

View file

@ -0,0 +1,26 @@
public class BridgeModule : VesselModule
{
public double BaseFlops { get; private set; }
public double WattHoursCapacity { get; private set; }
public double WattHoursActual { get; private set; }
public BridgeModule()
{
WattHoursActual = WattHoursCapacity;
}
public override double GetFlops()
{
return BaseFlops;
}
public override double GetWattHoursCapacity()
{
return WattHoursCapacity;
}
public override double GetWattHoursActual()
{
return WattHoursActual;
}
}

View file

@ -0,0 +1 @@
uid://dqft6vnajtjhg

View file

@ -0,0 +1,9 @@
public class CombinedModule : VesselModule
{
public double BaseWatts { get; private set; }
public override double GetWatts()
{
return BaseWatts;
}
}

View file

@ -0,0 +1 @@
uid://bq0qr0g8gokhb

View file

@ -0,0 +1,9 @@
public class EngineModule : VesselModule
{
public double BaseThrust { get; private set; }
public override double GetThrust()
{
return BaseThrust;
}
}

View file

@ -0,0 +1 @@
uid://crmpdo2d6oqke

View file

@ -10,7 +10,7 @@ public partial class Player : CharacterBody3D
private Vector3 gravityVelocity = Vector3.Zero;
private Vector3 movementVelocity = Vector3.Zero;
private float cameraPitch = 0f;
private double cameraPitch = 0f;
private Camera3D camera;
private GravityReceiver gravityReceiver;
@ -60,10 +60,10 @@ public partial class Player : CharacterBody3D
if (@event is InputEventMouseMotion motion)
{
float yawDelta = -motion.Relative.X * MouseSensitivity;
float yawDeltaRad = Mathf.DegToRad(yawDelta);
float pitchDelta = -motion.Relative.Y * MouseSensitivity;
float pitchDeltaRad = Mathf.DegToRad(pitchDelta);
double yawDelta = -motion.Relative.X * MouseSensitivity;
double yawDeltaRad = Mathf.DegToRad(yawDelta);
double pitchDelta = -motion.Relative.Y * MouseSensitivity;
double pitchDeltaRad = Mathf.DegToRad(pitchDelta);
RotateObjectLocal(Vector3.Up, yawDeltaRad);
@ -82,7 +82,7 @@ public partial class Player : CharacterBody3D
public bool IsInGravity()
{
return gravityReceiver.InGravityZone && gravityReceiver.GravityStrength > 0f;
return gravityReceiver.InGravityZone && gravityReceiver.GetGravityStrength() > 0f;
}
public bool IsOnGravityFloor()
@ -92,7 +92,7 @@ public partial class Player : CharacterBody3D
KinematicCollision3D collision = GetSlideCollision(i);
Vector3 collisionNormal = collision.GetNormal();
float alignment = collisionNormal.Dot(gravityReceiver.GravityDirection);
double alignment = collisionNormal.Dot(gravityReceiver.GetGravityDirection());
if (alignment > 0.7f)
{
@ -107,35 +107,35 @@ public partial class Player : CharacterBody3D
{
if (!IsOnGravityFloor())
{
newGravityVelocity -= gravityReceiver.GravityDirection * gravityReceiver.GravityStrength * (float)delta;
newGravityVelocity -= gravityReceiver.GetGravityDirection() * gravityReceiver.GetGravityStrength() * (float)delta;
}
else
{
newGravityVelocity = -gravityReceiver.GravityDirection;
newGravityVelocity = -gravityReceiver.GetGravityDirection();
}
Vector3 currentUp = GlobalTransform.Basis.Y;
Vector3 targetUp = gravityReceiver.GravityDirection;
Vector3 targetUp = gravityReceiver.GetGravityDirection();
Vector3 axis = currentUp.Cross(targetUp);
if (axis.Length() < 0.00001f)
{
return;
}
float angle = currentUp.AngleTo(targetUp);
double angle = currentUp.AngleTo(targetUp);
Rotate(axis.Normalized(), angle * (float)delta * 10f);
GlobalRotate(axis.Normalized(), angle * delta * 10f);
}
private void ProcessCamera(double delta)
{
if (cameraPitch > 0.01f)
{
cameraPitch -= 90f * (float)delta;
cameraPitch -= 90f * delta;
}
else if (cameraPitch < -0.001f)
{
cameraPitch += 90f * (float)delta;
cameraPitch += 90f * delta;
}
if (Mathf.Abs(cameraPitch) < 1f)
{
@ -149,7 +149,7 @@ public partial class Player : CharacterBody3D
float inputX = Input.GetAxis("move_left", "move_right");
float inputZ = Input.GetAxis("move_forward", "move_back");
Vector3 direction = Transform.Basis.X * inputX + Transform.Basis.Z * inputZ;
Vector3 direction = GlobalTransform.Basis.X * inputX + GlobalTransform.Basis.Z * inputZ;
if (direction != Vector3.Zero)
{
@ -166,7 +166,7 @@ public partial class Player : CharacterBody3D
if (Input.IsActionJustPressed("jump") && IsOnGravityFloor())
{
newGravityVelocity = gravityReceiver.GravityDirection * JumpForce;
newGravityVelocity = gravityReceiver.GetGravityDirection() * JumpForce;
}
}
@ -191,9 +191,9 @@ public partial class Player : CharacterBody3D
newMovementVelocity.Z = 0f;
}
float inputRotateZ = Input.GetAxis("rotate_left", "rotate_right");
double inputRotateZ = Input.GetAxis("rotate_left", "rotate_right");
float rotateAmountZ = Mathf.DegToRad(inputRotateZ);
double rotateAmountZ = Mathf.DegToRad(inputRotateZ);
RotateObjectLocal(Vector3.Forward, rotateAmountZ);
}

29
scripts/VesselModule.cs Normal file
View file

@ -0,0 +1,29 @@
public abstract class VesselModule
{
public virtual void Simulate(double delta) { }
public virtual double GetFlops()
{
return 0;
}
public virtual double GetWattHoursCapacity()
{
return 0;
}
public virtual double GetWattHoursActual()
{
return 0;
}
public virtual double GetWatts()
{
return 0;
}
public virtual double GetThrust()
{
return 0;
}
}

View file

@ -0,0 +1 @@
uid://4boxx2qksadv

View file

@ -0,0 +1,11 @@
using Godot;
public partial class VesselModuleNode : StaticBody3D
{
public VesselModule ModuleData { get; private set; }
public Node3D GetAnchor(string name)
{
return GetNode<Node3D>(name);
}
}

View file

@ -0,0 +1 @@
uid://d4naw4kwsaxgi

41
scripts/VesselNode.cs Normal file
View file

@ -0,0 +1,41 @@
using System.Collections.Generic;
using Godot;
public partial class VesselNode : RigidBody3D
{
public Vessel VesselData = new();
public List<VesselModuleNode> Modules = [];
public override void _Ready()
{
VesselModuleNode moduleBridge = InstantiateModule("test_module_bridge");
VesselModuleNode moduleCombined = InstantiateModule("test_module_combined");
VesselModuleNode moduleEngine1 = InstantiateModule("test_module_engine");
VesselModuleNode moduleEngine2 = InstantiateModule("test_module_engine");
CallDeferred(nameof(AttachModule), moduleBridge, moduleCombined, "TestModuleBridge_AnchorBack", "TestModuleCombined_AnchorFront");
CallDeferred(nameof(AttachModule), moduleBridge, moduleEngine1, "TestModuleBridge_AnchorLeft", "TestModuleEngine_AnchorRight");
CallDeferred(nameof(AttachModule), moduleBridge, moduleEngine2, "TestModuleBridge_AnchorRight", "TestModuleEngine_AnchorLeft");
}
private VesselModuleNode InstantiateModule(string moduleName)
{
PackedScene modulePrefab = ResourceLoader.Load<PackedScene>($"res://prefabs/modules/{moduleName}.tscn");
VesselModuleNode moduleInstance = modulePrefab.Instantiate<VesselModuleNode>();
CallDeferred("add_child", moduleInstance);
VesselData.Modules.Add(moduleInstance.ModuleData);
Modules.Add(moduleInstance);
return moduleInstance;
}
private void AttachModule(VesselModuleNode attachToModule, VesselModuleNode newModule, string existingAnchorName, string newAnchorName)
{
Node3D existingAnchor = attachToModule.GetAnchor(existingAnchorName);
Node3D newAnchor = newModule.GetAnchor(newAnchorName);
newModule.GlobalTransform = existingAnchor.GlobalTransform * newAnchor.Transform.AffineInverse();
}
}

View file

@ -0,0 +1 @@
uid://dvd8i36joinsc