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.

120 lines
2.7 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. const air_accel = 0.02
  9. var gravity = -1
  10. var tf = Basis()
  11. var velocity = 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. var health = 100
  17. var stamina = 10000
  18. var ray_length = 10
  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. #set_physics_process(true)
  24. get_node("Crosshair").set_text("+")
  25. if is_network_master():
  26. get_node("Yaw/Camera").make_current()
  27. func _input(event):
  28. if is_network_master():
  29. if event is InputEventMouseMotion:
  30. yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
  31. pitch = max(min(pitch - event.relative.y * view_sensitivity, 85), -85)
  32. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  33. get_node("Yaw/Camera").set_rotation(Vector3(deg2rad(pitch), 0, 0))
  34. # Toggle mouse capture:
  35. if Input.is_action_pressed("toggle_mouse_capture"):
  36. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  37. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  38. view_sensitivity = 0
  39. else:
  40. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  41. view_sensitivity = 0.25
  42. # Quit the game:
  43. if Input.is_action_pressed("quit"):
  44. quit()
  45. func _physics_process(delta):
  46. if is_network_master():
  47. control_player(delta)
  48. rset_unreliable("tf", get_transform())
  49. rset_unreliable("velocity", velocity)
  50. else:
  51. set_transform(tf)
  52. move_and_slide(velocity)
  53. func control_player(delta):
  54. var aim = get_node("Yaw").get_global_transform().basis
  55. var direction = Vector3()
  56. if Input.is_action_pressed("move_forwards"):
  57. direction -= aim[2]
  58. if Input.is_action_pressed("move_backwards"):
  59. direction += aim[2]
  60. if Input.is_action_pressed("move_left"):
  61. direction -= aim[0]
  62. if Input.is_action_pressed("move_right"):
  63. direction += aim[0]
  64. direction = direction.normalized()
  65. var ray = get_node("Ray")
  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. move_and_slide(velocity, Vector3(0, 1, 0))
  82. func _exit_scene():
  83. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  84. # Functions
  85. # =========
  86. # Quits the game:
  87. func quit():
  88. get_tree().quit()