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.

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