Guest User

player.gd

a guest
May 10th, 2026
77
0
198 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. extends CharacterBody2D
  2.  
  3. @export var max_speed = 250.0
  4. @export var acceleration = 2000.0
  5. @export var deceleration = 2500.0
  6. var movement_modifier = 1.0
  7.  
  8. @export var forward_multiplier = 2.0
  9. @export var backward_multiplier = 0.5
  10. @export var strafe_multiplier = 1.0
  11.  
  12. @export var dash_duration = 0.1
  13. @export var dash_speed_multiplier = 2.0
  14. @export var dash_accel_multiplier = 3.0
  15.  
  16. var is_dashing = false
  17. var dash_timer = 0.0
  18.  
  19. @export var stick_deadzone = 0.2
  20. @export var slash_scene: PackedScene
  21.  
  22. var external_velocity = Vector2.ZERO
  23.  
  24. @onready var combat = $CombatComponent
  25.  
  26.  
  27. func _physics_process(delta):
  28.  
  29. handle_input_state(delta)
  30.  
  31. var input_vector = Input.get_vector(
  32. "move_left",
  33. "move_right",
  34. "move_up",
  35. "move_down"
  36. )
  37.  
  38. # -------------------------
  39. # HITSTUN CHECK
  40. # -------------------------
  41. if combat and combat.is_stunned:
  42. external_velocity = external_velocity.move_toward(Vector2.ZERO, deceleration * delta)
  43. velocity = external_velocity
  44. move_and_slide()
  45. return
  46.  
  47. handle_rotation(input_vector)
  48. handle_movement(delta, input_vector)
  49. handle_attack()
  50.  
  51. move_and_slide()
  52.  
  53.  
  54. func handle_input_state(delta):
  55. if Input.is_action_just_pressed("dash"):
  56. is_dashing = true
  57. dash_timer = dash_duration
  58.  
  59. if is_dashing:
  60. dash_timer -= delta
  61. if dash_timer <= 0:
  62. is_dashing = false
  63.  
  64.  
  65. func handle_movement(delta, input_vector):
  66. var speed = max_speed * movement_modifier
  67. var accel = acceleration
  68.  
  69. if is_dashing:
  70. speed *= dash_speed_multiplier
  71. accel *= dash_accel_multiplier
  72.  
  73. external_velocity = external_velocity.move_toward(Vector2.ZERO, deceleration * delta)
  74.  
  75. var forward = Vector2.RIGHT.rotated(rotation)
  76. var right = Vector2.UP.rotated(rotation)
  77.  
  78. var forward_amount = input_vector.dot(forward)
  79. var right_amount = input_vector.dot(right)
  80.  
  81. var move_dir = Vector2.ZERO
  82.  
  83. if forward_amount >= 0:
  84. move_dir += forward * forward_amount * forward_multiplier
  85. else:
  86. move_dir += forward * forward_amount * backward_multiplier
  87.  
  88. move_dir += right * right_amount * strafe_multiplier
  89.  
  90. # idle dash
  91. if is_dashing and input_vector == Vector2.ZERO:
  92. move_dir = Vector2.RIGHT.rotated(rotation)
  93.  
  94. var target_velocity = move_dir * speed
  95. velocity = velocity.move_toward(target_velocity, accel * delta) + external_velocity
  96.  
  97.  
  98. func handle_rotation(input_vector):
  99. var stick_aim = Input.get_vector(
  100. "aim_left",
  101. "aim_right",
  102. "aim_up",
  103. "aim_down"
  104. )
  105.  
  106. if is_dashing:
  107. if velocity.length() > 0.1:
  108. rotation = velocity.angle()
  109. return
  110.  
  111. if stick_aim.length_squared() > stick_deadzone * stick_deadzone:
  112. rotation = stick_aim.normalized().angle()
  113. return
  114.  
  115. look_at(get_global_mouse_position())
  116.  
  117.  
  118. func handle_attack():
  119.  
  120. if not Input.is_action_just_pressed("attack"):
  121. return
  122.  
  123. print("ATTACK PRESSED")
  124. print("SCRIPT:", get_script().resource_path)
  125. print("slash_scene =", slash_scene)
  126.  
  127. if slash_scene == null:
  128. print("SLASH SCENE IS NULL")
  129. print("===== ATTACK DEBUG END =====")
  130. return
  131.  
  132. print("ABOUT TO INSTANTIATE")
  133.  
  134. var slash = slash_scene.instantiate()
  135.  
  136. print("INSTANTIATED:", slash)
  137.  
  138. get_parent().add_child(slash)
  139. print("ADDED TO TREE")
  140.  
  141. slash.global_position = global_position + Vector2.RIGHT.rotated(rotation) * 16
  142. print("POSITION SET")
  143.  
  144. slash.rotation = rotation
  145. print("ROTATION SET")
  146.  
  147. slash.direction = Vector2.RIGHT.rotated(rotation)
  148. print("DIRECTION SET")
  149.  
  150. slash.source = self
  151. print("SOURCE SET")
  152.  
  153. print("SLASH SPAWNED SUCCESSFULLY")
  154. print("===== ATTACK DEBUG END =====")
  155.  
Advertisement
Add Comment
Please, Sign In to add comment