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.

135 lines
3.2 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 weight = .1
  18. var health = 100
  19. var stamina = 10000
  20. var ray_length = 10
  21. var debug_node
  22. func _ready():
  23. set_process_input(true)
  24. # Capture mouse once game is started:
  25. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  26. debug_node = get_node("/root/world/Debug")
  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 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. # Just for testing TODO
  83. if Input.is_action_pressed("jump"):
  84. velocity.y += jump_speed * 0.1
  85. debug_node.set_text("%8.f,%8.f,%8.f" % [velocity.x, velocity.y, velocity.z])
  86. velocity = 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. var rel_pos = collision.position - collision.collider.get_translation()
  91. debug_node.set_text("%8.f,%8.f,%8.f" % [rel_pos.x, rel_pos.y, rel_pos.z])
  92. collision.collider.apply_impulse(collision.position, Vector3(0, -weight, 0))
  93. func _exit_scene():
  94. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  95. # Functions
  96. # =========
  97. # Quits the game:
  98. func quit():
  99. get_tree().quit()