forked from Miziziziz/WitchyPlatformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.gd
137 lines (109 loc) · 2.94 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
extends CharacterBody2D
@export var move_speed = 200
@export var gravity = 20
@export var less_gravity = 10
@export var jump_force = 400
var velo = Vector2()
var drag = 0.3
const jump_buffer = 0.08
var time_pressed_jump = 0.0
var time_left_ground = 0.0
var last_grounded = false
var facing_right = true
@onready var anim_player = $AnimationPlayer
var being_knocked_back = false
var knockback_time = 0.3
var cur_knockback_time = 0.0
var knockback_vec = Vector2()
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
get_tree().call_group("need_player_ref", "set_player", self)
func _process(delta):
if Input.is_action_pressed("exit"):
get_tree().quit()
if Input.is_action_pressed("restart"):
RaceTimer.restart_race()
func _physics_process(delta):
if being_knocked_back:
set_velocity(knockback_vec)
set_up_direction(Vector2.UP)
move_and_slide()
cur_knockback_time += delta
if cur_knockback_time > knockback_time:
being_knocked_back = 0.0
velo = knockback_vec
else:
calc_regular_movement(delta)
func calc_regular_movement(delta):
var move_vec = Vector2()
if Input.is_action_pressed("move_left"):
move_vec.x -= 1
if Input.is_action_pressed("move_right"):
move_vec.x += 1
velo += move_vec * move_speed - drag * Vector2(velo.x, 0)
var cur_grounded = is_on_floor()
if !cur_grounded and last_grounded:
time_left_ground = get_cur_time()
#var will_jump = false
var pressed_jump = Input.is_action_just_pressed("jump")
if pressed_jump:
time_pressed_jump = get_cur_time()
if (pressed_jump and cur_grounded):
jump()
elif (!last_grounded and cur_grounded and get_cur_time() - time_pressed_jump < jump_buffer):
jump()
elif pressed_jump and get_cur_time() - time_left_ground < jump_buffer:
jump()
var holding_jump = Input.is_action_pressed("jump")
if holding_jump:
velo.y += less_gravity
else:
velo.y += gravity
if cur_grounded and velo.y > 10:
velo.y = 10
set_velocity(velo)
set_up_direction(Vector2.UP)
move_and_slide()
velo = velocity
if move_vec.x > 0.0 and !facing_right:
flip()
elif move_vec.x < 0.0 and facing_right:
flip()
$JumpParticles.emitting = false
if cur_grounded:
if move_vec == Vector2():
play_anim("idle")
else:
play_anim("run")
else:
if !holding_jump:
play_anim("fall")
else:
play_anim("jump")
$JumpParticles.emitting = true
last_grounded = cur_grounded
func jump():
velo.y = -jump_force
func get_cur_time():
return Time.get_ticks_msec() / 1000.0
func flip():
$Sprite2D.flip_h = !$Sprite2D.flip_h
facing_right = !facing_right
func play_anim(anim):
if anim_player.current_animation == anim:
return
anim_player.play(anim)
func knockback(dir, power):
cur_knockback_time = 0.0
being_knocked_back = true
knockback_vec = dir * power
var won = false
func win():
if won:
return
$WinSoundPlayer.play()
won = true
$Sprite2D.texture = load("res://spritesheets/Witchcraft_spr_1.png")
$WinParticles.emitting = true
jump_force = 450
move_speed = 80