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.

131 lines
3.0 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 weight = 1
  18. var health = 100
  19. var stamina = 10000
  20. var ray_length = 10
  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. #set_physics_process(true)
  26. get_node("Crosshair").set_text("+")
  27. if is_network_master():
  28. get_node("Yaw/Camera").make_current()
  29. func _input(event):
  30. if is_network_master():
  31. if event is InputEventMouseMotion:
  32. yaw = fmod(yaw - event.relative.x * view_sensitivity, 360)
  33. pitch = max(min(pitch - event.relative.y * view_sensitivity, 85), -85)
  34. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  35. get_node("Yaw/Camera").set_rotation(Vector3(deg2rad(pitch), 0, 0))
  36. # Toggle mouse capture:
  37. if Input.is_action_pressed("toggle_mouse_capture"):
  38. if (Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED):
  39. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  40. view_sensitivity = 0
  41. else:
  42. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  43. view_sensitivity = 0.25
  44. # Quit the game:
  45. if Input.is_action_pressed("quit"):
  46. quit()
  47. func _physics_process(delta):
  48. if is_network_master():
  49. control_player(delta)
  50. rset_unreliable("slave_tf", get_transform())
  51. rset_unreliable("slave_vel", velocity)
  52. else:
  53. set_transform(slave_tf)
  54. move_and_slide(slave_vel)
  55. func control_player(delta):
  56. var aim = get_node("Yaw").get_global_transform().basis
  57. var direction = Vector3()
  58. if Input.is_action_pressed("move_forwards"):
  59. direction -= aim[2]
  60. if Input.is_action_pressed("move_backwards"):
  61. direction += aim[2]
  62. if Input.is_action_pressed("move_left"):
  63. direction -= aim[0]
  64. if Input.is_action_pressed("move_right"):
  65. direction += aim[0]
  66. direction = direction.normalized()
  67. var ray = get_node("Ray")
  68. var friction
  69. if is_on_floor():
  70. friction = 1 - 0.16
  71. velocity.x *= friction
  72. velocity.z *= friction
  73. velocity.y = 0
  74. velocity += direction * walk_speed
  75. if Input.is_action_pressed("jump"):
  76. velocity.y += jump_speed
  77. else:
  78. friction = 1 - 0.01
  79. velocity.x *= friction
  80. velocity.z *= friction
  81. velocity.y += gravity
  82. velocity += direction * air_accel
  83. # Just for testing TODO
  84. if Input.is_action_pressed("jump"):
  85. velocity.y += jump_speed * 0.1
  86. move_and_slide(velocity, Vector3(0, 1, 0))
  87. for i in range(get_slide_count()):
  88. var collision = get_slide_collision(i)
  89. if collision.collider.is_in_group("objective"):
  90. collision.collider.apply_impulse(collision.position, Vector3(0, -weight, 0))
  91. func _exit_scene():
  92. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  93. # Functions
  94. # =========
  95. # Quits the game:
  96. func quit():
  97. get_tree().quit()