Add throwing blocks

This commit is contained in:
Sofia 2024-08-20 01:50:55 +03:00
parent bf10022d3b
commit b8613ed60c
10 changed files with 156 additions and 36 deletions

View File

@ -123,7 +123,7 @@ toggle_pause_menu={
}
interact={
"deadzone": 0.5,
"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)
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":1,"position":Vector2(238, 22),"global_position":Vector2(252, 92),"factor":1.0,"button_index":1,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
drag_orbit={
@ -131,6 +131,11 @@ drag_orbit={
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":4,"position":Vector2(272, 24),"global_position":Vector2(286, 94),"factor":1.0,"button_index":3,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
release_block={
"deadzone": 0.5,
"events": [Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"button_mask":2,"position":Vector2(224, 40),"global_position":Vector2(238, 110),"factor":1.0,"button_index":2,"canceled":false,"pressed":true,"double_click":false,"script":null)
]
}
[internationalization]

View File

@ -54,7 +54,7 @@ size = Vector3(43.3174, 26.5295, 43.4319)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.332064, 1.5426, 3.65156)
[node name="table" parent="." node_paths=PackedStringArray("TrenchbroomMap") instance=ExtResource("11_7trvw")]
transform = Transform3D(-0.0123217, 0, 0.999924, 0, 1, 0, -0.999924, 0, -0.0123217, 0, 0, 0)
transform = Transform3D(-0.0123217, 0, 0.999924, 0, 1, 0, -0.999924, 0, -0.0123217, 2.83695, 0, -4.36558)
TrenchbroomMap = NodePath("../DemoMap")
RelativeScale = 0.005

View File

@ -20,13 +20,12 @@ anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
offset_left = -180.5
offset_top = -166.0
offset_right = 180.5
offset_bottom = -78.0
offset_left = -94.0
offset_top = -163.0
offset_right = 95.0
grow_horizontal = 2
grow_vertical = 0
theme_override_font_sizes/normal_font_size = 64
theme_override_font_sizes/normal_font_size = 32
bbcode_enabled = true
text = "[Hover Text]"
fit_content = true

View File

@ -10,10 +10,10 @@
size = Vector3(1.26587, 1.10037, 2.3989)
[sub_resource type="BoxShape3D" id="BoxShape3D_kn6mn"]
size = Vector3(1.3, 5, 0.01)
size = Vector3(5, 5, 5)
[sub_resource type="BoxShape3D" id="BoxShape3D_5k7xn"]
size = Vector3(2.4, 5, 0.01)
size = Vector3(5, 5, 5)
[sub_resource type="BoxShape3D" id="BoxShape3D_3kmm7"]
size = Vector3(1.3, 0.2, 2.4)
@ -45,31 +45,35 @@ collision_layer = 2
collision_mask = 2
[node name="Wall1" type="CollisionShape3D" parent="TableWalls"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.6, 1.2)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.6, 3.66627)
shape = SubResource("BoxShape3D_kn6mn")
[node name="Wall2" type="CollisionShape3D" parent="TableWalls"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.6, -1.2)
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3.6, -3.69933)
shape = SubResource("BoxShape3D_kn6mn")
[node name="Wall3" type="CollisionShape3D" parent="TableWalls"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -0.65, 3.6, 0)
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -3.1173, 3.6, 0)
shape = SubResource("BoxShape3D_5k7xn")
[node name="Wall4" type="CollisionShape3D" parent="TableWalls"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0.65, 3.6, 0)
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 3.1173, 3.6, 0)
shape = SubResource("BoxShape3D_5k7xn")
[node name="Roof" type="CollisionShape3D" parent="TableWalls"]
transform = Transform3D(-1, 0, -8.74228e-08, 0, 1, 0, 8.74228e-08, 0, -1, 0, 6, 0)
shape = SubResource("BoxShape3D_3kmm7")
[node name="Orbit" type="Node3D" parent="." node_paths=PackedStringArray("Camera")]
[node name="Orbit" type="Node3D" parent="." node_paths=PackedStringArray("Camera", "Hand")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.12311, 0)
script = ExtResource("4_nyqjn")
Camera = NodePath("OrbitCamera")
Hand = NodePath("OrbitCamera/OrbitHand")
[node name="OrbitCamera" type="Camera3D" parent="Orbit"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1)
[node name="OrbitHand" type="Node3D" parent="Orbit/OrbitCamera"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.3, -0.1, -0.26)
[connection signal="Interacted" from="." to="." method="Interact"]

