Player.gd 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. extends KinematicBody
  2. var speed = 0.4
  3. var normal = Vector3(0.0,1.0,0.0)
  4. var rotspeed = 0.01*1024.0
  5. var theta_limit = PI/2.0*0.9
  6. var camtheta = 0
  7. var body_phi = 0
  8. var cam_speed = Vector2(0,0)
  9. var camera
  10. var camera_menu_mode = false
  11. var current_hand = ""
  12. var flycrazy = false
  13. var flyspeed = Vector3(1,0,0)
  14. var maxflyspeed = 5.0
  15. onready var camera_default_transform = $Camera.transform
  16. onready var vp_size = get_viewport().size
  17. var chat
  18. var dont_end_chat = false
  19. class_name Player
  20. # Called when the node enters the scene tree for the first time.
  21. func _input(event):
  22. if not camera_menu_mode:
  23. if event is InputEventMouseMotion:
  24. body_phi= wrapf(body_phi-event.relative.x * rotspeed/vp_size.x,-PI,PI)
  25. camtheta = clamp(camtheta - event.relative.y*rotspeed/vp_size.x,-theta_limit,theta_limit)
  26. # else:
  27. # print ("nonmouse event captured")
  28. if event.is_action_pressed("ui_cancel"):
  29. if not camera_menu_mode:
  30. #Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  31. $NagPlace.ask_exit()
  32. # elif not dont_end_chat:
  33. # end_chat()
  34. func end_chat():
  35. #print("esc pressed in menu mode")
  36. if !dont_end_chat:
  37. var t = camera.global_transform
  38. camera.get_parent().remove_child(camera)
  39. add_child(camera)
  40. camera.global_transform = t
  41. camera_menu_mode = false
  42. #camera.translation,camera.rotation)
  43. var tw1 = SDTween.new()
  44. var camplace = camera_default_transform
  45. tw1.interpolate_property(camera,"transform",camera.transform,camplace,1,Tween.TRANS_CUBIC,Tween.EASE_IN_OUT)
  46. add_child(tw1)
  47. tw1.start()
  48. chat.close()
  49. chat=null
  50. func _process(delta):
  51. if !camera_menu_mode and !flycrazy:
  52. if Input.is_action_pressed("ui_down"):
  53. move_and_slide(Vector3(0,0,speed).rotated(normal,rotation.y),normal)
  54. if Input.is_action_pressed("ui_up"):
  55. move_and_slide(Vector3(0,0,-speed).rotated(normal,rotation.y),normal)
  56. if Input.is_action_pressed("ui_right"):
  57. move_and_slide(Vector3(speed,0,0).rotated(normal,rotation.y),normal)
  58. if Input.is_action_pressed("ui_left"):
  59. move_and_slide(Vector3(-speed,0,0).rotated(normal,rotation.y),normal)
  60. elif flycrazy:
  61. move_and_slide(flyspeed)
  62. var target = camera.get_node("RayCast").get_collider()
  63. if target!=null:
  64. var act_target = target.get_parent().get_parent()
  65. if !camera_menu_mode:
  66. if act_target is Interactable:
  67. act_target.bump_E()
  68. if Input.is_action_just_pressed("interact"):
  69. print(target.name if target!=null else "null")
  70. var c = camera
  71. var t = c.global_transform
  72. remove_child(c)
  73. act_target.add_child(c)
  74. c.global_transform = t
  75. camera_menu_mode = true
  76. var tw1 = SDTween.new()
  77. var camplace = act_target.get_node("CameraPlace")
  78. chat = act_target.start_chat()
  79. tw1.interpolate_property(c,"transform",
  80. c.transform,
  81. camplace.transform,
  82. 1,
  83. Tween.TRANS_CUBIC,
  84. Tween.EASE_IN_OUT)
  85. add_child(tw1)
  86. tw1.start()
  87. if not camera_menu_mode:
  88. camera.rotation.x += (camtheta-camera.rotation.x)*0.3
  89. var dphi = body_phi - rotation.y
  90. if dphi>PI:
  91. dphi = dphi - 2*PI
  92. elif dphi<-PI:
  93. dphi = dphi + 2*PI
  94. dphi*=0.3
  95. rotation.y=wrapf(rotation.y+dphi,-PI,PI)
  96. func _ready():
  97. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  98. camera = $Camera
  99. pass # Replace with function body.
  100. func take(itemname):
  101. if current_hand == "":
  102. current_hand = itemname
  103. var item = load("res://"+itemname+".tscn").instance()
  104. $Spatial.add_child(item)
  105. show_hand()
  106. func give_hand():
  107. for c in $Spatial.get_children():
  108. c.queue_free()
  109. current_hand = ""
  110. func show_hand():
  111. $Spatial.visible=true