-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.gd
79 lines (60 loc) · 2.31 KB
/
Camera.gd
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
# Copyright © 2017 Hugo Locurcio and contributors - MIT license
# See LICENSE.md included in the source distribution for more information.
extends Camera
const MOVE_SPEED = 0.5
const MOUSE_SENSITIVITY = 0.002
onready var speed = 1
onready var velocity = Vector3()
onready var initial_rotation = PI/2
func _unhandled_input(event):
if event is InputEventKey:
if event.pressed and event.scancode == KEY_ESCAPE:
get_tree().quit()
func _enter_tree():
# Capture the mouse (can be toggled by pressing F10)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
# Horizontal mouse look
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotation.y -= event.relative.x*MOUSE_SENSITIVITY
# Vertical mouse look, clamped to -90..90 degrees
if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
rotation.x = clamp(rotation.x - event.relative.y*MOUSE_SENSITIVITY, deg2rad(-90), deg2rad(90))
# Toggle HUD
#if event.is_action_pressed("toggle_hud"):
# $"../FPSCounter".visible = !$"../FPSCounter".visible
# Toggle mouse capture
if event.is_action_pressed("toggle_mouse_capture"):
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
else:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _physics_process(delta):
# Speed modifier
if Input.is_action_pressed("move_speed"):
speed = 2
else:
speed = 1
# Movement
if Input.is_action_pressed("move_forward"):
velocity.x -= MOVE_SPEED*speed*delta
if Input.is_action_pressed("move_backward"):
velocity.x += MOVE_SPEED*speed*delta
if Input.is_action_pressed("move_left"):
velocity.z += MOVE_SPEED*speed*delta
if Input.is_action_pressed("move_right"):
velocity.z -= MOVE_SPEED*speed*delta
if Input.is_action_pressed("move_up"):
velocity.y += MOVE_SPEED*speed*delta
if Input.is_action_pressed("move_down"):
velocity.y -= MOVE_SPEED*speed*delta
# Friction
velocity *= 0.875
# Apply velocity
translation += velocity \
.rotated(Vector3(0, 1, 0), rotation.y - initial_rotation) \
.rotated(Vector3(1, 0, 0), cos(rotation.y)*rotation.x) \
.rotated(Vector3(0, 0, 1), -sin(rotation.y)*rotation.x)
func _exit_tree():
# Restore the mouse cursor upon quitting
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)