diff options
author | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2023-09-20 21:06:07 -0400 |
---|---|---|
committer | Lucas Fryzek <lucas.fryzek@fryzekconcepts.com> | 2023-09-20 21:06:07 -0400 |
commit | a16c3354383f476602b25b079636c9c89b95cd9c (patch) | |
tree | 930dfa6d9facaa0f4dc246863f885702aecb72b9 | |
parent | 7bfa7fcf02124a4508dffa9d78e8ef921b86a315 (diff) |
Add run and climb
-rw-r--r-- | prefab/player.tscn | 16 | ||||
-rw-r--r-- | prefab/rock.tscn | 9 | ||||
-rw-r--r-- | project.godot | 33 | ||||
-rw-r--r-- | scenes/main.tscn | 21 | ||||
-rw-r--r-- | scripts/player.gd | 91 |
5 files changed, 153 insertions, 17 deletions
diff --git a/prefab/player.tscn b/prefab/player.tscn index b4f8630..809c557 100644 --- a/prefab/player.tscn +++ b/prefab/player.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=5 format=3 uid="uid://dsq68sqy2ldjm"] +[gd_scene load_steps=6 format=3 uid="uid://dsq68sqy2ldjm"] [ext_resource type="Script" path="res://scripts/player.gd" id="1_l6xtg"] [ext_resource type="PackedScene" uid="uid://wmf2eu0uuhrg" path="res://addons/third-person-camera/third_person_camera/ThirdPersonCamera.tscn" id="1_stkca"] @@ -9,6 +9,9 @@ size = Vector3(1, 2, 1) [sub_resource type="BoxMesh" id="BoxMesh_wkmld"] size = Vector3(1, 2, 1) +[sub_resource type="BoxShape3D" id="BoxShape3D_eybym"] +size = Vector3(1.28475, 2, 1) + [node name="Player" type="CharacterBody3D"] script = ExtResource("1_l6xtg") @@ -20,3 +23,14 @@ shape = SubResource("BoxShape3D_ibgtc") mesh = SubResource("BoxMesh_wkmld") [node name="ThirdPersonCamera" parent="." instance=ExtResource("1_stkca")] + +[node name="Picker" type="Area3D" parent="."] +collision_layer = 2 +collision_mask = 2 + +[node name="PickerBox" type="CollisionShape3D" parent="Picker"] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, -1) +shape = SubResource("BoxShape3D_eybym") + +[node name="Holder" type="Node3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 3, 0) diff --git a/prefab/rock.tscn b/prefab/rock.tscn index d5e8828..d6786da 100644 --- a/prefab/rock.tscn +++ b/prefab/rock.tscn @@ -4,12 +4,11 @@ [sub_resource type="SphereMesh" id="SphereMesh_l2tu3"] -[node name="rock" type="Node3D"] +[node name="Rock" type="RigidBody3D"] +collision_layer = 3 -[node name="StaticBody3D" type="StaticBody3D" parent="."] - -[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] +[node name="CollisionShape3D" type="CollisionShape3D" parent="."] shape = SubResource("SphereShape3D_os5og") -[node name="MeshInstance3D" type="MeshInstance3D" parent="StaticBody3D/CollisionShape3D"] +[node name="MeshInstance3D" type="MeshInstance3D" parent="CollisionShape3D"] mesh = SubResource("SphereMesh_l2tu3") diff --git a/project.godot b/project.godot index 1cdc7e7..b8e7658 100644 --- a/project.godot +++ b/project.godot @@ -19,6 +19,39 @@ config/icon="res://icon.svg" MarchingCubes="*res://scripts/marching_cubes.gd" +[input] + +forward={ +"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":87,"physical_keycode":0,"key_label":0,"unicode":119,"echo":false,"script":null) +] +} +backward={ +"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":83,"physical_keycode":0,"key_label":0,"unicode":115,"echo":false,"script":null) +] +} +left={ +"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":65,"physical_keycode":0,"key_label":0,"unicode":97,"echo":false,"script":null) +] +} +right={ +"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":68,"physical_keycode":0,"key_label":0,"unicode":100,"echo":false,"script":null) +] +} +pickup={ +"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":69,"physical_keycode":0,"key_label":0,"unicode":101,"echo":false,"script":null) +] +} +run={ +"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":4194325,"physical_keycode":0,"key_label":0,"unicode":0,"echo":false,"script":null) +] +} + [rendering] renderer/rendering_method="gl_compatibility" diff --git a/scenes/main.tscn b/scenes/main.tscn index cf072fa..fba8f18 100644 --- a/scenes/main.tscn +++ b/scenes/main.tscn @@ -1,8 +1,9 @@ -[gd_scene load_steps=8 format=3 uid="uid://bb55ocpenupao"] +[gd_scene load_steps=11 format=3 uid="uid://bb55ocpenupao"] [ext_resource type="Script" path="res://scripts/level_gen.gd" id="1_sfx65"] [ext_resource type="PackedScene" uid="uid://dsq68sqy2ldjm" path="res://prefab/player.tscn" id="3_8dect"] [ext_resource type="Material" uid="uid://uiwp0gpofjrx" path="res://materials/terrain.tres" id="3_nsy4g"] +[ext_resource type="PackedScene" uid="uid://2a3fyuye46hm" path="res://prefab/rock.tscn" id="3_so64k"] [sub_resource type="BoxMesh" id="BoxMesh_2l1xl"] material = ExtResource("3_nsy4g") @@ -18,6 +19,12 @@ size = Vector3(200, 1, 200) [sub_resource type="PlaneMesh" id="PlaneMesh_ypd0i"] size = Vector2(200, 200) +[sub_resource type="BoxShape3D" id="BoxShape3D_kjqqf"] +size = Vector3(3, 10, 3) + +[sub_resource type="BoxMesh" id="BoxMesh_f2n6h"] +size = Vector3(3, 10, 3) + [node name="Node3D" type="Node3D"] script = ExtResource("1_sfx65") @@ -32,6 +39,9 @@ mesh = SubResource("BoxMesh_2l1xl") [node name="WorldEnvironment" type="WorldEnvironment" parent="."] environment = SubResource("Environment_w27kj") +[node name="Rock" parent="WorldEnvironment" instance=ExtResource("3_so64k")] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 11.6853, 2, 0) + [node name="Water" type="StaticBody3D" parent="."] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 100, 0, 90) @@ -44,3 +54,12 @@ skeleton = NodePath("../..") [node name="Player" parent="." instance=ExtResource("3_8dect")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 18.7109, 0, 0) + +[node name="StaticBody3D" type="StaticBody3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.7194, 5, -6.35033) + +[node name="CollisionShape3D" type="CollisionShape3D" parent="StaticBody3D"] +shape = SubResource("BoxShape3D_kjqqf") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="StaticBody3D"] +mesh = SubResource("BoxMesh_f2n6h") diff --git a/scripts/player.gd b/scripts/player.gd index 49bf1b7..375d700 100644 --- a/scripts/player.gd +++ b/scripts/player.gd @@ -2,38 +2,109 @@ extends CharacterBody3D const SPEED = 5.0 +const RUN_SPEED = 1.5 * SPEED const JUMP_VELOCITY = 4.5 +const THROW_FORCE = 10.0 # Get the gravity from the project settings to be synced with RigidBody nodes. var gravity = ProjectSettings.get_setting("physics/3d/default_gravity") +var current_node: Node3D = null +var holding: Node3D = null + +var climbing: bool = false +var last_wall_direction: Vector3 = Vector3() + +func got_body(body: Node3D): + if current_node == null: + current_node = body + +func leave_body(body: Node3D): + if current_node == body: + current_node = null + +func pickup(): + if holding == null: + holding = current_node + current_node.get_parent().remove_child(current_node) + holding.position = Vector3.ZERO + holding.freeze = true + $Holder.add_child(holding) + +func throw(): + var g_pos = holding.global_position + holding.get_parent().remove_child(holding) + get_parent().add_child(holding) + holding.global_position = g_pos + + var fwd = $ThirdPersonCamera.get_front_direction() + var right = Vector3.UP + var dir = (fwd + right).normalized() + holding.freeze = false + holding.apply_impulse(THROW_FORCE*dir) + holding = null + func _ready(): Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) $ThirdPersonCamera.mouse_follow = true + + $Picker.connect("body_entered", got_body) + $Picker.connect("body_exited", leave_body) func _physics_process(delta): + var speed = SPEED + + if Input.is_action_pressed("run"): + speed = RUN_SPEED + + if Input.is_action_just_pressed("pickup"): + if holding == null and current_node != null: + pickup() + elif holding != null: + throw() + # Add the gravity. - if not is_on_floor(): + if not is_on_floor() and not climbing: velocity.y -= gravity * delta + + var fwd = $ThirdPersonCamera.get_front_direction() + var right = $ThirdPersonCamera.get_right_direction() + + var climb_direction_fwd = Vector3() + var climb_direction_right = Vector3() + if is_on_wall(): + last_wall_direction = get_wall_normal() + climbing = true + print("Climbing now") + elif climbing: + climbing = false + print("Not climbing") + + if climbing: + climb_direction_fwd = (Vector3.UP - Vector3.UP.project(last_wall_direction)).normalized() + climb_direction_right = (right - right.project(last_wall_direction)).normalized() # Handle Jump. if Input.is_action_just_pressed("ui_accept") and is_on_floor(): velocity.y = JUMP_VELOCITY - - var fwd = $ThirdPersonCamera.get_front_direction() - var right = $ThirdPersonCamera.get_right_direction() look_at(position + fwd, Vector3.UP) # Get the input direction and handle the movement/deceleration. # As good practice, you should replace UI actions with custom gameplay actions. - var input_dir = Input.get_vector("ui_left", "ui_right", "ui_down", "ui_up") + var input_dir = Input.get_vector("left", "right", "backward", "forward") var direction = (input_dir.y * fwd + input_dir.x * right).normalized() - if direction: - velocity.x = direction.x * SPEED - velocity.z = direction.z * SPEED + var climb_direction = (input_dir.y * climb_direction_fwd + input_dir.x * climb_direction_right).normalized() + if climbing: + print("Climb direction is ", climb_direction_fwd, " ", climb_direction_right, " ", get_wall_normal()) + velocity = climb_direction * speed + velocity += -speed * last_wall_direction + print("Velocity is ", velocity) + elif direction: + velocity.x = direction.x * speed + velocity.z = direction.z * speed else: - velocity.x = move_toward(velocity.x, 0, SPEED) - velocity.z = move_toward(velocity.z, 0, SPEED) + velocity.x = move_toward(velocity.x, 0, speed) + velocity.z = move_toward(velocity.z, 0, speed) move_and_slide() |