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.

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