diff --git a/dice.gd b/dice.gd index 6d71985..59ea9d1 100644 --- a/dice.gd +++ b/dice.gd @@ -4,7 +4,9 @@ class_name Dice @export var start_marker: Marker3D func random_throw() -> void: - global_position = start_marker.global_transform.origin + var position_variation = .01 + var position_jitter = Vector3(randf_range(-position_variation, position_variation), randf_range(-position_variation, position_variation), randf_range(-position_variation, position_variation)) + global_position = start_marker.global_transform.origin + position_jitter linear_velocity = Vector3(0, 0, 0) angular_velocity = Vector3(0, 0, 0) var lateral_variation = .04 diff --git a/main.gd b/main.gd index 9e1c965..9067b12 100644 --- a/main.gd +++ b/main.gd @@ -1,16 +1,40 @@ extends Node3D -@onready var dice: Dice = $dice1 +@onready var dice1: Dice = $dice1 +@onready var dice2: Dice = $dice2 +@onready var camera: Camera3D = $Camera3D +@onready var camera_start: Marker3D = $CameraPositionDefault +@onready var ui: Ui = $Ui + +enum GameState { + START, + THROWING_START, + THROWING, + THROWN +} + +var state: GameState = GameState.START func _ready() -> void: pass func _process(_delta: float) -> void: - pass + if (!dice1.can_be_thrown() or !dice2.can_be_thrown()) and state == GameState.THROWING_START: + state = GameState.THROWING + if dice1.can_be_thrown() and dice2.can_be_thrown() and state == GameState.THROWING: + state = GameState.THROWN + var avg_position = (dice1.global_transform.origin + dice2.global_transform.origin) / 2 + var diff = dice1.global_position - dice2.global_position + camera.global_position = avg_position + Vector3.UP * (diff.length()) + camera.look_at(avg_position, Vector3.FORWARD) -func _unhandled_input(event: InputEvent) -> void: - if event.is_action_pressed("ui_accept"): - dice.random_throw() +func throw_dice() -> void: + if dice1.can_be_thrown() and dice2.can_be_thrown(): + dice1.random_throw() + dice2.random_throw() + state = GameState.THROWING_START + reset_camera() -func _on_dice_1_sleeping_state_changed() -> void: - pass \ No newline at end of file +func reset_camera() -> void: + camera.global_transform.origin = camera_start.global_transform.origin + camera.global_transform.basis = camera_start.global_transform.basis diff --git a/main.tscn b/main.tscn index 68e7d62..7a62316 100644 --- a/main.tscn +++ b/main.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=13 format=3 uid="uid://bxphc5t7er28i"] +[gd_scene load_steps=14 format=3 uid="uid://bxphc5t7er28i"] [ext_resource type="Script" path="res://main.gd" id="1_h1pkq"] [ext_resource type="ArrayMesh" uid="uid://6ygsl6phockr" path="res://dice.res" id="1_qesf4"] [ext_resource type="Script" path="res://dice.gd" id="2_jh75e"] +[ext_resource type="PackedScene" uid="uid://c8jkqtbqtg7ub" path="res://ui.tscn" id="4_80via"] [sub_resource type="PhysicsMaterial" id="PhysicsMaterial_kmncl"] bounce = 0.06 @@ -42,7 +43,7 @@ glow_enabled = true script = ExtResource("1_h1pkq") [node name="dice1" type="RigidBody3D" parent="." node_paths=PackedStringArray("start_marker")] -transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.31759, 0.217006, 0.0105869) +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.57225, 0.217006, 0.0105869) mass = 0.1 physics_material_override = SubResource("PhysicsMaterial_kmncl") continuous_cd = true @@ -57,6 +58,22 @@ skeleton = NodePath("../..") [node name="CollisionShape3D" type="CollisionShape3D" parent="dice1"] shape = SubResource("BoxShape3D_jujds") +[node name="dice2" type="RigidBody3D" parent="." node_paths=PackedStringArray("start_marker")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.31759, 0.217006, 0.0105869) +mass = 0.1 +physics_material_override = SubResource("PhysicsMaterial_kmncl") +continuous_cd = true +script = ExtResource("2_jh75e") +start_marker = NodePath("../DiceThrowStart") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="dice2"] +transform = Transform3D(0.05, 0, 0, 0, 0.05, 0, 0, 0, 0.05, 0, 0, 0) +mesh = ExtResource("1_qesf4") +skeleton = NodePath("../..") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="dice2"] +shape = SubResource("BoxShape3D_jujds") + [node name="CSGMesh3D" type="CSGMesh3D" parent="."] use_collision = true mesh = SubResource("PlaneMesh_qwxpl") @@ -99,6 +116,9 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.323323, 0.972071) current = true near = 0.003 +[node name="CameraPositionDefault" type="Marker3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.323323, 0.972071) + [node name="WorldEnvironment" type="WorldEnvironment" parent="."] environment = SubResource("Environment_rm3d5") camera_attributes = SubResource("CameraAttributesPractical_i6kyw") @@ -106,8 +126,9 @@ camera_attributes = SubResource("CameraAttributesPractical_i6kyw") [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] transform = Transform3D(1, 0, 0, 0, 0.751874, 0.659306, 0, -0.659306, 0.751874, 0, 1.52296, 0) shadow_enabled = true +directional_shadow_max_distance = 10.0 [node name="DiceThrowStart" type="Marker3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.408271, 0.326392, 0) -[connection signal="sleeping_state_changed" from="dice1" to="." method="_on_dice_1_sleeping_state_changed"] +[node name="Ui" parent="." instance=ExtResource("4_80via")] diff --git a/ui.gd b/ui.gd new file mode 100644 index 0000000..a23ea8f --- /dev/null +++ b/ui.gd @@ -0,0 +1,8 @@ +extends Control + +class_name Ui + +signal roll_pressed + +func _on_roll_button_pressed() -> void: + roll_pressed.emit() diff --git a/ui.tscn b/ui.tscn new file mode 100644 index 0000000..558a08c --- /dev/null +++ b/ui.tscn @@ -0,0 +1,39 @@ +[gd_scene load_steps=4 format=3 uid="uid://c8jkqtbqtg7ub"] + +[ext_resource type="Script" path="res://ui.gd" id="1_qipxr"] + +[sub_resource type="InputEventKey" id="InputEventKey_6n7ot"] +device = -1 +keycode = 32 +unicode = 32 + +[sub_resource type="Shortcut" id="Shortcut_0osx3"] +events = [SubResource("InputEventKey_6n7ot")] + +[node name="Ui" type="Control"] +layout_mode = 3 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("1_qipxr") + +[node name="roll button" type="Button" parent="."] +layout_mode = 1 +anchors_preset = 7 +anchor_left = 0.5 +anchor_top = 1.0 +anchor_right = 0.5 +anchor_bottom = 1.0 +offset_left = -18.0 +offset_top = -41.0 +offset_right = 19.0 +offset_bottom = -10.0 +grow_horizontal = 2 +grow_vertical = 0 +tooltip_text = "Click or press space to roll dice" +shortcut = SubResource("Shortcut_0osx3") +text = "Roll" + +[connection signal="pressed" from="roll button" to="." method="_on_roll_button_pressed"]