Object parenting; Ship module prototype; Use 64 bit
This commit is contained in:
parent
3f97e18615
commit
8335714acc
30 changed files with 436 additions and 62 deletions
12
scripts/GameManager.cs
Normal file
12
scripts/GameManager.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
scripts/GameManager.cs.uid
Normal file
1
scripts/GameManager.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bwgjvm21oi3d6
|
||||
4
scripts/GameObject.cs
Normal file
4
scripts/GameObject.cs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
public abstract class GameObject
|
||||
{
|
||||
public abstract void Simulate(double delta);
|
||||
}
|
||||
1
scripts/GameObject.cs.uid
Normal file
1
scripts/GameObject.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dknsws58ej0bp
|
||||
14
scripts/GameObjects/Vessel.cs
Normal file
14
scripts/GameObjects/Vessel.cs
Normal 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);
|
||||
});
|
||||
}
|
||||
}
|
||||
1
scripts/GameObjects/Vessel.cs.uid
Normal file
1
scripts/GameObjects/Vessel.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://b8lwtj6w6u5d1
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
26
scripts/Modules/BridgeModule.cs
Normal file
26
scripts/Modules/BridgeModule.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
1
scripts/Modules/BridgeModule.cs.uid
Normal file
1
scripts/Modules/BridgeModule.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dqft6vnajtjhg
|
||||
9
scripts/Modules/CombinedModule.cs
Normal file
9
scripts/Modules/CombinedModule.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
public class CombinedModule : VesselModule
|
||||
{
|
||||
public double BaseWatts { get; private set; }
|
||||
|
||||
public override double GetWatts()
|
||||
{
|
||||
return BaseWatts;
|
||||
}
|
||||
}
|
||||
1
scripts/Modules/CombinedModule.cs.uid
Normal file
1
scripts/Modules/CombinedModule.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://bq0qr0g8gokhb
|
||||
9
scripts/Modules/EngineModule.cs
Normal file
9
scripts/Modules/EngineModule.cs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
public class EngineModule : VesselModule
|
||||
{
|
||||
public double BaseThrust { get; private set; }
|
||||
|
||||
public override double GetThrust()
|
||||
{
|
||||
return BaseThrust;
|
||||
}
|
||||
}
|
||||
1
scripts/Modules/EngineModule.cs.uid
Normal file
1
scripts/Modules/EngineModule.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://crmpdo2d6oqke
|
||||
|
|
@ -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
29
scripts/VesselModule.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
1
scripts/VesselModule.cs.uid
Normal file
1
scripts/VesselModule.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://4boxx2qksadv
|
||||
11
scripts/VesselModuleNode.cs
Normal file
11
scripts/VesselModuleNode.cs
Normal 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);
|
||||
}
|
||||
}
|
||||
1
scripts/VesselModuleNode.cs.uid
Normal file
1
scripts/VesselModuleNode.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://d4naw4kwsaxgi
|
||||
41
scripts/VesselNode.cs
Normal file
41
scripts/VesselNode.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
1
scripts/VesselNode.cs.uid
Normal file
1
scripts/VesselNode.cs.uid
Normal file
|
|
@ -0,0 +1 @@
|
|||
uid://dvd8i36joinsc
|
||||
Loading…
Add table
Add a link
Reference in a new issue