-
Notifications
You must be signed in to change notification settings - Fork 1
/
Player.gd
155 lines (125 loc) · 3.8 KB
/
Player.gd
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
147
148
149
150
151
152
153
154
155
extends KinematicBody2D
const UP = Vector2(0, -1)
export(int) var GRAVITY = 25
export(int) var WALLGRAVITY = 10
export(int) var MAXFALLSPEED = 400
export(int) var MAXSPEED = 100
export(int) var WALLJUMPFORCE = 200
export(int) var JUMPFORCE = 300
export(int) var WALLDROPFORCE = 100
export(int) var ACCEL = 15
export(float) var AIRMULTIPLIERCONTROLS = 0.7
const FacingX = {LEFT = -1, RIGHT = 1}
var motion = Vector2()
var direction: float = 1.0
var facingX = FacingX.RIGHT
# Called when the node enters the scene tree for the first time.
func _ready():
pass # Replace with function body.
func get_motion_multiplier():
if not is_on_floor() and not is_on_wall():
return AIRMULTIPLIERCONTROLS
return 1.0
func handle_gravity():
if not is_on_wall():
motion.y += GRAVITY
else:
motion.x += (WALLGRAVITY * is_near_wall())
func handle_terminal_velocity():
if motion.y > MAXFALLSPEED:
motion.y = MAXFALLSPEED
func handle_facing_x():
if Input.is_action_pressed("right"):
facingX = FacingX.RIGHT
if Input.is_action_pressed("left"):
facingX = FacingX.LEFT
func get_direction():
if Input.is_action_pressed("right"):
return 1
if Input.is_action_pressed("left"):
return -1
return 0
func get_move_axis():
if is_on_floor():
return "x"
if is_on_wall():
return "y"
return "x"
func _physics_process(delta):
handle_gravity()
handle_terminal_velocity()
handle_facing_x()
animate_mouse()
direction = get_direction()
var move_axis = get_move_axis()
if move_axis == "y":
motion[move_axis] += (ACCEL * direction * -is_near_wall() * get_motion_multiplier())
else:
motion[move_axis] += (ACCEL * direction * get_motion_multiplier())
# if Input.get_action_strength("ui_accept") > 0:
$Laser.laser_update()
if is_on_floor():
if Input.is_action_just_pressed("jump"):
motion.y = -JUMPFORCE
if is_on_wall():
motion.y = clamp(motion.y, -MAXSPEED, MAXSPEED)
if get_direction() == 0:
motion.y = lerp(motion.y, 0, 0.2)
if Input.is_action_just_pressed("jump"):
motion.x = WALLJUMPFORCE * -is_near_wall()
motion.y = -WALLJUMPFORCE
if Input.is_action_just_pressed("down"):
motion.x = WALLDROPFORCE * -is_near_wall()
if get_direction() == 0:
motion.x = lerp(motion.x, 0, 0.2)
motion.x = clamp(motion.x, -MAXSPEED, MAXSPEED)
motion = move_and_slide(motion, UP)
# show the pause menu
if Input.is_action_just_pressed("pause"):
Global.prevScene = get_tree().get_current_scene().get_name()
get_tree().change_scene("res://Menus/GameMenu.tscn");
func is_near_wall():
if $Wallcheckerfront.is_colliding():
return 1
if $Wallcheckerback.is_colliding():
return -1
else:
return 0
func get_corner_inner_caster():
if is_on_wall():
if is_near_wall() == 1:
if facingX == FacingX.LEFT:
return $WallDistanceDown
return $WallDistanceLeft
if is_near_wall() == -1 :
if facingX == FacingX.RIGHT:
return $WallDistanceDown
return $WallDistanceRight
if facingX == FacingX.LEFT:
return $WallDistanceLeft
return $WallDistanceRight
func animate_mouse():
$Sprite.animation = "moving"
$Sprite.playing = true
var caster = get_corner_inner_caster()
if caster.is_colliding():
#WallDistanceBack
var origin = caster.global_transform.get_origin()
var collision = caster.get_collision_point()
var distance = origin.distance_to(collision)
var upper = 8
var lower = 4
var num_frames = 3
if distance < upper and distance > lower:
var percentage = ((distance - lower) / (upper - lower))
var framePercentage = percentage;
if facingX == FacingX.LEFT or is_on_wall():
framePercentage = 1 - percentage;
var frame = round(framePercentage * num_frames)
$Sprite.animation = "corner-inner"
$Sprite.playing = false
$Sprite.frame = frame
$Sprite.scale.x = abs($Sprite.scale.x) * facingX
$Sprite.rotation_degrees = 0
if is_on_wall():
$Sprite.rotation_degrees = 90 * -is_near_wall()