Player.gd 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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,1800.4), 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. func _input(event):
  27. if event is InputEventMouseMotion:
  28. mouseDelta = event.relative
  29. func _physics_process(delta):
  30. # reset the x and z velocity
  31. vel.x = 0
  32. vel.y = 0
  33. vel.z = 0
  34. var input = Vector3()
  35. # movement inputs
  36. if Input.is_action_pressed("move_forward"):
  37. input.y -= 1
  38. if Input.is_action_pressed("move_backward"):
  39. input.y += 1
  40. if Input.is_action_pressed("move_left"):
  41. input.x -= 1
  42. if Input.is_action_pressed("move_right"):
  43. input.x += 1
  44. if Input.is_action_pressed("move_up"):
  45. input.z += 1
  46. if Input.is_action_pressed("move_down"):
  47. input.z -= 1
  48. input = input.normalized()
  49. # get the forward and right directions
  50. var forward = global_transform.basis.z
  51. var right = global_transform.basis.x
  52. var up = global_transform.basis.y
  53. var relativeDir = (forward * input.y + right * input.x + up * input.z)
  54. var moveSpeed2 = moveSpeed
  55. if Input.is_action_pressed("sprint"):
  56. moveSpeed2 = moveSpeed * 3
  57. # set the velocity
  58. vel.x = relativeDir.x * moveSpeed2
  59. vel.z = relativeDir.z * moveSpeed2
  60. vel.y = relativeDir.y * moveSpeed2
  61. # move the player
  62. vel = move_and_slide(vel, Vector3.UP)
  63. #ray = ray.look_at(self.global_transform.origin, Vector3.UP)
  64. var pos = self.global_transform.origin
  65. .get_parent().get_node("UI/1").text="x:%s y:%s z:%s" %[pos.x, pos.y, pos.z]
  66. var polPos = Common.globalToLatLon(pos.x,pos.y,pos.z)
  67. .get_parent().get_node("UI/2").text="lat:%s lon:%s" %[polPos.x, polPos.y]
  68. #.get_node("UI/3").text="LatLonPos=="+str(latLonToGlobal( self.global_transform.origin.x,self.global_transform.origin.y))