summaryrefslogtreecommitdiff
path: root/Scripts/Player/PlayerMovement.gd
blob: 648a5e6147bdfd8c56a2fffd0c3d4b9b7b658ceb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
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