-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCamera.gd
More file actions
108 lines (87 loc) · 2.73 KB
/
Camera.gd
File metadata and controls
108 lines (87 loc) · 2.73 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
extends Camera
# settings
export (float, 0.0, 1.0) var sensitivity = 0.5
export (float, 0.0, 0.999, 0.001) var smoothness = 0.5 setget set_smoothness
export(NodePath) var privot setget set_privot
export var distance = 12000.0 setget set_distance
export (int, 0, 360) var yaw_limit = 360
export (int, 0, 360) var pitch_limit = 360
export var zoomStep = 100
#export var default_distance = 200.0
# internal variables
var _yaw = 0.0
var _pitch = 0.0
var _total_yaw = 0.0
var _total_pitch = 0.0
var _mouse_pos = Vector2(0.0,0.0)
var _key_pos = Vector2(0.0,0.0)
var _camera_drag = false
# Called when the node enters the scene tree for the first time.
func _ready():
pass
func _input_event(_event):
pass
#if event is InputEventMouseButton:
# if event.button_index == BUTTON_LEFT:
# _camera_drag = event.pressed
func _input(event):
if event is InputEventKey and event.pressed:
match event.scancode:
KEY_UP:
_key_pos.y = 10.0
KEY_DOWN:
_key_pos.y = -10.0
KEY_LEFT:
_key_pos.x = 10.0
KEY_RIGHT:
_key_pos.x = -10.0
KEY_PAGEUP:
distance -= zoomStep * 4.0
KEY_PAGEDOWN:
distance += zoomStep * 4.0
if event is InputEventMouseMotion:
if _camera_drag:
_mouse_pos = event.relative
if event is InputEventMouseButton:
if event.button_index == BUTTON_LEFT:
_camera_drag = event.pressed
elif event.button_index == BUTTON_WHEEL_UP:
distance -= zoomStep
elif event.button_index == BUTTON_WHEEL_DOWN:
distance += zoomStep
#elif event.button_index == BUTTON_MIDDLE:
# distance = default_distance
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
_update_distance()
_update_view()
func _update_process_func():
set_process(true)
func _update_view():
_yaw = _yaw * smoothness + (_mouse_pos.x + _key_pos.x) * sensitivity * (1.0 - smoothness)
_pitch = _pitch * smoothness + (_mouse_pos.y + _key_pos.y) * sensitivity * (1.0 - smoothness)
_mouse_pos = Vector2(0.0,0.0)
_key_pos = Vector2(0.0,0.0)
if yaw_limit < 360:
_yaw = clamp(_yaw, -yaw_limit - _total_yaw, yaw_limit - _total_yaw)
if pitch_limit < 360:
_pitch = clamp(_pitch, -pitch_limit - _total_pitch, pitch_limit - _total_pitch)
_total_yaw += _yaw
_total_pitch += _pitch
var target = privot.get_translation()
var offset = get_translation().distance_to(target)
set_translation(target)
rotate_y(deg2rad(-_yaw))
rotate_object_local(Vector3(1.0,0.0,0.0), deg2rad(-_pitch))
translate(Vector3(0.0,0.0,offset))
func _update_distance():
var t = privot.get_translation()
t.z -= distance
set_translation(t)
func set_privot(value):
privot = value
_update_process_func()
func set_smoothness(value):
smoothness = clamp(value, 0.001, 0.999)
func set_distance(value):
distance = max(0, value)