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.

121 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 velocity = Vector3()
  11. slave var slave_tf = Basis()
  12. slave var slave_vel = Vector3()
  13. var timer = 0
  14. # Walking speed and jumping height are defined later.
  15. var walk_speed = 3
  16. var jump_speed = 15
  17. var health = 100
  18. var stamina = 10000
  19. var ray_length = 10
  20. func _ready():
  21. set_process_input(true)
  22. # Capture mouse once game is started:
  23. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  24. #set_physics_process(true)
  25. get_node("Crosshair").set_text("+")
  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 ray = get_node("Ray")
  67. var friction
  68. if is_on_floor():
  69. friction = 1 - 0.16
  70. velocity.x *= friction
  71. velocity.z *= friction
  72. velocity.y = 0
  73. velocity += direction * walk_speed
  74. if Input.is_action_pressed("jump"):
  75. velocity.y += jump_speed
  76. else:
  77. friction = 1 - 0.01
  78. velocity.x *= friction
  79. velocity.z *= friction
  80. velocity.y += gravity
  81. velocity += direction * air_accel
  82. 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()