Add offset when placing blocks

This commit is contained in:
Sofia 2024-08-20 03:29:32 +03:00
parent e4504fbfd3
commit da60de0148
3 changed files with 42 additions and 9 deletions

View File

@ -10,11 +10,14 @@ namespace Gmtk24 {
public ShaderMaterial Material; public ShaderMaterial Material;
public ShaderMaterial HoverMaterial; public ShaderMaterial HoverMaterial;
public float RelativeScale; public float RelativeScale;
public uint PhysicalModeLayer; public uint PhysicalModeLayer;
public uint NonPhysicalModeLayer = 0;
public uint PlacedLayer = 0b10000;
public BlockMode Mode = BlockMode.Physical; public BlockMode Mode = BlockMode.Physical;
private MeshInstance3D MeshInstance; public MeshInstance3D MeshInstance { private set; get; }
private ShaderMaterial[] NormalMaterialArr; private ShaderMaterial[] NormalMaterialArr;
private ShaderMaterial[] HoverMaterialArr; private ShaderMaterial[] HoverMaterialArr;
@ -88,7 +91,7 @@ namespace Gmtk24 {
private void UpdatePhysics() { private void UpdatePhysics() {
if (Mode == BlockMode.Physical || Mode == BlockMode.Dragged) { if (Mode == BlockMode.Physical || Mode == BlockMode.Dragged) {
CollisionLayer = PhysicalModeLayer; CollisionLayer = PhysicalModeLayer;
CollisionMask = PhysicalModeLayer + 0b10000; CollisionMask = PhysicalModeLayer | PlacedLayer;
if (Mode == BlockMode.Dragged) { if (Mode == BlockMode.Dragged) {
FreezeMode = FreezeModeEnum.Kinematic; FreezeMode = FreezeModeEnum.Kinematic;
Freeze = true; Freeze = true;
@ -97,11 +100,11 @@ namespace Gmtk24 {
} }
} else if (Mode == BlockMode.NonPhysical || Mode == BlockMode.Placed) { } else if (Mode == BlockMode.NonPhysical || Mode == BlockMode.Placed) {
if (Mode == BlockMode.Placed) { if (Mode == BlockMode.Placed) {
CollisionLayer = 0b10000; CollisionLayer = PlacedLayer;
CollisionMask = 0b10000; CollisionMask = PlacedLayer;
} else { } else {
CollisionLayer = 0; CollisionLayer = NonPhysicalModeLayer;
CollisionMask = 0; CollisionMask = NonPhysicalModeLayer;
} }
FreezeMode = FreezeModeEnum.Static; FreezeMode = FreezeModeEnum.Static;
Freeze = true; Freeze = true;
@ -127,6 +130,29 @@ namespace Gmtk24 {
EmitSignal(SignalName.PickUp, this); EmitSignal(SignalName.PickUp, this);
} }
// Only works for Ghost blocks, which are always on Collision Layer 6
// (nothing else should ever be on that layer.)
public Vector3 OffsetFrom(Vector3 Normal, uint mask = 0b100000) {
var origin = ((-Normal) * 10f) + GlobalPosition;
var normal = (Normal * 10f) + GlobalPosition;
var ray = new PhysicsRayQueryParameters3D {
From = origin,
To = normal,
CollideWithBodies = true,
CollisionMask = mask,
HitBackFaces = false,
HitFromInside = false,
};
var newRes = GetWorld3D().DirectSpaceState.IntersectRay(ray);
if (newRes.ContainsKey("position")) {
var GlobalPos = (Vector3)newRes["position"];
var Offset = GlobalPos - GlobalPosition;
return -Offset;
}
return Vector3.Zero;
}
public struct BaseBlock { public struct BaseBlock {
public Mesh Mesh; public Mesh Mesh;
public Array<CollisionShape3D> Colliders; public Array<CollisionShape3D> Colliders;

View File

@ -15,6 +15,8 @@ namespace Gmtk24 {
public Node3D SpawnPoint; public Node3D SpawnPoint;
[Export(PropertyHint.LayersAvoidance)] [Export(PropertyHint.LayersAvoidance)]
public uint BlockMask; public uint BlockMask;
[Export(PropertyHint.LayersAvoidance)]
public uint GhostOnlyMask = 0b100000;
[ExportCategory("BlockMaterials")] [ExportCategory("BlockMaterials")]
[Export] [Export]
public ShaderMaterial BlockMaterial; public ShaderMaterial BlockMaterial;
@ -71,8 +73,12 @@ namespace Gmtk24 {
if (GhostBlock != null) { if (GhostBlock != null) {
var res = Util.RaycastFromMouse(Orbit.Camera, 0b10100); var res = Util.RaycastFromMouse(Orbit.Camera, 0b10100);
if (res is RaycastResult results) { if (res is RaycastResult results) {
Vector3 Pos = results.Position - GlobalPosition; Vector3 LocalPos = results.Position - GlobalPosition;
GhostBlock.Position = Pos * Basis; if (results.Collider is BuildingBlock) {
Vector3 offset = GhostBlock.OffsetFrom(results.Normal, GhostOnlyMask);
LocalPos += offset;
}
GhostBlock.Position = LocalPos * Basis;
} }
} }
} }
@ -84,6 +90,7 @@ namespace Gmtk24 {
Base = block.Base, Base = block.Base,
Material = BlockGhostMaterial, Material = BlockGhostMaterial,
PhysicalModeLayer = 0, PhysicalModeLayer = 0,
NonPhysicalModeLayer = GhostOnlyMask,
RelativeScale = RelativeScale, RelativeScale = RelativeScale,
Position = LocalPos, Position = LocalPos,
Mode = BuildingBlock.BlockMode.NonPhysical, Mode = BuildingBlock.BlockMode.NonPhysical,

View File

@ -3,7 +3,7 @@
[ext_resource type="Shader" path="res://textures/building_block.gdshader" id="1_dis1h"] [ext_resource type="Shader" path="res://textures/building_block.gdshader" id="1_dis1h"]
[resource] [resource]
render_priority = 0 render_priority = 1
shader = ExtResource("1_dis1h") shader = ExtResource("1_dis1h")
shader_parameter/albedo = Color(0.276387, 0.574926, 0.813629, 0.54902) shader_parameter/albedo = Color(0.276387, 0.574926, 0.813629, 0.54902)
shader_parameter/roughness = 0.533 shader_parameter/roughness = 0.533