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.

152 lines
4.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 RigidBody
  5. var view_sensitivity = 0.25
  6. var yaw = 0
  7. var pitch = 0
  8. var timer = 0
  9. # Walking speed and jumping height are defined later.
  10. var walk_speed = 3
  11. var jump_speed = 15
  12. const air_accel = .5
  13. var health = 100
  14. var stamina = 10000
  15. var ray_length = 10
  16. var debug_node
  17. slave var slave_transform = Basis()
  18. slave var slave_lin_v = Vector3()
  19. slave var slave_ang_v = Vector3()
  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. debug_node = get_node("/root/world/Debug")
  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. master func _integrate_forces(state):
  46. control_player(state)
  47. rpc_unreliable("set_status", get_transform(), get_linear_velocity(), get_angular_velocity())
  48. slave func set_status(tf, lv, av):
  49. set_transform(tf)
  50. set_linear_velocity(lv)
  51. set_angular_velocity(av)
  52. func control_player(state):
  53. # Default walk speed:
  54. walk_speed = 5
  55. # Default jump height:
  56. jump_speed = 3
  57. var aim = get_node("Yaw").get_global_transform().basis
  58. var direction = Vector3()
  59. if Input.is_action_pressed("move_forwards"):
  60. direction -= aim[2]
  61. if Input.is_action_pressed("move_backwards"):
  62. direction += aim[2]
  63. if Input.is_action_pressed("move_left"):
  64. direction -= aim[0]
  65. if Input.is_action_pressed("move_right"):
  66. direction += aim[0]
  67. direction = direction.normalized()
  68. var ray = get_node("Ray")
  69. #print("---")
  70. if ray.is_colliding():
  71. var up = state.get_total_gravity().normalized()
  72. var normal = ray.get_collision_normal()
  73. var floor_velocity = Vector3()
  74. var object = ray.get_collider()
  75. if object is RigidBody or object is StaticBody:
  76. var point = ray.get_collision_point() - object.get_translation()
  77. var floor_angular_vel = Vector3()
  78. if object is RigidBody:
  79. floor_velocity = object.get_linear_velocity()
  80. floor_angular_vel = object.get_angular_velocity()
  81. elif object is StaticBody:
  82. floor_velocity = object.get_constant_linear_velocity()
  83. floor_angular_vel = object.get_constant_angular_velocity()
  84. # Surely there should be a function to convert Euler angles to a 3x3 matrix
  85. var tf = Basis(Vector3(1, 0, 0), floor_angular_vel.x)
  86. tf = tf.rotated(Vector3(0, 1, 0), floor_angular_vel.y)
  87. tf = tf.rotated(Vector3(0, 0, 1), floor_angular_vel.z)
  88. floor_velocity += tf.xform_inv(point) - point
  89. yaw = fmod(yaw + rad2deg(floor_angular_vel.y) * state.get_step(), 360)
  90. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  91. print("isRB||isSB")
  92. var diff = floor_velocity + direction * walk_speed - state.get_linear_velocity()
  93. var vertdiff = aim[1] * diff.dot(aim[1])
  94. print("VD: " + str(vertdiff))
  95. diff -= vertdiff
  96. diff = diff.normalized() * clamp(diff.length(), 0, max_accel / state.get_step())
  97. diff += vertdiff
  98. print("D: " + str(diff))
  99. apply_impulse(Vector3(), diff * get_mass())
  100. if Input.is_action_pressed("jump"):
  101. apply_impulse(Vector3(), normal * jump_speed * get_mass())
  102. else:
  103. apply_impulse(Vector3(), direction * air_accel * get_mass())
  104. print(get_translation())
  105. print(state.get_linear_velocity())
  106. var vel = get_linear_velocity()
  107. debug_node.set_text("%8.f,%8.f,%8.f" % [vel.x, vel.y, vel.z])
  108. state.integrate_forces()
  109. func _exit_scene():
  110. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  111. # Functions
  112. # =========
  113. # Quits the game:
  114. func quit():
  115. get_tree().quit()