Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. extends KinematicBody
  2.  
  3. var view_angle: float = 0.0
  4. var sensitivity: float = 0.2
  5.  
  6. const GRAVITY: float = -9.8 * 2.6
  7. const MAX_SPEED: float = 16.0
  8. const MAX_RUNNING_SPEED: float = 22.0
  9.  
  10. var can_freeze: bool = false
  11. var accel: float = 5.4
  12. var deaccel: float = 1
  13. var speed: float = 8.0
  14. var jump_height: float = 8
  15.  
  16. var movement = Vector3(0, 0, 0)
  17. var looking = Vector3(0, 0, 0)
  18.  
  19. # Coyote Timers
  20. # [ Current Time, Starting Value, Optional Values ]
  21. var coyote_landing = [0.0, 0.4]
  22. var coyote_cliff = [0.0, 0.65]
  23. var wacky_time = [0.0, 0.33]
  24.  
  25. func jir(action: String) -> bool:
  26. return Input.is_action_just_released(action)
  27.  
  28. func jiv(action: String) -> bool:
  29. return Input.is_action_just_pressed(action)
  30.  
  31. func civ(action: String) -> bool:
  32. return Input.is_action_pressed(action)
  33.  
  34. func _ready() -> void:
  35. Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  36. Input.set_custom_mouse_cursor(load("res://static/cursor.png"), 0)
  37.  
  38. func _process(delta: float) -> void:
  39. if jiv("exit"):
  40. get_tree().quit()
  41. elif jiv("restart"):
  42. get_tree().reload_current_scene()
  43.  
  44. looking = Vector3(0, 0, 0)
  45.  
  46. var perspective = $Head/Camera.get_camera_transform().basis
  47. if civ("forward"): looking -= perspective.z
  48. if civ("backward"): looking += perspective.z
  49.  
  50. if civ("right"): looking += perspective.x
  51. if civ("left"): looking -= perspective.x
  52.  
  53. looking = looking.normalized()
  54.  
  55. if civ("timefreeze") and can_freeze:
  56. wacky_time[0] -= delta
  57. if wacky_time[0] < 0:
  58. wacky_time[0] = 0
  59. can_freeze = false
  60. Engine.time_scale = 1
  61. else:
  62. Engine.time_scale = 0.2
  63.  
  64. elif not civ("timefreeze"):
  65. wacky_time[0] += 0.02
  66. if wacky_time[0] > wacky_time[1]:
  67. wacky_time[0] = wacky_time[1]
  68. can_freeze = true
  69. Engine.time_scale = 1
  70.  
  71. coyote_cliff[0] -= delta
  72. if is_on_floor():
  73. coyote_cliff[0] = coyote_cliff[1]
  74.  
  75. coyote_landing[0] -= delta
  76. if jiv("jump"):
  77. coyote_landing[0] = coyote_landing[1]
  78.  
  79. if coyote_landing[0] > 0 and coyote_cliff[0] > 0:
  80. coyote_landing[0] = 0
  81. coyote_cliff[0] = 0
  82. movement.y = jump_height
  83.  
  84. if not is_on_floor(): movement.y += GRAVITY * delta
  85.  
  86. var current_speed: float
  87. if civ("sprint"):
  88. current_speed = MAX_RUNNING_SPEED
  89. else:
  90. current_speed = MAX_SPEED
  91.  
  92. var true_movement = movement
  93. true_movement.y = 0
  94.  
  95. if jiv("turn"):
  96. $Head.rotate_y(deg2rad(180))
  97.  
  98. var current_acceleration
  99. if looking.dot(true_movement) > 0:
  100. current_acceleration = accel
  101. else:
  102. current_acceleration = deaccel
  103.  
  104. var target: Vector3 = looking * speed
  105. true_movement = movement.linear_interpolate(target, accel*delta)
  106.  
  107. movement.x = true_movement.x
  108. movement.z = true_movement.z
  109.  
  110. movement = move_and_slide(movement, Vector3.UP)
  111.  
  112.  
  113. func _input(event: InputEvent) -> void:
  114. if event is InputEventMouseMotion:
  115. var horizontal = -event.relative.x * sensitivity
  116. var vertical = -event.relative.y * sensitivity
  117.  
  118. $Head.rotate_y(deg2rad(horizontal))
  119. if vertical + view_angle < 90 and vertical + view_angle > -90:
  120. view_angle += vertical
  121. $Head/Camera.rotate_x(deg2rad(vertical))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement