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.

199 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. # Walking speed and jumping height are defined later.
  5. var walk_speed = 0.8 # Actually acceleration; m/s/s
  6. var jump_speed = 3 # m/s
  7. var air_accel = .3 # 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 = 10
  34. var y_varies = 20
  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.y += rand_range(0, y_varies)
  43. set_transform(Basis())
  44. set_translation(placement)
  45. set_linear_velocity(Vector3())
  46. func _input(event):
  47. if is_network_master():
  48. if Input.is_action_just_pressed("switch_hero"):
  49. switch_hero_interface()
  50. # Quit the game:
  51. if Input.is_action_pressed("quit"):
  52. quit()
  53. func toggle_mouse_capture():
  54. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  55. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  56. view_sensitivity = 0
  57. else:
  58. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  59. view_sensitivity = 0.25
  60. # Update visual yaw + pitch components to match camera
  61. func set_rotation():
  62. get_node("Yaw").set_rotation(Vector3(0, deg2rad(get_node(tp_camera).cam_yaw), 0))
  63. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(-get_node(tp_camera).cam_pitch), 0, 0))
  64. func _integrate_forces(state):
  65. if is_network_master():
  66. control_player(state)
  67. rpc_unreliable("set_status", get_status())
  68. set_rotation()
  69. slave func set_status(s):
  70. set_transform(s[0])
  71. set_linear_velocity(s[1])
  72. set_angular_velocity(s[2])
  73. get_node(tp_camera).cam_yaw = s[3]
  74. get_node(tp_camera).cam_pitch = s[4]
  75. func get_status():
  76. return [
  77. get_transform(),
  78. get_linear_velocity(),
  79. get_angular_velocity(),
  80. get_node(tp_camera).cam_yaw,
  81. get_node(tp_camera).cam_pitch,
  82. ]
  83. func control_player(state):
  84. var aim = get_node("Yaw").get_global_transform().basis
  85. var direction = Vector3()
  86. if Input.is_action_pressed("move_forwards"):
  87. direction -= aim[2]
  88. if Input.is_action_pressed("move_backwards"):
  89. direction += aim[2]
  90. if Input.is_action_pressed("move_left"):
  91. direction -= aim[0]
  92. if Input.is_action_pressed("move_right"):
  93. direction += aim[0]
  94. direction = direction.normalized()
  95. var ray = get_node("Ray")
  96. if ray.is_colliding():
  97. var up = state.get_total_gravity().normalized()
  98. var normal = ray.get_collision_normal()
  99. var floor_velocity = Vector3()
  100. var object = ray.get_collider()
  101. var accel = (1 + switch_charge * walk_speed_build) * walk_speed
  102. state.apply_impulse(Vector3(), direction * accel * get_mass())
  103. var lin_v = state.get_linear_velocity()
  104. lin_v.x *= floor_friction
  105. lin_v.z *= floor_friction
  106. state.set_linear_velocity(lin_v)
  107. if Input.is_action_pressed("jump"):
  108. state.apply_impulse(Vector3(), normal * jump_speed * get_mass())
  109. else:
  110. var accel = (1 + switch_charge * air_speed_build) * air_accel
  111. state.apply_impulse(Vector3(), direction * 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. if is_network_master():
  120. var vel = get_linear_velocity()
  121. switch_charge += movement_charge * vel.length() * delta
  122. var switch_node = get_node("MasterOnly/SwitchCharge")
  123. switch_node.set_text("%.f%%" % switch_charge)
  124. if switch_charge >= 100:
  125. # Let switch_charge keep building, because we use it for walk_speed and things
  126. switch_node.set_text("100%% (%.f)\nQ - Switch hero" % switch_charge)
  127. if switch_charge > switch_charge_cap:
  128. # There is however a cap
  129. switch_charge = switch_charge_cap
  130. if get_translation().y < fall_height:
  131. spawn()
  132. switch_hero_interface()
  133. func switch_hero_interface():
  134. # Interface needs the mouse!
  135. toggle_mouse_capture()
  136. # Pause so if we have walls and such nothing funny happens
  137. get_tree().set_pause(true)
  138. var interface = preload("res://scenes/HeroSelect.tscn").instance()
  139. add_child(interface)
  140. interface.get_node("Confirm").connect("pressed", self, "switch_hero_master")
  141. func switch_hero_master():
  142. rpc("switch_hero", get_node("HeroSelect/Hero").get_selected_id())
  143. # Remove the mouse and enable looking again
  144. toggle_mouse_capture()
  145. get_tree().set_pause(false)
  146. sync func switch_hero(hero):
  147. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  148. var net_id = int(get_name())
  149. set_name("%d-delete" % net_id) # Can't have duplicate names
  150. new_hero.set_name("%d" % net_id)
  151. new_hero.set_network_master(net_id)
  152. new_hero.player_info = player_info
  153. get_node("/root/Level/Players").call_deferred("add_child", new_hero)
  154. # We must wait until after _ready is called, so that we don't end up at spawn
  155. new_hero.call_deferred("set_status", get_status())
  156. queue_free()
  157. func _exit_scene():
  158. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  159. # Functions
  160. # =========
  161. # Quits the game:
  162. func quit():
  163. get_tree().quit()