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.

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