summaryrefslogtreecommitdiff
path: root/Player/player.gd
blob: fb1219fcac31735e45b6c6556f3e9e8b6acf2d5f (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
extends CharacterBody2D


const SPEED = 60.0
const JUMP_VELOCITY = -300.0

var is_in_air = false
var is_crouching = false


func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
		$AnimatedSprite2D.play("default_jump")
		is_in_air = true

	# Handle jump.
	if (Input.is_action_just_pressed("ui_accept") or Input.is_action_just_pressed("ui_up")) and is_on_floor():
		velocity.y = JUMP_VELOCITY
		$AnimatedSprite2D.play("default_jump")
		$Audio/Jump.play()
	
	if Input.is_action_pressed("ui_down") and is_on_floor():
		is_crouching = true
	elif Input.is_action_just_released("ui_down"):
		is_crouching = false
	
	# 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("ui_left", "ui_right")
	if direction:
		if is_crouching:
			velocity.x = direction * (SPEED / 2)
		else:
			velocity.x = direction * SPEED
		if is_on_floor():
			if is_crouching:
				$AnimatedSprite2D.play("default_crouch")
			else:
				$AnimatedSprite2D.play("default_walk")
		$AnimatedSprite2D.flip_h = direction < 0
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)
		if is_on_floor():
			if is_crouching:
				$AnimatedSprite2D.play("default_crouch_idle")
			else:
				$AnimatedSprite2D.play("default_idle")
	
	if is_on_floor() and is_in_air:
		is_in_air = false
		$Audio/Land.play()
	
	move_and_slide()