A team game with an emphasis on movement (with no shooting), inspired by Overwatch and Zineth
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.0 KiB

  1. # Original: https://raw.githubusercontent.com/Calinou/fps-test/master/scripts/player.gd
  2. extends RigidBody
  3. var view_sensitivity = 0.25
  4. var yaw = 0
  5. var pitch = 0
  6. var timer = 0
  7. # Walking speed and jumping height are defined later.
  8. var walk_speed = 2
  9. var jump_speed = 3
  10. const air_accel = .6
  11. var floor_friction = 0.92
  12. var air_friction = 0.98
  13. var health = 100
  14. var stamina = 10000
  15. var debug_node
  16. slave var slave_transform = Basis()
  17. slave var slave_lin_v = Vector3()
  18. slave var slave_ang_v = Vector3()
  19. func _ready():
  20. set_process_input(true)
  21. # Capture mouse once game is started:
  22. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  23. debug_node = get_node("/root/world/Debug")
  24. if is_network_master():
  25. get_node("Yaw/Pitch/Camera").make_current()
  26. func _input(event):
  27. if is_network_master():
  28. if event is InputEventMouseMotion:
  29. yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
  30. pitch = max(min(pitch - event.relative.y * view_sensitivity, 85), -85)
  31. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  32. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(pitch), 0, 0))
  33. # Toggle mouse capture:
  34. if Input.is_action_pressed("toggle_mouse_capture"):
  35. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  36. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  37. view_sensitivity = 0
  38. else:
  39. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  40. view_sensitivity = 0.25
  41. # Quit the game:
  42. if Input.is_action_pressed("quit"):
  43. quit()
  44. master func _integrate_forces(state):
  45. control_player(state)
  46. rpc_unreliable("set_status", get_transform(), get_linear_velocity(), get_angular_velocity())
  47. slave func set_status(tf, lv, av):
  48. set_transform(tf)
  49. set_linear_velocity(lv)
  50. set_angular_velocity(av)
  51. func control_player(state):
  52. var aim = get_node("Yaw").get_global_transform().basis
  53. var direction = Vector3()
  54. if Input.is_action_pressed("move_forwards"):
  55. direction -= aim[2]
  56. if Input.is_action_pressed("move_backwards"):
  57. direction += aim[2]
  58. if Input.is_action_pressed("move_left"):
  59. direction -= aim[0]
  60. if Input.is_action_pressed("move_right"):
  61. direction += aim[0]
  62. direction = direction.normalized()
  63. var ray = get_node("Ray")
  64. if ray.is_colliding():
  65. var up = state.get_total_gravity().normalized()
  66. var normal = ray.get_collision_normal()
  67. var floor_velocity = Vector3()
  68. var object = ray.get_collider()
  69. state.apply_impulse(Vector3(), direction * walk_speed * get_mass())
  70. var lin_v = state.get_linear_velocity()
  71. lin_v.x *= floor_friction
  72. lin_v.z *= floor_friction
  73. state.set_linear_velocity(lin_v)
  74. if Input.is_action_pressed("jump"):
  75. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  76. else:
  77. state.apply_impulse(Vector3(), direction * air_accel * get_mass())
  78. var lin_v = state.get_linear_velocity()
  79. lin_v.x *= air_friction
  80. lin_v.z *= air_friction
  81. state.set_linear_velocity(lin_v)
  82. var vel = get_linear_velocity()
  83. state.integrate_forces()
  84. func _exit_scene():
  85. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  86. # Functions
  87. # =========
  88. # Quits the game:
  89. func quit():
  90. get_tree().quit()