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 HoverMaterial;
public float RelativeScale;
public uint PhysicalModeLayer;
public uint NonPhysicalModeLayer = 0;
public uint PlacedLayer = 0b10000;
public BlockMode Mode = BlockMode.Physical;
private MeshInstance3D MeshInstance;
public MeshInstance3D MeshInstance { private set; get; }
private ShaderMaterial[] NormalMaterialArr;
private ShaderMaterial[] HoverMaterialArr;
@ -88,7 +91,7 @@ namespace Gmtk24 {
private void UpdatePhysics() {
if (Mode == BlockMode.Physical || Mode == BlockMode.Dragged) {
CollisionLayer = PhysicalModeLayer;
CollisionMask = PhysicalModeLayer + 0b10000;
CollisionMask = PhysicalModeLayer | PlacedLayer;
if (Mode == BlockMode.Dragged) {
FreezeMode = FreezeModeEnum.Kinematic;
Freeze = true;
@ -97,11 +100,11 @@ namespace Gmtk24 {
}
} else if (Mode == BlockMode.NonPhysical || Mode == BlockMode.Placed) {
if (Mode == BlockMode.Placed) {
CollisionLayer = 0b10000;
CollisionMask = 0b10000;
CollisionLayer = PlacedLayer;
CollisionMask = PlacedLayer;
} else {
CollisionLayer = 0;
CollisionMask = 0;
CollisionLayer = NonPhysicalModeLayer;
CollisionMask = NonPhysicalModeLayer;
}
FreezeMode = FreezeModeEnum.Static;
Freeze = true;
@ -127,6 +130,29 @@ namespace Gmtk24 {
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 Mesh Mesh;
public Array<CollisionShape3D> Colliders;

View File

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

View File

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