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.

122 lines
2.8 KiB

  1. # Copyright (c) 2015 Calinou
  2. # This source code form is governed by the MIT license.
  3. # Original: https://raw.githubusercontent.com/Calinou/fps-test/master/scripts/player.gd
  4. extends KinematicBody
  5. var view_sensitivity = 0.25
  6. var yaw = 0
  7. var pitch = 0
  8. var gravity = -.8
  9. var velocity = Vector3()
  10. slave var slave_tf = Basis()
  11. slave var slave_vel = Vector3()
  12. var timer = 0
  13. # Walking speed and jumping height are defined later.
  14. var walk_speed = 3
  15. var jump_speed = 15
  16. const air_accel = .5
  17. var health = 100
  18. var stamina = 10000
  19. var ray_length = 10
  20. var debug_node
  21. func _ready():
  22. set_process_input(true)
  23. # Capture mouse once game is started:
  24. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  25. debug_node = get_node("/root/world/Debug")
  26. if is_network_master():
  27. get_node("Yaw/Camera").make_current()
  28. func _input(event):
  29. if is_network_master():
  30. if event is InputEventMouseMotion:
  31. yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
  32. pitch = max(min(pitch - event.relative.y * view_sensitivity, 85), -85)
  33. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  34. get_node("Yaw/Camera").set_rotation(Vector3(deg2rad(pitch), 0, 0))
  35. # Toggle mouse capture:
  36. if Input.is_action_pressed("toggle_mouse_capture"):
  37. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  38. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  39. view_sensitivity = 0
  40. else:
  41. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  42. view_sensitivity = 0.25
  43. # Quit the game:
  44. if Input.is_action_pressed("quit"):
  45. quit()
  46. func _physics_process(delta):
  47. if is_network_master():
  48. control_player(delta)
  49. rset_unreliable("slave_tf", get_transform())
  50. rset_unreliable("slave_vel", velocity)
  51. else:
  52. set_transform(slave_tf)
  53. move_and_slide(slave_vel)
  54. func control_player(delta):
  55. var aim = get_node("Yaw").get_global_transform().basis
  56. var direction = Vector3()
  57. if Input.is_action_pressed("move_forwards"):
  58. direction -= aim[2]
  59. if Input.is_action_pressed("move_backwards"):
  60. direction += aim[2]
  61. if Input.is_action_pressed("move_left"):
  62. direction -= aim[0]
  63. if Input.is_action_pressed("move_right"):
  64. direction += aim[0]
  65. direction = direction.normalized()
  66. var friction
  67. if is_on_floor():
  68. friction = 1 - 0.16
  69. velocity.x *= friction
  70. velocity.z *= friction
  71. velocity.y = 0
  72. velocity += direction * walk_speed
  73. if Input.is_action_pressed("jump"):
  74. velocity.y += jump_speed
  75. else:
  76. friction = 1 - 0.01
  77. velocity.x *= friction
  78. velocity.z *= friction
  79. velocity.y += gravity
  80. velocity += direction * air_accel
  81. debug_node.set_text("%8.f,%8.f,%8.f" % [velocity.x, velocity.y, velocity.z])
  82. velocity = move_and_slide(velocity, Vector3(0, 1, 0))
  83. func _exit_scene():
  84. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  85. # Functions
  86. # =========
  87. # Quits the game:
  88. func quit():
  89. get_tree().quit()