-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplayer.gd
More file actions
78 lines (68 loc) · 2.86 KB
/
player.gd
File metadata and controls
78 lines (68 loc) · 2.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# An unofficial map editor for Trailmakers
# Copyright (C) 2025 lamersc
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
extends Node3D
signal node_clicked(node: UTMENode3D)
@onready var player_view := $PlayerView
@onready var world_space := get_world_3d().direct_space_state
@export var move_speed := 20
var direction_vector: Vector3 = Vector3.ZERO
var rotation_maximum: float = PI / 2
var grid_center: Vector3 = Vector3(0, 0, 0)
var right_button_pressed: bool = false
var can_move := true
func _unhandled_input(event: InputEvent) -> void:
# player movement logic
if can_move:
if Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
if event is InputEventMouseMotion:
rotate_y(-event.relative.x * 0.005)
player_view.rotate_x(-event.relative.y * 0.005)
player_view.rotation.x = clampf(player_view.rotation.x, -rotation_maximum, rotation_maximum)
# player worldspace click detection
if event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_LEFT:
var mouse_position := get_viewport().get_mouse_position()
var from: Vector3 = player_view.project_ray_origin(mouse_position)
var to: Vector3 = player_view.project_ray_normal(mouse_position) * 10000
var raycast_query := PhysicsRayQueryParameters3D.new()
raycast_query.from = from
raycast_query.to = to
var raycast_result := world_space.intersect_ray(raycast_query)
if not raycast_result.is_empty():
var utme_node: UTMENode3D = raycast_result.collider.get_meta("UTME_node")
if utme_node != null:
node_clicked.emit(utme_node)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if can_move:
var direction: Vector3 = Vector3.ZERO
if Input.is_action_pressed("move_up"):
direction.y += 1
if Input.is_action_pressed("move_down"):
direction.y -= 1
if Input.is_action_pressed("move_left"):
direction.x -= 1
if Input.is_action_pressed("move_right"):
direction.x += 1
if Input.is_action_pressed("move_forward"):
direction.z -= 1
if Input.is_action_pressed("move_backward"):
direction.z += 1
if direction != Vector3.ZERO:
direction = direction.normalized() * move_speed * delta
if Input.is_action_pressed("increase_speed", true):
direction *= 2
direction = basis * direction
position += direction