Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends CharacterBody2D
- @export var max_speed = 250.0
- @export var acceleration = 2000.0
- @export var deceleration = 2500.0
- var movement_modifier = 1.0
- @export var forward_multiplier = 2.0
- @export var backward_multiplier = 0.5
- @export var strafe_multiplier = 1.0
- @export var dash_duration = 0.1
- @export var dash_speed_multiplier = 2.0
- @export var dash_accel_multiplier = 3.0
- var is_dashing = false
- var dash_timer = 0.0
- @export var stick_deadzone = 0.2
- @export var slash_scene: PackedScene
- var external_velocity = Vector2.ZERO
- @onready var combat = $CombatComponent
- func _physics_process(delta):
- handle_input_state(delta)
- var input_vector = Input.get_vector(
- "move_left",
- "move_right",
- "move_up",
- "move_down"
- )
- # -------------------------
- # HITSTUN CHECK
- # -------------------------
- if combat and combat.is_stunned:
- external_velocity = external_velocity.move_toward(Vector2.ZERO, deceleration * delta)
- velocity = external_velocity
- move_and_slide()
- return
- handle_rotation(input_vector)
- handle_movement(delta, input_vector)
- handle_attack()
- move_and_slide()
- func handle_input_state(delta):
- if Input.is_action_just_pressed("dash"):
- is_dashing = true
- dash_timer = dash_duration
- if is_dashing:
- dash_timer -= delta
- if dash_timer <= 0:
- is_dashing = false
- func handle_movement(delta, input_vector):
- var speed = max_speed * movement_modifier
- var accel = acceleration
- if is_dashing:
- speed *= dash_speed_multiplier
- accel *= dash_accel_multiplier
- external_velocity = external_velocity.move_toward(Vector2.ZERO, deceleration * delta)
- var forward = Vector2.RIGHT.rotated(rotation)
- var right = Vector2.UP.rotated(rotation)
- var forward_amount = input_vector.dot(forward)
- var right_amount = input_vector.dot(right)
- var move_dir = Vector2.ZERO
- if forward_amount >= 0:
- move_dir += forward * forward_amount * forward_multiplier
- else:
- move_dir += forward * forward_amount * backward_multiplier
- move_dir += right * right_amount * strafe_multiplier
- # idle dash
- if is_dashing and input_vector == Vector2.ZERO:
- move_dir = Vector2.RIGHT.rotated(rotation)
- var target_velocity = move_dir * speed
- velocity = velocity.move_toward(target_velocity, accel * delta) + external_velocity
- func handle_rotation(input_vector):
- var stick_aim = Input.get_vector(
- "aim_left",
- "aim_right",
- "aim_up",
- "aim_down"
- )
- if is_dashing:
- if velocity.length() > 0.1:
- rotation = velocity.angle()
- return
- if stick_aim.length_squared() > stick_deadzone * stick_deadzone:
- rotation = stick_aim.normalized().angle()
- return
- look_at(get_global_mouse_position())
- func handle_attack():
- if not Input.is_action_just_pressed("attack"):
- return
- print("ATTACK PRESSED")
- print("SCRIPT:", get_script().resource_path)
- print("slash_scene =", slash_scene)
- if slash_scene == null:
- print("SLASH SCENE IS NULL")
- print("===== ATTACK DEBUG END =====")
- return
- print("ABOUT TO INSTANTIATE")
- var slash = slash_scene.instantiate()
- print("INSTANTIATED:", slash)
- get_parent().add_child(slash)
- print("ADDED TO TREE")
- slash.global_position = global_position + Vector2.RIGHT.rotated(rotation) * 16
- print("POSITION SET")
- slash.rotation = rotation
- print("ROTATION SET")
- slash.direction = Vector2.RIGHT.rotated(rotation)
- print("DIRECTION SET")
- slash.source = self
- print("SOURCE SET")
- print("SLASH SPAWNED SUCCESSFULLY")
- print("===== ATTACK DEBUG END =====")
Advertisement
Add Comment
Please, Sign In to add comment