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.

210 lines
6.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. # Walking speed and jumping height are defined later.
  7. var walk_speed = 0.8 # Actually acceleration; m/s/s
  8. var jump_speed = 3 # m/s
  9. var air_accel = .3 # m/s/s
  10. var floor_friction = 1-0.08
  11. var air_friction = 1-0.03
  12. var player_info # Set by lobby
  13. var walk_speed_build = 0.006 # `walk_speed` per `switch_charge`
  14. var air_speed_build = 0.006 # `air_accel` per `switch_chare`
  15. var switch_charge = 0
  16. var switch_charge_cap = 200 # While switching is always at 100, things like speed boost might go higher!
  17. var movement_charge = 0.15 # In percent per meter (except when heroes change that)
  18. const fall_height = -25
  19. var debug_node
  20. slave var slave_transform = Basis()
  21. slave var slave_lin_v = Vector3()
  22. slave var slave_ang_v = Vector3()
  23. func _ready():
  24. set_process_input(true)
  25. # Capture mouse once game is started:
  26. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  27. debug_node = get_node("/root/Level/Debug")
  28. if is_network_master():
  29. get_node("Yaw/Pitch/Camera").make_current()
  30. spawn()
  31. else:
  32. remove_child(get_node("MasterOnly"))
  33. func spawn():
  34. var placement = Vector3()
  35. var x_varies = 10
  36. var y_varies = 20
  37. # No Z, because that's the left-right question
  38. if player_info.is_right_team:
  39. placement = get_node("/root/Level/RightSpawn").get_translation()
  40. else:
  41. placement = get_node("/root/Level/LeftSpawn").get_translation()
  42. # So we don't all spawn on top of each other
  43. placement.x += rand_range(0, x_varies)
  44. placement.y += rand_range(0, y_varies)
  45. set_transform(Basis())
  46. set_translation(placement)
  47. set_linear_velocity(Vector3())
  48. func _input(event):
  49. if is_network_master():
  50. if event is InputEventMouseMotion:
  51. yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
  52. pitch = max(min(pitch - event.relative.y * view_sensitivity, 85), -85)
  53. set_rotation()
  54. # Toggle mouse capture:
  55. if Input.is_action_pressed("toggle_mouse_capture"):
  56. toggle_mouse_capture()
  57. if Input.is_action_just_pressed("switch_hero"):
  58. switch_hero_interface()
  59. # Quit the game:
  60. if Input.is_action_pressed("quit"):
  61. quit()
  62. func toggle_mouse_capture():
  63. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  64. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  65. view_sensitivity = 0
  66. else:
  67. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  68. view_sensitivity = 0.25
  69. func set_rotation():
  70. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  71. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(pitch), 0, 0))
  72. func _integrate_forces(state):
  73. if is_network_master():
  74. control_player(state)
  75. rpc_unreliable("set_status", get_status())
  76. slave func set_status(s):
  77. set_transform(s[0])
  78. set_linear_velocity(s[1])
  79. set_angular_velocity(s[2])
  80. yaw = s[3]
  81. pitch = s[4]
  82. set_rotation() # Confirm yaw + pitch changes
  83. func get_status():
  84. return [
  85. get_transform(),
  86. get_linear_velocity(),
  87. get_angular_velocity(),
  88. yaw,
  89. pitch,
  90. ]
  91. func control_player(state):
  92. var aim = get_node("Yaw").get_global_transform().basis
  93. var direction = Vector3()
  94. if Input.is_action_pressed("move_forwards"):
  95. direction -= aim[2]
  96. if Input.is_action_pressed("move_backwards"):
  97. direction += aim[2]
  98. if Input.is_action_pressed("move_left"):
  99. direction -= aim[0]
  100. if Input.is_action_pressed("move_right"):
  101. direction += aim[0]
  102. direction = direction.normalized()
  103. var ray = get_node("Ray")
  104. if ray.is_colliding():
  105. var up = state.get_total_gravity().normalized()
  106. var normal = ray.get_collision_normal()
  107. var floor_velocity = Vector3()
  108. var object = ray.get_collider()
  109. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  110. state.apply_impulse(Vector3(), direction * accel * get_mass())
  111. var lin_v = state.get_linear_velocity()
  112. lin_v.x *= floor_friction
  113. lin_v.z *= floor_friction
  114. state.set_linear_velocity(lin_v)
  115. if Input.is_action_pressed("jump"):
  116. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  117. else:
  118. var accel = (1 + switch_charge * air_speed_build) * air_accel
  119. state.apply_impulse(Vector3(), direction * accel * get_mass())
  120. var lin_v = state.get_linear_velocity()
  121. lin_v.x *= air_friction
  122. lin_v.z *= air_friction
  123. state.set_linear_velocity(lin_v)
  124. state.integrate_forces()
  125. func _process(delta):
  126. # All player code not caused by input, and not causing movement
  127. var vel = get_linear_velocity()
  128. switch_charge += movement_charge * vel.length() * delta
  129. var switch_node = get_node("MasterOnly/SwitchCharge")
  130. switch_node.set_text("%.f%%" % switch_charge)
  131. if switch_charge >= 100:
  132. # Let switch_charge keep building, because we use it for walk_speed and things
  133. switch_node.set_text("100%% (%.f)\nQ - Switch hero" % switch_charge)
  134. if switch_charge > switch_charge_cap:
  135. # There is however a cap
  136. switch_charge = switch_charge_cap
  137. if get_translation().y < fall_height:
  138. spawn()
  139. switch_hero_interface()
  140. func switch_hero_interface():
  141. # Interface needs the mouse!
  142. toggle_mouse_capture()
  143. # Pause so if we have walls and such nothing funny happens
  144. get_tree().set_pause(true)
  145. var interface = preload("res://scenes/HeroSelect.tscn").instance()
  146. add_child(interface)
  147. interface.get_node("Confirm").connect("pressed", self, "switch_hero_master")
  148. func switch_hero_master():
  149. rpc("switch_hero", get_node("HeroSelect/Hero").get_selected_id())
  150. # Remove the mouse and enable looking again
  151. toggle_mouse_capture()
  152. get_tree().set_pause(false)
  153. sync func switch_hero(hero):
  154. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  155. var net_id = get_tree().get_network_unique_id()
  156. set_name("%d-delete" % net_id) # Can't have duplicate names
  157. new_hero.set_name("%d" % net_id)
  158. new_hero.set_network_master(net_id)
  159. new_hero.player_info = player_info
  160. get_node("/root/Level/Players").call_deferred("add_child", new_hero)
  161. # We must wait until after _ready is called, so that we don't end up at spawn
  162. new_hero.call_deferred("set_status", get_status())
  163. queue_free()
  164. func _exit_scene():
  165. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  166. # Functions
  167. # =========
  168. # Quits the game:
  169. func quit():
  170. get_tree().quit()