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.

136 lines
3.5 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. # Walking speed and jumping height are defined later.
  7. var walk_speed = 2
  8. var jump_speed = 3
  9. const air_accel = .6
  10. var floor_friction = 0.92
  11. var air_friction = 0.98
  12. var player_info # Set by lobby
  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. spawn()
  27. func spawn():
  28. var placement = Vector3()
  29. var x_varies = 10
  30. var y_varies = 20
  31. # No Z, because that's the left-right question
  32. if player_info.is_right_team:
  33. placement = get_node("/root/world/RightSpawn").get_translation()
  34. else:
  35. placement = get_node("/root/world/LeftSpawn").get_translation()
  36. # So we don't all spawn on top of each other
  37. placement.x += rand_range(0, x_varies)
  38. placement.y += rand_range(0, y_varies)
  39. set_translation(placement)
  40. func _input(event):
  41. if is_network_master():
  42. if event is InputEventMouseMotion:
  43. yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
  44. pitch = max(min(pitch - event.relative.y * view_sensitivity, 85), -85)
  45. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  46. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(pitch), 0, 0))
  47. # Toggle mouse capture:
  48. if Input.is_action_pressed("toggle_mouse_capture"):
  49. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  50. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  51. view_sensitivity = 0
  52. else:
  53. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  54. view_sensitivity = 0.25
  55. # Quit the game:
  56. if Input.is_action_pressed("quit"):
  57. quit()
  58. master func _integrate_forces(state):
  59. control_player(state)
  60. rpc_unreliable("set_status", get_transform(), get_linear_velocity(), get_angular_velocity())
  61. slave func set_status(tf, lv, av):
  62. set_transform(tf)
  63. set_linear_velocity(lv)
  64. set_angular_velocity(av)
  65. func control_player(state):
  66. var aim = get_node("Yaw").get_global_transform().basis
  67. var direction = Vector3()
  68. if Input.is_action_pressed("move_forwards"):
  69. direction -= aim[2]
  70. if Input.is_action_pressed("move_backwards"):
  71. direction += aim[2]
  72. if Input.is_action_pressed("move_left"):
  73. direction -= aim[0]
  74. if Input.is_action_pressed("move_right"):
  75. direction += aim[0]
  76. direction = direction.normalized()
  77. var ray = get_node("Ray")
  78. if ray.is_colliding():
  79. var up = state.get_total_gravity().normalized()
  80. var normal = ray.get_collision_normal()
  81. var floor_velocity = Vector3()
  82. var object = ray.get_collider()
  83. state.apply_impulse(Vector3(), direction * walk_speed * get_mass())
  84. var lin_v = state.get_linear_velocity()
  85. lin_v.x *= floor_friction
  86. lin_v.z *= floor_friction
  87. state.set_linear_velocity(lin_v)
  88. if Input.is_action_pressed("jump"):
  89. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  90. else:
  91. state.apply_impulse(Vector3(), direction * air_accel * get_mass())
  92. var lin_v = state.get_linear_velocity()
  93. lin_v.x *= air_friction
  94. lin_v.z *= air_friction
  95. state.set_linear_velocity(lin_v)
  96. var vel = get_linear_velocity()
  97. state.integrate_forces()
  98. func _exit_scene():
  99. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  100. # Functions
  101. # =========
  102. # Quits the game:
  103. func quit():
  104. get_tree().quit()