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.

174 lines
4.6 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.0015
  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. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  51. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  52. view_sensitivity = 0
  53. else:
  54. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  55. view_sensitivity = 0.25
  56. if Input.is_action_just_pressed("switch_hero"):
  57. switch_hero_interface()
  58. # Quit the game:
  59. if Input.is_action_pressed("quit"):
  60. quit()
  61. func set_rotation():
  62. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  63. get_node("Yaw/Pitch").set_rotation(Vector3(deg2rad(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. slave func set_status(s):
  69. set_transform(s[0])
  70. set_linear_velocity(s[1])
  71. set_angular_velocity(s[2])
  72. yaw = s[3]
  73. pitch = s[4]
  74. set_rotation() # Confirm yaw + pitch changes
  75. func get_status():
  76. return [
  77. get_transform(),
  78. get_linear_velocity(),
  79. get_angular_velocity(),
  80. yaw,
  81. 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. state.apply_impulse(Vector3(), direction * walk_speed * 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. state.apply_impulse(Vector3(), direction * air_accel * get_mass())
  110. var lin_v = state.get_linear_velocity()
  111. lin_v.x *= air_friction
  112. lin_v.z *= air_friction
  113. state.set_linear_velocity(lin_v)
  114. var vel = get_linear_velocity()
  115. switch_charge += movement_charge * vel.length()
  116. get_node("MasterOnly/SwitchCharge").set_text("%.f%%" % switch_charge)
  117. state.integrate_forces()
  118. func switch_hero_interface():
  119. # TODO: Make a real interface
  120. player_info.hero += 1
  121. rpc("switch_hero", player_info.hero)
  122. sync func switch_hero(hero):
  123. var new_hero = load("res://scenes/heroes/%d.tscn" % hero).instance()
  124. var net_id = get_tree().get_network_unique_id()
  125. set_name("%d-delete" % net_id) # Can't have duplicate names
  126. new_hero.set_name("%d" % net_id)
  127. new_hero.set_network_master(net_id)
  128. new_hero.player_info = player_info
  129. get_node("/root/world/players").call_deferred("add_child", new_hero)
  130. # We must wait until after _ready is called, so that we don't end up at spawn
  131. new_hero.call_deferred("set_status", get_status())
  132. queue_free()
  133. func _exit_scene():
  134. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  135. # Functions
  136. # =========
  137. # Quits the game:
  138. func quit():
  139. get_tree().quit()