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.

160 lines
4.4 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. func _integrate_forces(state):
  47. if is_network_master():
  48. control_player(state)
  49. rset_unreliable("slave_transform", get_transform())
  50. rset_unreliable("slave_lin_v", get_linear_velocity())
  51. rset_unreliable("slave_ang_v", get_angular_velocity())
  52. else:
  53. set_transform(slave_transform)
  54. set_linear_velocity(slave_lin_v)
  55. set_angular_velocity(slave_ang_v)
  56. func control_player(state):
  57. # Default walk speed:
  58. walk_speed = 5
  59. # Default jump height:
  60. jump_speed = 3
  61. var aim = get_node("Yaw").get_global_transform().basis
  62. var direction = Vector3()
  63. if Input.is_action_pressed("move_forwards"):
  64. direction -= aim[2]
  65. if Input.is_action_pressed("move_backwards"):
  66. direction += aim[2]
  67. if Input.is_action_pressed("move_left"):
  68. direction -= aim[0]
  69. if Input.is_action_pressed("move_right"):
  70. direction += aim[0]
  71. direction = direction.normalized()
  72. var ray = get_node("Ray")
  73. # Increase walk speed and jump height while running and decrement stamina:
  74. if Input.is_action_pressed("run") and is_moving and ray.is_colliding():
  75. walk_speed *= 1.4
  76. jump_speed *= 1.2
  77. print("---")
  78. print(state.get_linear_velocity())
  79. if ray.is_colliding():
  80. var up = state.get_total_gravity().normalized()
  81. var normal = ray.get_collision_normal()
  82. var floor_velocity = Vector3()
  83. var object = ray.get_collider()
  84. if object is RigidBody or object is StaticBody:
  85. var point = ray.get_collision_point() - object.get_translation()
  86. var floor_angular_vel = Vector3()
  87. if object is RigidBody:
  88. floor_velocity = object.get_linear_velocity()
  89. floor_angular_vel = object.get_angular_velocity()
  90. elif object is StaticBody:
  91. floor_velocity = object.get_constant_linear_velocity()
  92. floor_angular_vel = object.get_constant_angular_velocity()
  93. # Surely there should be a function to convert Euler angles to a 3x3 matrix
  94. var tf = Basis(Vector3(1, 0, 0), floor_angular_vel.x)
  95. tf = tf.rotated(Vector3(0, 1, 0), floor_angular_vel.y)
  96. tf = tf.rotated(Vector3(0, 0, 1), floor_angular_vel.z)
  97. floor_velocity += tf.xform_inv(point) - point
  98. yaw = fmod(yaw + rad2deg(floor_angular_vel.y) * state.get_step(), 360)
  99. get_node("Yaw").set_rotation(Vector3(0, deg2rad(yaw), 0))
  100. print("isRB||isSB")
  101. var diff = floor_velocity + direction * walk_speed - state.get_linear_velocity()
  102. var vertdiff = aim[1] * diff.dot(aim[1])
  103. print("VD: " + str(vertdiff))
  104. diff -= vertdiff
  105. diff = diff.normalized() * clamp(diff.length(), 0, max_accel / state.get_step())
  106. diff += vertdiff
  107. print("D: " + str(diff))
  108. apply_impulse(Vector3(), diff * get_mass())
  109. if Input.is_action_pressed("jump"):
  110. apply_impulse(Vector3(), normal * jump_speed * get_mass())
  111. else:
  112. apply_impulse(Vector3(), direction * air_accel * get_mass())
  113. print(get_translation())
  114. print(state.get_linear_velocity())
  115. state.integrate_forces()
  116. func _exit_scene():
  117. Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
  118. # Functions
  119. # =========
  120. # Quits the game:
  121. func quit():
  122. get_tree().quit()