View File

@ -3,10 +3,16 @@ using Godot.Collections;
namespace Gmtk24 {
public partial class BuildingBlock : Interactible {
[Signal]
public delegate void PickUpEventHandler(BuildingBlock block);
public BaseBlock Base;
public ShaderMaterial Material;
public ShaderMaterial HoverMaterial;
public float RelativeScale;
public uint PhysicalModeLayer;
public BlockMode Mode { private set; get; } = BlockMode.Physical;
private MeshInstance3D MeshInstance;
@ -15,14 +21,19 @@ namespace Gmtk24 {
public BuildingBlock() {
Name = "BuildingBlock";
FreezeMode = FreezeModeEnum.Kinematic;
CanSleep = false;
}
public override void _Ready() {
base._Ready();
UpdatePhysics();
HoverMaterial ??= Material;
Hovered += OnHover;
Interacted += OnInteract;
MeshInstance = new MeshInstance3D() {
Mesh = Base.Mesh,
@ -67,15 +78,50 @@ namespace Gmtk24 {
}
}
public void SetMode(BlockMode mode) {
GD.Print(mode);
Mode = mode;
SetMaterial(false);
UpdatePhysics();
}
private void UpdatePhysics() {
if (Mode != BlockMode.NonPhysical) {
CollisionLayer = PhysicalModeLayer;
CollisionMask = PhysicalModeLayer;
if (Mode == BlockMode.Dragged) {
FreezeMode = FreezeModeEnum.Kinematic;
Freeze = true;
} else {
Freeze = false;
}
} else {
CollisionLayer = 0;
CollisionMask = 0;
FreezeMode = FreezeModeEnum.Static;
Freeze = true;
}
}
public void OnHover(bool hovered) {
if (Mode != BlockMode.NonPhysical) {
SetMaterial(hovered);
}
}
private void SetMaterial(bool hoverMaterial) {
for (int i = 0; i < MeshInstance.GetSurfaceOverrideMaterialCount(); i++) {
if (hovered)
if (hoverMaterial)
MeshInstance.SetSurfaceOverrideMaterial(i, HoverMaterialArr[i]);
else
MeshInstance.SetSurfaceOverrideMaterial(i, NormalMaterialArr[i]);
}
}
public void OnInteract(InputEvent _) {
EmitSignal(SignalName.PickUp, this);
}
public struct BaseBlock {
public Mesh Mesh;
public Array<CollisionShape3D> Colliders;
@ -88,5 +134,11 @@ namespace Gmtk24 {
}
}
}
public enum BlockMode {
Physical,
Dragged,
NonPhysical,
}
}
}

View File

@ -19,23 +19,8 @@ namespace Gmtk24 {
if (Player.Eye.Current)
collider = Player.LookingAt.GetCollider();
} else {
var viewport = GetViewport();
var camera = GetViewport().GetCamera3D();
var origin = camera.ProjectRayOrigin(viewport.GetMousePosition());
var normal = camera.ProjectRayNormal(viewport.GetMousePosition()) * 10000;
var ray = new PhysicsRayQueryParameters3D {
From = origin,
To = normal,
CollideWithBodies = true,
CollisionMask = 0b1000,
HitBackFaces = false,
HitFromInside = false,
};
var results = camera.GetWorld3D().DirectSpaceState.IntersectRay(ray);
if (results.ContainsKey("collider")) {
collider = (GodotObject)results["collider"];
}
var results = Util.RaycastFromMouse(GetViewport().GetCamera3D());
collider = results?.Collider;
}
@ -61,7 +46,7 @@ namespace Gmtk24 {
}
public override void _UnhandledInput(InputEvent @event) {
if (@event.IsActionPressed("interact") && Hovered != null) {
if (@event.IsAction("interact") && Hovered != null) {
Hovered.HandleInput(@event);
}
}

View File

@ -12,6 +12,7 @@ namespace Gmtk24 {
public string InteractName;
public void HandleInput(InputEvent e) {
if (e.IsPressed())
EmitSignal(SignalName.Interacted, e);
}

View File

@ -10,6 +10,10 @@ namespace Gmtk24 {
[Export]
public Camera3D Camera;
[Export]
public Node3D Hand;
[Export]
public BuildingBlock HeldBlock { private set; get; }
[Export]
public bool IsEnabled { private set; get; } = false;
@ -24,6 +28,8 @@ namespace Gmtk24 {
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) {
Quaternion = new Quaternion(Vector3.Up, CurrentYaw) * new Quaternion(Vector3.Right, CurrentPitch);
if (HeldBlock != null)
HeldBlock.Position = HeldBlock.Position.Lerp(Vector3.Zero, 0.2f);
}
public void SetEnabled(bool enabled) {
@ -52,6 +58,33 @@ namespace Gmtk24 {
CurrentPitch = Mathf.Clamp(CurrentPitch, -Mathf.Pi * 0.49f, 0);
}
if (@event.IsActionPressed("release_block")) {
HeldBlock.SetMode(BuildingBlock.BlockMode.Physical);
HeldBlock.Reparent(GetParent());
var normal = Camera.ProjectRayNormal(Camera.GetViewport().GetMousePosition());
var direction = normal * 10;
var res = Util.RaycastFromMouse(Camera, 0b1111);
if (res is RaycastResult results) {
GD.Print(results.Position);
direction = (results.Position - HeldBlock.GlobalPosition + Vector3.Up * 0.5f) * 3;
}
HeldBlock.ApplyImpulse(direction);
HeldBlock = null;
}
}
public bool HoldBlock(BuildingBlock block) {
if (HeldBlock != null)
return false; // TODO
HeldBlock = block;
HeldBlock.SetMode(BuildingBlock.BlockMode.NonPhysical);
HeldBlock.Reparent(Hand, true);
HeldBlock.Rotation = Vector3.Zero;
return true;
}
}
}

View File

@ -42,12 +42,13 @@ namespace Gmtk24 {
Material = BlockMaterial,
HoverMaterial = BlockHoverMaterial,
Position = position + Vector3.One * (rng.Randf() * 0.5f - 0.25f) + Vector3.Up * rng.Randf(),
CollisionMask = BlockMask,
CollisionLayer = BlockMask,
PhysicalModeLayer = BlockMask,
RelativeScale = RelativeScale,
};
AddChild(block);
block.PickUp += OnBlockPickup;
}
}
@ -55,5 +56,9 @@ namespace Gmtk24 {
Orbit.SetEnabled(true);
}
public void OnBlockPickup(BuildingBlock block) {
Orbit.HoldBlock(block);
}
}
}

36
scripts/Util.cs Normal file
View File

@ -0,0 +1,36 @@
using Godot;
namespace Gmtk24 {
public static class Util {
public static RaycastResult? RaycastFromMouse(Camera3D camera, uint mask = 0b1000) {
var viewport = camera.GetViewport();
var origin = camera.ProjectRayOrigin(viewport.GetMousePosition());
var normal = camera.ProjectRayNormal(viewport.GetMousePosition()) * 10000;
var ray = new PhysicsRayQueryParameters3D {
From = origin,
To = normal,
CollideWithBodies = true,
CollisionMask = mask,
HitBackFaces = false,
HitFromInside = false,
};
var results = camera.GetWorld3D().DirectSpaceState.IntersectRay(ray);
if (results.ContainsKey("position")) {
return new RaycastResult() {
Collider = (GodotObject)results["collider"],
Position = (Vector3)results["position"],
Normal = (Vector3)results["normal"],
};
}
return null;
}
}
public struct RaycastResult {
public Vector3 Position;
public Vector3 Normal;
public GodotObject Collider;
}
}