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.

200 lines
5.9 KiB

  1. # Original: https://raw.githubusercontent.com/Calinou/fps-test/master/scripts/player.gd
  2. extends RigidBody
  3. var view_sensitivity = 0.25
  4. # Walking speed and jumping height are defined later.
  5. var walk_speed = 0.8 # Actually acceleration; m/s/s
  6. var jump_speed = 1 # m/s
  7. var air_accel = .1 # m/s/s
  8. var floor_friction = 1-0.08
  9. var air_friction = 1-0.03
  10. var player_info # Set by lobby
  11. var walk_speed_build = 0.006 # `walk_speed` per `switch_charge`
  12. var air_speed_build = 0.006 # `air_accel` per `switch_chare`
  13. var switch_charge = 0
  14. var switch_charge_cap = 200 # While switching is always at 100, things like speed boost might go higher!
  15. var movement_charge = 0.15 # In percent per meter (except when heroes change that)
  16. const fall_height = -50
  17. var debug_node
  18. slave var slave_transform = Basis()
  19. slave var slave_lin_v = Vector3()
  20. slave var slave_ang_v = Vector3()
  21. export(NodePath) var tp_camera
  22. export(NodePath) var master_only
  23. func _ready():
  24. set_process_input(true)
  25. debug_node = get_node("/root/Level/Debug")
  26. if is_network_master():
  27. get_node(tp_camera).set_enabled(true)
  28. spawn()
  29. else:
  30. remove_child(get_node(master_only))
  31. func spawn():
  32. var placement = Vector3()
  33. var x_varies = 5
  34. var z_varies = 5
  35. # No Z, because that's the left-right question
  36. if player_info.is_right_team:
  37. placement = get_node("/root/Level/RightSpawn").get_translation()
  38. else:
  39. placement = get_node("/root/Level/LeftSpawn").get_translation()
  40. # So we don't all spawn on top of each other
  41. placement.x += rand_range(0, x_varies)
  42. placement.z += rand_range(0, z_varies)
  43. recording.spawn = placement
  44. set_transform(Basis())
  45. set_translation(placement)
  46. set_linear_velocity(Vector3())
  47. func _input(event):
  48. if is_network_master():
  49. if Input.is_action_just_pressed("switch_hero"):
  50. switch_hero_interface()
  51. # Quit the game:
  52. if Input.is_action_pressed("quit"):
  53. quit()
  54. func toggle_mouse_capture():
  55. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  56. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  57. view_sensitivity = 0
  58. else:
  59. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  60. view_sensitivity = 0.25
  61. # Update visual yaw + pitch components to match camera
  62. func set_rotation():
  63. get_node("Yaw").set_rotation(Vector3(0, deg2rad(get_node(tp_camera).cam_yaw), 0))
  64. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(-get_node(tp_camera).cam_pitch), 0, 0))
  65. func _integrate_forces(state):
  66. if is_network_master():
  67. control_player(state)
  68. rpc_unreliable("set_status", get_status())
  69. set_rotation()
  70. slave func set_status(s):
  71. set_transform(s[0])
  72. set_linear_velocity(s[1])
  73. set_angular_velocity(s[2])
  74. get_node(tp_camera).cam_yaw = s[3]
  75. get_node(tp_camera).cam_pitch = s[4]
  76. func get_status():
  77. return [
  78. get_transform(),
  79. get_linear_velocity(),
  80. get_angular_velocity(),
  81. get_node(tp_camera).cam_yaw,
  82. get_node(tp_camera).cam_pitch,
  83. ]
  84. func control_player(state):
  85. var aim = get_node("Yaw").get_global_transform().basis
  86. var direction = Vector3()
  87. if Input.is_action_pressed("move_forwards"):
  88. direction -= aim[2]
  89. if Input.is_action_pressed("move_backwards"):
  90. direction += aim[2]
  91. if Input.is_action_pressed("move_left"):
  92. direction -= aim[0]
  93. if Input.is_action_pressed("move_right"):
  94. direction += aim[0]
  95. direction = direction.normalized()
  96. var ray = get_node("Ray")
  97. if ray.is_colliding():
  98. var up = state.get_total_gravity().normalized()
  99. var normal = ray.get_collision_normal()
  100. var floor_velocity = Vector3()
  101. var object = ray.get_collider()
  102. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  103. state.apply_impulse(Vector3(), direction * accel * 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. var accel = (1 + switch_charge * air_speed_build) * air_accel
  112. state.apply_impulse(Vector3(), direction * accel * get_mass())
  113. var lin_v = state.get_linear_velocity()
  114. lin_v.x *= air_friction
  115. lin_v.z *= air_friction
  116. state.set_linear_velocity(lin_v)
  117. state.integrate_forces()
  118. func _process(delta):
  119. # All player code not caused by input, and not causing movement
  120. if is_network_master():
  121. var vel = get_linear_velocity()
  122. switch_charge += movement_charge * vel.length() * delta
  123. var switch_node = get_node("MasterOnly/SwitchCharge")
  124. switch_node.set_text("%.f%%" % switch_charge)
  125. if switch_charge >= 100:
  126. # Let switch_charge keep building, because we use it for walk_speed and things
  127. switch_node.set_text("100%% (%.f)\nQ - Switch hero" % switch_charge)
  128. if switch_charge > switch_charge_cap:
  129. # There is however a cap
  130. switch_charge = switch_charge_cap
  131. if get_translation().y < fall_height:
  132. spawn()
  133. switch_hero_interface()
  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 = int(get_name())
  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/Level/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()