Skip to content

Commit

Permalink
Merge pull request #2 from dsg-jam/player-path
Browse files Browse the repository at this point in the history
Player path & movement
  • Loading branch information
siku2 authored Oct 5, 2024
2 parents 305d0c2 + 7d9b5b0 commit dfadb00
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
45 changes: 45 additions & 0 deletions prefabs/player.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
[gd_scene load_steps=5 format=3 uid="uid://bxhfa4fltovbh"]

[ext_resource type="Script" path="res://scripts/draw_path.gd" id="1_s6kjp"]

[sub_resource type="Curve3D" id="Curve3D_kfjlp"]

[sub_resource type="BoxMesh" id="BoxMesh_wrw54"]
size = Vector3(0.1, 0.1, 0.1)

[sub_resource type="SphereShape3D" id="SphereShape3D_74w4j"]
radius = 0.1

[node name="PlayerController" type="Node3D"]
script = ExtResource("1_s6kjp")

[node name="PlayerPath" type="Path3D" parent="."]
curve = SubResource("Curve3D_kfjlp")

[node name="PlayerPathFollow" type="PathFollow3D" parent="PlayerPath"]
transform = Transform3D(0.707105, 0, -0.707105, 0, 1, 0, 0.707105, 0, 0.707105, 0, 0, 0)

[node name="Player" type="MeshInstance3D" parent="PlayerPath/PlayerPathFollow"]
transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 0, 0, 0)
mesh = SubResource("BoxMesh_wrw54")
skeleton = NodePath("../../..")

[node name="Area3D" type="Area3D" parent="PlayerPath/PlayerPathFollow/Player"]

[node name="CollisionShape3D" type="CollisionShape3D" parent="PlayerPath/PlayerPathFollow/Player/Area3D"]
shape = SubResource("SphereShape3D_74w4j")

[node name="CSGPolygon3D" type="CSGPolygon3D" parent="."]
polygon = PackedVector2Array(0, 0, 0, 0.01, 0.01, 0.01, 0.01, 0)
mode = 2
path_node = NodePath("../PlayerPath")
path_interval_type = 0
path_interval = 0.03
path_simplify_angle = 0.0
path_rotation = 1
path_local = false
path_continuous_u = false
path_u_distance = 1.0
path_joined = false

[connection signal="input_event" from="PlayerPath/PlayerPathFollow/Player/Area3D" to="." method="_on_area_3d_input_event"]
8 changes: 8 additions & 0 deletions project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ run/main_scene="res://scenes/game.tscn"
config/features=PackedStringArray("4.3", "GL Compatibility")
config/icon="res://assets/icon.svg"

[input]

click={
"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":0,"position":Vector2(0, 0),"global_position":Vector2(0, 0),"factor":1.0,"button_index":1,"canceled":false,"pressed":false,"double_click":false,"script":null)
]
}

[rendering]

renderer/rendering_method="gl_compatibility"
Expand Down
10 changes: 10 additions & 0 deletions scenes/player_movement_test.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[gd_scene load_steps=2 format=3 uid="uid://b570nnkcavtyv"]

[ext_resource type="PackedScene" uid="uid://bxhfa4fltovbh" path="res://prefabs/player.tscn" id="1_n46cj"]

[node name="PlayerMovementTest" type="Node3D"]

[node name="Camera3D" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 1, 0)

[node name="Player" parent="." instance=ExtResource("1_n46cj")]
49 changes: 49 additions & 0 deletions scripts/draw_path.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
extends Node3D

@onready var player_path = $PlayerPath
@onready var player_follow = $PlayerPath/PlayerPathFollow
@onready var player = $PlayerPath/PlayerPathFollow/Player

var path_draw_ongoing: bool = false
var temp_curve: Curve3D = Curve3D.new()
var latest_point: Vector3 = Vector3()
var in_free_move: bool = true

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
player_follow.loop = false

func _physics_process(delta):
player_follow.progress += delta * 0.2
if Input.is_action_just_released("click"):
path_draw_ongoing = false
if temp_curve.point_count <= 0:
return
var last_point = temp_curve.get_point_position(temp_curve.get_point_count() - 1)
var second_last_point = temp_curve.get_point_position(temp_curve.get_point_count() - 2)
var direction = (last_point - second_last_point).normalized()
temp_curve.add_point(last_point + direction * 100)
player_path.curve = temp_curve

if path_draw_ongoing:
mouse_path()


func mouse_path():
var pos = get_viewport().get_mouse_position()
var cam = get_tree().root.get_camera_3d()
pos = cam.project_position(pos, 1.0)
latest_point = pos
var last_point: Vector3 = player.global_position
if temp_curve.point_count > 0:
last_point = temp_curve.get_point_position(temp_curve.point_count-1)

if last_point.distance_to(latest_point) > 0.025:
temp_curve.add_point(pos)


func _on_area_3d_input_event(_camera: Node, event: InputEvent, _event_position: Vector3, _normal: Vector3, _shape_idx: int) -> void:
if event.is_action_pressed("click"):
player_follow.progress = 0
temp_curve.clear_points()
path_draw_ongoing = true

0 comments on commit dfadb00

Please sign in to comment.