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.

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