main_player.gd 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. extends KinematicBody
  2. var mouse_c=false
  3. var is_ui=false
  4. var _mouse_position = Vector2(0.0, 0.0)
  5. var _yaw = 0.0
  6. var _pitch = 0.0
  7. var _total_yaw = 0.0
  8. var _total_pitch = 0.0
  9. const sensitivity = 0.25
  10. const smoothness = 0.25
  11. const yaw_limit = 360
  12. const pitch_limit = 360
  13. const GRAVITY = -0.35
  14. const JUMP_SPEED = 10.0
  15. var jump_timer=0.0
  16. var hfloor_timer=0.0
  17. var hit_floor=false
  18. var _direction = Vector3(0.0, 0.0, 0.0)
  19. var vel=Vector3(0,0,0)
  20. var _speed = Vector3(0.0, 0.0, 0.0)
  21. const max_speed = Vector3(5.0, 5.0, 5.0)
  22. const acceleration = 1.0
  23. const deceleration = 0.1
  24. const MAX_SLOPE_ANGLE = 80
  25. var forward_action = "ui_up"
  26. var backward_action = "ui_down"
  27. var left_action = "ui_left"
  28. var right_action = "ui_right"
  29. var up_action = "ui_page_up"
  30. var down_action = "ui_page_down"
  31. var iTime=0.0
  32. var start_pos=Vector3()
  33. func _ready():
  34. start_pos=self.translation
  35. func _process(delta):
  36. anti_fall()
  37. _update_mouselook()
  38. update_all_cam()
  39. iTime+=delta
  40. func _physics_process(delta):
  41. process_movement(delta)
  42. process_collision(delta)
  43. func _input(event):
  44. if event is InputEventMouseButton:
  45. mouse_c=!mouse_c
  46. if mouse_c and !is_ui:
  47. if event is InputEventMouseMotion:
  48. _mouse_position = event.relative
  49. if event.is_action_pressed(forward_action):
  50. _direction.z = -1
  51. elif event.is_action_pressed(backward_action):
  52. _direction.z = 1
  53. elif not Input.is_action_pressed(forward_action) and not Input.is_action_pressed(backward_action):
  54. _direction.z = 0
  55. if event.is_action_pressed(left_action):
  56. _direction.x = -1
  57. elif event.is_action_pressed(right_action):
  58. _direction.x = 1
  59. elif not Input.is_action_pressed(left_action) and not Input.is_action_pressed(right_action):
  60. _direction.x = 0
  61. if is_on_floor()||(hit_floor):
  62. hfloor_timer=iTime
  63. if is_on_floor()||hit_floor||iTime-hfloor_timer>1:
  64. hit_floor=true
  65. if Input.is_action_just_pressed("ui_select"):
  66. hit_floor=false
  67. vel.y = JUMP_SPEED
  68. jump_timer=iTime
  69. func anti_fall():
  70. if(self.translation.length()>100):
  71. self.translation=start_pos+Vector3(0,5,0)
  72. func process_movement(delta):
  73. var offset = max_speed * acceleration * _direction
  74. _speed.x = clamp(_speed.x + offset.x, -max_speed.x, max_speed.x)
  75. _speed.y = clamp(_speed.y + offset.y, -max_speed.y, max_speed.y)
  76. _speed.z = clamp(_speed.z + offset.z, -max_speed.z, max_speed.z)
  77. # Apply deceleration if no input
  78. if _direction.x == 0:
  79. _speed.x *= (1.0 - deceleration)
  80. if _direction.y == 0:
  81. _speed.y *= (1.0 - deceleration)
  82. if _direction.z == 0:
  83. _speed.z *= (1.0 - deceleration)
  84. vel.x = _speed.x
  85. vel.z = _speed.z
  86. var tvely=0.99*vel.y
  87. vel.y=0
  88. var md=get_node("Camera").transform
  89. md=md.translated(vel)
  90. vel=md.origin
  91. vel.y = tvely+GRAVITY
  92. vel = move_and_slide(vel,Vector3(0,1,0), true, 4, deg2rad(MAX_SLOPE_ANGLE),false)
  93. func process_collision(delta):
  94. for a in get_slide_count():
  95. var collision = get_slide_collision(a)
  96. if(collision.collider.is_in_group("spheres")):
  97. if(collision.collider is RigidBody):
  98. if(collision.collider.mode==RigidBody.MODE_RIGID):
  99. collision.collider.apply_central_impulse(-collision.normal * vel.length() * 1.0)
  100. func update_all_cam():
  101. var cam_node=Array()
  102. cam_node.append(get_node("../p1/p1/Camera"))
  103. cam_node.append(get_node("../p2/p2/Camera"))
  104. cam_node.append(get_node("../p3/p3/Camera"))
  105. cam_node.append(get_node("../p4/p4/Camera"))
  106. cam_node.append(get_node("../p5/p5/Camera"))
  107. cam_node.append(get_node("../p0/Camera"))
  108. for a in cam_node:
  109. a.rotation=get_node("Camera").rotation
  110. a.translation=self.translation
  111. a.translation.y+=1
  112. func _update_mouselook():
  113. _mouse_position *= sensitivity
  114. _yaw = _yaw * smoothness + _mouse_position.x * (1.0 - smoothness)
  115. _pitch = _pitch * smoothness + _mouse_position.y * (1.0 - smoothness)
  116. _mouse_position = Vector2(0, 0)
  117. if yaw_limit < 360:
  118. _yaw = clamp(_yaw, -yaw_limit - _total_yaw, yaw_limit - _total_yaw)
  119. if pitch_limit < 360:
  120. _pitch = clamp(_pitch, -pitch_limit - _total_pitch, pitch_limit - _total_pitch)
  121. _total_yaw += _yaw
  122. _total_pitch += _pitch
  123. get_node("Camera").rotate_y(deg2rad(-_yaw))
  124. get_node("Camera").rotate_object_local(Vector3(1,0,0), deg2rad(-_pitch))