From 60e6b8c5640a42a200bc0fcf8f678c3c16411bd6 Mon Sep 17 00:00:00 2001 From: copygirl Date: Mon, 1 Jan 2024 19:09:13 +0100 Subject: [PATCH] Separate interable layer into pickup and place --- objects/Item.cs | 2 +- objects/grid.tscn | 2 +- player/PickupController.cs | 11 +++++++---- project.godot | 3 ++- scripts/globals/PhysicsLayer.cs | 19 ++++++++++++++----- 5 files changed, 25 insertions(+), 12 deletions(-) diff --git a/objects/Item.cs b/objects/Item.cs index 0000169..0e4beb3 100644 --- a/objects/Item.cs +++ b/objects/Item.cs @@ -19,7 +19,7 @@ public partial class Item : RigidBody3D } // Set the collision properties here so we don't have to specify them in each item scene separately. - CollisionLayer = (uint)(PhysicsLayer.Item | PhysicsLayer.Interactable); + CollisionLayer = (uint)(PhysicsLayer.Item | PhysicsLayer.Pickup); CollisionMask = (uint)(PhysicsLayer.Static | PhysicsLayer.Dynamic | PhysicsLayer.Player | PhysicsLayer.Item); Freeze = FindParent("Grid") != null; diff --git a/objects/grid.tscn b/objects/grid.tscn index 5a1c996..637bf5a 100644 --- a/objects/grid.tscn +++ b/objects/grid.tscn @@ -3,7 +3,7 @@ [ext_resource type="Script" path="res://objects/Grid.cs" id="1_7yhbt"] [node name="Grid" type="Area3D"] -collision_layer = 256 +collision_layer = 512 collision_mask = 0 monitoring = false script = ExtResource("1_7yhbt") diff --git a/player/PickupController.cs b/player/PickupController.cs index 11ef40f..ee03618 100644 --- a/player/PickupController.cs +++ b/player/PickupController.cs @@ -94,7 +94,10 @@ public partial class PickupController : Node3D } if (HasItemsHeld) { - if ((RayToMouseCursor() is RayResult ray) && (ray.Collider is Grid grid) + // This ray will be blocked by static and dynamic objects. + const PhysicsLayer Mask = PhysicsLayer.Place | PhysicsLayer.Static | PhysicsLayer.Dynamic; + + if ((RayToMouseCursor(Mask) is RayResult ray) && (ray.Collider is Grid grid) // Make sure this is placed against the top of the grid. && CanPlaceAgainst(grid, ray.Normal) // Ensure item is not being added to itself or nested items. @@ -114,7 +117,7 @@ public partial class PickupController : Node3D _grid = null; } } else { - var interactable = RayToMouseCursor()?.Collider; + var interactable = RayToMouseCursor(PhysicsLayer.Pickup)?.Collider; // Remove the outline from the previously looked-at item. if (CurrentItem != null) SetMeshLayerOutline(CurrentItem.Model, OutlineMode.Disable); @@ -138,7 +141,7 @@ public partial class PickupController : Node3D } } - RayResult RayToMouseCursor() + RayResult RayToMouseCursor(PhysicsLayer collisionMask) { var camera = _player.Camera.Camera; var mouse = GetViewport().GetMousePosition(); @@ -146,7 +149,7 @@ public partial class PickupController : Node3D var to = from + camera.ProjectRayNormal(mouse) * PickupDistance; var query = PhysicsRayQueryParameters3D.Create(from, to); - query.CollisionMask = (uint)PhysicsLayer.Interactable; + query.CollisionMask = (uint)collisionMask; query.CollideWithAreas = true; // Exclude the `CurrentItem` from collision checking if it's being held. query.Exclude = HasItemsHeld ? [ CurrentItem.GetRid() ] : []; diff --git a/project.godot b/project.godot index f6b4067..9b9bf43 100644 --- a/project.godot +++ b/project.godot @@ -87,7 +87,8 @@ interact_place={ 3d_physics/layer_2="Dynamic" 3d_physics/layer_3="Player" 3d_physics/layer_4="Item" -3d_physics/layer_9="Interactable" +3d_physics/layer_9="Pickup" +3d_physics/layer_10="Place" [rendering] diff --git a/scripts/globals/PhysicsLayer.cs b/scripts/globals/PhysicsLayer.cs index c168bb0..12f7520 100644 --- a/scripts/globals/PhysicsLayer.cs +++ b/scripts/globals/PhysicsLayer.cs @@ -1,8 +1,17 @@ [Flags] public enum PhysicsLayer : uint { - Static = 1 << 0, - Dynamic = 1 << 1, - Player = 1 << 2, - Item = 1 << 3, - Interactable = 1 << 8, + /// Objects that are part of the scene, and never move. + Static = 1 << 0, + /// Objects the player collides with, but may move. + Dynamic = 1 << 1, + /// The player. + Player = 1 << 2, + /// Small objects the player doesn't collide with, but collide with the player. + Item = 1 << 3, + + + /// Objects that may be picked up. + Pickup = 1 << 8, + /// Objects that other objects may be placed against. + Place = 1 << 9, }