diff options
Diffstat (limited to 'Scripts/Player/PlayerMovement.gd')
-rw-r--r-- | Scripts/Player/PlayerMovement.gd | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/Scripts/Player/PlayerMovement.gd b/Scripts/Player/PlayerMovement.gd new file mode 100644 index 0000000..648a5e6 --- /dev/null +++ b/Scripts/Player/PlayerMovement.gd @@ -0,0 +1,146 @@ +extends Node +class_name PlayerMovement + +@export var character : CharacterBody2D + +@export var speed = 300.0 +@export var jump_vel = -400.0 + +@export var plr_sprite : Sprite2D +@export var mask_node : Node2D +@export var hand_pivot : Node2D + +@export var cape : Sprite2D +@export var skew_min : float +@export var skew_max : float +@export var skew_maxmax : float +@export var lerp_step_on : float +@export var lerp_step_off : float + +var target_skew = 0.0 +var curr_step = 0.0 + +@export var min_knock = 100 +@export var knockback_decay = 1.3 +var knockback : Vector2 + +var dir_manipulated : bool = false + +var dash_timer : Timer +var dashing : bool = false +var dash_dir : int = 0 +@export var dash_time : float = 0.2 +@export var dash_speed : float = 1500 + +var grapple_velocity : Vector2 = Vector2.ZERO +var grapple_x_vel_aft : float = 0 + +func _ready() -> void: + dash_timer = Timer.new() + dash_timer.wait_time = dash_time + dash_timer.one_shot = true + dash_timer.timeout.connect(_on_dash_end) + add_child(dash_timer) + + manipulate_timer = Timer.new() + add_child(manipulate_timer) + manipulate_timer.wait_time = 0.5 + manipulate_timer.one_shot = true + manipulate_timer.timeout.connect(_on_manipulate_end) + + +func _physics_process(delta: float) -> void: + if knockback.length_squared() > min_knock**2: + knockback /= knockback_decay + character.velocity = knockback + character.move_and_slide() + return + + if grapple_velocity != Vector2.ZERO: + character.velocity = grapple_velocity + character.move_and_slide() + return + + # Add the gravity. + if not character.is_on_floor(): + character.velocity += character.get_gravity() * delta + + if dashing: + character.velocity.x = 1000 * dash_dir + character.move_and_slide() + return + + # Handle jump. + if Input.is_action_just_pressed("jump") and character.is_on_floor(): + character.velocity.y = jump_vel + + # Get the input direction and handle the movement/deceleration. + # As good practice, you should replace UI actions with custom gameplay actions. + var direction := Input.get_axis("left", "right") + if direction: + grapple_x_vel_aft = 0 + character.velocity.x = direction * speed + if character.velocity.y < 0: + target_skew = skew_min + elif character.is_on_floor(): + target_skew = skew_max + curr_step = lerp_step_on + else: + if grapple_x_vel_aft != 0: + grapple_x_vel_aft = move_toward(character.velocity.x, 0, speed * delta) + character.velocity.x = grapple_x_vel_aft + else: + character.velocity.x = move_toward(character.velocity.x, 0, speed) + target_skew = skew_min + curr_step = lerp_step_off + + if character.velocity.y > 0: + target_skew = skew_maxmax + curr_step = lerp_step_off + + cape.skew = lerp_angle(cape.skew, deg_to_rad(target_skew), curr_step) + + if direction == -1 && !dir_manipulated: look_left() + elif direction == 1 && !dir_manipulated: look_right() + + character.move_and_slide() + +var manipulate_timer : Timer + +func look_left(manipulate : bool = false) -> void: + if manipulate: + dir_manipulated = true + manipulate_timer.start() + + if cape.skew > 0: cape.skew = -cape.skew + if skew_min > 0: skew_min = -skew_min + if skew_max > 0: skew_max = -skew_max + if skew_maxmax > 0: skew_maxmax = -skew_maxmax + if mask_node.position.x > 0: mask_node.position.x *= -1 + if hand_pivot.position.x > 0: hand_pivot.position.x *= -1 + plr_sprite.flip_h = true + + +func look_right(manipulate : bool = false) -> void: + if manipulate: + dir_manipulated = true + manipulate_timer.start() + + if cape.skew < 0: cape.skew = -cape.skew + if skew_min < 0: skew_min = -skew_min + if skew_max < 0: skew_max = -skew_max + if skew_maxmax < 0: skew_maxmax = -skew_maxmax + if mask_node.position.x < 0: mask_node.position.x *= -1 + if hand_pivot.position.x < 0: hand_pivot.position.x *= -1 + plr_sprite.flip_h = false + +func dash(dir): + dashing = true + dash_dir = dir + dash_timer.start() + +func _on_manipulate_end() -> void: + dir_manipulated = false + +func _on_dash_end() -> void: + dashing = false |