diff options
| -rw-r--r-- | AutoCollisionShape.cs | 15 | ||||
| -rw-r--r-- | AutoCollisionShape.cs.uid | 1 | ||||
| -rw-r--r-- | AutoMeshInstance.cs | 36 | ||||
| -rw-r--r-- | AutoMeshInstance.cs.uid | 1 | ||||
| -rw-r--r-- | WorldCamera.cs | 29 | ||||
| -rw-r--r-- | game.tscn | 27 | ||||
| -rw-r--r-- | project.godot | 4 |
7 files changed, 107 insertions, 6 deletions
diff --git a/AutoCollisionShape.cs b/AutoCollisionShape.cs new file mode 100644 index 0000000..62031b8 --- /dev/null +++ b/AutoCollisionShape.cs @@ -0,0 +1,15 @@ +using Godot; +using System; + +public partial class AutoCollisionShape : MeshInstance3D +{ + public override void _Ready() + { + base._Ready(); + var collisionShape = new CollisionShape3D(); + collisionShape.Shape = new BoxShape3D(); + collisionShape.Scale = Scale; + collisionShape.Transform = Transform; + AddChild(collisionShape); + } +} diff --git a/AutoCollisionShape.cs.uid b/AutoCollisionShape.cs.uid new file mode 100644 index 0000000..ed79353 --- /dev/null +++ b/AutoCollisionShape.cs.uid @@ -0,0 +1 @@ +uid://b6xupv6v8ls5t diff --git a/AutoMeshInstance.cs b/AutoMeshInstance.cs new file mode 100644 index 0000000..43c6748 --- /dev/null +++ b/AutoMeshInstance.cs @@ -0,0 +1,36 @@ +using Godot; +using System; + +/** +* Used to auto-generate Meshes that are the same as collision objects, for convenience. +* makes a new MeshInstance3D as a sibling (child of this object's parent) +* and applies the same shape, tranform, and scale. +*/ +public partial class AutoMeshInstance : CollisionShape3D +{ + public override void _Ready() + { + base._Ready(); + + MeshInstance3D mi = new MeshInstance3D(); + if (Shape is BoxShape3D boxshp){ + BoxMesh m = new BoxMesh(); + m.Size = boxshp.Size; + mi.Mesh = m; + } + if (Shape is CylinderShape3D cylshp){ + CylinderMesh m = new CylinderMesh(); + m.TopRadius = cylshp.Radius; + m.BottomRadius = cylshp.Radius; + m.Height = cylshp.Height; + mi.Mesh = m; + } + if (Shape is CapsuleShape3D){} + if (Shape is SphereShape3D){} + + mi.Rotation = Rotation; + mi.Position = Position; + + GetParent().CallDeferred("add_child", mi); + } +} diff --git a/AutoMeshInstance.cs.uid b/AutoMeshInstance.cs.uid new file mode 100644 index 0000000..1a87e5a --- /dev/null +++ b/AutoMeshInstance.cs.uid @@ -0,0 +1 @@ +uid://dso0esuf7j1o7 diff --git a/WorldCamera.cs b/WorldCamera.cs index 77fa7cb..6675fac 100644 --- a/WorldCamera.cs +++ b/WorldCamera.cs @@ -14,6 +14,9 @@ public partial class WorldCamera : Camera3D private float ct = 0.5f; //time to reach destination [Export] private float rotateSpeed = 2f; //radians per second + [Export] + private float mouseSensitivity = 0.005f; + private bool midDrag = false; private enum Mode { @@ -35,9 +38,33 @@ public partial class WorldCamera : Camera3D } } + public override void _Input(InputEvent @event) + { + if (@event is InputEventMouseButton mb && mb.ButtonIndex == MouseButton.Middle) + { + midDrag = mb.Pressed; + } + else if (@event is InputEventMouseMotion motion && midDrag) + { + // Horizontal orbit + offset = offset.Rotated(Vector3.Up, -motion.Relative.X * mouseSensitivity); + + // Vertical orbit around the axis perpendicular to offset in the XZ plane + Vector3 right = Vector3.Up.Cross(offset).Normalized(); + if (right.LengthSquared() > 0.001f) + { + Vector3 newOffset = offset.Rotated(right, -motion.Relative.Y * mouseSensitivity); + // Clamp to prevent flipping over top or going below horizontal + float angle = newOffset.AngleTo(Vector3.Up); + if (angle > 0.15f && angle < Mathf.Pi * 0.85f) + offset = newOffset; + } + } + } + public override void _Process(double delta) { - float rotInput = Input.GetAxis("cam_rotate_right", "cam_rotate_left"); + float rotInput = Input.GetAxis("cam_rotate_cw", "cam_rotate_ccw"); switch (mode){ case Mode.Follow: @@ -4,6 +4,7 @@ [ext_resource type="Script" uid="uid://bqmbio4jvy1ev" path="res://Floor.cs" id="2_7jktm"] [ext_resource type="Script" uid="uid://c6yxockt738ut" path="res://WorldCamera.cs" id="3_ryrav"] [ext_resource type="Script" uid="uid://c7vsvparcf0cp" path="res://Player.cs" id="4_eow3j"] +[ext_resource type="Script" uid="uid://dso0esuf7j1o7" path="res://AutoMeshInstance.cs" id="5_7jktm"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_ufxkl"] @@ -24,6 +25,12 @@ size = Vector3(50, 0.1, 50) [sub_resource type="Environment" id="Environment_ufxkl"] +[sub_resource type="BoxShape3D" id="BoxShape3D_vlr3u"] +size = Vector3(0.17041016, 3.7247314, 10.8758545) + +[sub_resource type="BoxShape3D" id="BoxShape3D_a5lrf"] +size = Vector3(0.9614258, 2, 10) + [node name="GameNode" type="Node" unique_id=770944947] [node name="Level" type="Node3D" parent="." unique_id=632228714] @@ -60,6 +67,20 @@ shape = SubResource("SphereShape3D_ufxkl") [node name="WorldEnvironment" type="WorldEnvironment" parent="Level" unique_id=520831362] environment = SubResource("Environment_ufxkl") -[node name="OmniLight3D" type="OmniLight3D" parent="Level" unique_id=810219011] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 50, 0) -light_color = Color(1, 0.79607844, 0.5137255, 1) +[node name="DirectionalLight3D" type="DirectionalLight3D" parent="Level" unique_id=438599063] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 80, 0) +sky_mode = 1 + +[node name="StaticBody3D" type="StaticBody3D" parent="Level" unique_id=130495979] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Level/StaticBody3D" unique_id=1097271189] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.41479492, 2.3623657, -0.62994385) +shape = SubResource("BoxShape3D_vlr3u") +script = ExtResource("5_7jktm") + +[node name="StaticBody3D2" type="StaticBody3D" parent="Level" unique_id=765331073] + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Level/StaticBody3D2" unique_id=407375134] +transform = Transform3D(-4.371139e-08, 0, 1, 0, 1, 0, -1, 0, -4.371139e-08, 4.528, 1, -13.146) +shape = SubResource("BoxShape3D_a5lrf") +script = ExtResource("5_7jktm") diff --git a/project.godot b/project.godot index 539fc50..b3500b2 100644 --- a/project.godot +++ b/project.godot @@ -60,14 +60,14 @@ down={ , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":1,"axis_value":1.0,"script":null) ] } -cam_rotate_right={ +cam_rotate_cw={ "deadzone": 0.2, "events": [Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":2,"axis_value":1.0,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":81,"key_label":0,"unicode":113,"location":0,"echo":false,"script":null) , Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":4194321,"key_label":0,"unicode":0,"location":0,"echo":false,"script":null) ] } -cam_rotate_left={ +cam_rotate_ccw={ "deadzone": 0.2, "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":69,"key_label":0,"unicode":101,"location":0,"echo":false,"script":null) , Object(InputEventJoypadMotion,"resource_local_to_scene":false,"resource_name":"","device":-1,"axis":2,"axis_value":-1.0,"script":null) |
