khanat-assets---3d-godot-cl.../previewer/previewer.gd

91 lines
No EOL
3.3 KiB
GDScript

extends Spatial
var _mouse_look = false
var _mouse_position = Vector2( 0.0, 0.0 )
var _yaw = 0.0
var _pitch = 0.0
var _total_yaw = 0.0
var _total_pitch = 0.0
var _current_camera_speed = 1.0
export(int) var yaw_limit = 360
export(int) var pitch_limit = 360
export(float) var camera_sensitivity = 0.5
export(float) var camera_smoothness = 0.5
export(float) var camera_speed = 1.0
export(float) var camera_accel = 10.0
var iTime=0.0
var iFrame=0
func _ready():
self._current_camera_speed = self.camera_speed
$settings.show()
$sky/viewport/sprite.set_time_of_day( 8.0, $light_system/directional_light, 25.0 )
func _process( delta ):
iTime+=delta
iFrame+=1
if Input.is_action_pressed("move_up"):
$dummy/camera_stand.translate( Vector3( 0.0, self._current_camera_speed*delta, 0.0 ) )
elif Input.is_action_pressed("move_down"):
$dummy/camera_stand.translate( Vector3( 0.0,-self._current_camera_speed*delta, 0.0 ) )
elif Input.is_action_pressed("move_forward"):
$dummy/camera_stand.translate( Vector3( 0.0, 0.0,-self._current_camera_speed*delta ) )
elif Input.is_action_pressed("move_backward"):
$dummy/camera_stand.translate( Vector3( 0.0, 0.0, self._current_camera_speed*delta ) )
if Input.is_action_pressed("move_left"):
$dummy/camera_stand.translate( Vector3(-self._current_camera_speed*delta, 0.0, 0.0 ) )
elif Input.is_action_pressed("move_right"):
$dummy/camera_stand.translate( Vector3( self._current_camera_speed*delta, 0.0, 0.0 ) )
if _mouse_look:
_update_mouselook()
func _update_mouselook():
_mouse_position *= self.camera_sensitivity
_yaw = _yaw * self.camera_smoothness + _mouse_position.x * (1.0 - self.camera_smoothness)
_pitch = _pitch * self.camera_smoothness + _mouse_position.y * (1.0 - self.camera_smoothness)
_mouse_position = Vector2(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
$dummy/camera_stand.rotate_y(deg2rad(-_yaw))
$dummy/camera_stand.rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))
func _input(event):
if event.is_action_pressed( "move_look" ):
Input.set_mouse_mode( Input.MOUSE_MODE_CAPTURED )
self._mouse_look = true
elif event.is_action_released( "move_look" ):
Input.set_mouse_mode( Input.MOUSE_MODE_VISIBLE )
self._mouse_look = false
if event is InputEventMouseMotion:
if _mouse_look:
_mouse_position = event.relative
if event.is_action_pressed( "move_zoom_in" ):
$dummy/camera_stand/camera.set_fov( $dummy/camera_stand/camera.get_fov()-1.0 )
elif event.is_action_pressed( "move_zoom_out" ):
$dummy/camera_stand/camera.set_fov( $dummy/camera_stand/camera.get_fov()+1.0 )
if event.is_action_pressed( "move_speed" ):
self._current_camera_speed = self.camera_speed + self.camera_accel
elif event.is_action_released( "move_speed" ):
self._current_camera_speed = self.camera_speed
if event.is_action_pressed( "ui_settings_window" ):
$settings.visible = !$settings.visible