Files
Diabolo/Demon_v0.19/orbit.gd
T

61 lines
2.4 KiB
GDScript

extends RefCounted
var g
var dragging=false
var yaw=atan2(10.0,20.0)
var pitch=0.68
var focus=Vector3(0,0.4,-0.2)
var cursor=Vector2.ZERO
var pan=Vector3.ZERO
var keys={}
func setup(game):
g=game
update(1.0)
func begin():
if dragging or not g.game_started or (g.paused and not (g.workshop and g.workshop.build_mode)):return
dragging=true
cursor=g.get_viewport().get_mouse_position()
Input.mouse_mode=Input.MOUSE_MODE_CAPTURED
func stop():
if not dragging:return
dragging=false
Input.mouse_mode=Input.MOUSE_MODE_VISIBLE
g.get_viewport().warp_mouse(cursor)
func input(event):
if event is InputEventKey:
keys[event.physical_keycode if event.physical_keycode else event.keycode]=event.pressed
if event is InputEventMouseButton and event.button_index==MOUSE_BUTTON_MIDDLE and event.pressed:
if (g.workshop and g.workshop.build_mode) or g.get_viewport().gui_get_hovered_control()==null:begin()
if dragging:g.get_viewport().set_input_as_handled()
if event is InputEventMouseButton and event.button_index==MOUSE_BUTTON_MIDDLE and not event.pressed:stop()
if event is InputEventMouseMotion and dragging:
if Input.is_physical_key_pressed(KEY_SHIFT):
pan+=(-g.camera.global_basis.x*event.relative.x+g.camera.global_basis.y*event.relative.y)*g.camera.size/g.get_viewport().get_visible_rect().size.y;pan.y=0
else:
yaw=wrapf(yaw-event.relative.x*0.006,-PI,PI)
pitch=clampf(pitch+event.relative.y*0.004,0.52,1.15)
g.get_viewport().set_input_as_handled()
func update(dt):
if g.workshop and g.workshop.build_mode:
var move=Vector2(float(held(KEY_D))-float(held(KEY_A)),float(held(KEY_S))-float(held(KEY_W)))
var right=g.camera.global_basis.x;right.y=0;right=right.normalized()
var down=g.camera.global_basis.z;down.y=0;down=down.normalized()
pan+=(right*move.x+down*move.y).limit_length(1)*dt*9.0
if not (g.workshop and g.workshop.build_mode) and not dragging:
pan=pan.lerp(Vector3.ZERO,1-exp(-maxf(dt,0)*1.7))
focus=focus.lerp(base_focus()+pan,1-exp(-maxf(dt,0)*3))
var offset=Vector3(sin(yaw)*cos(pitch),sin(pitch),cos(yaw)*cos(pitch))*30.0
g.camera.position=focus+offset
g.camera.look_at(focus)
g.architecture.update(g.camera)
func held(key):
return keys.get(key,false) or Input.is_physical_key_pressed(key)
func base_focus():
if g.workshop and g.workshop.build_mode:
return g.CAMERA_TARGET+Vector3(g.architecture.growth.extra_width(g.architecture.growth.level)*.35,0,0)
return g.player.position+Vector3(0,.4,0)
func preserve_focus():pan=focus-base_focus()