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.

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