Advertisement
CirilXD

Godot 3D Real Time Rope Mesh Generator V.0

Feb 8th, 2024 (edited)
1,078
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 25.73 KB | None | 0 0
  1. #contains unused code
  2. #also split between 2 scripts: first one is character controller and second one rope generation
  3. #PLAYER MOVEMENT SCRIPT------------------
  4. extends CharacterBody3D
  5.  
  6. enum WalkState{
  7.     NORMAL,
  8.     SPRINT,
  9.     CROUCH,
  10.     PRONE,
  11.     SLIDE,
  12.     JETPACK
  13. }
  14.  
  15. #movement attribute values
  16. const TARGET_LERP = .8
  17. const JETPACK_TARGER_LERP = .6
  18.  
  19. const SPRINT_SPEED = 10.0
  20. const SPRINT_LERP_ACC = 2.0
  21. const SPRINT_LERP_DEC = 8.0
  22.  
  23. const WALK_SPEED = 6.0
  24. const WALK_LERP_ACC = 3.5
  25. const WALK_LERP_DEC = 10.0
  26.  
  27. const CROUCH_SPEED = 3.0
  28. const CROUCH_LERP_ACC = 8.0
  29. const CROUCH_LERP_DEC = 14.0
  30.  
  31. const PRONE_SPEED = 1.5
  32. const PRONE_LERP_ACC = 12.0
  33. const PRONE_LERP_DEC = 22.0
  34.  
  35. const JETPACK_SPEED = 6.0
  36. const JETPACK_LERP_ACC = 6.0
  37. const JETPACK_LERP_DEC = 12.0
  38. const JETPACK_VERTICAL_FORCE = 5.0
  39. const JETPACK_VERTICAL_VELOCITY_MAX = 2.5
  40. const JETPACK_VERTICAL_DIFFERENCE_MAX = 5.0
  41. var current_jetpack_vertical = 0.0
  42. const JETPACK_VELOCITY_THRESHOLD = .5
  43. const ENERGY_MAX = 100.0
  44. const ENERGY_DRAIN_JETPACK_START = 10
  45. const ENERGY_DRAIN_JETPACK_MULT = 10.0
  46. const ENERGY_DRAIN_SPRINT_MULT = 5.0
  47. const ENERGY_REGEN_MULT = 30.0
  48. const ENERGY_REGEN_COOLDOWN = 2.5
  49. var current_energy_regen_cooldown = 0.0
  50. var current_energy = ENERGY_MAX
  51. const JETPACK_START_TIMER = 0.65
  52. var current_jetpack_start_timer = 0
  53.  
  54. const SLIDE_SPEED = 30
  55. const SLIDE_TIME_MAX = .7
  56. const SLIDE_DAMPEN_RATE = .05
  57. const SLIDE_FLAT_DAMPEN_RATE = .001
  58. const SLOPE_SLIDE_THRESHOLD = .1
  59. var current_slide_time = 0
  60. var current_slide_vector : Vector3 = Vector3.ZERO
  61.  
  62. var SPRINT_CD_MAX = .2
  63. var current_sprint_cd = 0
  64.  
  65. const FALL_SPEED_MAX = 15
  66. const JUMP_VELOCITY = 6
  67. const JUMP_HANG_TIME_THRESHOLD = .3
  68. const JUMP_HANG_TIME_SPEED_MULT = 1.05
  69. const JUMP_HANG_TIME_ACC_MULT = 3
  70.  
  71. const COYOTE_TIME_MAX = .1
  72. var current_coyote_time = 0
  73. const JUMP_CD_MAX = .25
  74. var current_jump_cd = 0
  75.  
  76.  
  77.  
  78. # Get the gravity from the project settings to be synced with RigidBody nodes.
  79. var gravity_default = ProjectSettings.get_setting("physics/3d/default_gravity")
  80. var gravity_falling = 2 * gravity_default
  81. var gravity_hang_time = .5 * gravity_default
  82. var current_gravity = gravity_default
  83.  
  84. @export var camera_sensitivity = .25
  85.  
  86. var input_dir = Vector2.ZERO
  87. var direction = Vector3.ZERO
  88.  
  89. @onready var collider = $Collider
  90. const COLLIDER_HEIGHT_NORMAL = 2
  91. const COLLIDER_HEIGHT_CROUCH = 1.1
  92. const COLLIDER_HEIGHT_PRONE = .5
  93.  
  94. const CAMERA_HEIGHT_NORMAL = 1.8
  95. const CAMERA_HEIGHT_CROUCH = .9
  96. const CAMERA_HEIGHT_PRONE = .3
  97.  
  98. const HEAD_BOB_INTENSITY_SPRINT = .15
  99. const HEAD_BOB_INTENSITY_NORMAL = .12
  100. const HEAD_BOB_INTENSITY_CROUCH = .08
  101. const HEAD_BOB_INTENSITY_PRONE = .04
  102.  
  103. const HEAD_BOB_FREQUENCY_SPRINT = 18
  104. const HEAD_BOB_FREQUENCY_NORMAL = 14
  105. const HEAD_BOB_FREQUENCY_CROUCH = 10
  106. const HEAD_BOB_FREQUENCY_PRONE = 8
  107.  
  108. const HEAD_BOB_LERP_RATE = 50
  109.  
  110. var head_bob_var = 0
  111. var is_head_bob_active = true
  112. var current_head_bob_intensity = 0
  113. var current_head_bob_frequency = 0
  114.  
  115. const CAMERA_LERP = 10
  116. const CAMERA_FOV_NORMAL = 70.0
  117. const CAMERA_FOV_MAX_SPEED = 100.0
  118.  
  119. @onready var camera_pivot = $CameraPivot
  120. @onready var debug_label = $DebugLabel
  121. @onready var energy_bar = $EnergyBar
  122. @onready var energy_bar_bar = $EnergyBar/Bar
  123. @onready var energy_bar_label = $EnergyBar/Bar/Label
  124. @onready var height_raycast = $HeightRaycast
  125. @onready var camera_3d = $CameraPivot/HeadBobPivot/Camera3D
  126. @onready var head_bob_pivot = $CameraPivot/HeadBobPivot
  127. @onready var jetpack_stream = $jetpackStream
  128. @onready var camera_cast = $CameraPivot/HeadBobPivot/Camera3D/CameraCast
  129.  
  130. var current_max_speed : float = 0
  131. var current_lerp_acc : float = 0
  132. var current_lerp_dec : float = 0
  133. var current_camera_height : float = 0
  134.  
  135.  
  136. const JETPACK_VOLUME_LERP_MULT = 5
  137. const JETPACK_STREAM_STOP_THRESHOLD = .01
  138.  
  139. var current_walk_state : WalkState = WalkState.NORMAL
  140.  
  141.  
  142. var grapple_hook_position : Vector3 = Vector3.ZERO
  143. var grapple_raycast_hit = null
  144. const GRAPPLE_RAY_MAX = 50.0
  145. const GRAPPLE_FORCE_MAX = 75.0
  146. var is_grappling = false
  147. @onready var grapple_mesh = $RopePivot/mesh
  148. @onready var grapple_pivot = $RopePivot
  149.  
  150. @onready var rope_generator = $RopeGenerator
  151.  
  152.  
  153.  
  154. var floor_angle
  155.  
  156. var debug = null
  157. var debug1 = null
  158. var debug2 = null
  159. var debug3 = null
  160.  
  161. func _ready():
  162.     Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
  163.     _UpdateCollider()
  164.     debug =  get_tree().root.get_node("ROOT/raycasts")
  165.     debug1 = debug.get_node("DebugRaycast")
  166.     debug2 = debug.get_node("DebugRaycast2")
  167.     debug3 = debug.get_node("DebugRaycast3")
  168.     floor_angle = 0
  169.    
  170.     camera_cast.set_target_position(Vector3(0, 0, -1 * GRAPPLE_RAY_MAX))
  171.    
  172.     energy_bar_bar.min_value = 0
  173.     energy_bar_bar.max_value = ENERGY_MAX
  174.     _UpdateEnergyLabel()
  175.    
  176. func _input(event):
  177.     if event is InputEventMouseMotion:
  178.         rotate_y(deg_to_rad(camera_sensitivity * -event.relative.x))
  179.         camera_pivot.rotate_x(deg_to_rad(camera_sensitivity * -event.relative.y))
  180.         camera_pivot.rotation.x = clamp(camera_pivot.rotation.x, deg_to_rad(-89), deg_to_rad(89))
  181.        
  182.        
  183. func _process(delta):
  184.     input_dir = Input.get_vector("left", "right", "forward", "back")
  185.  
  186.     direction = transform.basis * Vector3(input_dir.x, 0, input_dir.y).normalized()
  187.  
  188.     if WalkState.SLIDE != current_walk_state && WalkState.JETPACK != current_walk_state:
  189.         if Input.is_action_pressed("prone") && is_on_floor():
  190.             if current_walk_state != WalkState.PRONE:
  191.                 current_walk_state = WalkState.PRONE
  192.                 _UpdateCollider()
  193.         elif Input.is_action_pressed("crouch") and !height_raycast.is_colliding() && is_on_floor():
  194.             if current_walk_state != WalkState.CROUCH:
  195.                 if current_sprint_cd > 0:
  196.                     current_walk_state = WalkState.SLIDE
  197.                     current_slide_time = SLIDE_TIME_MAX
  198.                     current_slide_vector = abs(velocity) * direction
  199.                     current_slide_vector.y = 0
  200.                     _UpdateCollider()
  201.                 else:
  202.                     current_walk_state = WalkState.CROUCH
  203.                     _UpdateCollider()
  204.         elif !height_raycast.is_colliding():
  205.             if current_walk_state == WalkState.PRONE:
  206.                 current_walk_state = WalkState.CROUCH
  207.                 _UpdateCollider()
  208.             elif Input.is_action_pressed("sprint"):
  209.                 current_sprint_cd = SPRINT_CD_MAX
  210.                 if current_walk_state != WalkState.SPRINT:
  211.                     current_walk_state = WalkState.SPRINT
  212.                     _UpdateCollider()
  213.             elif current_walk_state != WalkState.NORMAL:
  214.                 current_walk_state = WalkState.NORMAL
  215.                 _UpdateCollider()
  216.                
  217.     floor_angle = get_floor_angle()
  218.  
  219.    
  220.     _UpdateEnergyLabel()
  221.  
  222.  
  223. func _physics_process(delta):
  224.     grapple_raycast_hit = camera_cast.get_collider()
  225.     if grapple_raycast_hit:
  226.         $ColorRect.color = "#DDDDDD99"
  227.     else:
  228.         $ColorRect.color = "#22222255"
  229.     if Input.is_action_just_pressed("grapple"):
  230.         if grapple_raycast_hit:
  231.             grapple_hook_position = camera_cast.get_collision_point()
  232.             is_grappling = true
  233.             grapple_mesh.visible = true
  234.             rope_generator.SetPlayerPosition(position)
  235.             rope_generator.SetGrappleHookPosition(grapple_hook_position)
  236.             rope_generator.StartDrawing()
  237.             rope_generator.visible = true
  238.         else:
  239.             is_grappling = false
  240.     elif Input.is_action_just_released("grapple"):
  241.         grapple_mesh.visible = false
  242.         rope_generator.StopDrawing()
  243.         rope_generator.visible = false
  244.  
  245.  
  246.     if is_grappling && Input.is_action_pressed("grapple"):
  247.         rope_generator.SetPlayerPosition(position)
  248.         $ColorRect.color = "#FF555599"
  249.         grapple_pivot.look_at(grapple_hook_position)
  250.        
  251.         var grapple_direction = (grapple_hook_position - position).normalized()
  252.         var grapple_target_speed = grapple_direction * GRAPPLE_FORCE_MAX
  253.        
  254.         var grapple_dif = (grapple_target_speed - velocity)
  255. #       grapple_dif = lerp(velocity, grapple_dif, .8)
  256.        
  257.         velocity += grapple_dif * delta
  258. #       current_gravity = 0
  259.        
  260. #       if position.y < grapple_hook_position.y:
  261. #           current_gravity = -gravity_hang_time
  262. #       else:
  263. #           current_gravity = gravity_default
  264.  
  265.     if WalkState.SLIDE == current_walk_state:
  266.         if current_slide_time > 0:
  267.             if floor_angle < SLOPE_SLIDE_THRESHOLD || velocity.y > 0:
  268.                 current_slide_time -= delta
  269.                 current_slide_time = clamp(current_slide_time, 0, SLIDE_TIME_MAX)
  270.            
  271.         else:
  272.             current_walk_state = WalkState.CROUCH
  273.             _UpdateCollider()
  274.    
  275.     if current_sprint_cd > 0:
  276.         current_sprint_cd -= delta
  277.  
  278.     #calc target speed based on current input
  279.     var target_speed : Vector3 = direction * current_max_speed
  280.  
  281.     var current_acc_rate : Vector3 = Vector3.ZERO
  282.     if input_dir:
  283.         current_acc_rate = Vector3(current_lerp_acc,
  284.                 0,
  285.                 current_lerp_acc)
  286.     else:
  287.         current_acc_rate = Vector3(current_lerp_dec,
  288.             0,
  289.             current_lerp_dec)
  290.    
  291.    
  292.     #lerp the target speed for smoother change
  293.     #if the movement is in the same direction make the target closer to the current velocity
  294.     #otherwise, since direction shift is required make the target closer to actual target
  295.     #if player is faster than top speed don't slow down on X and Z axis
  296.     if (target_speed.x != 0 &&
  297.         abs(velocity.x) >= abs(target_speed.x) &&
  298.         sign(velocity.x) == sign(target_speed.x)):
  299.            
  300.         target_speed.x = lerp(velocity.x, target_speed.x, 1-TARGET_LERP)
  301.     else:
  302.         target_speed.x = lerp(velocity.x, target_speed.x, TARGET_LERP)
  303.        
  304.     if (target_speed.z != 0 &&
  305.         abs(velocity.z) >= abs(target_speed.z) &&
  306.         sign(velocity.z) == sign(target_speed.z)):
  307.  
  308.         target_speed.z = lerp(velocity.z, target_speed.z, 1-TARGET_LERP)
  309.     else:
  310.         target_speed.z = lerp(velocity.z, target_speed.z, TARGET_LERP)
  311.    
  312.     #Handle jetpack
  313.     if Input.is_action_pressed("jump"):
  314.         current_jetpack_start_timer += delta
  315.         current_jetpack_start_timer = min(current_jetpack_start_timer, JETPACK_START_TIMER)
  316.     elif Input.is_action_just_released("jump"):
  317.         current_jetpack_start_timer = 0
  318.        
  319.     if current_walk_state != WalkState.JETPACK:
  320.         if current_energy_regen_cooldown >= ENERGY_REGEN_COOLDOWN:
  321.             current_energy += delta * ENERGY_REGEN_MULT
  322.             current_energy = min(current_energy, ENERGY_MAX)
  323.         else:
  324.             current_energy_regen_cooldown += delta
  325.        
  326.         if current_energy > ENERGY_DRAIN_JETPACK_START && (
  327.             current_jetpack_start_timer >= JETPACK_START_TIMER ||
  328.             (Input.is_action_just_pressed("jump") &&
  329.             abs(velocity.y) > JETPACK_VELOCITY_THRESHOLD)):
  330.             current_walk_state = WalkState.JETPACK
  331.             _UpdateCollider()
  332.             jetpack_stream.volume_db = linear_to_db(0.00)
  333.             jetpack_stream.play()
  334.             current_energy -= ENERGY_DRAIN_JETPACK_START
  335.  
  336.     if current_walk_state != WalkState.JETPACK:
  337.         if jetpack_stream.playing:
  338.             jetpack_stream.volume_db = linear_to_db(lerp(
  339.                 db_to_linear(jetpack_stream.volume_db),
  340.                 0.0,
  341.                 JETPACK_VOLUME_LERP_MULT * delta
  342.             ))
  343.             if linear_to_db(JETPACK_STREAM_STOP_THRESHOLD) >= jetpack_stream.volume_db:
  344.                 jetpack_stream.volume_db = linear_to_db(0)
  345.                 jetpack_stream.stop()
  346.        
  347.         # Handle Jump.
  348.         if Input.is_action_just_released("jump") and velocity.y > 0:
  349.             current_jump_cd = 0
  350.             velocity.y = velocity.y / 2.0
  351. #           current_gravity = gravity_falling
  352.         elif current_coyote_time > 0 && (
  353.             Input.is_action_just_pressed("jump") || current_jump_cd > 0):
  354.             current_jump_cd = JUMP_CD_MAX
  355.             current_coyote_time = 0
  356.             velocity.y = JUMP_VELOCITY
  357.             if Input.is_action_pressed("sprint"): current_walk_state = WalkState.SPRINT
  358.             else: current_walk_state = WalkState.NORMAL
  359.             _UpdateCollider()
  360.            
  361.         if abs(velocity.y) < JUMP_HANG_TIME_THRESHOLD && !is_on_floor():
  362.             #make the gravity weaker around apex of jump
  363.             current_gravity = gravity_hang_time
  364.            
  365.             #increase responsiveness
  366.             target_speed *= JUMP_HANG_TIME_SPEED_MULT
  367.             current_acc_rate *= JUMP_HANG_TIME_ACC_MULT
  368.         else:
  369.             #if falling make gravity stronger
  370.             if velocity.y < 0:
  371.                 current_gravity = gravity_falling
  372.             else:
  373.                 current_gravity = gravity_default
  374.     else:  
  375.         if jetpack_stream.volume_db < linear_to_db(1):
  376.             jetpack_stream.volume_db = linear_to_db(lerp(
  377.                 db_to_linear(jetpack_stream.volume_db),
  378.                 1.0,
  379.                 JETPACK_VOLUME_LERP_MULT * delta
  380.             ))
  381.             jetpack_stream.volume_db = min(linear_to_db(1),
  382.             jetpack_stream.volume_db)
  383.        
  384.         current_gravity = 0
  385.         current_energy -= delta * ENERGY_DRAIN_JETPACK_MULT
  386.         current_energy_regen_cooldown = 0
  387.        
  388.         var vertical_target = JETPACK_VERTICAL_VELOCITY_MAX
  389.         vertical_target = lerp(velocity.y, vertical_target, JETPACK_TARGER_LERP)
  390.         var vertical_difference : float = vertical_target - velocity.y
  391.  
  392.         #final force that will be applied to character
  393.         var jetpack_movement = vertical_difference * JETPACK_VERTICAL_FORCE
  394.        
  395.         if position.y <= current_jetpack_vertical + JETPACK_VERTICAL_DIFFERENCE_MAX:
  396.             velocity.y += jetpack_movement * delta
  397.             velocity.y = min(velocity.y, JETPACK_VERTICAL_VELOCITY_MAX)
  398.         else:
  399.             velocity.y = lerp(velocity.y, 0.0, 15 * delta)
  400.  
  401.  
  402.         if Input.is_action_just_released("jump") || current_energy <= 0:
  403.             current_jetpack_start_timer = 0
  404.             if Input.is_action_pressed("sprint"): current_walk_state = WalkState.SPRINT
  405.             else: current_walk_state = WalkState.NORMAL
  406.             _UpdateCollider()
  407.    
  408.     # Add the gravity.
  409.     if not is_on_floor():
  410.         current_coyote_time -= delta
  411.         if current_coyote_time < 0: current_coyote_time = 0
  412.         velocity.y -= current_gravity * delta
  413.         current_jetpack_vertical = min(position.y, current_jetpack_vertical)
  414.     else:
  415.         current_jetpack_vertical = position.y
  416.         current_coyote_time = COYOTE_TIME_MAX
  417.  
  418.    
  419.     #calculate dif between max and current speed
  420.     #ignore y axis
  421.     var speed_difference : Vector3 = target_speed - velocity
  422.     speed_difference.y = 0
  423.  
  424.     #final force that will be applied to character
  425.     var movement = speed_difference * current_acc_rate
  426.  
  427.     if is_on_floor() && floor_angle > SLOPE_SLIDE_THRESHOLD:
  428.         var plane = Plane(get_floor_normal())
  429.         movement = plane.project(movement)
  430.         current_slide_vector = plane.project(current_slide_vector)
  431.  
  432.     if WalkState.SLIDE == current_walk_state:
  433.         velocity = velocity + (movement) * delta * SLIDE_DAMPEN_RATE
  434.         velocity = velocity + current_slide_vector * delta * (current_slide_time) * (-(current_slide_vector.y) + .01)
  435.     else:
  436.         velocity = velocity + (movement) * delta
  437.  
  438.     if velocity.y < -FALL_SPEED_MAX:
  439.         velocity.y = -FALL_SPEED_MAX
  440.        
  441.     current_jump_cd -= delta
  442.     if current_jump_cd < 0: current_jump_cd = 0
  443.    
  444.     debug_label.text = "State: " + WalkState.keys()[current_walk_state] + "\n" + str(velocity.length()) + "\n" + (
  445.         "Vertical:" + str(velocity.y)) + "\nJetpack: " + str(current_energy) + (
  446.         "\nGravity: " + str(current_gravity)) + "\nVolume: " + str(jetpack_stream.volume_db)
  447.    
  448.     _UpdateCameraPosition(delta, inverse_lerp(0, abs(SPRINT_SPEED), velocity.length()))
  449.     move_and_slide()
  450.  
  451. func _UpdateCameraPosition(delta, speed_t):
  452.     var t = CAMERA_LERP * delta
  453.  
  454.     if WalkState.SLIDE == current_walk_state:
  455.         camera_3d.rotation.z = lerp(camera_3d.rotation.z, deg_to_rad(15.0), t)
  456.     else:
  457.         camera_3d.rotation.z = lerp(camera_3d.rotation.z, 0.0, t)
  458.        
  459.     if is_head_bob_active && WalkState.SLIDE != current_walk_state && is_on_floor() && direction != Vector3.ZERO:
  460.         head_bob_var += current_head_bob_frequency * delta
  461.         head_bob_pivot.position.y = lerp(
  462.             head_bob_pivot.position.y,
  463.             sin(head_bob_var) * current_head_bob_intensity / 2.0,
  464.             t)
  465.         head_bob_pivot.position.x = lerp(
  466.             head_bob_pivot.position.x,
  467.             cos(head_bob_var / 2.0) * current_head_bob_intensity,
  468.             t)
  469.     else:
  470.         head_bob_pivot.position.y = lerp(
  471.             head_bob_pivot.position.y,
  472.             0.0,
  473.             t)
  474.         head_bob_pivot.position.x = lerp(
  475.             head_bob_pivot.position.x,
  476.             0.0,
  477.             t)
  478.        
  479.     var tmp = lerp(CAMERA_FOV_NORMAL, CAMERA_FOV_MAX_SPEED, speed_t)
  480.     camera_3d.fov = lerp(camera_3d.fov, min(tmp, CAMERA_FOV_MAX_SPEED), t)
  481.     camera_pivot.position.y = lerp(camera_pivot.position.y, current_camera_height, t)
  482.  
  483. func _UpdateCollider():
  484.     match current_walk_state:
  485.         WalkState.NORMAL:
  486.             collider.shape.height = COLLIDER_HEIGHT_NORMAL
  487.             collider.position.y = COLLIDER_HEIGHT_NORMAL / 2.0
  488.             height_raycast.target_position.y = COLLIDER_HEIGHT_NORMAL
  489.             current_camera_height = CAMERA_HEIGHT_NORMAL
  490.             current_max_speed = WALK_SPEED
  491.             current_head_bob_frequency = HEAD_BOB_FREQUENCY_NORMAL
  492.             current_head_bob_intensity = HEAD_BOB_INTENSITY_NORMAL
  493.             current_lerp_acc = WALK_LERP_ACC
  494.             current_lerp_dec = WALK_LERP_DEC
  495.         WalkState.CROUCH:  
  496.             collider.shape.height = COLLIDER_HEIGHT_CROUCH
  497.             collider.position.y = COLLIDER_HEIGHT_CROUCH / 2.0
  498.             height_raycast.target_position.y = COLLIDER_HEIGHT_NORMAL
  499.             current_camera_height = CAMERA_HEIGHT_CROUCH
  500.             current_max_speed = CROUCH_SPEED
  501.             current_head_bob_frequency = HEAD_BOB_FREQUENCY_CROUCH
  502.             current_head_bob_intensity = HEAD_BOB_INTENSITY_CROUCH
  503.             current_lerp_acc = CROUCH_LERP_ACC
  504.             current_lerp_dec = CROUCH_LERP_DEC
  505.         WalkState.SLIDE:   
  506.             collider.shape.height = COLLIDER_HEIGHT_CROUCH
  507.             collider.position.y = COLLIDER_HEIGHT_CROUCH / 2.0
  508.             height_raycast.target_position.y = COLLIDER_HEIGHT_NORMAL
  509.             current_camera_height = CAMERA_HEIGHT_CROUCH
  510.             current_max_speed = WALK_SPEED
  511.             current_head_bob_frequency = HEAD_BOB_FREQUENCY_CROUCH
  512.             current_head_bob_intensity = HEAD_BOB_INTENSITY_CROUCH
  513.             current_lerp_acc = CROUCH_LERP_ACC
  514.             current_lerp_dec = CROUCH_LERP_DEC
  515.         WalkState.PRONE:   
  516.             collider.shape.height = COLLIDER_HEIGHT_PRONE
  517.             collider.position.y = COLLIDER_HEIGHT_PRONE / 2.0
  518.             height_raycast.target_position.y = COLLIDER_HEIGHT_CROUCH
  519.             current_camera_height = CAMERA_HEIGHT_PRONE
  520.             current_max_speed = PRONE_SPEED
  521.             current_head_bob_frequency = HEAD_BOB_FREQUENCY_PRONE
  522.             current_head_bob_intensity = HEAD_BOB_INTENSITY_PRONE
  523.             current_lerp_acc = PRONE_LERP_ACC
  524.             current_lerp_dec = PRONE_LERP_DEC
  525.         WalkState.SPRINT:  
  526.             collider.shape.height = COLLIDER_HEIGHT_NORMAL
  527.             collider.position.y = COLLIDER_HEIGHT_NORMAL / 2.0
  528.             height_raycast.target_position.y = COLLIDER_HEIGHT_NORMAL
  529.             current_camera_height = CAMERA_HEIGHT_NORMAL
  530.             current_max_speed = SPRINT_SPEED
  531.             current_head_bob_frequency = HEAD_BOB_FREQUENCY_SPRINT
  532.             current_head_bob_intensity = HEAD_BOB_INTENSITY_SPRINT
  533.             current_lerp_acc = SPRINT_LERP_ACC
  534.             current_lerp_dec = SPRINT_LERP_DEC
  535.         WalkState.JETPACK
  536.             collider.shape.height = COLLIDER_HEIGHT_NORMAL
  537.             collider.position.y = COLLIDER_HEIGHT_NORMAL / 2.0
  538.             height_raycast.target_position.y = COLLIDER_HEIGHT_NORMAL
  539.             current_camera_height = CAMERA_HEIGHT_NORMAL
  540.             current_max_speed = JETPACK_SPEED
  541.             current_head_bob_frequency = HEAD_BOB_FREQUENCY_PRONE
  542.             current_head_bob_intensity = HEAD_BOB_INTENSITY_PRONE
  543.             current_lerp_acc = JETPACK_LERP_ACC
  544.             current_lerp_dec = JETPACK_LERP_DEC
  545.  
  546. func _UpdateEnergyLabel():
  547.     energy_bar_label.text = str(floor(current_energy))
  548.     energy_bar_bar.value = current_energy
  549.    
  550. #2nd PART
  551. #ROPE GENERATION SCRIPT------------------
  552. @tool
  553. extends MeshInstance3D
  554.  
  555. @export var iterations = 5
  556. @export var dirty = false
  557. @export var resolution = 10
  558.  
  559. var grapple_hook_position : Vector3 = Vector3.ZERO
  560. var player_position : Vector3 = Vector3.ZERO
  561.  
  562. var vertex_array : PackedVector3Array = []
  563. var index_array : PackedInt32Array = []
  564. var normal_array : PackedVector3Array = []
  565.  
  566. var tangent_array : PackedVector3Array = []
  567. var uv_array : PackedVector2Array = []
  568.  
  569. var points : PackedVector3Array = []
  570. var points_old : PackedVector3Array = []
  571.  
  572. @export var point_count = 20
  573.  
  574. var rope_length : float = 0.0
  575. var point_spacing : float = 0.0
  576. @export var rope_width = .02
  577.  
  578. @export var isDrawing = false
  579. @export var firstTime = true
  580.  
  581. @onready var temp_player = $tempPlayer
  582. @onready var temp_hook = $tempHook
  583.  
  584. var gravity_default = ProjectSettings.get_setting("physics/3d/default_gravity")
  585.  
  586. # Called when the node enters the scene tree for the first time.
  587. func _ready():
  588.     SetGrappleHookPosition(temp_hook.position)
  589.     SetPlayerPosition(temp_player.position)
  590.     PreparePoints()
  591.     pass # Replace with function body.
  592.  
  593.  
  594. # Called every frame. 'delta' is the elapsed time since the previous frame.
  595. func _process(delta):
  596. #   SetPlayerPosition(temp_player.position)
  597. #   SetGrappleHookPosition(temp_hook.position)
  598.    
  599.     if isDrawing || dirty:
  600.         if firstTime:
  601.             PreparePoints()
  602.             firstTime = false
  603.         UpdatePoints(delta)
  604.        
  605.         GenerateMesh()
  606.        
  607.         dirty = false
  608.  
  609. func SetGrappleHookPosition(val : Vector3):
  610.     grapple_hook_position = val
  611.     firstTime = true
  612.  
  613. func SetPlayerPosition(val : Vector3):
  614.     player_position = val
  615.  
  616. func StartDrawing():
  617.     isDrawing = true
  618.    
  619. func StopDrawing():
  620.     isDrawing = false
  621.  
  622. func PreparePoints():
  623.     points.clear()
  624.     points_old.clear()
  625.    
  626.     for i in range(point_count):
  627.         var t = i / (point_count - 1.0)
  628.        
  629.         points.append(lerp(player_position, grapple_hook_position, t))
  630.         points_old.append(points[i])
  631.        
  632.     _UpdatePointSpacing()
  633.    
  634. func _UpdatePointSpacing():
  635.     rope_length = (grapple_hook_position - player_position).length()
  636.     point_spacing = rope_length / (point_count - 1.0)
  637.  
  638. func UpdatePoints(delta):
  639.     points[0] = player_position
  640.     points[point_count-1] = grapple_hook_position
  641.  
  642.     _UpdatePointSpacing()
  643.  
  644.     for i in range(1, point_count - 1):
  645.         var curr : Vector3 = points[i]
  646.         points[i] = points[i] + (points[i] - points_old[i]) + (
  647.             Vector3.DOWN * gravity_default * delta * delta)
  648.         points_old[i] = curr
  649.    
  650.     for i in range(iterations):
  651.         ConstraintConnections()
  652.    
  653. func ConstraintConnections():
  654.     for i in range(point_count - 1):
  655.         var centre : Vector3 = (points[i+1] + points[i]) / 2.0
  656.         var offset : Vector3 = (points[i+1] - points[i])
  657.         var length : float = offset.length()
  658.         var dir : Vector3 = offset.normalized()
  659.        
  660.         var d = length - point_spacing
  661.        
  662.         if i != 0:
  663. #           points[i] = centre + dir * d / 2.0
  664.             points[i] += dir * d * 0.5
  665.        
  666.         if i + 1 != point_count - 1:
  667. #           points[i+1] = centre - dir * d / 2.0
  668.             points[i+1] -= dir * d * 0.5
  669.  
  670. func GenerateMesh():
  671.    
  672.     vertex_array.clear()
  673.    
  674.     CalculateNormals()
  675.  
  676.     index_array.clear()
  677.    
  678.     #segment / point
  679.     for p in range(point_count):
  680.        
  681.         var center : Vector3 = points[p]
  682.        
  683.         var forward = tangent_array[p]
  684.         var norm = normal_array[p]
  685.         var bitangent = norm.cross(forward).normalized()
  686.        
  687.         #current resolution
  688.         for c in range(resolution):
  689.             var angle = (float(c) / resolution) * 2.0 * PI
  690.            
  691.             var xVal = sin(angle) * rope_width
  692.             var yVal = cos(angle) * rope_width
  693.            
  694.             var point = (norm * xVal) + (bitangent * yVal) + center
  695.            
  696.             vertex_array.append(point)
  697.            
  698.             if p < point_count - 1:
  699.                 var start_index = resolution * p
  700.                 #INT values
  701.                 index_array.append(start_index + c);
  702.                 index_array.append(start_index + c + resolution);
  703.                 index_array.append(start_index + (c + 1) % resolution);
  704.                
  705.  
  706.                 index_array.append(start_index + (c + 1) % resolution);
  707.                 index_array.append(start_index + c + resolution);
  708.                 index_array.append(start_index + (c + 1) % resolution + resolution);
  709.                
  710.        
  711.    
  712.     mesh.clear_surfaces()
  713.     mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
  714.  
  715.     for i in range(index_array.size() / 3):
  716.         var p1 = vertex_array[index_array[3*i]]
  717.         var p2 = vertex_array[index_array[3*i+1]]
  718.         var p3 = vertex_array[index_array[3*i+2]]
  719.        
  720.         var tangent = Plane(p1, p2, p3)
  721.         var normal = tangent.normal
  722.        
  723.         mesh.surface_set_tangent(tangent)
  724.         mesh.surface_set_normal(normal)
  725.         mesh.surface_add_vertex(p1)
  726.        
  727.         mesh.surface_set_tangent(tangent)
  728.         mesh.surface_set_normal(normal)
  729.         mesh.surface_add_vertex(p2)
  730.        
  731.         mesh.surface_set_tangent(tangent)
  732.         mesh.surface_set_normal(normal)
  733.         mesh.surface_add_vertex(p3)
  734.        
  735.     # End drawing.
  736.     mesh.surface_end()
  737.    
  738.  
  739. func CalculateNormals():
  740.     normal_array.clear()
  741.     tangent_array.clear()
  742.    
  743.     var helper
  744.    
  745.     for i in range(point_count):
  746.         var tangent := Vector3(0,0,0)
  747.         var normal := Vector3(0,0,0)
  748.        
  749.         var temp_helper_vector := Vector3(0,0,0)
  750.        
  751.         #first point
  752.         if i == 0:
  753.             tangent = (points[i+1] - points[i]).normalized()
  754.         #last point
  755.         elif i == point_count - 1:
  756.             tangent = (points[i] - points[i-1]).normalized()
  757.         #between
  758.         else:
  759.             tangent = (points[i+1] - points[i]).normalized() + (
  760.                 points[i] - points[i-1]).normalized()
  761.            
  762.         if i == 0:
  763.             temp_helper_vector = -Vector3.FORWARD if (
  764.                 tangent.dot(Vector3.UP) > 0.5) else Vector3.UP
  765.                
  766.             normal = temp_helper_vector.cross(tangent).normalized()
  767.            
  768.         else:
  769.             var tangent_prev = tangent_array[i-1]
  770.             var normal_prev = normal_array[i-1]
  771.             var bitangent = tangent_prev.cross(tangent)
  772.            
  773.             if bitangent.length() == 0:
  774.                 normal = normal_prev
  775.             else:
  776.                 var bitangent_dir = bitangent.normalized()
  777.                 var theta = acos(tangent_prev.dot(tangent))
  778.  
  779.                 var rotate_matrix = Basis(bitangent_dir, theta)
  780.                 normal = rotate_matrix * normal_prev
  781.  
  782.         tangent_array.append(tangent)
  783.         normal_array.append(normal)
  784.  
  785.  
  786. func PopulateVertexArray():
  787.     vertex_array.clear()
  788.  
  789. #   #add player points
  790. #   for i in range(-1, 2, 2):
  791. #       for j in range(-1, 2, 2):
  792. #           vertex_array.append(Vector3(
  793. #               player_position.x + rope_width * i,
  794. #               player_position.y + rope_width * j,
  795. #               player_position.z))
  796. #
  797. #   #add grapple points
  798. #   for i in range(-1, 2, 2):
  799. #       for j in range(-1, 2, 2):
  800. #           vertex_array.append(Vector3(
  801. #               grapple_hook_position.x + rope_width * i,
  802. #               grapple_hook_position.y + rope_width * j,
  803. #               grapple_hook_position.z))
  804.    
  805.     for point in points:
  806.         vertex_array.append(point)
  807.     pass
  808.    
  809. func PopulateIndexArray():
  810.     index_array.clear()
  811.    
  812. #   index_array = [
  813. #       0,1,2,
  814. #       1,2,3,
  815. #       4,5,6,
  816. #       5,6,7,
  817. #       2,3,6,
  818. #       3,6,7,
  819. #       0,1,4,
  820. #       1,4,5,
  821. #       1,3,5,
  822. #       3,5,7,
  823. #       0,2,4,
  824. #       2,4,6
  825. #   ]
  826.     for i in range(vertex_array.size()-1):
  827.         index_array.append(i)
  828.         index_array.append(i+1)
  829.     pass
  830.  
  831. func DrawRope():
  832.     mesh.clear_surfaces()
  833.     # Begin draw.
  834. #   mesh.surface_begin(Mesh.PRIMITIVE_TRIANGLES)
  835.     mesh.surface_begin(Mesh.PRIMITIVE_LINES)
  836.     # Draw mesh.
  837.     # Prepare attributes for add_vertex.
  838. #   mesh.surface_set_normal(Vector3(0, 0, 1))
  839. #   mesh.surface_set_uv(Vector2(0, 0))
  840. #   # Call last for each vertex, adds the above attributes.
  841. #   mesh.surface_add_vertex(Vector3(-1, -1, 0))
  842. #
  843. #   mesh.surface_set_normal(Vector3(0, 0, 1))
  844. #   mesh.surface_set_uv(Vector2(0, 1))
  845. #   mesh.surface_add_vertex(Vector3(-1, 1, 0))
  846. #
  847. #   mesh.surface_set_normal(Vector3(0, 0, 1))
  848. #   mesh.surface_set_uv(Vector2(1, 1))
  849. #   mesh.surface_add_vertex(Vector3(1, 1, 0))
  850.     for i in index_array:
  851.         mesh.surface_add_vertex(vertex_array[i])
  852.  
  853.     # End drawing.
  854.     mesh.surface_end()
  855.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement