Player.gd 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. extends KinematicBody
  2. # physics
  3. var moveSpeed : float = 5.0
  4. # cam look
  5. var minLookAngle : float = -90.0
  6. var maxLookAngle : float = 90.0
  7. var lookSensitivity : float = 10.0
  8. var ray: RayCast
  9. # vectors
  10. var vel : Vector3 = Vector3()
  11. var mouseDelta : Vector2 = Vector2()
  12. onready var camera : Camera = get_node("Camera")
  13. func _ready():
  14. move_and_slide(Common.latLonToGlobal(0,0,11), Vector3.UP)
  15. ray = RayCast.new()
  16. .get_parent().add_child(ray)
  17. func _process(delta):
  18. # rotate the camera along the x axis
  19. camera.rotation_degrees.x -= mouseDelta.y * lookSensitivity * delta
  20. # clamp camera x rotation axis
  21. camera.rotation_degrees.x = clamp(camera.rotation_degrees.x, minLookAngle, maxLookAngle)
  22. # rotate the player along their y-axis
  23. rotation_degrees.y -= mouseDelta.x * lookSensitivity * delta
  24. # reset the mouseDelta vector
  25. mouseDelta = Vector2()
  26. var origin = get_global_transform().origin
  27. .get_parent().get_node("UI/2").text="latlonhight: "+str(Common.globalToLatLon(origin.x,origin.y,origin.z, origin.length()))+","+str(origin.length())
  28. func _input(event):
  29. if event is InputEventMouseMotion and Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED:
  30. mouseDelta = event.relative
  31. func _physics_process(delta):
  32. # reset the x and z velocity
  33. vel.x = 0
  34. vel.y = 0
  35. vel.z = 0
  36. var input = Vector3()
  37. # movement inputs
  38. if Input.is_action_pressed("move_forward"):
  39. input.y -= 1
  40. if Input.is_action_pressed("move_backward"):
  41. input.y += 1
  42. if Input.is_action_pressed("move_left"):
  43. input.x -= 1
  44. if Input.is_action_pressed("move_right"):
  45. input.x += 1
  46. if Input.is_action_pressed("move_up"):
  47. input.z += 1
  48. if Input.is_action_pressed("move_down"):
  49. input.z -= 1
  50. input = input.normalized()
  51. # get the forward and right directions
  52. var forward = global_transform.basis.z
  53. var right = global_transform.basis.x
  54. var up = global_transform.basis.y
  55. var relativeDir = (forward * input.y + right * input.x + up * input.z)
  56. var moveSpeed2 = moveSpeed
  57. if Input.is_action_pressed("sprint"):
  58. moveSpeed2 = moveSpeed * 3
  59. # set the velocity
  60. vel.x = relativeDir.x * moveSpeed2
  61. vel.z = relativeDir.z * moveSpeed2
  62. vel.y = relativeDir.y * moveSpeed2
  63. var gravity = self.global_transform.origin.normalized()
  64. # move the player
  65. vel = move_and_slide(vel, Vector3.UP)
  66. #ray = ray.look_at(self.global_transform.origin, Vector3.UP)
  67. var pos = self.global_transform.origin
  68. var polPos = Common.globalToLatLon(pos.x,pos.y,pos.z)
  69. var rotationDeg=get_rotation_degrees().x
  70. var posNorm=abs(pos.normalized().y)
  71. var targetRot = posNorm * 90
  72. #if rotationDeg < targetRot:
  73. #rotate_x(1)
  74. #elif rotationDeg > targetRot:
  75. #rotate_x(-1)
  76. .get_parent().get_node("UI/1").text="x:%s y:%s z:%s" %[pos.x, pos.y, pos.z]
  77. #.get_node("UI/3").text="LatLonPos=="+str(latLonToGlobal( self.global_transform.origin.x,self.global_transform.origin.y))