forked from Miziziziz/Platformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.gd
53 lines (43 loc) · 1.12 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
extends CharacterBody2D
const MOVE_SPEED = 500
const JUMP_FORCE = 1000
const GRAVITY = 50
const MAX_FALL_SPEED = 1000
@onready var anim_player = $AnimationPlayer
@onready var sprite = $Sprite
var y_velo = 0
var facing_right = false
func _physics_process(delta):
var move_dir = 0
if Input.is_action_pressed("move_right"):
move_dir += 1
if Input.is_action_pressed("move_left"):
move_dir -= 1
move_and_slide()
set_velocity(Vector2(move_dir * MOVE_SPEED, y_velo))#, Vector2(0, -1))
var grounded = is_on_floor()
y_velo += GRAVITY
if grounded and Input.is_action_just_pressed("jump"):
y_velo = -JUMP_FORCE
if grounded and y_velo >= 0:
y_velo = 5
if y_velo > MAX_FALL_SPEED:
y_velo = MAX_FALL_SPEED
if facing_right and move_dir < 0:
flip()
if !facing_right and move_dir > 0:
flip()
if grounded:
if move_dir == 0:
play_anim("idle")
else:
play_anim("walk")
else:
play_anim("jump")
func flip():
facing_right = !facing_right
sprite.flip_h = !sprite.flip_h
func play_anim(anim_name):
if anim_player.is_playing() and anim_player.current_animation == anim_name:
return
anim_player.play(anim_name)